KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > 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.xml;
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 java.io.IOException JavaDoc;
43 import java.util.ArrayList JavaDoc;
44
45 /**
46  * QAbstractNode is an abstract implementation for any DOM node.
47  */

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

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

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

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

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

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

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

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

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

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

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

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

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

422
423   // Caucho stuff
424

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