blob: 87b058e279ba52df150e32dcf40aa27cb477371a [file] [log] [blame]
Stuart McCulloch3fdcd852011-10-17 10:31:43 +00001package aQute.libg.sax;
2
3import javax.xml.parsers.SAXParserFactory;
4import javax.xml.transform.Result;
5import javax.xml.transform.sax.SAXTransformerFactory;
6import javax.xml.transform.sax.TransformerHandler;
7
8import org.xml.sax.ContentHandler;
9import org.xml.sax.XMLReader;
10
11public class SAXUtil {
12
13 public static XMLReader buildPipeline(Result output, ContentFilter... filters) throws Exception {
14 SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
15 TransformerHandler handler = factory.newTransformerHandler();
16 handler.setResult(output);
17
18 ContentHandler last = handler;
19 if (filters != null) for (ContentFilter filter : filters) {
20 filter.setParent(last);
21 last = filter;
22 }
23 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
24 reader.setContentHandler(last);
25
26 return reader;
27 }
28
29}