KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > management > XercesDOMWriter


1 package com.jcorporate.expresso.kernel.management;
2
3 import com.jcorporate.expresso.kernel.exception.ExpressoRuntimeException;
4 import com.jcorporate.expresso.kernel.util.ClassLocator;
5 import org.apache.log4j.Logger;
6 import org.apache.xml.serialize.OutputFormat;
7 import org.apache.xml.serialize.XMLSerializer;
8 import org.w3c.dom.Document JavaDoc;
9
10 import java.io.IOException JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13
14 /**
15  * This class writes the DOM structures to a file using specific Xerces capabilies.
16  * If you cannot use Xerces, then you'll need a TRAX compliant XSL parser and
17  * use the TraxDOMWriter supplied class instead.
18  *
19  * @author Michael Rimov
20  * @version $Revision: 1.6 $ on $Date: 2004/11/17 20:48:17 $
21  */

22
23 public class XercesDOMWriter implements DOMWriter {
24     static Logger log = Logger.getLogger(XercesDOMWriter.class);
25
26     public XercesDOMWriter() {
27         super();
28     }
29
30     /**
31      * Checks to see if Xerces classes are available via the classpath
32      *
33      * @return true if Xerces appears to be installed.
34      */

35     protected boolean isXercesInstalled() {
36
37         try {
38             ClassLocator.loadClass(getRequiredClass());
39         } catch (ClassNotFoundException JavaDoc ex) {
40             return false;
41         }
42
43         return true;
44     }
45
46     /**
47      * Retrieve a class that must exist in the classpath for this to work.
48      * Used as a sanity check to make sure the appropriate jars are installed.
49      *
50      * @return java.lang.String: ...xerces.parsers.SAXParer
51      */

52     public String JavaDoc getRequiredClass() {
53         return "org.apache.xerces.parsers.SAXParser";
54     }
55
56
57     public void saveDocument(OutputStream JavaDoc os, Document JavaDoc document) throws ExpressoRuntimeException {
58         try {
59             // Serialize the document onto System.out
60
OutputFormat format = new OutputFormat(document);
61             format.setLineWidth(65);
62             format.setIndenting(true);
63             format.setIndent(4);
64             format.setEncoding("ISO-8859-1");
65             format.setMediaType("application/xml");
66             format.setOmitComments(true);
67             format.setOmitXMLDeclaration(false);
68             format.setVersion("1.0");
69             format.setDoctype("-//Jcorporate Ltd//DTD Expresso Services Configuration 5.1//EN",
70                     "http://www.jcorporate.com/dtds/expresso-services_5_1.dtd");
71
72             XMLSerializer serializer
73                     = new XMLSerializer(os, format);
74             serializer.serialize(document);
75         } catch (IOException JavaDoc ex) {
76             throw new ExpressoRuntimeException("I/O Error writing config file", ex);
77         }
78
79     }
80 }
Popular Tags