001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021: package org.josso;
022:
023: import org.w3c.dom.Node;
024: import org.xmldb.common.xml.queries.XUpdateQuery;
025: import org.xmldb.xupdate.lexus.XUpdateQueryImpl;
026:
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.transform.Result;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.Transformer;
032: import javax.xml.transform.TransformerFactory;
033: import javax.xml.transform.dom.DOMSource;
034: import javax.xml.transform.stream.StreamResult;
035: import java.io.BufferedReader;
036: import java.io.File;
037: import java.io.FileReader;
038:
039: /**
040: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
041: * @version $Id: XUpdate.java 508 2008-02-18 13:32:29Z sgonzalez $
042: */
043:
044: public class XUpdate {
045:
046: /**
047: * Main-method. You can manually test your update operations.
048: */
049: public static void main(String args[]) throws Exception {
050: if (args.length == 0) {
051: System.err
052: .println("usage: java org.xmldb.xupdate.lexus.XUpdateQueryImpl update document");
053: System.err
054: .println(" update - filename of the file which contains XUpdate operations");
055: System.err
056: .println(" document - filename of the file which contains the content to update");
057: System.exit(0);
058: }
059:
060: // parse the update file
061: File file = new File(args[0]);
062: BufferedReader br = new BufferedReader(new FileReader(file));
063: char[] characters = new char[new Long(file.length()).intValue()];
064: br.read(characters, 0, new Long(file.length()).intValue());
065: String queryStr = new String(characters);
066:
067: // parse the document file
068: Node myDocument = null;
069: DocumentBuilderFactory parserFactory = DocumentBuilderFactory
070: .newInstance();
071:
072: parserFactory.setValidating(false);
073: parserFactory.setNamespaceAware(true);
074: parserFactory.setIgnoringElementContentWhitespace(true);
075:
076: DocumentBuilder builder = parserFactory.newDocumentBuilder();
077: myDocument = builder.parse(args[1]);
078:
079: System
080: .setProperty(
081: "org.xmldb.common.xml.queries.XPathQueryFactory",
082: "org.xmldb.common.xml.queries.xalan2.XPathQueryFactoryImpl");
083: // update the document and print time used for the updates
084: XUpdateQuery xq = new XUpdateQueryImpl();
085:
086: System.err.println("Starting updates...");
087: long timeStart = System.currentTimeMillis();
088:
089: xq.setQString(queryStr);
090: xq.execute(myDocument);
091:
092: File out = new File("data-mod.xml");
093: Result result = new StreamResult(out);
094:
095: // Write the DOM document to the file
096: Source source = new DOMSource(myDocument);
097: Transformer xformer = TransformerFactory.newInstance()
098: .newTransformer();
099: xformer.transform(source, result);
100:
101: long timeEnd = System.currentTimeMillis();
102: System.err.println("Updates done in " + (timeEnd - timeStart)
103: + " ms ...");
104: }
105: }
|