KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > dom > DOMDocument


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Sam
28  */

29
30 package com.caucho.quercus.lib.dom;
31
32 import com.caucho.quercus.UnimplementedException;
33 import com.caucho.quercus.annotation.Optional;
34 import com.caucho.quercus.annotation.ReturnNullAsFalse;
35 import com.caucho.quercus.env.BooleanValue;
36 import com.caucho.quercus.env.Env;
37 import com.caucho.quercus.env.LongValue;
38 import com.caucho.quercus.env.StringValue;
39 import com.caucho.quercus.env.TempBufferStringValue;
40 import com.caucho.quercus.env.Value;
41 import com.caucho.util.L10N;
42 import com.caucho.vfs.Path;
43 import com.caucho.vfs.ReadStream;
44 import com.caucho.vfs.StringStream;
45 import com.caucho.vfs.TempStream;
46 import com.caucho.vfs.WriteStream;
47 import com.caucho.xml.XmlPrinter;
48
49 import org.w3c.dom.Document JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51
52 import java.io.IOException JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import java.util.logging.Logger JavaDoc;
55
56 public class DOMDocument
57   extends DOMNode<Document JavaDoc>
58 {
59   private final static L10N L = new L10N(DOMDocument.class);
60   private final static Logger JavaDoc log = Logger.getLogger(DOMDocument.class.getName());
61
62   private String JavaDoc _encoding;
63
64   public static DOMDocument __construct(Env env, @Optional("'1.0'") String JavaDoc version, @Optional String JavaDoc encoding)
65   {
66     DOMDocument document = getImpl(env).createDocument();
67
68     if (version != null && version.length() > 0)
69       document.setVersion(version);
70
71     if (encoding != null && encoding.length() > 0)
72       document.setEncoding(encoding);
73
74     return document;
75   }
76
77   public void setVersion(String JavaDoc version)
78   {
79     _delegate.setXmlVersion(version);
80   }
81
82   DOMDocument(DOMImplementation impl, Document JavaDoc document)
83   {
84     super(impl, document);
85   }
86
87   public String JavaDoc getEncoding()
88   {
89     return _encoding;
90   }
91
92   public void setEncoding(String JavaDoc encoding)
93   {
94     _encoding = encoding;
95   }
96
97   public DOMNode adoptNode(DOMNode source)
98     throws DOMException
99   {
100     return wrap(_delegate.adoptNode(source.getDelegate()));
101   }
102
103   public DOMAttr createAttribute(String JavaDoc name)
104     throws DOMException
105   {
106     try {
107       return wrap(_delegate.createAttribute(name));
108     }
109     catch (org.w3c.dom.DOMException JavaDoc ex) {
110       throw wrap(ex);
111     }
112   }
113
114   public DOMAttr createAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
115     throws DOMException
116   {
117     try {
118       return wrap(_delegate.createAttributeNS(namespaceURI, qualifiedName));
119     }
120     catch (org.w3c.dom.DOMException JavaDoc ex) {
121       throw wrap(ex);
122     }
123   }
124
125   public DOMCDATASection createCDATASection(String JavaDoc data)
126   {
127     return wrap(_delegate.createCDATASection(data));
128   }
129
130   public DOMComment createComment(String JavaDoc data)
131   {
132     return wrap(_delegate.createComment(data));
133   }
134
135   public DOMDocumentFragment createDocumentFragment()
136   {
137     return wrap(_delegate.createDocumentFragment());
138   }
139
140   public DOMElement createElement(String JavaDoc tagName)
141     throws DOMException
142   {
143     try {
144       return wrap(_delegate.createElement(tagName));
145     }
146     catch (org.w3c.dom.DOMException JavaDoc ex) {
147       throw wrap(ex);
148     }
149   }
150
151   public DOMElement createElement(String JavaDoc tagName, String JavaDoc textContent)
152     throws DOMException
153   {
154     try {
155       DOMElement element = createElement(tagName);
156
157       element.setTextContent(textContent);
158
159       return element;
160     }
161     catch (org.w3c.dom.DOMException JavaDoc ex) {
162       throw wrap(ex);
163     }
164   }
165
166   public DOMElement createElementNS(String JavaDoc namespaceURI, String JavaDoc tagName)
167     throws DOMException
168   {
169     try {
170       return wrap(_delegate.createElementNS(namespaceURI, tagName));
171     }
172     catch (org.w3c.dom.DOMException JavaDoc ex) {
173       throw wrap(ex);
174     }
175   }
176
177   public DOMElement createElementNS(String JavaDoc namespaceURI,
178                                     String JavaDoc tagName,
179                                     String JavaDoc textContent)
180     throws DOMException
181   {
182     try {
183       DOMElement element = createElementNS(namespaceURI, tagName);
184
185       element.setTextContent(textContent);
186
187       return element;
188     }
189     catch (org.w3c.dom.DOMException JavaDoc ex) {
190       throw wrap(ex);
191     }
192   }
193
194   public DOMEntityReference createEntityReference(String JavaDoc name)
195     throws DOMException
196   {
197     try {
198       return wrap(_delegate.createEntityReference(name));
199     }
200     catch (org.w3c.dom.DOMException JavaDoc ex) {
201       throw wrap(ex);
202     }
203   }
204
205   public DOMProcessingInstruction createProcessingInstruction(String JavaDoc target)
206     throws DOMException
207   {
208     try {
209       return createProcessingInstruction(target, null);
210     }
211     catch (org.w3c.dom.DOMException JavaDoc ex) {
212       throw wrap(ex);
213     }
214   }
215
216   public DOMProcessingInstruction createProcessingInstruction(String JavaDoc target,
217                                                               String JavaDoc data)
218     throws DOMException
219   {
220     try {
221       return wrap(_delegate.createProcessingInstruction(target, data));
222     }
223     catch (org.w3c.dom.DOMException JavaDoc ex) {
224       throw wrap(ex);
225     }
226   }
227
228   public DOMText createTextNode(String JavaDoc data)
229   {
230     return wrap(_delegate.createTextNode(data));
231   }
232
233   public DOMConfiguration getConfig()
234   {
235     throw new UnimplementedException();
236   }
237
238   public DOMDocumentType getDoctype()
239   {
240     return wrap(_delegate.getDoctype());
241   }
242
243   public DOMElement getDocumentElement()
244   {
245     return wrap(_delegate.getDocumentElement());
246   }
247
248   public String JavaDoc getDocumentURI()
249   {
250     return _delegate.getDocumentURI();
251   }
252
253   public DOMConfiguration getDomConfig()
254   {
255     return wrap(_delegate.getDomConfig());
256   }
257
258   public DOMElement getElementById(String JavaDoc elementId)
259   {
260     return wrap(_delegate.getElementById(elementId));
261   }
262
263   public DOMNodeList getElementsByTagName(String JavaDoc name)
264   {
265     return wrap(_delegate.getElementsByTagName(name));
266   }
267
268   public DOMNodeList getElementsByTagNameNS(String JavaDoc uri, String JavaDoc name)
269   {
270     return wrap(_delegate.getElementsByTagNameNS(uri, name));
271   }
272
273   public boolean getFormatOutput()
274   {
275     throw new UnimplementedException();
276   }
277
278   public DOMImplementation getImplementation()
279   {
280     return getImpl();
281   }
282
283   public String JavaDoc getInputEncoding()
284   {
285     return _delegate.getInputEncoding();
286   }
287
288   public boolean getPreserveWhiteSpace()
289   {
290     throw new UnimplementedException();
291   }
292
293   public boolean getRecover()
294   {
295     throw new UnimplementedException();
296   }
297
298   public boolean getResolveExternals()
299   {
300     throw new UnimplementedException();
301   }
302
303   public boolean getStrictErrorChecking()
304   {
305     return _delegate.getStrictErrorChecking();
306   }
307
308   public boolean getSubstituteEntities()
309   {
310     throw new UnimplementedException();
311   }
312
313   public boolean getValidateOnParse()
314   {
315     throw new UnimplementedException();
316   }
317
318   public String JavaDoc getVersion()
319   {
320     return _delegate.getXmlVersion();
321   }
322
323   public String JavaDoc getXmlEncoding()
324   {
325     return _delegate.getXmlEncoding();
326   }
327
328   public boolean getXmlStandalone()
329   {
330     return _delegate.getXmlStandalone();
331   }
332
333   public String JavaDoc getXmlVersion()
334   {
335     return _delegate.getXmlVersion();
336   }
337
338   public DOMNode importNode(DOMNode node)
339   {
340     return importNode(node, false);
341   }
342
343   public DOMNode importNode(DOMNode importedNode, boolean deep)
344     throws DOMException
345   {
346     try {
347       return wrap(_delegate.importNode(importedNode.getDelegate(), deep));
348     }
349     catch (org.w3c.dom.DOMException JavaDoc ex) {
350       throw wrap(ex);
351     }
352   }
353
354   // XXX: also can be called statically, returns a DOMDocument in that case
355
public boolean load(Env env, Path path, @Optional Value options)
356     throws IOException JavaDoc
357   {
358     if (options != null)
359       env.stub(L.l("`{0}' is ignored", "options"));
360
361     ReadStream is = null;
362
363     try {
364       is = path.openRead();
365
366       getImpl().parseXMLDocument(_delegate, is, path.getPath());
367     }
368     catch (SAXException JavaDoc ex) {
369       env.warning(ex);
370
371       return false;
372     }
373     catch (IOException JavaDoc ex) {
374       env.warning(ex);
375       return false;
376     }
377     finally {
378       if (is != null) {
379     is.close();
380       }
381     }
382
383     return true;
384   }
385
386   /**
387    * @param source A string containing the HTML
388    */

389   // XXX: also can be called statically, returns a DOMDocument in that case
390
public boolean loadHTML(Env env, String JavaDoc source)
391   {
392     ReadStream is = StringStream.open(source);
393
394     try {
395       getImpl().parseHTMLDocument(_delegate, is, null);
396
397       _delegate.setXmlStandalone(true);
398
399       /**
400        * XXX:
401        _delegate.setDoctype(new QDocumentType("html",
402        "-//W3C//DTD HTML 4.0 Transitional//EN",
403        "http://www.w3.org/TR/REC-html40/loose.dtd"));
404        */

405     }
406     catch (SAXException JavaDoc ex) {
407       env.warning(ex);
408       return false;
409     }
410     catch (IOException JavaDoc ex) {
411       env.warning(ex);
412       return false;
413     }
414     finally {
415       if (is != null) {
416     is.close();
417       }
418     }
419
420     return true;
421   }
422
423   // XXX: also can be called statically, returns a DOMDocument in that case
424
public boolean loadHTMLFile(Env env, Path path)
425   {
426     ReadStream is = null;
427
428     try {
429       is = path.openRead();
430
431       getImpl().parseHTMLDocument(_delegate, is, path.getPath());
432
433       _delegate.setXmlStandalone(true);
434       /**
435        * XXX:
436        _delegate.setDoctype(new QDocumentType("html",
437        "-//W3C//DTD HTML 4.0 Transitional//EN",
438        "http://www.w3.org/TR/REC-html40/loose.dtd"));
439        */

440     }
441     catch (SAXException JavaDoc ex) {
442       env.warning(ex);
443       return false;
444     }
445     catch (IOException JavaDoc ex) {
446       env.warning(ex);
447       return false;
448     }
449     finally {
450       if (is != null) {
451     is.close();
452       }
453     }
454
455     return true;
456   }
457
458   // XXX: also can be called statically, returns a DOMDocument in that case
459
public boolean loadXML(Env env, String JavaDoc source, @Optional Value options)
460   {
461     if (options != null)
462       env.stub(L.l("`{0}' is ignored", "options"));
463
464     ReadStream is = StringStream.open(source);
465
466     try {
467       getImpl().parseXMLDocument(_delegate, is, null);
468     }
469     catch (SAXException JavaDoc ex) {
470       env.warning(ex);
471
472       return false;
473     }
474     catch (IOException JavaDoc ex) {
475       env.warning(ex);
476       return false;
477     }
478     finally {
479       if (is != null) {
480     is.close();
481       }
482     }
483
484     return true;
485   }
486
487   public void normalizeDocument()
488   {
489     _delegate.normalizeDocument();
490   }
491
492   public boolean relaxNGValidate(String JavaDoc rngFilename)
493   {
494     throw new UnimplementedException();
495   }
496
497   public boolean relaxNGValidateSource(String JavaDoc rngSource)
498   {
499     throw new UnimplementedException();
500   }
501
502   public DOMNode renameNode(DOMNode node, String JavaDoc namespaceURI, String JavaDoc qualifiedName)
503     throws DOMException
504   {
505     try {
506       return wrap(_delegate.renameNode(node.getDelegate(), namespaceURI, qualifiedName));
507     }
508     catch (org.w3c.dom.DOMException JavaDoc ex) {
509       throw wrap(ex);
510     }
511   }
512
513   /**
514    * @return the number of bytes written, or FALSE for an error
515    */

516   public Value save(Env env, Path path, @Optional Value options)
517   {
518     if (options != null)
519       env.stub(L.l("`{0}' is ignored", "options"));
520
521     return saveToFile(env, path, false);
522   }
523
524   private Value saveToFile(Env env, Path path, boolean isHTML)
525   {
526     WriteStream os = null;
527
528     try {
529       os = path.openWrite();
530       saveToStream(os, isHTML);
531     }
532     catch (IOException JavaDoc ex) {
533       env.warning(ex);
534       return BooleanValue.FALSE;
535     }
536     finally {
537       if (os != null) {
538         try {
539           os.close();
540         }
541         catch (Exception JavaDoc ex) {
542           log.log(Level.FINE, ex.toString(), ex);
543         }
544       }
545     }
546
547     return new LongValue(path.getLength());
548   }
549
550   private void saveToStream(WriteStream os, boolean isHTML)
551     throws IOException JavaDoc
552   {
553     XmlPrinter printer = new XmlPrinter(os);
554
555     printer.setMethod(isHTML ? "html" : "xml");
556
557     printer.setPrintDeclaration(true);
558
559     printer.setVersion(_delegate.getXmlVersion());
560     printer.setEncoding(_encoding);
561
562     if (_delegate.getXmlStandalone())
563       printer.setStandalone("yes");
564
565     printer.printXml(_delegate);
566
567     if (hasChildNodes())
568       os.println();
569   }
570
571   @ReturnNullAsFalse
572   public StringValue saveHTML(Env env)
573   {
574     return saveToString(env, true);
575   }
576
577   private StringValue saveToString(Env env, boolean isHTML)
578   {
579     TempStream tempStream = new TempStream();
580
581     try {
582       tempStream.openWrite();
583       WriteStream os = new WriteStream(tempStream);
584
585       saveToStream(os, isHTML);
586
587       os.close();
588     }
589     catch (IOException JavaDoc ex) {
590       tempStream.discard();
591       env.warning(ex);
592       return null;
593     }
594
595     TempBufferStringValue result = new TempBufferStringValue(tempStream.getHead());
596
597     tempStream.discard();
598
599     return result;
600   }
601
602   /**
603    * @return the number of bytes written, or FALSE for an error
604    */

605
606   public Value saveHTMLFile(Env env, Path path)
607   {
608     return saveToFile(env, path, true);
609   }
610
611   @ReturnNullAsFalse
612   public StringValue saveXML(Env env)
613   {
614     return saveToString(env, false);
615   }
616
617   public boolean schemaValidate(String JavaDoc schemaFilename)
618   {
619     throw new UnimplementedException();
620   }
621
622   public boolean schemaValidateSource(String JavaDoc schemaSource)
623   {
624     throw new UnimplementedException();
625   }
626
627   public void setDocumentURI(String JavaDoc documentURI)
628   {
629     _delegate.setDocumentURI(documentURI);
630   }
631
632   public void setFormatOutput(boolean formatOutput)
633   {
634     throw new UnimplementedException();
635   }
636
637   public void setPreserveWhiteSpace(boolean preserveWhiteSpace)
638   {
639     throw new UnimplementedException();
640   }
641
642   public void setRecover(boolean recover)
643   {
644     throw new UnimplementedException();
645   }
646
647   public void setResolveExternals(boolean resolveExternals)
648   {
649     throw new UnimplementedException();
650   }
651
652   public void setStrictErrorChecking(boolean strictErrorChecking)
653   {
654     _delegate.setStrictErrorChecking(strictErrorChecking);
655   }
656
657   public void setSubstituteEntities(boolean substituteEntities)
658   {
659     throw new UnimplementedException();
660   }
661
662   public void setValidateOnParse(boolean validateOnParse)
663   {
664     throw new UnimplementedException();
665   }
666
667   public void setXmlStandalone(boolean xmlStandalone)
668     throws DOMException
669   {
670     _delegate.setXmlStandalone(xmlStandalone);
671   }
672
673   public void setXmlVersion(String JavaDoc xmlVersion)
674     throws DOMException
675   {
676     _delegate.setXmlVersion(xmlVersion);
677   }
678
679   public boolean validate()
680   {
681     throw new UnimplementedException();
682   }
683
684   public int xinclude(Env env, @Optional Value options)
685   {
686     if (options != null)
687       env.stub(L.l("`{0}' is ignored", "options"));
688
689     throw new UnimplementedException();
690   }
691 }
692
Popular Tags