01: package com.jcorporate.expresso.kernel.management;
02:
03: import com.jcorporate.expresso.kernel.exception.ExpressoRuntimeException;
04: import com.jcorporate.expresso.kernel.util.ClassLocator;
05: import org.apache.log4j.Logger;
06: import org.apache.xml.serialize.OutputFormat;
07: import org.apache.xml.serialize.XMLSerializer;
08: import org.w3c.dom.Document;
09:
10: import java.io.IOException;
11: import java.io.OutputStream;
12:
13: /**
14: * This class writes the DOM structures to a file using specific Xerces capabilies.
15: * If you cannot use Xerces, then you'll need a TRAX compliant XSL parser and
16: * use the TraxDOMWriter supplied class instead.
17: *
18: * @author Michael Rimov
19: * @version $Revision: 1.6 $ on $Date: 2004/11/17 20:48:17 $
20: */
21:
22: public class XercesDOMWriter implements DOMWriter {
23: static Logger log = Logger.getLogger(XercesDOMWriter.class);
24:
25: public XercesDOMWriter() {
26: super ();
27: }
28:
29: /**
30: * Checks to see if Xerces classes are available via the classpath
31: *
32: * @return true if Xerces appears to be installed.
33: */
34: protected boolean isXercesInstalled() {
35:
36: try {
37: ClassLocator.loadClass(getRequiredClass());
38: } catch (ClassNotFoundException ex) {
39: return false;
40: }
41:
42: return true;
43: }
44:
45: /**
46: * Retrieve a class that must exist in the classpath for this to work.
47: * Used as a sanity check to make sure the appropriate jars are installed.
48: *
49: * @return java.lang.String: ...xerces.parsers.SAXParer
50: */
51: public String getRequiredClass() {
52: return "org.apache.xerces.parsers.SAXParser";
53: }
54:
55: public void saveDocument(OutputStream os, Document document)
56: throws ExpressoRuntimeException {
57: try {
58: // Serialize the document onto System.out
59: OutputFormat format = new OutputFormat(document);
60: format.setLineWidth(65);
61: format.setIndenting(true);
62: format.setIndent(4);
63: format.setEncoding("ISO-8859-1");
64: format.setMediaType("application/xml");
65: format.setOmitComments(true);
66: format.setOmitXMLDeclaration(false);
67: format.setVersion("1.0");
68: format
69: .setDoctype(
70: "-//Jcorporate Ltd//DTD Expresso Services Configuration 5.1//EN",
71: "http://www.jcorporate.com/dtds/expresso-services_5_1.dtd");
72:
73: XMLSerializer serializer = new XMLSerializer(os, format);
74: serializer.serialize(document);
75: } catch (IOException ex) {
76: throw new ExpressoRuntimeException(
77: "I/O Error writing config file", ex);
78: }
79:
80: }
81: }
|