View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *
18   *  The contributing author(s) would like to thank all those who
19   *  helped with testing, bug fixes, and patience.  This wouldn't
20   *  have been possible without all of you.
21   *
22   *  COPYRIGHT NOTICE:
23   * 
24   *  This software and documentation is provided "as is," and
25   *  the copyright holders and contributing author(s) make no
26   *  representations or warranties, express or implied, including
27   *  but not limited to, warranties of merchantability or fitness
28   *  for any particular purpose or that the use of the software or
29   *  documentation will not infringe any third party patents,
30   *  copyrights, trademarks or other rights. 
31   *
32   *  The copyright holders and contributing author(s) will not be
33   *  liable for any direct, indirect, special or consequential damages
34   *  arising out of any use of the software or documentation, even if
35   *  advised of the possibility of such damage.
36   *
37   *  Permission is hereby granted to use, copy, modify, and distribute
38   *  this source code, or portions hereof, documentation and executables,
39   *  for any purpose, without fee, subject to the following restrictions:
40   *
41   *  1. The origin of this source code must not be misrepresented.
42   *  2. Altered versions must be plainly marked as such and must
43   *     not be misrepresented as being the original source.
44   *  3. This Copyright notice may not be removed or altered from any
45   *     source or altered source distribution.
46   * 
47   *  The copyright holders and contributing author(s) specifically
48   *  permit, without fee, and encourage the use of this source code
49   *  as a component for supporting the Hypertext Markup Language in
50   *  commercial products. If you use this source code in a product,
51   *  acknowledgment is not required but would be appreciated.
52   *
53   */
54  package org.w3c.tidy;
55  
56  import org.w3c.dom.DOMConfiguration;
57  import org.w3c.dom.DOMException;
58  
59  
60  /**
61   * DOMDocumentImpl.
62   * @author Dave Raggett <a href="mailto:dsr@w3.org">dsr@w3.org </a>
63   * @author Andy Quick <a href="mailto:ac.quick@sympatico.ca">ac.quick@sympatico.ca </a> (translation to Java)
64   * @author Fabrizio Giustina
65   * @version $Revision: 779 $ ($Author: fgiust $)
66   */
67  public class DOMDocumentImpl extends DOMNodeImpl implements org.w3c.dom.Document
68  {
69  
70      /**
71       * A DOM Document has its own TagTable.
72       */
73      private TagTable tt;
74  
75      /**
76       * Instantiates a new Dom document with a default tag table.
77       * @param adaptee tidy Node
78       */
79      protected DOMDocumentImpl(Node adaptee)
80      {
81          super(adaptee);
82          this.tt = new TagTable();
83      }
84  
85      /**
86       * @see org.w3c.dom.Node#getNodeName
87       */
88      public String getNodeName()
89      {
90          return "#document";
91      }
92  
93      /**
94       * @see org.w3c.dom.Node#getNodeType
95       */
96      public short getNodeType()
97      {
98          return org.w3c.dom.Node.DOCUMENT_NODE;
99      }
100 
101     /**
102      * @see org.w3c.dom.Document#getDoctype
103      */
104     public org.w3c.dom.DocumentType getDoctype()
105     {
106         Node node = this.adaptee.content;
107         while (node != null)
108         {
109             if (node.type == Node.DOCTYPE_TAG)
110             {
111                 break;
112             }
113             node = node.next;
114         }
115         if (node != null)
116         {
117             return (org.w3c.dom.DocumentType) node.getAdapter();
118         }
119 
120         return null;
121     }
122 
123     /**
124      * @todo DOM level 2 getImplementation() Not implemented. Throws NOT_SUPPORTED_ERR.
125      * @see org.w3c.dom.Document#getImplementation
126      */
127     public org.w3c.dom.DOMImplementation getImplementation()
128     {
129         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
130     }
131 
132     /**
133      * @see org.w3c.dom.Document#getDocumentElement
134      */
135     public org.w3c.dom.Element getDocumentElement()
136     {
137         Node node = this.adaptee.content;
138         while (node != null)
139         {
140             if (node.type == Node.START_TAG || node.type == Node.START_END_TAG)
141             {
142                 break;
143             }
144             node = node.next;
145         }
146         if (node != null)
147         {
148             return (org.w3c.dom.Element) node.getAdapter();
149         }
150 
151         return null;
152     }
153 
154     /**
155      * @see org.w3c.dom.Document#createElement
156      */
157     public org.w3c.dom.Element createElement(String tagName) throws DOMException
158     {
159         Node node = new Node(Node.START_END_TAG, null, 0, 0, tagName, this.tt);
160         if (node != null)
161         {
162             if (node.tag == null) // Fix Bug 121206
163             {
164                 node.tag = TagTable.XML_TAGS;
165             }
166             return (org.w3c.dom.Element) node.getAdapter();
167         }
168 
169         return null;
170     }
171 
172     /**
173      * @todo DOM level 2 createDocumentFragment() Not implemented. Throws NOT_SUPPORTED_ERR.
174      * @see org.w3c.dom.Document#createDocumentFragment
175      */
176     public org.w3c.dom.DocumentFragment createDocumentFragment()
177     {
178         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
179     }
180 
181     /**
182      * @see org.w3c.dom.Document#createTextNode
183      */
184     public org.w3c.dom.Text createTextNode(String data)
185     {
186         byte[] textarray = TidyUtils.getBytes(data);
187         Node node = new Node(Node.TEXT_NODE, textarray, 0, textarray.length);
188         if (node != null)
189         {
190             return (org.w3c.dom.Text) node.getAdapter();
191         }
192 
193         return null;
194     }
195 
196     /**
197      * @see org.w3c.dom.Document#createComment
198      */
199     public org.w3c.dom.Comment createComment(String data)
200     {
201         byte[] textarray = TidyUtils.getBytes(data);
202         Node node = new Node(Node.COMMENT_TAG, textarray, 0, textarray.length);
203         if (node != null)
204         {
205             return (org.w3c.dom.Comment) node.getAdapter();
206         }
207 
208         return null;
209     }
210 
211     /**
212      * @todo DOM level 2 createCDATASection() Not supported. Throws NOT_SUPPORTED_ERR.
213      * @see org.w3c.dom.Document#createCDATASection
214      */
215     public org.w3c.dom.CDATASection createCDATASection(String data) throws DOMException
216     {
217         // NOT_SUPPORTED_ERR: Raised if this document is an HTML document.
218         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "HTML document");
219     }
220 
221     /**
222      * @todo DOM level 2 createProcessingInstruction() Not supported. Throws NOT_SUPPORTED_ERR.
223      * @see org.w3c.dom.Document#createProcessingInstruction
224      */
225     public org.w3c.dom.ProcessingInstruction createProcessingInstruction(String target, String data)
226         throws DOMException
227     {
228         // NOT_SUPPORTED_ERR: Raised if this document is an HTML document.
229         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "HTML document");
230     }
231 
232     /**
233      * @see org.w3c.dom.Document#createAttribute
234      */
235     public org.w3c.dom.Attr createAttribute(String name) throws DOMException
236     {
237         AttVal av = new AttVal(null, null, '"', name, null);
238         if (av != null)
239         {
240             av.dict = AttributeTable.getDefaultAttributeTable().findAttribute(av);
241             return av.getAdapter();
242         }
243 
244         return null;
245     }
246 
247     /**
248      * @todo DOM level 2 createEntityReference() Not supported. Throws NOT_SUPPORTED_ERR.
249      * @see org.w3c.dom.Document#createEntityReference
250      */
251     public org.w3c.dom.EntityReference createEntityReference(String name) throws DOMException
252     {
253         // NOT_SUPPORTED_ERR: Raised if this document is an HTML document
254         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "createEntityReference not supported");
255     }
256 
257     /**
258      * @see org.w3c.dom.Document#getElementsByTagName
259      */
260     public org.w3c.dom.NodeList getElementsByTagName(String tagname)
261     {
262         return new DOMNodeListByTagNameImpl(this.adaptee, tagname);
263     }
264 
265     /**
266      * @todo DOM level 2 importNode() Not supported. Throws NOT_SUPPORTED_ERR.
267      * @see org.w3c.dom.Document#importNode(org.w3c.dom.Node, boolean)
268      */
269     public org.w3c.dom.Node importNode(org.w3c.dom.Node importedNode, boolean deep) throws org.w3c.dom.DOMException
270     {
271         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "importNode not supported");
272     }
273 
274     /**
275      * @todo DOM level 2 createAttributeNS() Not supported. Throws NOT_SUPPORTED_ERR.
276      * @see org.w3c.dom.Document#createAttributeNS(java.lang.String, java.lang.String)
277      */
278     public org.w3c.dom.Attr createAttributeNS(String namespaceURI, String qualifiedName)
279         throws org.w3c.dom.DOMException
280     {
281         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "createAttributeNS not supported");
282     }
283 
284     /**
285      * @todo DOM level 2 createElementNS() Not supported. Throws NOT_SUPPORTED_ERR.
286      * @see org.w3c.dom.Document#createElementNS(java.lang.String, java.lang.String)
287      */
288     public org.w3c.dom.Element createElementNS(String namespaceURI, String qualifiedName)
289         throws org.w3c.dom.DOMException
290     {
291         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "createElementNS not supported");
292     }
293 
294     /**
295      * @todo DOM level 2 getElementsByTagNameNS() Not supported. Throws NOT_SUPPORTED_ERR.
296      * @see org.w3c.dom.Document#getElementsByTagNameNS(java.lang.String, java.lang.String)
297      */
298     public org.w3c.dom.NodeList getElementsByTagNameNS(String namespaceURI, String localName)
299     {
300         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getElementsByTagNameNS not supported");
301     }
302 
303     /**
304      * @todo DOM level 2 getElementById() Not implemented. Returns null.
305      * @see org.w3c.dom.Document#getElementById(java.lang.String)
306      */
307     public org.w3c.dom.Element getElementById(String elementId)
308     {
309         return null;
310     }
311 
312     /**
313      * @todo DOM level 3 adoptNode() Not implemented.
314      * @see org.w3c.dom.Document#adoptNode(org.w3c.dom.Node)
315      */
316     public org.w3c.dom.Node adoptNode(org.w3c.dom.Node source) throws DOMException
317     {
318         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
319     }
320 
321     /**
322      * @todo DOM level 3 getDocumentURI() Not implemented. Returns null.
323      * @see org.w3c.dom.Document#getDocumentURI()
324      */
325     public String getDocumentURI()
326     {
327         return null;
328     }
329 
330     /**
331      * @todo DOM level 3 getDomConfig() Not implemented. Returns null.
332      * @see org.w3c.dom.Document#getDomConfig()
333      */
334     public DOMConfiguration getDomConfig()
335     {
336         return null;
337     }
338 
339     /**
340      * @todo DOM level 3 getInputEncoding() Not implemented. Returns null.
341      * @see org.w3c.dom.Document#getInputEncoding()
342      */
343     public String getInputEncoding()
344     {
345         return null;
346     }
347 
348     /**
349      * @todo DOM level 3 getStrictErrorChecking() Not implemented. Returns true.
350      * @see org.w3c.dom.Document#getStrictErrorChecking()
351      */
352     public boolean getStrictErrorChecking()
353     {
354         return true;
355     }
356 
357     /**
358      * @todo DOM level 3 getXmlEncoding() Not implemented. Returns null.
359      * @see org.w3c.dom.Document#getXmlEncoding()
360      */
361     public String getXmlEncoding()
362     {
363         return null;
364     }
365 
366     /**
367      * @todo DOM level 3 getXmlStandalone() Not implemented. Returns false.
368      * @see org.w3c.dom.Document#getXmlStandalone()
369      */
370     public boolean getXmlStandalone()
371     {
372         return false;
373     }
374 
375     /**
376      * @todo DOM level 3 getXmlVersion() Not implemented. Always returns "1.0".
377      * @see org.w3c.dom.Document#getXmlVersion()
378      */
379     public String getXmlVersion()
380     {
381         // An attribute specifying, as part of the XML declaration, the version number of this document. If there is no
382         // declaration and if this document supports the "XML" feature, the value is "1.0"
383         return "1.0";
384     }
385 
386     /**
387      * @todo DOM level 3 normalizeDocument() Not implemented. Do nothing.
388      * @see org.w3c.dom.Document#normalizeDocument()
389      */
390     public void normalizeDocument()
391     {
392         // do nothing
393     }
394 
395     /**
396      * @todo DOM level 3 renameNode() Not implemented. Throws NOT_SUPPORTED_ERR.
397      * @see org.w3c.dom.Document#renameNode(org.w3c.dom.Node, java.lang.String, java.lang.String)
398      */
399     public org.w3c.dom.Node renameNode(org.w3c.dom.Node n, String namespaceURI, String qualifiedName)
400         throws DOMException
401     {
402         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DOM method not supported");
403     }
404 
405     /**
406      * @todo DOM level 3 setDocumentURI() Not implemented. Do nothing.
407      * @see org.w3c.dom.Document#setDocumentURI(java.lang.String)
408      */
409     public void setDocumentURI(String documentURI)
410     {
411         // do nothing
412     }
413 
414     /**
415      * @todo DOM level 3 setStrictErrorChecking() Not implemented. Do nothing.
416      * @see org.w3c.dom.Document#setStrictErrorChecking(boolean)
417      */
418     public void setStrictErrorChecking(boolean strictErrorChecking)
419     {
420         // do nothing
421     }
422 
423     /**
424      * @todo DOM level 3 setXmlStandalone() Not implemented. Do nothing.
425      * @see org.w3c.dom.Document#setXmlStandalone(boolean)
426      */
427     public void setXmlStandalone(boolean xmlStandalone) throws DOMException
428     {
429         // do nothing
430     }
431 
432     /**
433      * @todo DOM level 3 setXmlVersion() Not implemented. Do nothing.
434      * @see org.w3c.dom.Document#setXmlVersion(java.lang.String)
435      */
436     public void setXmlVersion(String xmlVersion) throws DOMException
437     {
438         // do nothing
439     }
440 }