Package org.apache.xindice.tools.command

Source Code of org.apache.xindice.tools.command.ExportTree

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: ExportTree.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/

package org.apache.xindice.tools.command;

import org.apache.xindice.tools.XMLTools;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;

/**
* ExportTree is designed to take a Collection tree and create a Directory
* tree from it.  Export tree requires a collection name and a file path to create the tree
*
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class ExportTree extends Command {

    /**
     * Exports a collection into a file system
     *
     * @param table Description of Parameter
     * @return Description of the Returned Value
     */
    public boolean execute(Hashtable table) throws Exception {

        Collection col = null;

        if (table.get(XMLTools.FILE_PATH).equals("")) {
            System.out.println("ERROR: Directory name and switch required");
            return false;
        }

        if (table.get(XMLTools.COLLECTION) == null) {
            System.out.println("ERROR : Collection name and switch required");
            return false;
        }

        File fp = new File((String) table.get(XMLTools.FILE_PATH));

        System.out.println();

        if (table.get(XMLTools.COLLECTION) != null) {
            String parent = parentDir((String) table.get(XMLTools.COLLECTION));
            File dir = new File(fp, parent);

            System.out.println("Creating directory " + dir.getPath());
            dir.mkdir();

            process(dir, table, col);
        }

        return true;
    }

    private String parentDir(String parent) {
        int idx = parent.lastIndexOf("/");
        if (idx != -1) {
            return parent.substring(idx + 1);
        } else {
            return parent;
        }
    }

    /**
     * Export given collection to the directory
     */
    boolean process(File directory, Hashtable table, Collection col) throws Exception {
        try {
            String[] list = null;
            String collection = (String) table.get(XMLTools.COLLECTION);

            // Get a Collection reference to the collection
            String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
                                                      (String) table.get(XMLTools.LOCAL));
            col = DatabaseManager.getCollection(colstring);
            if (col == null) {
                System.out.println("ERROR : Collection not found!");
                return false;
            }

            String[] files = null;
            try {
                files = col.listResources();
            } catch (XMLDBException e) {
                // Xindice will throw an error if there is no indexer
                if (e.errorCode != ErrorCodes.VENDOR_ERROR) {
                    throw e;
                }
            }

            if (files != null) {
                System.out.println("Extracting " + files.length + " files from " + table.get(XMLTools.COLLECTION));
                for (int j = 0; j < files.length; j++) {
                    Resource res = col.getResource(files[j]);
                    Object content = res.getContent();
                    FileOutputStream output = new FileOutputStream(new File(directory, files[j]));
                    try {
                        if (content instanceof String) {
                            // UTF8FIXED: as we omit encoding declaration in output XML
                            // for the moment, we MUST write file in UTF-8
                            output.write(((String)content).getBytes("utf-8"));
                        } else {
                            output.write((byte[]) content);
                        }
                    } finally {
                        output.close();
                    }
                }
            }

            list = col.listChildCollections();
            for (int i = 0; i < list.length; i++) {
                File file = new File(directory, list[i]);

                System.out.println("Creating directory " + file.getPath());
                file.mkdirs();

                table.put(XMLTools.COLLECTION, (collection + "/" + file.getName()));

                process(new File(directory + "//" + file.getName()), table, col);
            }

        } finally {
            if (col != null) {
                col.close();
            }
        }
        return true;
    }
}
TOP

Related Classes of org.apache.xindice.tools.command.ExportTree

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.