KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > QElement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2;
30
31 import com.caucho.util.CharBuffer;
32
33 import org.w3c.dom.*;
34
35 import java.io.IOException JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import javax.xml.namespace.QName JavaDoc;
38
39 /**
40  * Resin's implementation of the DOM element.
41  */

42 public class QElement extends QAttributedNode implements CauchoElement {
43   private QName JavaDoc _name;
44
45   /**
46    * Create a new element.
47    */

48   public QElement()
49   {
50   }
51
52   /**
53    * Create a new named element.
54    *
55    * @param name the element's name.
56    */

57   public QElement(String JavaDoc name)
58   {
59     _name = new QName JavaDoc(name);
60   }
61
62   /**
63    * Create a new named element.
64    *
65    * @param name the element's name.
66    */

67   public QElement(String JavaDoc name, String JavaDoc namespace)
68   {
69     _name = new QName JavaDoc(name, namespace);
70   }
71
72   /**
73    * Create a new named element.
74    *
75    * @param name the element's name.
76    */

77   public QElement(QName JavaDoc name)
78   {
79     _name = name;
80   }
81
82   protected QElement(QDocument owner, QName JavaDoc name)
83   {
84     _owner = owner;
85     _name = name;
86   }
87
88   /**
89    * Create a new named element with initial parameters.
90    *
91    * @param name the element's name.
92    * @param attributes the element's attributes.
93    */

94   QElement(QName JavaDoc name, HashMap JavaDoc attributes)
95   {
96     _name = name;
97   }
98
99   /**
100    * Assign a name to the element. Not normally called by external
101    * API.
102    *
103    * @param name the element's name.
104    */

105   public void setName(QName JavaDoc name)
106   {
107     _name = name;
108   }
109
110   /**
111    * Returns the qname
112    */

113   public QName JavaDoc getQName()
114   {
115     return _name;
116   }
117
118   /**
119    * Returns the element's qualified-name as the node name.
120    */

121   public String JavaDoc getNodeName()
122   {
123     return _name.getLocalPart();
124   }
125
126   /**
127    * Returns the element's qualified-name as the node name.
128    */

129   public String JavaDoc getTagName()
130   {
131     return _name.getLocalPart();
132   }
133
134   /**
135    * Returns the local part of the element's name.
136    */

137   public String JavaDoc getLocalName()
138   {
139     return _name.getLocalPart();
140   }
141
142   /**
143    * Returns the namespace prefix for the element.
144    */

145   public String JavaDoc getPrefix()
146   {
147     return _name.getPrefix();
148   }
149
150   /**
151    * Returns the canonical name of the element.
152    */

153   public String JavaDoc getCanonicalName()
154   {
155     return _name.toString();
156   }
157
158   /**
159    * Returns the namespace of the element.
160    */

161   public String JavaDoc getNamespaceURI()
162   {
163     return _name.getNamespaceURI();
164   }
165
166   /**
167    * Given a prefix, returns the namespace in effect at this element.
168    *
169    * @param prefix the prefix to test.
170    * @return the namespace URL matching the prefix or null.
171    */

172   public String JavaDoc getNamespace(String JavaDoc prefix)
173   {
174     if (prefix == null)
175       return getNamespace("", "xmlns");
176     else
177       return getNamespace(prefix, "xmlns:" + prefix);
178   }
179
180   private String JavaDoc getNamespace(String JavaDoc prefix, String JavaDoc xmlns)
181   {
182     Attr namespace = getAttributeNode(xmlns);
183
184     if (namespace != null)
185       return namespace.getNodeValue();
186
187     if (_parent instanceof QElement)
188       return ((QElement) _parent).getNamespace(prefix, xmlns);
189
190     return _owner.getNamespace(prefix);
191   }
192
193   /**
194    * Returns the DOM NodeType, ELEMENT_NODE.
195    */

196   public short getNodeType()
197   {
198     return ELEMENT_NODE;
199   }
200
201   /**
202    * Returns the schema type.
203    */

204   public TypeInfo getSchemaTypeInfo()
205   {
206     return null;
207   }
208
209   /**
210    * Returns a list of elements, given a tag name.
211    */

212   public NodeList getElementsByTagName(String JavaDoc tagName)
213   {
214     QAbstractNode child = (QAbstractNode) getFirstChild();
215     
216     if (child != null)
217       return new QDeepNodeList(this, child, new TagPredicate(tagName));
218     else
219       return new QDeepNodeList(this, null, new TagPredicate(tagName));
220   }
221   
222   /**
223    * Returns a list of elements, given a namespace and a local name.
224    */

225   public NodeList getElementsByTagNameNS(String JavaDoc uri, String JavaDoc name)
226   {
227     QAbstractNode child = (QAbstractNode) getFirstChild();
228     
229     if (child != null)
230       return new QDeepNodeList(this, child, new NSTagPredicate(uri, name));
231     else
232       return new QDeepNodeList(this, null, new NSTagPredicate(uri, name));
233   }
234
235   /**
236    * Appends a new node as the last child of the element.
237    *
238    * @param child the new child.
239    * @return the child.
240    */

241   public Node appendChild(Node child)
242     throws DOMException
243   {
244     Node result = super.appendChild(child);
245
246     if (child instanceof QElement) {
247       QElement elt = (QElement) child;
248       QName JavaDoc name = elt._name;
249
250       if (name.getNamespaceURI() != "") {
251         addNamespace(name);
252       }
253
254       for (QAttr attr = (QAttr) elt.getFirstAttribute();
255        attr != null;
256        attr = (QAttr) attr.getNextSibling()) {
257     name = attr._name;
258
259     if (name.getNamespaceURI() != "") {
260       addNamespace(name);
261     }
262       }
263     }
264
265     return result;
266   }
267
268   /**
269    * Adds the name to the global namespace, if possible.
270    */

271   void addNamespace(QName JavaDoc name)
272   {
273     _owner.addNamespace(name.getPrefix(), name.getNamespaceURI());
274   }
275
276   /**
277    * Normalize the element, i.e. smash all neighboring text nodes together.
278    */

279   public void normalize()
280   {
281     Node node = _firstChild;
282
283     while (node != null) {
284       if (node.getNodeType() == TEXT_NODE &&
285       node.getNextSibling() != null &&
286       node.getNextSibling().getNodeType() == TEXT_NODE) {
287     Text text = (Text) node;
288     Text next = (Text) node.getNextSibling();
289     text.appendData(next.getData());
290     removeChild(next);
291       } else if (node.getNodeType() == ELEMENT_NODE) {
292     Element elt = (Element) node;
293     elt.normalize();
294     node = node.getNextSibling();
295       } else
296     node = node.getNextSibling();
297     }
298   }
299
300   public boolean hasContent()
301   {
302     return true;
303   }
304
305   public boolean equals(Object JavaDoc arg)
306   {
307     return this == arg;
308   }
309
310   public boolean equals(Node arg, boolean deep)
311   {
312     return this == arg;
313   }
314
315   /**
316    * Returns the text value of the element. For an element, the text
317    * value is the smashing together of all the child text nodes.
318    */

319   public String JavaDoc getTextValue()
320   {
321     CharBuffer cb = CharBuffer.allocate();
322
323     for (QAbstractNode node = _firstChild; node != null; node = node._next) {
324       cb.append(node.getTextValue());
325     }
326
327     return cb.close();
328   }
329
330   void print(XmlPrinter out) throws IOException JavaDoc
331   {
332     out.startElement(getNamespaceURI(), getLocalName(), getNodeName());
333     for (QAbstractNode node = (QAbstractNode) getFirstAttribute();
334          node != null;
335          node = (QAbstractNode) node.getNextSibling()) {
336       out.attribute(node.getNamespaceURI(),
337                     node.getLocalName(),
338                     node.getNodeName(),
339                     node.getNodeValue());
340     }
341     for (Node node = getFirstChild();
342          node != null;
343          node = node.getNextSibling()) {
344       ((QAbstractNode) node).print(out);
345     }
346     out.endElement(getNamespaceURI(), getLocalName(), getNodeName());
347   }
348
349   public String JavaDoc toString()
350   {
351     CharBuffer cb = CharBuffer.allocate();
352
353     cb.append("Element[" + _name);
354
355     for (QAttr attr = (QAttr) getFirstAttribute();
356          attr != null;
357          attr = (QAttr) attr.getNextSibling())
358       cb.append(" " + attr);
359     cb.append("]");
360
361     return cb.close();
362   }
363
364   private Object JavaDoc writeReplace()
365   {
366     return new SerializedXml(this);
367   }
368
369   static class TagPredicate implements QNodePredicate {
370     String JavaDoc _name;
371
372     TagPredicate(String JavaDoc name)
373     {
374       if (name == null)
375         name = "*";
376       
377       _name = name;
378     }
379
380     public boolean isMatch(QAbstractNode node)
381     {
382       return (node.getNodeName().equals(_name) ||
383               _name.equals("*") && node instanceof Element);
384     }
385   }
386
387   static class NSTagPredicate implements QNodePredicate {
388     String JavaDoc _uri;
389     String JavaDoc _local;
390
391     NSTagPredicate(String JavaDoc uri, String JavaDoc local)
392     {
393       _uri = uri;
394       _local = local;
395     }
396
397     public boolean isMatch(QAbstractNode node)
398     {
399       return (_local.equals(node.getLocalName()) &&
400               _uri.equals(node.getNamespaceURI()));
401     }
402   }
403 }
404
Popular Tags