Package org.apache.xindice.tools.command

Source Code of org.apache.xindice.tools.command.XUpdate

/*
* 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: XUpdate.java 593399 2007-11-09 02:27:03Z 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.modules.XUpdateQueryService;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
* XUpdate runs an XUpdate query stored in a file against the specified collection or
* document.
*
* @author Kimbro Staken <kstaken@xmldatabases.org>
* @version $Revision: 593399 $, $Date: 2007-11-08 21:27:03 -0500 (Thu, 08 Nov 2007) $
*/
public class XUpdate extends Command {

    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 ("".equals(table.getString(XMLTools.FILE_PATH))) {
            System.out.println("ERROR : Path to file containing XUpdate to execute required");
            return false;
        }

        Collection col = null;
        try {

            String name = table.getString(XMLTools.NAME_OF);
           
            // Nomalize the collection URI
            String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
                                                      table.getBoolean(XMLTools.LOCAL));

            // Read in the XUpdate commands from the file
            StringBuffer commands = new StringBuffer();
            File file = new File(table.getString(XMLTools.FILE_PATH));

            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                commands.append(line);
            }

            reader.close();

            // Get the collection reference for the requested collection
            col = DatabaseManager.getCollection(colstring);
            if (col == null) {
                System.out.println("ERROR : Collection not found!");
                return false;
            }

            // To run XUpdate commands we use the XUpdateQueryService
            XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
           
            long result;
            // See if we're updating one document or the whole collection.
            if (name == null) {
                result = service.update(commands.toString());
            }
            else {
                result = service.updateResource(name, commands.toString());
            }
               
            System.out.println(result + " documents updated");
        } finally {
            if (col != null) {
                col.close();
            }
        }

        return true;
    }

    public void usage() {
        System.out.println("Format: xindice xupdate -c <context> [-l [-d <path>]] [-v] [parameters...]");
        System.out.println();
        System.out.println("Runs XUpdate query stored in a file against a specified collection or");
        System.out.println("document");
        System.out.println();
        System.out.println("Command-specific switches:");
        System.out.println("    -f|--filepath <file>");
        System.out.println("                 Name of the file that holds XUpdate query");
        System.out.println("    -n|--nameOf <name>");
        System.out.println("                 Document name, if query to be run against single document");
        System.out.println();
        System.out.println("Examples:");
        System.out.println("    xindice xupdate -c /db/test -f /path/to/xupdate.xml");
        System.out.println("    xindice xupdate -c /db/test -n document-to-update.xml -f /path/to/xupdate.xml");
        System.out.println();
    }
}
TOP

Related Classes of org.apache.xindice.tools.command.XUpdate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.