KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > xml > Dom4jNavigator


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.ext.xml;
54
55 import java.io.StringWriter JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.List JavaDoc;
58
59 import freemarker.template.TemplateModelException;
60
61 import org.dom4j.Attribute;
62 import org.dom4j.Branch;
63 import org.dom4j.Document;
64 import org.dom4j.DocumentType;
65 import org.dom4j.Element;
66 import org.dom4j.Node;
67 import org.dom4j.ProcessingInstruction;
68 import org.dom4j.tree.DefaultAttribute;
69 import org.jaxen.Context;
70 import org.jaxen.NamespaceContext;
71 import org.jaxen.dom4j.Dom4jXPath;
72
73 /**
74  * @version $Id: Dom4jNavigator.java,v 1.3 2003/01/31 11:39:17 szegedia Exp $
75  * @author Attila Szegedi
76  */

77 class Dom4jNavigator extends Navigator {
78
79     Dom4jNavigator() {
80     }
81
82     void getAsString(Object JavaDoc node, StringWriter JavaDoc sw) {
83         sw.getBuffer().append(((Node)node).asXML());
84     }
85
86     void getChildren(Object JavaDoc node, String JavaDoc localName, String JavaDoc namespaceUri, List JavaDoc result) {
87         if(node instanceof Element) {
88             Element e = (Element)node;
89             if(localName == null) {
90                 result.addAll(e.elements());
91             }
92             else {
93                 result.addAll(e.elements(e.getQName().getDocumentFactory().createQName(localName, "", namespaceUri)));
94             }
95         }
96         else if(node instanceof Document) {
97             Element root = ((Document)node).getRootElement();
98             if(localName == null || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) {
99                 result.add(root);
100             }
101         }
102     }
103     
104     void getAttributes(Object JavaDoc node, String JavaDoc localName, String JavaDoc namespaceUri, List JavaDoc result) {
105         if(node instanceof Element) {
106             Element e = (Element)node;
107             if(localName == null) {
108                 result.addAll(e.attributes());
109             }
110             else {
111                 Attribute attr = e.attribute(e.getQName().getDocumentFactory().createQName(localName, "", namespaceUri));
112                 if(attr != null) {
113                     result.add(attr);
114                 }
115             }
116         }
117         else if (node instanceof ProcessingInstruction) {
118             ProcessingInstruction pi = (ProcessingInstruction)node;
119             if ("target".equals(localName)) {
120                 result.add(new DefaultAttribute("target", pi.getTarget()));
121             }
122             else if ("data".equals(localName)) {
123                 result.add(new DefaultAttribute("data", pi.getText()));
124             }
125             else {
126                 result.add(new DefaultAttribute(localName, pi.getValue(localName)));
127             }
128         } else if (node instanceof DocumentType) {
129             DocumentType doctype = (DocumentType)node;
130             if ("publicId".equals(localName)) {
131                 result.add(new DefaultAttribute("publicId", doctype.getPublicID()));
132             }
133             else if ("systemId".equals(localName)) {
134                 result.add(new DefaultAttribute("systemId", doctype.getSystemID()));
135             }
136             else if ("elementName".equals(localName)) {
137                 result.add(new DefaultAttribute("elementName", doctype.getElementName()));
138             }
139         }
140     }
141
142     void getDescendants(Object JavaDoc node, List JavaDoc result) {
143         if(node instanceof Branch) {
144             getDescendants((Branch)node, result);
145         }
146     }
147     
148     private void getDescendants(Branch node, List JavaDoc result) {
149         List JavaDoc content = node.content();
150         for (Iterator JavaDoc iter = content.iterator(); iter.hasNext();) {
151             Node subnode = (Node) iter.next();
152             if(subnode instanceof Element) {
153                 result.add(subnode);
154                 getDescendants(subnode, result);
155             }
156         }
157     }
158
159     Object JavaDoc getParent(Object JavaDoc node) {
160         return ((Node)node).getParent();
161     }
162
163     Object JavaDoc getDocument(Object JavaDoc node) {
164         return ((Node)node).getDocument();
165     }
166
167     Object JavaDoc getDocumentType(Object JavaDoc node) {
168         return
169             node instanceof Document
170             ? ((Document)node).getDocType()
171             : null;
172     }
173     
174     void getContent(Object JavaDoc node, List JavaDoc result) {
175         if(node instanceof Branch) {
176             result.addAll(((Branch)node).content());
177         }
178     }
179
180     String JavaDoc getText(Object JavaDoc node) {
181         return ((Node)node).getText();
182     }
183
184     String JavaDoc getLocalName(Object JavaDoc node) {
185         return ((Node)node).getName();
186     }
187
188     String JavaDoc getNamespacePrefix(Object JavaDoc node) {
189         if(node instanceof Element) {
190             return ((Element)node).getNamespacePrefix();
191         }
192         if(node instanceof Attribute) {
193             return ((Attribute)node).getNamespacePrefix();
194         }
195         return null;
196     }
197
198     String JavaDoc getNamespaceUri(Object JavaDoc node) {
199         if(node instanceof Element) {
200             return ((Element)node).getNamespaceURI();
201         }
202         if(node instanceof Attribute) {
203             return ((Attribute)node).getNamespaceURI();
204         }
205         return null;
206     }
207
208     String JavaDoc getType(Object JavaDoc node) {
209         switch(((Node)node).getNodeType()) {
210             case Node.ATTRIBUTE_NODE: {
211                 return "attribute";
212             }
213             case Node.CDATA_SECTION_NODE: {
214                 return "cdata";
215             }
216             case Node.COMMENT_NODE: {
217                 return "comment";
218             }
219             case Node.DOCUMENT_NODE: {
220                 return "document";
221             }
222             case Node.DOCUMENT_TYPE_NODE: {
223                 return "documentType";
224             }
225             case Node.ELEMENT_NODE: {
226                 return "element";
227             }
228             case Node.ENTITY_REFERENCE_NODE: {
229                 return "entityReference";
230             }
231             case Node.NAMESPACE_NODE: {
232                 return "namespace";
233             }
234             case Node.PROCESSING_INSTRUCTION_NODE: {
235                 return "processingInstruction";
236             }
237             case Node.TEXT_NODE: {
238                 return "text";
239             }
240         }
241         return "unknown";
242     }
243
244     XPathEx createXPathEx(String JavaDoc xpathString) throws TemplateModelException
245     {
246         try {
247             return new Dom4jXPathEx(xpathString);
248         }
249         catch(Exception JavaDoc e) {
250             throw new TemplateModelException(e);
251         }
252     }
253
254     private static final class Dom4jXPathEx
255     extends
256         Dom4jXPath
257     implements
258         XPathEx
259     {
260         Dom4jXPathEx(String JavaDoc path)
261         throws
262             Exception JavaDoc
263         {
264             super(path);
265         }
266
267         public List JavaDoc selectNodes(Object JavaDoc object, NamespaceContext namespaces)
268         throws
269             TemplateModelException
270         {
271             Context context = getContext(object);
272             context.getContextSupport().setNamespaceContext(namespaces);
273             try {
274                 return selectNodesForContext(context);
275             }
276             catch(Exception JavaDoc e) {
277                 throw new TemplateModelException(e);
278             }
279         }
280     }
281 }
282
Popular Tags