public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); } public ContentHandler getContentHandler() {
public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); } public ContentHandler getContentHandler() {
public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); } public ContentHandler getContentHandler() {
public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); }
public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); } public ContentHandler getContentHandler() {
public void setContentHandler(ContentHandler handler) { repeater.setContentHandler(handler); }
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { DefaultHandler defaultHandler = new DefaultHandler() { @Override public void characters(char[] ch, int start, int length) throws SAXException { //This method receives the entity as is System.out.println(new String(ch, start, length)); } }; XMLFilter xmlFilter = new XMLFilterEntityImpl(XMLReaderFactory.createXMLReader()); xmlFilter.setContentHandler(defaultHandler); String xml = "<html><head><title>title</title></head><body>&</body></html>"; xmlFilter.parse(new InputSource(new StringReader(xml))); }
public StylesheetTransformer setSource(XMLReader reader, InputSource in, String xsl, ContentHandler handler) throws TransformerException { if (transformerFactory == null) { transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); } XMLFilter filter = transformerFactory.newXMLFilter(resolver.resolve(xsl, null)); filter.setParent(reader); filter.setContentHandler(handler); this.source = new SAXSource(filter, in); return this; }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
/** Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
static SVGRoot flexibleNamespaceParse(InputSource input) throws IOException { try { XMLFilter filter = new SvgNamespaceFilter(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); filter.setParent(factory.newSAXParser().getXMLReader()); UnmarshallerHandler umHandler = unmarshaller().getUnmarshallerHandler(); filter.setContentHandler(umHandler); filter.parse(input); return (SVGRoot) umHandler.getResult(); } catch (ParserConfigurationException | SAXException | JAXBException x) { throw new IOException("Invalid svg or other error: "+input, x); } }
/** * Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws SAXException, IOException { if (f != null) { // prepare an output Document Document o = db.newDocument(); // use TrAX to adapt SAX events to a Document object th.setResult(new DOMResult(o)); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setEntityResolver(new JstlEntityResolver(pageContext)); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); f.setContentHandler(th); f.parse(s); return o; } else return parseInputSource(s); }
// Create the XMLFilter XMLFilter filter = new NamespaceFilter(); // Set the parent XMLReader on the XMLFilter SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); filter.setParent(xr); // Set UnmarshallerHandler as ContentHandler on XMLFilter Unmarshaller unmarshaller = jc.createUnmarshaller(); UnmarshallerHandler unmarshallerHandler = unmarshaller .getUnmarshallerHandler(); filter.setContentHandler(unmarshallerHandler); // Parse the XML InputSource xml = new InputSource("input.xml"); filter.parse(xml); Object result = unmarshallerHandler.getResult();
/** * Parses the given InputSource after, applying the given XMLFilter. */ private Document parseInputSourceWithFilter(InputSource s, XMLFilter f) throws JspException { try { XMLReader xr = XmlUtil.newXMLReader(entityResolver); // (note that we overwrite the filter's parent. this seems // to be expected usage. we could cache and reset the old // parent, but you can't setParent(null), so this wouldn't // be perfect.) f.setParent(xr); TransformerHandler th = XmlUtil.newTransformerHandler(); Document o = XmlUtil.newEmptyDocument(); th.setResult(new DOMResult(o)); f.setContentHandler(th); f.parse(s); return o; } catch (IOException e) { throw new JspException(e); } catch (SAXException e) { throw new JspException(e); } catch (TransformerConfigurationException e) { throw new JspException(e); } catch (ParserConfigurationException e) { throw new JspException(e); } }
private XMLFilter createXmlFilter(Unmarshaller unmarshaller, String namespaceToSet) throws JAXBException { try { XMLFilter filter = new XmlNamespaceInjectionFilter(createXmlReader(), namespaceToSet); filter.setContentHandler(unmarshaller.getUnmarshallerHandler()); return filter; } catch (SAXException | ParserConfigurationException e) { throw new JAXBException(e); } }
/** Simple command-line interface, mostly for testing. */ public static void main(String args[]) throws ParseException, IOException, SAXException { // temporary... System.setProperty("org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser"); // retrieve and parse the expression String expr = args[0]; SPathParser s = new SPathParser(expr); Path p = s.expression(); // construct the appropriate SAX chain // (reader -> us -> serializer) XMLReader r = XMLReaderFactory.createXMLReader(); XMLFilter f1 = new SPathFilter(p); XMLFilter f2 = new XMLFilterImpl(); f1.setParent(r); f2.setParent(f1); Serializer sz = SerializerFactory.getSerializer (OutputProperties.getDefaultMethodProperties("xml")); sz.setOutputStream(System.out); f2.setContentHandler(sz.asContentHandler()); // go! f2.parse(new InputSource(System.in)); System.out.println(); }