Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 159   Methods: 4
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExportTree.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: ExportTree.java 578602 2007-09-23 20:33:31Z natalia $
 18    */
 19   
 20    package org.apache.xindice.tools.command;
 21   
 22    import org.apache.xindice.tools.XMLTools;
 23    import org.xmldb.api.DatabaseManager;
 24    import org.xmldb.api.base.Collection;
 25    import org.xmldb.api.base.ErrorCodes;
 26    import org.xmldb.api.base.Resource;
 27    import org.xmldb.api.base.XMLDBException;
 28   
 29    import java.io.File;
 30    import java.io.FileOutputStream;
 31   
 32    /**
 33    * ExportTree is designed to take a Collection tree and create a Directory
 34    * tree from it. Export tree requires a collection name and a file path to create the tree
 35    *
 36    * @version $Revision: 578602 $, $Date: 2007-09-23 13:33:31 -0700 (Sun, 23 Sep 2007) $
 37    */
 38    public class ExportTree extends Command {
 39   
 40    /**
 41    * Exports a collection into a file system
 42    *
 43    * @param table Description of Parameter
 44    * @return Description of the Returned Value
 45    */
 46  0 public boolean execute(XMLTools.Config table) throws Exception {
 47   
 48  0 if (table.getString(XMLTools.COLLECTION) == null) {
 49  0 System.out.println("ERROR : Collection name and switch required");
 50  0 return false;
 51    }
 52   
 53  0 if (table.getString(XMLTools.FILE_PATH).equals("")) {
 54  0 System.out.println("ERROR: Directory name and switch required");
 55  0 return false;
 56    }
 57   
 58  0 File fp = new File(table.getString(XMLTools.FILE_PATH));
 59  0 System.out.println();
 60   
 61  0 String parent = parentDir(table.getString(XMLTools.COLLECTION));
 62  0 File dir = new File(fp, parent);
 63   
 64  0 System.out.println("Creating directory " + dir.getPath());
 65  0 dir.mkdir();
 66   
 67  0 process(dir, table);
 68   
 69  0 return true;
 70    }
 71   
 72  0 private String parentDir(String parent) {
 73  0 int idx = parent.lastIndexOf("/");
 74  0 if (idx != -1) {
 75  0 return parent.substring(idx + 1);
 76    } else {
 77  0 return parent;
 78    }
 79    }
 80   
 81    /**
 82    * Export given collection to the directory
 83    */
 84  0 boolean process(File directory, XMLTools.Config table) throws Exception {
 85  0 Collection col = null;
 86  0 try {
 87  0 String collection = table.getString(XMLTools.COLLECTION);
 88   
 89    // Get a Collection reference to the collection
 90  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 91    table.getBoolean(XMLTools.LOCAL));
 92  0 col = DatabaseManager.getCollection(colstring);
 93  0 if (col == null) {
 94  0 System.out.println("ERROR : Collection not found!");
 95  0 return false;
 96    }
 97   
 98  0 String[] files = null;
 99  0 try {
 100  0 files = col.listResources();
 101    } catch (XMLDBException e) {
 102    // Xindice will throw an error if there is no indexer
 103  0 if (e.errorCode != ErrorCodes.VENDOR_ERROR) {
 104  0 throw e;
 105    }
 106    }
 107   
 108  0 if (files != null) {
 109  0 System.out.println("Extracting " + files.length + " files from " + table.getString(XMLTools.COLLECTION));
 110  0 for (int j = 0; j < files.length; j++) {
 111  0 Resource res = col.getResource(files[j]);
 112  0 Object content = res.getContent();
 113  0 FileOutputStream output = new FileOutputStream(new File(directory, files[j]));
 114  0 try {
 115  0 if (content instanceof String) {
 116    // UTF8FIXED: as we omit encoding declaration in output XML
 117    // for the moment, we MUST write file in UTF-8
 118  0 output.write(((String)content).getBytes("utf-8"));
 119    } else {
 120  0 output.write((byte[]) content);
 121    }
 122    } finally {
 123  0 output.close();
 124    }
 125    }
 126    }
 127   
 128  0 String[] list = col.listChildCollections();
 129  0 for (int i = 0; i < list.length; i++) {
 130  0 File file = new File(directory, list[i]);
 131   
 132  0 System.out.println("Creating directory " + file.getPath());
 133  0 file.mkdirs();
 134   
 135  0 table.setString(XMLTools.COLLECTION, collection + "/" + file.getName());
 136   
 137  0 process(new File(directory + "//" + file.getName()), table);
 138    }
 139   
 140    } finally {
 141  0 if (col != null) {
 142  0 col.close();
 143    }
 144    }
 145  0 return true;
 146    }
 147   
 148  0 public void usage() {
 149  0 System.out.println("Format: xindice export -c <context> [-l [-d <path>]] [-v] [parameters...]");
 150  0 System.out.println();
 151  0 System.out.println("Takes a collection tree and creates a directory tree from it");
 152  0 System.out.println();
 153  0 System.out.println("Command-specific switches:");
 154  0 System.out.println(" -f|--filepath <path>");
 155  0 System.out.println(" Directory name where tree will be created");
 156  0 System.out.println();
 157    }
 158    }
 159