001: package demo;
002:
003: import java.util.Iterator;
004: import java.io.OutputStreamWriter;
005: import java.io.PrintWriter;
006:
007: import org.w3c.dom.Node;
008: import org.w3c.dom.NodeList;
009: import org.w3c.dom.NamedNodeMap;
010:
011: import org.apache.commons.jxpath.JXPathContext;
012: import org.apache.commons.jxpath.Pointer;
013:
014: import org.jaxen.XPathSyntaxException;
015: import org.jaxen.JaxenException;
016: import org.jaxen.dom.html.HTMLXPath;
017:
018: import org.enhydra.xml.xmlc.StreamXMLCLogger;
019: import org.enhydra.xml.xmlc.XMLObject;
020: import org.enhydra.xml.xmlc.StreamXMLCLogger;
021: import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
022:
023: public class XPath {
024: public static final int XPATH_IMPL_JAXEN = 0;
025: public static final int XPATH_IMPL_JXPATH = 1;
026:
027: static private XMLCDeferredParsingFactory xmlcFactory = null;
028:
029: protected XMLCDeferredParsingFactory getFactory() {
030: if (xmlcFactory != null) {
031: return xmlcFactory;
032: }
033:
034: PrintWriter writer = new PrintWriter(new OutputStreamWriter(
035: System.err));
036: StreamXMLCLogger logger = new StreamXMLCLogger(writer, writer,
037: writer);
038: xmlcFactory = new XMLCDeferredParsingFactory(null, this
039: .getClass().getClassLoader(), null);
040: xmlcFactory.addResourceDir(".");
041: xmlcFactory.addResourceDir("./input");
042: xmlcFactory.addResourceDir("./build/src");
043: xmlcFactory.addPackagePrefix("demo");
044:
045: xmlcFactory.setDefaultMetaDataPath("input/options.xmlc");
046: return xmlcFactory;
047: }
048:
049: public Iterator query(XMLObject xmlObject, String query,
050: int xpathImpl) {
051: /* create an Iterator to hold the results of the query */
052: Iterator iter = new java.util.ArrayList().iterator(); //guarantee default non-null Iterator
053: switch (xpathImpl) {
054: case XPATH_IMPL_JAXEN:
055: try {
056: org.jaxen.XPath xpath = new HTMLXPath(query, true);
057: iter = xpath.selectNodes((Node) xmlObject).iterator();
058: } catch (XPathSyntaxException xse) {
059: System.err.println(xse.getMultilineMessage());
060: } catch (JaxenException je) {
061: je.printStackTrace();
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: break;
066: case XPATH_IMPL_JXPATH:
067: JXPathContext context = JXPathContext.newContext(xmlObject);
068: iter = context.iteratePointers(query);
069: break;
070: }
071: return iter;
072: }
073:
074: public Iterator query(String file, String query, int xpathImpl) {
075: /* loading the document with dyanmic loading */
076: XMLObject xmlObject = getFactory().createFromFile(file);
077: return query(xmlObject, query, xpathImpl);
078: }
079:
080: public static void main(String[] args) {
081: System.out.println("\nJXPath Result...");
082: JXPathMain(args);
083: System.out.println("\nJaxen Result...");
084: JaxenMain(args);
085: }
086:
087: public static void JXPathMain(String[] args) {
088: if (args.length == 2) {
089: XPath xpath = new XPath();
090: int count = 0;
091: for (Iterator iter = xpath.query(args[0], args[1],
092: XPATH_IMPL_JXPATH); iter.hasNext(); count++) {
093: Pointer pointer = (Pointer) iter.next();
094: Node node = (Node) pointer.getNode();
095: String nodeValue = node.getNodeValue();
096: if (nodeValue == null) {
097: NodeTreeGenerator ntgen = new NodeTreeGenerator();
098: ntgen.setDoIndent(true);
099: ntgen.makeNodeTree(node);
100: nodeValue = ntgen.getNodeTree();
101: }
102: //System.out.println ("Result " + count + " --- " + pointer.getNode().getClass() + " value:\n" + pointer.getValue());
103: System.out.println("Result " + count + " --- "
104: + node.getClass() + " value:\n" + nodeValue);
105: }
106: } else {
107: System.out.println("xpath file query");
108: }
109: }
110:
111: public static void JaxenMain(String[] args) {
112: if (args.length == 2) {
113: XPath xpath = new XPath();
114: int count = 0;
115: for (Iterator iter = xpath.query(args[0], args[1],
116: XPATH_IMPL_JAXEN); iter.hasNext(); count++) {
117: Node node = (Node) iter.next();
118: String nodeValue = node.getNodeValue();
119: if (nodeValue == null) {
120: NodeTreeGenerator ntgen = new NodeTreeGenerator();
121: ntgen.setDoIndent(true);
122: ntgen.makeNodeTree(node);
123: nodeValue = ntgen.getNodeTree();
124: }
125: System.out.println("Result " + count + " --- "
126: + node.getClass() + " value:\n" + nodeValue);
127: }
128: } else {
129: System.out.println("xpath file query");
130: }
131: }
132:
133: public static class NodeTreeGenerator {
134:
135: // a counter for keeping track of the "tabs"
136: private int tabCounter = 1;
137: private StringBuffer nodeTreeBuffer;
138: private boolean doIndent = true;
139:
140: public NodeTreeGenerator() {
141: nodeTreeBuffer = new StringBuffer(200);
142: }
143:
144: public void setDoIndent(boolean idoIndent) {
145: this .doIndent = idoIndent;
146: }
147:
148: public String getNodeTree() {
149: return this .nodeTreeBuffer.toString();
150: }
151:
152: // this is a recursive function to traverse the document tree
153: private void makeNodeTree(Node node) {
154: String content = "";
155: int type = node.getNodeType();
156:
157: // check if element
158: if (type == Node.ELEMENT_NODE) {
159: formatTree(tabCounter);
160: nodeTreeBuffer.append("Elt: ").append(
161: node.getNodeName()).append("\n");
162:
163: // check if the element has any attributes
164: if (node.hasAttributes()) {
165: // if it does, store it in a NamedNodeMap object
166: NamedNodeMap attributesList = node.getAttributes();
167:
168: // iterate through the NamedNodeMap and get the attribute names and values
169: for (int j = 0; j < attributesList.getLength(); j++) {
170: formatTree(tabCounter);
171: nodeTreeBuffer.append("Attr: ").append(
172: attributesList.item(j).getNodeName())
173: .append(" = ").append(
174: attributesList.item(j)
175: .getNodeValue())
176: .append("\n");
177: }
178: }
179: } else if (type == Node.TEXT_NODE) {
180: // check if text node and print value
181: content = node.getNodeValue();
182: if (!content.trim().equals("")) {
183: formatTree(tabCounter);
184: nodeTreeBuffer.append("Txt: ").append(content)
185: .append("\n");
186: }
187: } else if (type == Node.COMMENT_NODE) {
188: // check if comment node and print value
189: content = node.getNodeValue();
190: if (!content.trim().equals("")) {
191: formatTree(tabCounter);
192: nodeTreeBuffer.append("Cmnt: ").append(content)
193: .append("\n");
194: }
195: }
196:
197: // check if current node has any children
198: if (node.hasChildNodes()) {
199: // if it does, iterate through the collection
200: NodeList children = node.getChildNodes();
201: for (int i = 0; i < children.getLength(); i++) {
202: tabCounter++;
203: // recursively call function to proceed to next level
204: makeNodeTree(children.item(i));
205: tabCounter--;
206: }
207: }
208: }
209:
210: // this formats the output for the generated tree
211: private void formatTree(int tabCounter) {
212: if (this .doIndent) {
213: for (int j = 1; j < tabCounter; j++) {
214: nodeTreeBuffer.append(" ");
215: }
216: }
217: }
218: }
219:
220: }
|