Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 120   Methods: 2
NCLOC: 72   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AddDocument.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: AddDocument.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.xml.sax.InputSource;
 25    import org.xml.sax.XMLReader;
 26    import org.xmldb.api.DatabaseManager;
 27    import org.xmldb.api.base.Collection;
 28    import org.xmldb.api.base.Resource;
 29   
 30    import javax.xml.parsers.SAXParserFactory;
 31    import java.io.File;
 32    import java.io.FileInputStream;
 33    import java.io.InputStream;
 34   
 35    /**
 36    * AddDocument is designed to let the user add single document
 37    * and/or multiple documents to any existing collection
 38    * within the current database.
 39    *
 40    * @version $Revision: 578602 $, $Date: 2007-09-23 13:33:31 -0700 (Sun, 23 Sep 2007) $
 41    */
 42    public class AddDocument extends Command {
 43   
 44    /**
 45    * Adds a document to the collection
 46    */
 47  0 public boolean execute(XMLTools.Config table) throws Exception {
 48   
 49  0 if (table.getString(XMLTools.COLLECTION) == null) {
 50  0 System.out.println("ERROR : Collection and switch required");
 51  0 return false;
 52    }
 53   
 54  0 if ("".equals(table.getString(XMLTools.FILE_PATH))) {
 55  0 System.out.println("ERROR : File path required");
 56  0 return false;
 57    }
 58   
 59  0 Collection col = null;
 60  0 InputStream fis = null;
 61  0 try {
 62    // Create a collection instance
 63  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 64    table.getBoolean(XMLTools.LOCAL));
 65  0 col = DatabaseManager.getCollection(colstring);
 66  0 if (col == null) {
 67  0 System.out.println("ERROR : Collection not found!");
 68  0 return false;
 69    }
 70   
 71  0 File file = new File(table.getString(XMLTools.FILE_PATH));
 72  0 fis = new FileInputStream(file);
 73   
 74    // Parse in XML using Xerces
 75  0 SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
 76  0 spf.setNamespaceAware(true);
 77   
 78  0 XMLReader reader = spf.newSAXParser().getXMLReader();
 79  0 StringSerializer ser = new StringSerializer();
 80  0 reader.setContentHandler(ser);
 81  0 reader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
 82  0 reader.parse(new InputSource(fis));
 83   
 84    // Create the XMLResource and store the document
 85  0 Resource resource = col.createResource(table.getString(XMLTools.NAME_OF), "XMLResource");
 86  0 resource.setContent(ser.toString());
 87  0 col.storeResource(resource);
 88   
 89  0 System.out.println("Added document " + table.getString(XMLTools.COLLECTION) + "/" + resource.getId());
 90    } finally {
 91  0 if (col != null) {
 92  0 col.close();
 93    }
 94   
 95  0 if (fis != null) {
 96  0 fis.close();
 97    }
 98    }
 99   
 100  0 return true;
 101    }
 102   
 103  0 public void usage() {
 104  0 System.out.println("Format: xindice ad -c <context> [-l [-d <path>]] [-v] [parameters...]");
 105  0 System.out.println();
 106  0 System.out.println("Adds a single document to an existing collection");
 107  0 System.out.println();
 108  0 System.out.println("Command-specific switches:");
 109  0 System.out.println(" -f|--filepath <file>");
 110  0 System.out.println(" File path for the document being added to the collection");
 111  0 System.out.println(" -n|--nameOf <name>");
 112  0 System.out.println(" Document key. If this parameter is omitted, database will");
 113  0 System.out.println(" generate an unique key");
 114  0 System.out.println();
 115  0 System.out.println("Examples:");
 116  0 System.out.println(" xindice ad -c /db/test -f /tmp/xmldocument -n myxmldocument");
 117  0 System.out.println(" xindice ad -c /db/test/nested -f /tmp/data.xml");
 118  0 System.out.println();
 119    }
 120    }