KickJava   Java API By Example, From Geeks To Geeks.

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


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.L10N;
32 import com.caucho.vfs.Depend;
33 import com.caucho.vfs.WriteStream;
34
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.NamedNodeMap JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.UserDataHandler JavaDoc;
41
42 import javax.xml.namespace.QName JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 /**
47  * QAbstractNode is an abstract implementation for any DOM node.
48  */

49 public abstract class QAbstractNode implements CauchoNode, java.io.Serializable JavaDoc {
50   protected static L10N L = new L10N(QAbstractNode.class);
51   
52   QDocument _owner;
53
54   QNode _parent;
55
56   QAbstractNode _next;
57   QAbstractNode _previous;
58
59   String JavaDoc _systemId;
60   String JavaDoc _filename;
61   int _line;
62
63   protected QAbstractNode()
64   {
65   }
66
67   protected QAbstractNode(QDocument owner)
68   {
69     _owner = owner;
70   }
71
72   public void setLocation(String JavaDoc systemId, String JavaDoc filename,
73               int line, int column)
74   {
75     _systemId = systemId;
76     _filename = filename;
77     _line = line;
78   }
79
80   /**
81    * Returns the node's source filename.
82    */

83   public String JavaDoc getFilename()
84   {
85     if (_filename != null)
86       return _filename;
87     else if (_previous != null)
88       return _previous.getFilename();
89     else if (_parent != null)
90       return _parent.getFilename();
91     else
92       return null;
93   }
94
95   /**
96    * Returns the base URI of the node.
97    */

98   public String JavaDoc getBaseURI()
99   {
100     if (_systemId != null)
101       return _systemId;
102     else if (_previous != null)
103       return _previous.getBaseURI();
104     else if (_parent != null)
105       return _parent.getBaseURI();
106     else
107       return getFilename();
108   }
109
110   /**
111    * Returns the base URI
112    */

113   public static String JavaDoc baseURI(Node JavaDoc node)
114   {
115     if (node instanceof QAbstractNode)
116       return ((QAbstractNode) node).getBaseURI();
117     else
118       return null;
119   }
120
121   /**
122    * Returns the node's source line.
123    */

124   public int getLine()
125   {
126     if (_filename != null)
127       return _line;
128     else if (_previous != null)
129       return _previous.getLine();
130     else if (_parent != null)
131       return _parent.getLine();
132     else
133       return 0;
134   }
135
136   public int getColumn()
137   {
138     return 0;
139   }
140
141   /**
142    * Returns the owning document.
143    */

144   public Document JavaDoc getOwnerDocument()
145   {
146     return _owner;
147   }
148
149   public boolean isSupported(String JavaDoc feature, String JavaDoc version)
150   {
151     return _owner.getImplementation().hasFeature(feature, version);
152   }
153
154   /**
155    * Returns a feature value.
156    */

157   public Object JavaDoc getFeature(String JavaDoc feature, String JavaDoc version)
158   {
159     return null;
160   }
161
162   /**
163    * Sets a feature value.
164    */

165   public void setFeature(String JavaDoc feature, boolean value)
166   {
167   }
168
169   /**
170    * Compares the document position
171    */

172   public short compareDocumentPosition(Node JavaDoc node)
173   {
174     return 0;
175   }
176
177   /**
178    * Looks up a prefix value.
179    */

180   public String JavaDoc lookupPrefix(String JavaDoc feature)
181   {
182     return null;
183   }
184   
185   /**
186    * Returns true if the node has attributes.
187    */

188   public boolean hasAttributes()
189   {
190     return false;
191   }
192
193   public String JavaDoc getPrefix()
194   {
195     return "";
196   }
197
198   public void setPrefix(String JavaDoc prefix)
199   {
200   }
201
202   public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc value, UserDataHandler JavaDoc userData)
203   {
204     return null;
205   }
206
207   public Object JavaDoc getUserData(String JavaDoc data)
208   {
209     return null;
210   }
211
212   public String JavaDoc getCanonicalName()
213   {
214     return getNodeName();
215   }
216
217   public String JavaDoc getLocalName()
218   {
219     return getNodeName();
220   }
221
222   public String JavaDoc getNamespaceURI()
223   {
224     return "";
225   }
226
227   public QName JavaDoc getQName()
228   {
229     return new QName JavaDoc(getNodeName(), getNamespaceURI());
230   }
231
232   public String JavaDoc getNodeValue() { return null; }
233
234   public void setNodeValue(String JavaDoc value) {}
235
236   public Node JavaDoc getParentNode() { return _parent; }
237
238   public NodeList JavaDoc getChildNodes()
239   {
240     return new QEmptyNodeList();
241   }
242
243   public Node JavaDoc getFirstChild() { return null; }
244
245   public Node JavaDoc getLastChild() { return null; }
246
247   public Node JavaDoc getPreviousSibling() { return _previous; }
248
249   public Node JavaDoc getNextSibling() { return _next; }
250
251   public NamedNodeMap JavaDoc getAttributes() { return null; }
252
253   public Node JavaDoc insertBefore(Node JavaDoc newChild, Node JavaDoc refChild)
254     throws DOMException JavaDoc
255   {
256     throw new QDOMException(DOMException.HIERARCHY_REQUEST_ERR, "");
257   }
258
259   public Node JavaDoc replaceChild(Node JavaDoc newChild, Node JavaDoc refChild)
260     throws DOMException JavaDoc
261   {
262     throw new QDOMException(DOMException.HIERARCHY_REQUEST_ERR, "");
263   }
264
265   public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc
266   {
267     throw new QDOMException(DOMException.HIERARCHY_REQUEST_ERR, "");
268   }
269
270   public Node JavaDoc appendChild(Node JavaDoc newNode) throws DOMException JavaDoc
271   {
272     throw new QDOMException(DOMException.HIERARCHY_REQUEST_ERR, "");
273   }
274
275   public boolean hasChildNodes() { return false; }
276
277   public boolean equals(Node JavaDoc arg, boolean deep)
278   {
279     return this == arg;
280   }
281
282   void remove()
283   {
284     if (_owner != null)
285       _owner._changeCount++;
286     
287     if (_previous != null)
288       _previous._next = _next;
289     else if (_parent != null)
290       _parent._firstChild = _next;
291
292     if (_next != null)
293       _next._previous = _previous;
294     else if (_parent != null)
295       _parent._lastChild = _previous;
296
297     _previous = null;
298     _next = null;
299     _parent = null;
300   }
301
302   public QAbstractNode getNextPreorder()
303   {
304     if (_next != null)
305       return _next;
306
307     for (QNode ptr = _parent; ptr != null; ptr = ptr._parent) {
308       if (ptr._next != null)
309     return ptr._next;
310     }
311
312     return null;
313   }
314
315   public boolean hasContent() { return false; }
316
317   public QAbstractNode getNextContent()
318   {
319     for (QAbstractNode node = _next; node != null; node = node._next) {
320       if (node.hasContent())
321     return node;
322     }
323
324     return null;
325   }
326
327   public QAbstractNode getPreviousContent()
328   {
329     for (QAbstractNode node = _previous; node != null; node = node._previous) {
330       if (node.hasContent())
331     return node;
332     }
333
334     return null;
335   }
336
337   public String JavaDoc getTextValue()
338   {
339     return getNodeValue();
340   }
341
342   /**
343    * Support the same and the implementation
344    */

