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.xml;
030:
031: import org.w3c.dom.Attr;
032: import org.w3c.dom.DOMException;
033: import org.w3c.dom.NamedNodeMap;
034: import org.w3c.dom.Node;
035:
036: public abstract class QAttributedNode extends QNode {
037: QAttr _firstAttribute;
038:
039: /**
040: * Returns a map of the attributes.
041: */
042: public NamedNodeMap getAttributes() {
043: return new QAttributeMap(this );
044: }
045:
046: /**
047: * Returns true if the element has attributes.
048: */
049: public boolean hasAttributes() {
050: return _firstAttribute != null;
051: }
052:
053: /**
054: * Returns the first attribute in the attribute list.
055: */
056: public Attr getFirstAttribute() {
057: return _firstAttribute;
058: }
059:
060: /**
061: * Returns the named attribute.
062: */
063: public String getAttribute(String name) {
064: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
065: if (name.equals(attr.getNodeName()))
066: return attr.getNodeValue();
067: }
068:
069: return "";
070: }
071:
072: /**
073: * Returns the attribute specified by a namespace.
074: */
075: public String getAttributeNS(String namespaceURI, String local) {
076: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
077: String attrURI = attr.getNamespaceURI();
078:
079: if (attr.getLocalName().equals(local)
080: && (attrURI == namespaceURI || attrURI != null
081: && attrURI.equals(namespaceURI)))
082: return attr.getNodeValue();
083: }
084:
085: return "";
086: }
087:
088: public boolean hasAttribute(String name) {
089: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
090: if (attr.getNodeName().equals(name))
091: return true;
092: }
093:
094: return false;
095: }
096:
097: public boolean hasAttributeNS(String uri, String local) {
098: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
099: String attrURI = attr.getNamespaceURI();
100:
101: if (attr.getLocalName().equals(local)
102: && (attrURI == uri || attrURI != null
103: && attrURI.equals(uri)))
104: return true;
105: }
106:
107: return false;
108: }
109:
110: /**
111: * Returns the attribute specified by the name.
112: */
113: public Attr getAttributeNode(String name) {
114: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
115: if (attr.getNodeName().equals(name))
116: return (Attr) attr;
117: }
118:
119: return null;
120: }
121:
122: public Attr getAttributeNodeNS(String uri, String local) {
123: for (QAbstractNode attr = _firstAttribute; attr != null; attr = attr._next) {
124: String attrURI = attr.getNamespaceURI();
125:
126: if (attr.getLocalName().equals(local)
127: && (attrURI == uri || attrURI != null
128: && attrURI.equals(uri)))
129: return (Attr) attr;
130: }
131:
132: return null;
133: }
134:
135: public void setAttribute(String name, String value)
136: throws DOMException {
137: if (!isNameValid(name))
138: throw new QDOMException(DOMException.INVALID_CHARACTER_ERR,
139: "illegal attribute `" + name + "'");
140:
141: setAttributeNode(_owner.createAttribute(name, value));
142: }
143:
144: public void setAttributeNS(String uri, String local, String value) {
145: Attr attr = _owner.createAttributeNS(uri, local);
146: attr.setNodeValue(value);
147:
148: setAttributeNodeNS(attr);
149: }
150:
151: void setAttribute(QName name, String value) throws DOMException {
152: setAttributeNode(_owner.createAttribute(name, value));
153: }
154:
155: /**
156: * Sets an attribute, specified by the object.
157: */
158: public void setIdAttribute(String name, boolean isId)
159: throws DOMException {
160: }
161:
162: /**
163: * Sets an attribute, specified by the object.
164: */
165: public void setIdAttributeNS(String namespaceURI, String localName,
166: boolean isId) throws DOMException {
167: }
168:
169: /**
170: * Sets an attribute, specified by the object.
171: */
172: public void setIdAttributeNode(Attr attr, boolean isId)
173: throws DOMException {
174: }
175:
176: /**
177: * Sets an attribute, specified by the object.
178: */
179: public Attr setAttributeNode(Attr attr) throws DOMException {
180: QAttr qAttr = (QAttr) attr;
181:
182: if (qAttr._owner == null)
183: qAttr._owner = _owner;
184: else if (qAttr._owner != _owner)
185: throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR,
186: "attribute from wrong document");
187:
188: if (qAttr._parent != null)
189: throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR,
190: "attribute `" + attr.getNodeName() + "' is in use");
191:
192: qAttr._parent = this ;
193:
194: // remove any matching old attribute
195: QAttr old = unlink(attr.getNodeName());
196:
197: QAttr ptr = _firstAttribute;
198:
199: if (ptr == null) {
200: _firstAttribute = qAttr;
201: } else {
202: for (; ptr._next != null; ptr = (QAttr) ptr._next) {
203: }
204:
205: ptr._next = qAttr;
206: }
207:
208: return old;
209: }
210:
211: public Attr setAttributeNodeNS(Attr attr) throws DOMException {
212: QAttr qAttr = (QAttr) attr;
213:
214: if (qAttr._owner != _owner)
215: throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR,
216: "attribute from wrong document");
217:
218: if (qAttr._parent != null)
219: throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR,
220: "attribute `" + attr.getNodeName() + "' is in use");
221:
222: // remove any matching old attribute
223: QAttr old = unlink(qAttr.getNamespaceURI(), qAttr
224: .getLocalName());
225:
226: qAttr._parent = this ;
227:
228: qAttr._next = _firstAttribute;
229: _firstAttribute = qAttr;
230:
231: return old;
232: }
233:
234: /**
235: * Removes the named attribute.
236: */
237: public void removeAttribute(String name) {
238: if (!isNameValid(name))
239: throw new QDOMException(DOMException.INVALID_CHARACTER_ERR,
240: "illegal attribute `" + name + "'");
241:
242: unlink(name);
243: }
244:
245: /**
246: * Removes the attribute specified by the localname and namespace.
247: */
248: public void removeAttributeNS(String uri, String name) {
249: unlink(uri, name);
250: }
251:
252: /**
253: * Removes the matching attribute.
254: */
255: public Attr removeAttributeNode(Attr attr) {
256: return unlink(attr.getNodeName());
257: }
258:
259: /**
260: * Removes the matching attribute.
261: */
262: public Attr removeAttributeNodeNS(Attr attr) {
263: return unlink(attr.getNamespaceURI(), attr.getLocalName());
264: }
265:
266: /**
267: * Unlinks an attribute, returning it.
268: */
269: QAttr unlink(String name) {
270: QAttr prev = null;
271: QAttr ptr;
272:
273: for (ptr = _firstAttribute; ptr != null
274: && !ptr.getNodeName().equals(name); ptr = (QAttr) ptr._next) {
275: prev = ptr;
276: }
277:
278: if (ptr == null)
279: return null;
280:
281: if (prev == null)
282: _firstAttribute = (QAttr) ptr._next;
283: else
284: prev._next = ptr._next;
285:
286: ptr._next = null;
287:
288: return ptr;
289: }
290:
291: /**
292: * Removes the attribute named by the URI and local name.
293: */
294: public QAttr unlink(String uri, String local) {
295: if (local == null || uri == null)
296: return null;
297:
298: QAttr prev = null;
299: QAttr ptr;
300:
301: for (ptr = (QAttr) _firstAttribute; ptr != null
302: && (!local.equals(ptr.getLocalName()) || !uri
303: .equals(ptr.getNamespaceURI())); ptr = (QAttr) ptr._next) {
304: prev = ptr;
305: }
306:
307: if (ptr == null)
308: return null;
309:
310: if (prev == null)
311: _firstAttribute = (QAttr) ptr._next;
312: else
313: prev._next = ptr._next;
314:
315: ptr._next = null;
316:
317: return ptr;
318: }
319:
320: static class QAttributeMap implements NamedNodeMap {
321: QAttributedNode _elt;
322: int _i;
323: QAttr _attr;
324:
325: QAttributeMap(QAttributedNode elt) {
326: _elt = elt;
327: }
328:
329: public Node getNamedItem(String name) {
330: return _elt.getAttributeNode(name);
331: }
332:
333: public Node getNamedItemNS(String uri, String localName) {
334: return _elt.getAttributeNodeNS(uri, localName);
335: }
336:
337: public Node setNamedItem(Node arg) throws DOMException {
338: return _elt.setAttributeNode((Attr) arg);
339: }
340:
341: public Node setNamedItemNS(Node arg) {
342: return _elt.setAttributeNodeNS((Attr) arg);
343: }
344:
345: public Node removeNamedItem(String name) throws DOMException {
346: return _elt.unlink(name);
347: }
348:
349: public Node removeNamedItemNS(String uri, String localName) {
350: return _elt.getAttributeNodeNS(uri, localName);
351: }
352:
353: public Node item(int index) {
354: QAbstractNode attr = _elt._firstAttribute;
355:
356: while (index > 0 && attr != null) {
357: attr = attr._next;
358: index--;
359: }
360:
361: return attr;
362: }
363:
364: public int getLength() {
365: int length = 0;
366:
367: for (QAbstractNode attr = _elt._firstAttribute; attr != null; attr = attr._next)
368: length++;
369:
370: return length;
371: }
372: }
373: }
|