KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > xml > AbstractDescriptorHandler


1
2 package org.enhydra.tool.archive.xml;
3
4 //
5
import org.enhydra.tool.archive.ArchiveException;
6 import org.apache.xerces.parsers.DOMParser;
7 import org.apache.xml.serialize.XMLSerializer;
8 import org.apache.xml.serialize.OutputFormat;
9 import org.apache.xml.serialize.OutputFormat;
10 import org.w3c.dom.Document JavaDoc;
11 import org.w3c.dom.Element JavaDoc;
12 import org.w3c.dom.Node JavaDoc;
13 import org.w3c.dom.NodeList JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.InputSource JavaDoc;
16
17 //
18
import java.io.FileReader JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.BufferedReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.StringReader JavaDoc;
28
29 //
30
abstract public class AbstractDescriptorHandler implements DescriptorHandler {
31
32     //
33
private Document JavaDoc doc = null;
34     private String JavaDoc source = new String JavaDoc();
35     private OutputStream JavaDoc outStream = null;
36     private StringWriter JavaDoc writer = null;
37
38     //
39
public void setOutStream(OutputStream JavaDoc s) {
40         outStream = s;
41     }
42
43     public OutputStream JavaDoc getOutputStream() {
44         return outStream;
45     }
46
47     public void setWriter(StringWriter JavaDoc w) {
48         writer = w;
49     }
50
51     public StringWriter JavaDoc getWriter() {
52         return writer;
53     }
54
55     public String JavaDoc getSource() {
56         return source;
57     }
58
59     public void setSource(String JavaDoc s) {
60         source = s;
61     }
62
63     public InputStream JavaDoc getInputStream() {
64         ByteArrayInputStream JavaDoc stream = null;
65         byte[] bytes = new byte[0];
66
67         if (getWriter() != null) {
68             getWriter().flush();
69             bytes = getWriter().toString().getBytes();
70             stream = new ByteArrayInputStream JavaDoc(bytes);
71         }
72         return stream;
73     }
74
75     protected Document JavaDoc getDocument() throws ArchiveException {
76         DOMParser parser = null;
77         InputSource JavaDoc inputSource = null;
78         DocTypeFilterReader reader = null;
79
80         try {
81             reader = new DocTypeFilterReader(
82                  new BufferedReader JavaDoc(
83                    new FileReader JavaDoc(getSource())));
84         } catch (FileNotFoundException JavaDoc e) {
85             throw new ArchiveException(e, "Unable to prep: " + getSource());
86         }
87
88         inputSource = new InputSource JavaDoc(reader);
89         inputSource.setPublicId(null);
90         inputSource.setSystemId(null);
91         parser = new DOMParser();
92
93         //
94
try {
95             parser.parse(inputSource);
96         } catch (IOException JavaDoc e) {
97             e.printStackTrace();
98             throw new ArchiveException(e, "Unable to parse: " + getSource());
99         } catch (SAXException JavaDoc e) {
100             e.printStackTrace();
101             throw new ArchiveException(e, "Unable to parse: " + getSource());
102         }
103         return parser.getDocument();
104     }
105
106
107     public void prep() throws ArchiveException {
108         OutputFormat format = null;
109         XMLSerializer serializer = null;
110
111         format = new OutputFormat();
112         serializer = new XMLSerializer();
113
114         doc = getDocument();
115         prepElements();
116
117         //
118
format.setDoctype(getPublicID(), getSystemID());
119         format.setLineSeparator("\r\n");
120         format.setLineWidth(8192);
121         format.setIndent(2);
122         format.setIndenting(true);
123         format.setPreserveSpace(false);
124
125         //
126
if (getWriter() != null) {
127             serializer.setOutputCharStream(getWriter());
128         } else if (getOutputStream() != null) {
129             serializer.setOutputByteStream(getOutputStream());
130         } else {
131             initWriter();
132             serializer.setOutputCharStream(getWriter());
133         }
134         serializer.setOutputFormat(format);
135         try {
136             serializer.serialize(doc.getDocumentElement());
137         } catch (IOException JavaDoc e) {
138             throw new ArchiveException(e,
139                                        "Unable to serialize prep'd "
140                                        + getSource());
141         }
142     }
143
144     //
145
//
146
abstract protected void prepElements();
147     abstract protected String JavaDoc getPublicID();
148     abstract protected String JavaDoc getSystemID();
149
150     //
151
//
152
protected Document JavaDoc getDoc() {
153         return doc;
154     }
155
156     protected Element JavaDoc lookup(Element JavaDoc parent, String JavaDoc name) {
157         NodeList JavaDoc list = null;
158         Node JavaDoc cursor = null;
159         Element JavaDoc element = null;
160         int len = 0;
161
162         list = parent.getChildNodes();
163         len = list.getLength();
164         for (int i = 0; i < len; i++) {
165             cursor = list.item(i);
166             if (cursor.getNodeName().equals(name)
167                     && cursor instanceof Element JavaDoc) {
168                 element = (Element JavaDoc) cursor;
169                 break;
170             }
171         }
172         return element;
173     }
174
175     //
176
//
177
private void initWriter() {
178         setWriter(new StringWriter JavaDoc());
179     }
180
181 }
182
Popular Tags