345   public boolean supports(String JavaDoc feature, String JavaDoc version)
346   {
347     return _owner._implementation.hasFeature(feature, version);
348   }
349
350   public void normalize()
351   {
352     
353   }
354
355   public Node JavaDoc cloneNode(boolean deep)
356   {
357     return _owner.importNode(this, deep);
358   }
359
360   // DOM level 3
361

362   public short compareTreePosition(Node JavaDoc other)
363   {
364     throw new UnsupportedOperationException JavaDoc();
365   }
366
367   public String JavaDoc getTextContent()
368     throws DOMException JavaDoc
369   {
370     return XmlUtil.textValue(this);
371   }
372   
373   public void setTextContent(String JavaDoc textContent)
374     throws DOMException JavaDoc
375   {
376     throw new UnsupportedOperationException JavaDoc();
377   }
378
379   public boolean isSameNode(Node JavaDoc other)
380   {
381     return this == other;
382   }
383
384   public String JavaDoc lookupNamespacePrefix(String JavaDoc namespaceURI,
385                                       boolean useDefault)
386   {
387     throw new UnsupportedOperationException JavaDoc();
388   }
389
390   public boolean isDefaultNamespace(String JavaDoc namespaceURI)
391   {
392     throw new UnsupportedOperationException JavaDoc();
393   }
394
395   public String JavaDoc lookupNamespaceURI(String JavaDoc prefix)
396   {
397     throw new UnsupportedOperationException JavaDoc();
398   }
399
400   public boolean isEqualNode(Node JavaDoc arg)
401   {
402     return equals(arg);
403   }
404
405   public Node JavaDoc getInterface(String JavaDoc feature)
406   {
407     throw new UnsupportedOperationException JavaDoc();
408   }
409
410   /*
411   public Object setUserData(String key,
412                             Object data,
413                             UserDataHandler handler)
414   {
415     throw new UnsupportedOperationException();
416   }
417   
418   public Object getUserData(String key)
419   {
420     throw new UnsupportedOperationException();
421   }
422   */

