Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 121   Methods: 2
NCLOC: 71   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XUpdate.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: XUpdate.java 593399 2007-11-09 02:27:03Z 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.XUpdateQueryService;
 27   
 28    import java.io.BufferedReader;
 29    import java.io.File;
 30    import java.io.FileInputStream;
 31    import java.io.InputStreamReader;
 32   
 33    /**
 34    * XUpdate runs an XUpdate query stored in a file against the specified collection or
 35    * document.
 36    *
 37    * @author Kimbro Staken <kstaken@xmldatabases.org>
 38    * @version $Revision: 593399 $, $Date: 2007-11-09 02:27:03 +0000 (Fri, 09 Nov 2007) $
 39    */
 40    public class XUpdate extends Command {
 41   
 42  0 public boolean execute(XMLTools.Config table) throws Exception {
 43   
 44  0 if (table.getString(XMLTools.COLLECTION) == null) {
 45  0 System.out.println("ERROR : Collection name and switch required");
 46  0 return false;
 47    }
 48   
 49  0 if ("".equals(table.getString(XMLTools.FILE_PATH))) {
 50  0 System.out.println("ERROR : Path to file containing XUpdate to execute required");
 51  0 return false;
 52    }
 53   
 54  0 Collection col = null;
 55  0 try {
 56   
 57  0 String name = table.getString(XMLTools.NAME_OF);
 58   
 59    // Nomalize the collection URI
 60  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 61    table.getBoolean(XMLTools.LOCAL));
 62   
 63    // Read in the XUpdate commands from the file
 64  0 StringBuffer commands = new StringBuffer();
 65  0 File file = new File(table.getString(XMLTools.FILE_PATH));
 66   
 67  0 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
 68  0 String line;
 69  0 while ((line = reader.readLine()) != null) {
 70  0 commands.append(line);
 71    }
 72   
 73  0 reader.close();
 74   
 75    // Get the collection reference for the requested collection
 76  0 col = DatabaseManager.getCollection(colstring);
 77  0 if (col == null) {
 78  0 System.out.println("ERROR : Collection not found!");
 79  0 return false;
 80    }
 81   
 82    // To run XUpdate commands we use the XUpdateQueryService
 83  0 XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
 84   
 85  0 long result;
 86    // See if we're updating one document or the whole collection.
 87  0 if (name == null) {
 88  0 result = service.update(commands.toString());
 89    }
 90    else {
 91  0 result = service.updateResource(name, commands.toString());
 92    }
 93   
 94  0 System.out.println(result + " documents updated");
 95    } finally {
 96  0 if (col != null) {
 97  0 col.close();
 98    }
 99    }
 100   
 101  0 return true;
 102    }
 103   
 104  0 public void usage() {
 105  0 System.out.println("Format: xindice xupdate -c <context> [-l [-d <path>]] [-v] [parameters...]");
 106  0 System.out.println();
 107  0 System.out.println("Runs XUpdate query stored in a file against a specified collection or");
 108  0 System.out.println("document");
 109  0 System.out.println();
 110  0 System.out.println("Command-specific switches:");
 111  0 System.out.println(" -f|--filepath <file>");
 112  0 System.out.println(" Name of the file that holds XUpdate query");
 113  0 System.out.println(" -n|--nameOf <name>");
 114  0 System.out.println(" Document name, if query to be run against single document");
 115  0 System.out.println();
 116  0 System.out.println("Examples:");
 117  0 System.out.println(" xindice xupdate -c /db/test -f /path/to/xupdate.xml");
 118  0 System.out.println(" xindice xupdate -c /db/test -n document-to-update.xml -f /path/to/xupdate.xml");
 119  0 System.out.println();
 120    }
 121    }