KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > jxpath > DOMFactory


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 package org.apache.cocoon.util.jxpath;
17
18 import org.apache.commons.jxpath.AbstractFactory;
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.Pointer;
21 import org.w3c.dom.Attr JavaDoc;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 /**
27  * A <a HREF="http://jakarta.apache.org/commons/jxpath">JXPath</a> <code>AbstractFactory</code>
28  * that creates DOM elements.
29  *
30  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
31  * @version $Id: DOMFactory.java 125919 2005-01-21 11:12:03Z sylvain $
32  */

33
34 public class DOMFactory extends AbstractFactory {
35
36     /**
37      * Return <b>false</b> if this factory cannot create the requested object.
38      */

39     public boolean createObject(
40         JXPathContext context,
41         Pointer pointer,
42         Object JavaDoc parent,
43         String JavaDoc name,
44         int index)
45     {
46          //FIXME: JXPath automatically creates attributes if the element already exists,
47
//but does not call this method if the element does not exit
48

49         addDOMElement((Node JavaDoc) parent, index, name);
50         
51         return true;
52     }
53
54     private void addDOMElement(Node JavaDoc parent, int index, String JavaDoc tag) {
55         int pos = tag.indexOf(':');
56         String JavaDoc prefix = null;
57         if (pos != -1) {
58             prefix = tag.substring(0, pos);
59         }
60         String JavaDoc uri = null;
61         
62         Node JavaDoc child = parent.getFirstChild();
63         int count = 0;
64         while (child != null) {
65             if (child.getNodeName().equals(tag)) {
66                 count++;
67             }
68             child = child.getNextSibling();
69         }
70
71         Document JavaDoc doc = parent.getOwnerDocument();
72         
73         if (doc != null) {
74             uri = getNamespaceURI((Element JavaDoc)parent, prefix);
75         } else {
76             if (parent instanceof Document JavaDoc) {
77                 doc = (Document JavaDoc)parent;
78                 if (prefix != null) {
79                     throw new RuntimeException JavaDoc("Cannot map non-null prefix " +
80                         "when creating a document element");
81                 }
82             } else { // Shouldn't happen (must be a DocumentType object)
83
throw new RuntimeException JavaDoc("Node of class " +
84                     parent.getClass().getName() + " has null owner document " +
85                     "but is not a Document");
86             }
87
88         }
89
90         // Keep inserting new elements until we have index + 1 of them
91
while (count <= index) {
92             Node JavaDoc newElement = doc.createElementNS(uri, tag);
93             parent.appendChild(newElement);
94             count++;
95         }
96     }
97     
98     public String JavaDoc getNamespaceURI(Element JavaDoc element, String JavaDoc prefix) {
99         Node JavaDoc tmp = element;
100         String JavaDoc nsAttr = prefix == null ? "xmlns" : "xmlns:" + prefix;
101         
102         while (tmp != null && tmp.getNodeType() == Node.ELEMENT_NODE) {
103             element = (Element JavaDoc)tmp;
104             
105             // First test element prefixes
106
if (prefix == null) {
107                 if (element.getPrefix() == null) {
108                     return element.getNamespaceURI();
109                 }
110             } else if(prefix.equals(element.getPrefix())) {
111                 return element.getNamespaceURI();
112             }
113             
114             // Note: stupid DOM api returns "" when an attribute doesn't exist, so we use the Attr node.
115
Attr JavaDoc nsAttrNode = ((Element JavaDoc)tmp).getAttributeNode(nsAttr);
116             if (nsAttrNode != null) {
117                 return nsAttrNode.getValue();
118             }
119             tmp = tmp.getParentNode();
120         }
121         return null;
122     }
123
124     public boolean declareVariable(JXPathContext context, String JavaDoc name) {
125         return false;
126     }
127 }
128
Popular Tags