001: /* Copyright 2004 The Apache Software Foundation
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.apache.xmlbeans.samples.xquery;
016:
017: import org.apache.xmlbeans.XmlException;
018: import org.apache.xmlbeans.XmlObject;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: /**
024: * A sample to XMLBeans API features for executing XPath and XQuery
025: * expressions. The sample illustrates these features:
026: *
027: * - Using the XmlObject.selectPath and XmlCursor.selectPath methods
028: * to execute XPath expressions. The selectPath method's results (if
029: * any) are always chunks of the instance queried against. In other
030: * words, changes to query results change the original instance.
031: * However, you work with results differently depending on whether
032: * selectPath was called from an XmlObject or XmlCursor instance. See
033: * the SelectPath class for more information.
034: * - Using the XmlObject.execQuery and XmlCursor.execQuery methods
035: * to execute XQuery expressions. Results of these queries are copied
036: * into new XML, meaning that changes to results do not change the
037: * original instance. Here again, you work with results differently
038: * depending how which method you used to query. See the ExecQuery
039: * class for more information.
040: */
041: public class XQueryXPath {
042: /**
043: * Receives an employees list XML instance, passing the instance to
044: * methods that execute queries against it.
045: *
046: * @param args An array in which the first item is a
047: * path to the XML instance file.
048: */
049: public static void main(String[] args)
050: throws org.apache.xmlbeans.XmlException,
051: java.io.IOException {
052: XQueryXPath sample = new XQueryXPath();
053: sample.executeQueries(args);
054: }
055:
056: /**
057: * Returns <code>true</code> if all of the sample methods returned true
058: * (i.e., their query expressions returned results).
059: *
060: * @param args An array in which the first item is a
061: * path to the XML instance file.
062: * @return <code>true</code> if all of the sample methods returned true
063: * (i.e., their query expressions returned results); otherwise,
064: * <code>false</code>.
065: */
066: public boolean executeQueries(String[] args) {
067: XmlObject xml = this .parseXml(args[0]);
068:
069: // Execute the XQuery samples.
070: ExecQuery xquerySample = new ExecQuery();
071:
072: System.out
073: .println("Running ExecQuery.selectEmpsByStateCursor\n");
074: boolean xqWorkPhoneSuccessful = xquerySample
075: .updateWorkPhone(xml);
076:
077: System.out
078: .println("Running ExecQuery.selectZipsNewDocCursor\n");
079: boolean xqCollectZips = xquerySample.collectZips(xml);
080:
081: // Execute the XPath samples.
082: SelectPath xpathSample = new SelectPath();
083:
084: System.out.println("Running SelectPath.updateWorkPhone \n");
085: boolean xpWorkPhoneSuccessful = xpathSample
086: .updateWorkPhone(xml);
087:
088: System.out.println("Running SelectPath.collectNames \n");
089: boolean xpCollectNames = xpathSample.collectNames(xml);
090:
091: return (xqWorkPhoneSuccessful && xqCollectZips
092: && xpWorkPhoneSuccessful && xpCollectNames) ? true
093: : false;
094: }
095:
096: /**
097: * <p>Creates a File from the XML path provided in main arguments, then
098: * parses the file's contents into a type generated from schema.</p>
099:
100: * @param xmlFilePath A path to XML based on the schema in inventory.xsd.
101: * @return An instance of a generated schema type that contains the parsed
102: * XML.
103: */
104: public XmlObject parseXml(String xmlFilePath) {
105: File xmlFile = new File(xmlFilePath);
106: XmlObject xml = null;
107: try {
108: xml = XmlObject.Factory.parse(xmlFile);
109: } catch (XmlException e) {
110: e.printStackTrace();
111: } catch (IOException e) {
112: e.printStackTrace();
113: }
114: return xml;
115: }
116: }
|