KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > XMLProcessor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.dynamic;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import javax.xml.transform.TransformerConfigurationException JavaDoc;
19 import javax.xml.transform.TransformerException JavaDoc;
20
21 import org.eclipse.help.internal.UAElement;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /*
25  * Processes XML input streams by converting to a DOM, calling all the handlers,
26  * then converting back into an XML input stream.
27  */

28 public class XMLProcessor {
29
30     private DocumentProcessor processor;
31     private DocumentReader reader;
32     private DocumentWriter writer;
33     
34     /*
35      * Creates the processor, which will use the given handlers.
36      */

37     public XMLProcessor(ProcessorHandler[] handlers) {
38         this.processor = new DocumentProcessor(handlers);
39     }
40     
41     /*
42      * Processes the given input stream with the supplied document id,
43      * and returns a new processed input stream.
44      */

45     public InputStream JavaDoc process(InputStream JavaDoc in, String JavaDoc id, String JavaDoc charset) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc, TransformerConfigurationException JavaDoc {
46         if (reader == null) {
47             reader = new DocumentReader();
48         }
49         UAElement element = reader.read(in, charset);
50         processor.process(element, id);
51         if (writer == null) {
52             writer = new DocumentWriter();
53         }
54         return new ByteArrayInputStream JavaDoc(writer.writeBytes(element, true));
55     }
56 }
57
Popular Tags