001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml2;
030:
031: import com.caucho.util.CharBuffer;
032:
033: import org.w3c.dom.*;
034:
035: import java.io.IOException;
036: import java.util.HashMap;
037: import javax.xml.namespace.QName;
038:
039: /**
040: * Resin's implementation of the DOM element.
041: */
042: public class QElement extends QAttributedNode implements CauchoElement {
043: private QName _name;
044:
045: /**
046: * Create a new element.
047: */
048: public QElement() {
049: }
050:
051: /**
052: * Create a new named element.
053: *
054: * @param name the element's name.
055: */
056: public QElement(String name) {
057: _name = new QName(name);
058: }
059:
060: /**
061: * Create a new named element.
062: *
063: * @param name the element's name.
064: */
065: public QElement(String name, String namespace) {
066: _name = new QName(name, namespace);
067: }
068:
069: /**
070: * Create a new named element.
071: *
072: * @param name the element's name.
073: */
074: public QElement(QName name) {
075: _name = name;
076: }
077:
078: protected QElement(QDocument owner, QName name) {
079: _owner = owner;
080: _name = name;
081: }
082:
083: /**
084: * Create a new named element with initial parameters.
085: *
086: * @param name the element's name.
087: * @param attributes the element's attributes.
088: */
089: QElement(QName name, HashMap attributes) {
090: _name = name;
091: }
092:
093: /**
094: * Assign a name to the element. Not normally called by external
095: * API.
096: *
097: * @param name the element's name.
098: */
099: public void setName(QName name) {
100: _name = name;
101: }
102:
103: /**
104: * Returns the qname
105: */
106: public QName getQName() {
107: return _name;
108: }
109:
110: /**
111: * Returns the element's qualified-name as the node name.
112: */
113: public String getNodeName() {
114: return _name.getLocalPart();
115: }
116:
117: /**
118: * Returns the element's qualified-name as the node name.
119: */
120: public String getTagName() {
121: return _name.getLocalPart();
122: }
123:
124: /**
125: * Returns the local part of the element's name.
126: */
127: public String getLocalName() {
128: return _name.getLocalPart();
129: }
130:
131: /**
132: * Returns the namespace prefix for the element.
133: */
134: public String getPrefix() {
135: return _name.getPrefix();
136: }
137:
138: /**
139: * Returns the canonical name of the element.
140: */
141: public String getCanonicalName() {
142: return _name.toString();
143: }
144:
145: /**
146: * Returns the namespace of the element.
147: */
148: public String getNamespaceURI() {
149: return _name.getNamespaceURI();
150: }
151:
152: /**
153: * Given a prefix, returns the namespace in effect at this element.
154: *
155: * @param prefix the prefix to test.
156: * @return the namespace URL matching the prefix or null.
157: */
158: public String getNamespace(String prefix) {
159: if (prefix == null)
160: return getNamespace("", "xmlns");
161: else
162: return getNamespace(prefix, "xmlns:" + prefix);
163: }
164:
165: private String getNamespace(String prefix, String xmlns) {
166: Attr namespace = getAttributeNode(xmlns);
167:
168: if (namespace != null)
169: return namespace.getNodeValue();
170:
171: if (_parent instanceof QElement)
172: return ((QElement) _parent).getNamespace(prefix, xmlns);
173:
174: return _owner.getNamespace(prefix);
175: }
176:
177: /**
178: * Returns the DOM NodeType, ELEMENT_NODE.
179: */
180: public short getNodeType() {
181: return ELEMENT_NODE;
182: }
183:
184: /**
185: * Returns the schema type.
186: */
187: public TypeInfo getSchemaTypeInfo() {
188: return null;
189: }
190:
191: /**
192: * Returns a list of elements, given a tag name.
193: */
194: public NodeList getElementsByTagName(String tagName) {
195: QAbstractNode child = (QAbstractNode) getFirstChild();
196:
197: if (child != null)
198: return new QDeepNodeList(this , child, new TagPredicate(
199: tagName));
200: else
201: return new QDeepNodeList(this , null, new TagPredicate(
202: tagName));
203: }
204:
205: /**
206: * Returns a list of elements, given a namespace and a local name.
207: */
208: public NodeList getElementsByTagNameNS(String uri, String name) {
209: QAbstractNode child = (QAbstractNode) getFirstChild();
210:
211: if (child != null)
212: return new QDeepNodeList(this , child, new NSTagPredicate(
213: uri, name));
214: else
215: return new QDeepNodeList(this , null, new NSTagPredicate(
216: uri, name));
217: }
218:
219: /**
220: * Appends a new node as the last child of the element.
221: *
222: * @param child the new child.
223: * @return the child.
224: */
225: public Node appendChild(Node child) throws DOMException {
226: Node result = super .appendChild(child);
227:
228: if (child instanceof QElement) {
229: QElement elt = (QElement) child;
230: QName name = elt._name;
231:
232: if (name.getNamespaceURI() != "") {
233: addNamespace(name);
234: }
235:
236: for (QAttr attr = (QAttr) elt.getFirstAttribute(); attr != null; attr = (QAttr) attr
237: .getNextSibling()) {
238: name = attr._name;
239:
240: if (name.getNamespaceURI() != "") {
241: addNamespace(name);
242: }
243: }
244: }
245:
246: return result;
247: }
248:
249: /**
250: * Adds the name to the global namespace, if possible.
251: */
252: void addNamespace(QName name) {
253: _owner.addNamespace(name.getPrefix(), name.getNamespaceURI());
254: }
255:
256: /**
257: * Normalize the element, i.e. smash all neighboring text nodes together.
258: */
259: public void normalize() {
260: Node node = _firstChild;
261:
262: while (node != null) {
263: if (node.getNodeType() == TEXT_NODE
264: && node.getNextSibling() != null
265: && node.getNextSibling().getNodeType() == TEXT_NODE) {
266: Text text = (Text) node;
267: Text next = (Text) node.getNextSibling();
268: text.appendData(next.getData());
269: removeChild(next);
270: } else if (node.getNodeType() == ELEMENT_NODE) {
271: Element elt = (Element) node;
272: elt.normalize();
273: node = node.getNextSibling();
274: } else
275: node = node.getNextSibling();
276: }
277: }
278:
279: public boolean hasContent() {
280: return true;
281: }
282:
283: public boolean equals(Object arg) {
284: return this == arg;
285: }
286:
287: public boolean equals(Node arg, boolean deep) {
288: return this == arg;
289: }
290:
291: /**
292: * Returns the text value of the element. For an element, the text
293: * value is the smashing together of all the child text nodes.
294: */
295: public String getTextValue() {
296: CharBuffer cb = CharBuffer.allocate();
297:
298: for (QAbstractNode node = _firstChild; node != null; node = node._next) {
299: cb.append(node.getTextValue());
300: }
301:
302: return cb.close();
303: }
304:
305: void print(XmlPrinter out) throws IOException {
306: out.startElement(getNamespaceURI(), getLocalName(),
307: getNodeName());
308: for (QAbstractNode node = (QAbstractNode) getFirstAttribute(); node != null; node = (QAbstractNode) node
309: .getNextSibling()) {
310: out.attribute(node.getNamespaceURI(), node.getLocalName(),
311: node.getNodeName(), node.getNodeValue());
312: }
313: for (Node node = getFirstChild(); node != null; node = node
314: .getNextSibling()) {
315: ((QAbstractNode) node).print(out);
316: }
317: out
318: .endElement(getNamespaceURI(), getLocalName(),
319: getNodeName());
320: }
321:
322: public String toString() {
323: CharBuffer cb = CharBuffer.allocate();
324:
325: cb.append("Element[" + _name);
326:
327: for (QAttr attr = (QAttr) getFirstAttribute(); attr != null; attr = (QAttr) attr
328: .getNextSibling())
329: cb.append(" " + attr);
330: cb.append("]");
331:
332: return cb.close();
333: }
334:
335: private Object writeReplace() {
336: return new SerializedXml(this );
337: }
338:
339: static class TagPredicate implements QNodePredicate {
340: String _name;
341:
342: TagPredicate(String name) {
343: if (name == null)
344: name = "*";
345:
346: _name = name;
347: }
348:
349: public boolean isMatch(QAbstractNode node) {
350: return (node.getNodeName().equals(_name) || _name
351: .equals("*")
352: && node instanceof Element);
353: }
354: }
355:
356: static class NSTagPredicate implements QNodePredicate {
357: String _uri;
358: String _local;
359:
360: NSTagPredicate(String uri, String local) {
361: _uri = uri;
362: _local = local;
363: }
364:
365: public boolean isMatch(QAbstractNode node) {
366: return (_local.equals(node.getLocalName()) && _uri
367: .equals(node.getNamespaceURI()));
368: }
369: }
370: }
|