KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > xpointer > XalanXPointer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: XalanXPointer.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml.xpointer;
21
22 import java.util.Vector JavaDoc;
23
24 import org.apache.lenya.xml.DOMParserFactory;
25 import org.apache.log4j.Category;
26 import org.apache.xpath.XPathAPI;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32
33 /**
34  * XPointer implementation
35  */

36 public class XalanXPointer implements XPointer {
37     private static Category log = Category.getInstance(XalanXPointer.class);
38
39     /**
40      * DOCUMENT ME!
41      *
42      * @param args DOCUMENT ME!
43      */

44     public static void main(String JavaDoc[] args) {
45         XPointer xpointer = new XalanXPointer();
46
47         if (args.length != 2) {
48             System.err.println("Usage: java " + xpointer.getClass().getName() +
49                 " example.xml \"/Example/People/Person[position() < 2]/Street/@Number\"");
50
51             return;
52         }
53
54         DOMParserFactory dpf = new DOMParserFactory();
55         Document JavaDoc document = null;
56
57         try {
58             document = dpf.getDocument(args[0]);
59         } catch (Exception JavaDoc e) {
60             System.err.println(xpointer.getClass().getName() + ".main(): " + e);
61         }
62
63         Element JavaDoc root = document.getDocumentElement();
64         String JavaDoc xpath = args[1];
65
66         try {
67             Vector JavaDoc namespaces = new Vector JavaDoc();
68             Vector JavaDoc nodes = xpointer.select(root, xpath, namespaces);
69
70             for (int i = 0; i < nodes.size(); i++) {
71                 Node JavaDoc node = (Node JavaDoc) nodes.elementAt(i);
72                 short type = node.getNodeType();
73
74                 if (type == Node.ATTRIBUTE_NODE) {
75                     System.out.println("Attribute (" + node.getNodeName() + "): " +
76                         node.getNodeValue());
77                 } else if (type == Node.ELEMENT_NODE) {
78                     System.out.println("Element (" + node.getNodeName() + "): " +
79                         node.getFirstChild().getNodeValue());
80                 }
81             }
82         } catch (Exception JavaDoc e) {
83             System.err.println(e);
84         }
85     }
86
87     /**
88      * Select node by specified XPath
89      *
90      * @param node Node to select from
91      * @param xpath XPath to select nodes
92      *
93      * @return Selected nodes
94      *
95      * @exception Exception ...
96      */

97     public Vector JavaDoc select(Node JavaDoc node, String JavaDoc xpath, Vector JavaDoc namespaces) throws Exception JavaDoc {
98         NodeList JavaDoc children = node.getChildNodes();
99
100         log.debug("Select " + xpath + " from node " + node.getNodeName());
101
102         NodeList JavaDoc nl = null;
103         if (namespaces.size() > 0) {
104             org.w3c.dom.Document JavaDoc doc = org.apache.lenya.xml.DocumentHelper.createDocument("", "foo", null);
105             for (int i = 0; i < namespaces.size(); i++) {
106                 String JavaDoc namespace = (String JavaDoc)namespaces.elementAt(i);
107                 String JavaDoc prefix = namespace.substring(0, namespace.indexOf("="));
108                 String JavaDoc namespaceURI = namespace.substring(namespace.indexOf("=") + 1);
109                 log.debug("Namespace: " + prefix + " " + namespaceURI);
110
111                 doc.getDocumentElement().setAttribute("xmlns:" + prefix, namespaceURI);
112             }
113             nl = XPathAPI.selectNodeList(node, xpath, doc.getDocumentElement());
114         } else {
115             nl = XPathAPI.selectNodeList(node, xpath);
116         }
117
118
119     if (nl != null && nl.getLength() == 0) {
120             log.info("No such nodes: " + xpath);
121             return new Vector JavaDoc();
122         }
123
124         Vector JavaDoc nodes = new Vector JavaDoc();
125
126         for (int i = 0; i < nl.getLength(); i++) {
127             nodes.addElement(nl.item(i));
128         }
129
130         return nodes;
131     }
132 }
133
Popular Tags