423
424   // Caucho stuff
425

426   public ArrayList JavaDoc<Depend> getDependencyList()
427   {
428     if (_owner != null)
429       return _owner.getDependencyList();
430     else
431       return null;
432   }
433   
434   boolean isNameValid(String JavaDoc name)
435   {
436     if (name == null || name.length() == 0)
437       return false;
438
439     if (! XmlChar.isNameStart(name.charAt(0)))
440       return false;
441     
442     for (int i = 1; i < name.length(); i++) {
443       char ch = name.charAt(i);
444       if (! XmlChar.isNameChar(ch))
445     return false;
446     }
447
448     return true;
449   }
450
451   public boolean checkValid()
452     throws Exception JavaDoc
453   {
454     if (_parent == null) {
455       if (_next != null || _previous != null)
456     throw new Exception JavaDoc("null bad: " + this);
457       else
458     return true;
459     }
460
461     if (_parent._owner != _owner && _owner != _parent)
462       throw new Exception JavaDoc("owner bad: " + this);
463
464     QAbstractNode ptr = _parent._firstChild;
465     for (; ptr != null && ptr != this; ptr = ptr._next) {
466     }
467     if (ptr == null)
468       throw new Exception JavaDoc("not in parent: " + this);
469
470     ptr = _parent._lastChild;
471     for (; ptr != null && ptr != this; ptr = ptr._previous) {
472     }
473     if (ptr == null)
474       throw new Exception JavaDoc("not in parent: " + this);
475
476     if (_next == null && _parent._lastChild != this)
477       throw new Exception JavaDoc("bad tail: " + this);
478
479     else if (_next != null && _next._previous != this)
480       throw new Exception JavaDoc("bad link: " + this);
481
482     if (_previous == null && _parent._firstChild != this)
483       throw new Exception JavaDoc("bad head: " + this);
484     else if (_previous != null && _previous._next != this)
485       throw new Exception JavaDoc("bad link: " + this);
486
487     return true;
488   }
489
490   void print(XmlPrinter out) throws IOException JavaDoc
491   {
492   }
493
494   public void print(WriteStream out) throws IOException JavaDoc
495   {
496     new XmlPrinter(out).printXml(this);
497   }
498
499   public void printPretty(WriteStream out) throws IOException JavaDoc
500   {
501     new XmlPrinter(out).printPrettyXml(this);
502   }
503
504   public void printHtml(WriteStream out) throws IOException JavaDoc
505   {
506     new XmlPrinter(out).printHtml(this);
507   }
508
509   private Object JavaDoc writeReplace()
510   {
511     return new SerializedXml(this);
512   }
513 }
514
Popular Tags