SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource(new FileReader ("document.xml")));
private XMLReader getParser(boolean validate) throws SAXException, ParserConfigurationException, FactoryConfigurationError { XMLReader parser = XMLUtil.createXMLReader(validate); // create document handler and register it parser.setEntityResolver(this); parser.setContentHandler(this); parser.setErrorHandler(this); return parser; }
/** Bind our input streams to an XMLReader. * * Just a convenience routine; obviously you can explicitly register * this as a listener with the same effect. * */ public void setXMLReader(XMLReader eventsource) { fXMLReader=eventsource; eventsource.setContentHandler(this); eventsource.setDTDHandler(this); eventsource.setErrorHandler(this); // to report fatal errors in filtering mode // Not supported by all SAX2 filters: try { eventsource. setProperty("http://xml.org/sax/properties/lexical-handler", this); } catch(SAXNotRecognizedException e) { // Nothing we can do about it } catch(SAXNotSupportedException e) { // Nothing we can do about it } // Should we also bind as other varieties of handler? // (DTDHandler and so on) }
private SAXSource createSaxSource(byte[] original) throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser parser = parserFactory.newSAXParser(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(new SilentErrorHandler()); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(original))); }
reader.setContentHandler(connector); reader.setErrorHandler(coordinator); reader.parse(source); } catch( IOException e ) { coordinator.clearStates(); reader.setContentHandler(dummyHandler); reader.setErrorHandler(dummyHandler);
/** * Sets a content handler. * @param stream input stream * @throws IOException I/O exception * @throws SAXException SAX exception */ public void parse(final InputStream stream) throws IOException, SAXException { reader.setErrorHandler(new XmlHandler()); reader.parse(new InputSource(stream)); }
public XMLReader createParser() throws SAXException, ParserConfigurationException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); SAXParser saxParser = spf.newSAXParser(); XMLReader xmlrd = saxParser.getXMLReader(); xmlrd.setContentHandler (this); xmlrd.setErrorHandler (this); return xmlrd; } }
/** * Sets the error handler. * * @param handler The new error handler. */ public void setErrorHandler(ErrorHandler handler) { try { parser.getXMLReader().setErrorHandler(handler); } catch (SAXException ignore) {} }
/** * Validates the given <code>Document</code> by writing it to a validating * SAX Parser. * * @param document * is the Document to validate * * @throws SAXException * if a validation error occurs * @throws RuntimeException * DOCUMENT ME! */ public void validate(Document document) throws SAXException { if (document != null) { XMLReader reader = getXMLReader(); if (errorHandler != null) { reader.setErrorHandler(errorHandler); } try { reader.parse(new DocumentInputSource(document)); } catch (IOException e) { throw new RuntimeException("Caught and exception that should " + "never happen: " + e); } } }
private static final SAXParser createSAXParser(LibraryHandler handler) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setFeature("http://xml.org/sax/features/validation", true); factory.setValidating(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(handler); reader.setEntityResolver(handler); return parser; }
/** * init the parser : * load the parser class, and set features if necessary * It is only after this that the reader is valid * @throws BuildException if something went wrong */ protected void initValidator() { xmlReader = createXmlReader(); xmlReader.setEntityResolver(getEntityResolver()); xmlReader.setErrorHandler(errorHandler); if (!isSax1Parser()) { // turn validation on if (!lenient) { setFeature(XmlConstants.FEATURE_VALIDATION, true); } // set the feature from the attribute list for (final Attribute feature : attributeList) { setFeature(feature.getName(), feature.getValue()); } // Sets properties for (final Property prop : propertyList) { setProperty(prop.getName(), prop.getValue()); } } }
/** * Creates a new instance. * @param publicIdToResourceNameMap maps public IDs to DTD resource names * @throws SAXException if an error occurs * @throws ParserConfigurationException if an error occurs */ protected XmlLoader(Map<String, String> publicIdToResourceNameMap) throws SAXException, ParserConfigurationException { this.publicIdToResourceNameMap = new HashMap<>(publicIdToResourceNameMap); final SAXParserFactory factory = SAXParserFactory.newInstance(); FeaturesForVerySecureJavaInstallations.addFeaturesForVerySecureJavaInstallations(factory); factory.setValidating(true); parser = factory.newSAXParser().getXMLReader(); parser.setContentHandler(this); parser.setEntityResolver(this); parser.setErrorHandler(this); }
/** * Set up before a parse. * * <p>Before every parse, check whether the parent is * non-null, and re-register the filter for all of the * events.</p> */ private void setupParse () { if (parent == null) { throw new NullPointerException("No parent for filter"); } parent.setEntityResolver(this); parent.setDTDHandler(this); parent.setContentHandler(this); parent.setErrorHandler(this); }
/** * Runs the recorder on the specified input source. * * @param is The input source. * * @return The recorded sequence of events. * * @throws LoadingException If thrown whilst parsing. * @throws IOException Should I/O error occur. */ public EventSequence process(InputSource is) throws LoadingException, IOException { if (reader == null || newReader) { init(); } reader.setContentHandler(new RecorderHandler()); reader.setErrorHandler(new RecorderErrorHandler()); try { reader.setFeature("http://xml.org/sax/features/namespaces", this.config.isNamespaceAware()); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", this.config.isReportPrefixDifferences()); reader.parse(is); } catch (SAXException ex) { throw new LoadingException(ex); } return this.sequence; }
/** Bind our input streams to an XMLReader. * * Just a convenience routine; obviously you can explicitly register * this as a listener with the same effect. * */ public void setXMLReader(XMLReader eventsource) { fXMLReader=eventsource; eventsource.setContentHandler(this); eventsource.setDTDHandler(this); eventsource.setErrorHandler(this); // to report fatal errors in filtering mode // Not supported by all SAX2 filters: try { eventsource. setProperty("http://xml.org/sax/properties/lexical-handler", this); } catch(SAXNotRecognizedException e) { // Nothing we can do about it } catch(SAXNotSupportedException e) { // Nothing we can do about it } // Should we also bind as other varieties of handler? // (DTDHandler and so on) }
/** * Sets the error handler. * * @param handler The new error handler. */ public void setErrorHandler(ErrorHandler handler) { try { parser.getXMLReader().setErrorHandler(handler); } catch (SAXException ignore) {} }
r.setFeature(validationFeature,true); r.setFeature(schemaFeature,true); r.setErrorHandler(new MyErrorHandler()); r.parse(x); } catch (SAXException e) { System.out.println(e.toString());
parser.setEntityResolver(out.getEntityResolver()); parser.setErrorHandler(new DefaultHandler());
private static void parseXml(InputStream in, String systemId, DefaultHandler handler) throws SAXException, IOException { InputSource inSrc = new InputSource(); inSrc.setSystemId(systemId); inSrc.setByteStream(toCloseIgnoring(in)); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); // Especially as we use dummy empty DTD-s XMLReader reader; try { reader = factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { // Not expected throw new RuntimeException("XML parser setup failed", e); } reader.setEntityResolver(new EmptyContentEntityResolver()); // To deal with referred DTD-s reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.parse(inSrc); }
/** * Set up before a parse. * * <p>Before every parse, check whether the parent is * non-null, and re-register the filter for all of the * events.</p> */ private void setupParse () { XMLReader p = getParent(); if (p == null) { throw new NullPointerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_PARENT_FOR_FILTER, null)); //"No parent for filter"); } ContentHandler ch = m_transformer.getInputContentHandler(); // if(ch instanceof SourceTreeHandler) // ((SourceTreeHandler)ch).setUseMultiThreading(true); p.setContentHandler(ch); p.setEntityResolver(this); p.setDTDHandler(this); p.setErrorHandler(this); }