1 /*
2
3 Loader - tool for transfering data from one JDBC source to another and
4 doing transformations during copy.
5
6 Copyright (C) 2002 Together
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 LoaderXIncluder.java
23 Date: 22.09.2001.
24 @version 1.0
25 @author:
26 Milosevic Sinisa
27 */
28
29
30 package org.webdocwf.util.loader;
31
32 import java.io.OutputStreamWriter;
33 import java.io.OutputStream;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.BufferedInputStream;
37 import java.io.Writer;
38 import java.io.UnsupportedEncodingException;
39 import java.io.IOException;
40 import java.io.File;
41 import java.net.URL;
42 import java.net.URLConnection;
43 import java.net.MalformedURLException;
44 import org.xml.sax.XMLReader;
45 import org.xml.sax.helpers.XMLReaderFactory;
46 import org.xml.sax.AttributeList;
47 import org.xml.sax.HandlerBase;
48 import org.xml.sax.InputSource;
49 import org.xml.sax.SAXException;
50 import org.xml.sax.SAXParseException;
51 import org.apache.xerces.parsers.SAXParser;
52
53
54 /***
55 * LoaderXIncluder.java
56 * This class parses a document (text or xml file) and writes the documents
57 * contents back to standard output.
58 */
59 public class LoaderXIncluder extends HandlerBase {
60 private Writer out;
61 private String encoding;
62 private int level = 0;
63
64 /*** Main program entry point. */
65 public static void main (String argv[]) {
66 if (argv.length == 0) {
67 System.out.println("Usage: java LoaderXIncluder uri");
68 System.out.println(" where uri is the URI of your XML document.");
69 System.out.println(" Sample: java LoaderXIncluder demo.xml");
70 System.exit(1);
71 }
72 LoaderXIncluder s1 = new LoaderXIncluder();
73 s1.parseURI(argv[0]);
74 }
75
76 /***
77 * Constructor of class without parameters.
78 */
79 public LoaderXIncluder () {
80 }
81
82 /***
83 * Construct object LoaderXIncluder with associated outputStream and encoding.
84 * @param out - OutputStream where will be written final XML.
85 * @param encoding - encoding String representation of encoding.
86 */
87 public LoaderXIncluder (OutputStream out, String encoding) throws UnsupportedEncodingException
88 {
89 this.out = new OutputStreamWriter(out, encoding);
90 this.encoding = encoding;
91 }
92
93 /***
94 * Construct object LoaderXIncluder with associated outputStream.
95 * Class uses default "UTF-8" encoding.
96 * @param out - OutputStream where will be written final XML.
97 */
98 public LoaderXIncluder (OutputStream out) {
99 try {
100 this.out = new OutputStreamWriter(out, "UTF8");
101 this.encoding = "UTF-8";
102 } catch (UnsupportedEncodingException e) {}
103 }
104
105 /***
106 * Method parseUri parses a file "uri" and writes the file
107 * contents back to standard output including contents of 'include' files .
108 * @param uri - Name of XML file.
109 */
110 public void parseURI (String uri) {
111 try {
112
113 SAXParser parser = new SAXParser();
114 parser.setDocumentHandler(this);
115 parser.setErrorHandler(this);
116 System.out.println("Loader loads XML file : "+ uri);
117 parser.parse(uri);
118 } catch (Exception e) {
119 System.err.println(e);
120 }
121
122 }
123
124 /*** Processing instruction. */
125 public void processingInstruction (String target, String data) {
126 try {
127 out.write("<?");
128 out.write(target);
129 if (data != null && data.length() > 0) {
130 out.write(' ');
131 out.write(data);
132 }
133 out.write("?>");
134 } catch (IOException e) {
135 System.err.println(e);
136 }
137 }
138
139 /*** Start document. */
140 public void startDocument () {
141 if (level == 0) {
142 try {
143 out.write("<?xml version=\"1.0\"?>\r\n");
144 } catch (IOException e) {
145 System.err.println(e);
146 }
147 }
148 }
149
150 /*** Start element. */
151 public void startElement (String name, AttributeList atts) {
152 try {
153 if (name.equalsIgnoreCase("include")) {
154 String href = atts.getValue("href");
155 if (href == null) {
156 System.out.println("Missing href attribute in include Element");
157 System.exit(1);
158 }
159 String parse = atts.getValue("parse");
160 if (parse == null)
161 parse = "xml";
162 if (parse.equals("text")) {
163 String encoding = atts.getValue("encoding");
164 includeTextDocument(href, encoding);
165 }
166 else if (parse.equals("xml")) {
167 level++;
168 includeXMLDocument(href);
169 level--;
170 }
171 else {
172 System.out.println("Illegal value for parse attribute: "
173 + parse);
174 System.exit(1);
175 }
176 }
177 else if(name.equalsIgnoreCase("definitionInclude")) {
178 }
179 else {
180 out.write("<" + name);
181 for (int i = 0; i < atts.getLength(); i++) {
182 out.write(" ");
183 out.write(atts.getName(i));
184 out.write("='");
185 String value = atts.getValue(i);
186 // + 4 allows space for one entitiy reference.
187 // If there's more than that, then the StringBuffer
188 // will automatically expand
189 // Need to use character references if the encoding
190 // can't support the character
191 StringBuffer encodedValue = new StringBuffer(value.length()
192 + 4);
193 for (int j = 0; j < value.length(); j++) {
194 char c = value.charAt(j);
195 if (c == '&')
196 encodedValue.append("&");
197 else if (c == '<')
198 encodedValue.append("<");
199 else if (c == '>')
200 encodedValue.append(">");
201 else if (c == '\'')
202 encodedValue.append("'");
203 else
204 encodedValue.append(c);
205 }
206 out.write(encodedValue.toString());
207 out.write("'");
208 }
209 out.write(">");
210 }
211 } catch (IOException e) {
212 System.err.println(e);
213 } catch (SAXException ex) {
214 System.err.println(ex);
215 }
216 }
217
218 /*** Characters. */
219 public void characters (char ch[], int start, int length) throws SAXException {
220 try {
221 for (int i = 0; i < length; i++) {
222 char c = ch[start + i];
223 if (c == '&')
224 out.write("&");
225 else if (c == '<')
226 out.write("<");
227 else
228 out.write(c);
229 }
230 } catch (IOException e) {
231 System.err.println(e);
232 }
233 }
234
235 /*** Ignorable whitespace. */
236 public void ignorableWhitespace (char ch[], int start, int length) {
237 try {
238 this.characters(ch, start, length);
239 } catch (SAXException e) {
240 System.err.println(e);
241 }
242 }
243
244 /*** End element. */
245 public void endElement (String name) {
246 if (!name.equalsIgnoreCase("include") && !name.equalsIgnoreCase("definitionInclude")) {
247 try {
248 out.write("</");
249 out.write(name);
250 out.write(">");
251 } catch (IOException e) {
252 System.err.println(e);
253 }
254 }
255 }
256
257 /*** End document. */
258 public void endDocument () {
259 try {
260 out.flush();
261 } catch (IOException e) {
262 System.err.println(e);
263 }
264 }
265
266 //
267 // ErrorHandler methods
268 //
269 /*** Warning. */
270 public void warning (SAXParseException ex) {
271 System.err.println("[Warning] " + getLocationString(ex) + ": " + ex.getMessage());
272 }
273
274 /*** Error. */
275 public void error (SAXParseException ex) {
276 System.err.println("[Error] " + getLocationString(ex) + ": " + ex.getMessage());
277 }
278
279 /*** Fatal error. */
280 public void fatalError (SAXParseException ex) throws SAXException {
281 System.err.println("[Fatal Error] " + getLocationString(ex) + ": " +
282 ex.getMessage());
283 throw ex;
284 }
285
286 /*** Returns a string of the location. */
287 private String getLocationString (SAXParseException ex) {
288 StringBuffer str = new StringBuffer();
289 String systemId = ex.getSystemId();
290 if (systemId != null) {
291 int index = systemId.lastIndexOf('/');
292 if (index != -1)
293 systemId = systemId.substring(index + 1);
294 str.append(systemId);
295 }
296 str.append(':');
297 str.append(ex.getLineNumber());
298 str.append(':');
299 str.append(ex.getColumnNumber());
300 return str.toString();
301 }
302
303 /***
304 * <p>
305 * This utility method reads a document at a specified URL
306 * and fires off calls to <code>characters()</code>.
307 * It's used to include files with <code>parse="text"</code>
308 * </p>
309 *
310 * @param url URL of the document that will be read
311 * @param encoding Encoding of the document; e.g. UTF-8,
312 * ISO-8859-1, etc.
313 * @return void
314 * @throws SAXException if the requested document cannot
315 be downloaded from the specified URL
316 or if the encoding is not recognized
317 */
318 private void includeTextDocument (String url, String encoding) throws SAXException {
319 if (encoding == null || encoding.trim().equals(""))
320 encoding = "UTF-8";
321 File sourceXml = new File(url);
322 URL source;
323 try {
324 source = sourceXml.toURL();
325 } catch (MalformedURLException e) {
326 throw new SAXException("Unresolvable URL :", e);
327 }
328 try {
329 URLConnection uc = source.openConnection();
330 String encodingFromHeader = uc.getContentEncoding();
331 if (encodingFromHeader != null)
332 encoding = encodingFromHeader;
333 InputStream in = new BufferedInputStream(uc.getInputStream());
334 InputStreamReader reader = new InputStreamReader(in, encoding);
335 char[] c = new char[1024];
336 while (true) {
337 int charsRead = reader.read(c, 0, 1024);
338 if (charsRead == -1)
339 break;
340 if (charsRead > 0)
341 this.characters(c, 0, charsRead);
342 }
343 } catch (UnsupportedEncodingException e) {
344 throw new SAXException("Unsupported encoding: " + encoding, e);
345 } catch (IOException e) {
346 throw new SAXException("Document not found: " + source.toExternalForm(),
347 e);
348 }
349 }
350
351 /***
352 * <p>
353 * This utility method reads a document at a specified URL
354 * and fires off calls to various <code>DocumentHandler</code> methods.
355 * It's used to include files with <code>parse="xml"</code>
356 * </p>
357 *
358 * @param url URL of the document that will be read
359 * @return void
360 * @throws SAXException if the requested document cannot
361 be downloaded from the specified URL.
362 */
363 private void includeXMLDocument (String url) throws SAXException {
364 File sourceXml = new File(url);
365 URL source;
366 InputStream includeXML = null;
367 try {
368 source = sourceXml.toURL();
369 includeXML = source.openStream();
370 } catch (MalformedURLException e) {
371 throw new SAXException("Unresolvable URL :", e);
372 } catch (IOException e) {
373 throw new SAXException("Error in opening input stream :", e);
374 }
375 XMLReader parser;
376 try {
377 parser = XMLReaderFactory.createXMLReader();
378 } catch (SAXException e) {
379 try {
380 parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
381 } catch (SAXException e2) {
382 System.err.println("Could not find an XML parser");
383 return;
384 }
385 }
386 SAXParser includeParser = new SAXParser();
387 includeParser.setDocumentHandler(this);
388 includeParser.setErrorHandler(this);
389 try {
390 includeParser.parse(url);
391 } catch (Exception e) {
392 System.err.println(e);
393 }
394 }
395 }
396
397
This page automatically generated by Maven