Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 128   Methods: 2
NCLOC: 78   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RetrieveDocument.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: RetrieveDocument.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   
 24    import org.xmldb.api.DatabaseManager;
 25    import org.xmldb.api.base.Collection;
 26    import org.xmldb.api.modules.XMLResource;
 27   
 28    import java.io.File;
 29    import java.io.FileOutputStream;
 30   
 31    /**
 32    * RetrieveDocument.java is designed to let the user recall or
 33    * retrieve a Document from a specific Collection and save it to a new location.
 34    *
 35    * @version $Revision: 578602 $, $Date: 2007-09-23 13:33:31 -0700 (Sun, 23 Sep 2007) $
 36    */
 37    public class RetrieveDocument extends Command {
 38   
 39  0 public boolean execute(XMLTools.Config table) throws Exception {
 40   
 41  0 if (table.getString(XMLTools.COLLECTION) == null) {
 42  0 System.out.println("ERROR : Collection name and switch required");
 43  0 return false;
 44    }
 45   
 46  0 if (table.getString(XMLTools.NAME_OF) == null) {
 47  0 System.out.println("ERROR : Document Key and switch required");
 48  0 return false;
 49    }
 50   
 51  0 Collection col = null;
 52  0 try {
 53   
 54    // Create a collection instance
 55  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 56    table.getBoolean(XMLTools.LOCAL));
 57  0 col = DatabaseManager.getCollection(colstring);
 58    // Make sure that the collection was found
 59  0 if (col == null) {
 60  0 System.out.println("ERROR : Collection not found!");
 61  0 return false;
 62    }
 63   
 64  0 String docname = table.getString(XMLTools.NAME_OF);
 65  0 XMLResource resource = (XMLResource) col.getResource(docname);
 66    // Verify that we were able to find the document
 67  0 if (resource == null) {
 68  0 System.out.println("ERROR : Document not found!");
 69  0 return false;
 70    }
 71   
 72  0 String documentstr = (String) resource.getContent();
 73  0 String path = table.getString(XMLTools.FILE_PATH);
 74  0 if (documentstr != null && path != null && !"".equals(path)) {
 75   
 76  0 FileOutputStream out = null;
 77  0 try {
 78  0 File file = new File(table.getString(XMLTools.FILE_PATH));
 79    // Create the directory structure if necessary
 80  0 if (file.getParentFile() != null) {
 81  0 file.getParentFile().mkdirs();
 82    }
 83   
 84  0 out = new FileOutputStream(file);
 85   
 86  0 System.out.println("Writing...");
 87   
 88  0 out.write(documentstr.getBytes("utf-8"));
 89  0 out.close();
 90   
 91  0 System.out.println("Wrote file " + table.getString(XMLTools.FILE_PATH));
 92    } finally {
 93  0 if (out != null) {
 94  0 out.close();
 95    }
 96    }
 97    } else {
 98    // Send to stdout
 99  0 System.out.println(documentstr);
 100    }
 101    } finally {
 102    // Release the collection object
 103  0 if (col != null) {
 104  0 col.close();
 105    }
 106    }
 107   
 108  0 return true;
 109    }
 110   
 111  0 public void usage() {
 112  0 System.out.println("Format: xindice rd -c <context> [-l [-d <path>]] [-v] [parameters...]");
 113  0 System.out.println();
 114  0 System.out.println("Retrieves a document from a specific collection");
 115  0 System.out.println();
 116  0 System.out.println("Command-specific switches:");
 117  0 System.out.println(" -f|--filepath <file>");
 118  0 System.out.println(" File path where document will be stored, if omitted");
 119  0 System.out.println(" document will printed to standard output");
 120  0 System.out.println(" -n|--nameOf <name>");
 121  0 System.out.println(" Document key");
 122  0 System.out.println();
 123  0 System.out.println("Examples:");
 124  0 System.out.println(" xindice rd -c /db/test/ocs -f a:\\file.xml -n file.xml");
 125  0 System.out.println();
 126    }
 127    }
 128