Stuart McCulloch | 3fdcd85 | 2011-10-17 10:31:43 +0000 | [diff] [blame^] | 1 | package aQute.libg.sax; |
| 2 | |
| 3 | import javax.xml.parsers.SAXParserFactory; |
| 4 | import javax.xml.transform.Result; |
| 5 | import javax.xml.transform.sax.SAXTransformerFactory; |
| 6 | import javax.xml.transform.sax.TransformerHandler; |
| 7 | |
| 8 | import org.xml.sax.ContentHandler; |
| 9 | import org.xml.sax.XMLReader; |
| 10 | |
| 11 | public 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 | } |