Source Code Cross Referenced for XPath.java in  » J2EE » xmlc-2.3 » demo » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » xmlc 2.3 » demo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.DOMXPath;
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 DOMXPath(query);
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.