/*
* 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 578602 2007-09-23 20:33:31Z natalia $
*/
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;
/**
* 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: 578602 $, $Date: 2007-09-23 16:33:31 -0400 (Sun, 23 Sep 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(XMLTools.Config table) throws Exception {
if (table.getString(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection name and switch required");
return false;
}
if (table.getString(XMLTools.FILE_PATH).equals("")) {
System.out.println("ERROR: Directory name and switch required");
return false;
}
File fp = new File(table.getString(XMLTools.FILE_PATH));
System.out.println();
String parent = parentDir(table.getString(XMLTools.COLLECTION));
File dir = new File(fp, parent);
System.out.println("Creating directory " + dir.getPath());
dir.mkdir();
process(dir, table);
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, XMLTools.Config table) throws Exception {
Collection col = null;
try {
String collection = table.getString(XMLTools.COLLECTION);
// Get a Collection reference to the collection
String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
table.getBoolean(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.getString(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();
}
}
}
String[] 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.setString(XMLTools.COLLECTION, collection + "/" + file.getName());
process(new File(directory + "//" + file.getName()), table);
}
} finally {
if (col != null) {
col.close();
}
}
return true;
}
public void usage() {
System.out.println("Format: xindice export -c <context> [-l [-d <path>]] [-v] [parameters...]");
System.out.println();
System.out.println("Takes a collection tree and creates a directory tree from it");
System.out.println();
System.out.println("Command-specific switches:");
System.out.println(" -f|--filepath <path>");
System.out.println(" Directory name where tree will be created");
System.out.println();
}
}