use of org.xml.sax.ErrorHandler in project mybatis-3 by mybatis.
the class XPathParser method createDocument.
private Document createDocument(InputSource inputSource) {
// important: this must only be called AFTER common constructor
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setValidating(validation);
factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);
builder.setErrorHandler(new ErrorHandler() {
@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
// NOP
}
});
return builder.parse(inputSource);
} catch (Exception e) {
throw new BuilderException("Error creating document instance. Cause: " + e, e);
}
}
use of org.xml.sax.ErrorHandler in project cobar by alibaba.
the class ConfigUtil method getDocument.
public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(dtd);
}
});
builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) {
}
@Override
public void error(SAXParseException e) throws SAXException {
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
throw e;
}
});
return builder.parse(xml);
}
use of org.xml.sax.ErrorHandler in project spring-framework by spring-projects.
the class PersistenceUnitReader method readPersistenceUnitInfos.
/**
* Parse and build all persistence unit infos defined in the given XML files.
* @param persistenceXmlLocations the resource locations (can be patterns)
* @return the resulting PersistenceUnitInfo instances
*/
public SpringPersistenceUnitInfo[] readPersistenceUnitInfos(String[] persistenceXmlLocations) {
ErrorHandler handler = new SimpleSaxErrorHandler(logger);
List<SpringPersistenceUnitInfo> infos = new ArrayList<>(1);
String resourceLocation = null;
try {
for (String location : persistenceXmlLocations) {
Resource[] resources = this.resourcePatternResolver.getResources(location);
for (Resource resource : resources) {
resourceLocation = resource.toString();
try (InputStream stream = resource.getInputStream()) {
Document document = buildDocument(handler, stream);
parseDocument(resource, document, infos);
}
}
}
} catch (IOException ex) {
throw new IllegalArgumentException("Cannot parse persistence unit from " + resourceLocation, ex);
} catch (SAXException ex) {
throw new IllegalArgumentException("Invalid XML in persistence unit from " + resourceLocation, ex);
} catch (ParserConfigurationException ex) {
throw new IllegalArgumentException("Internal error parsing persistence unit from " + resourceLocation);
}
return infos.toArray(new SpringPersistenceUnitInfo[0]);
}
use of org.xml.sax.ErrorHandler in project nokogiri by sparklemotion.
the class XmlSchema method validate_document_or_file.
IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
RubyArray<?> errors = (RubyArray) this.getInstanceVariable("@errors");
ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
setErrorHandler(errorHandler);
try {
validate(xmlDocument.getDocument());
} catch (SAXException ex) {
XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
xmlSyntaxError.setException(ex);
errors.append(xmlSyntaxError);
} catch (IOException ex) {
throw context.runtime.newIOError(ex.getMessage());
}
return errors;
}
use of org.xml.sax.ErrorHandler in project tomcat by apache.
the class TestJspDocumentParser method testSchemaValidation.
@Test
public void testSchemaValidation() throws Exception {
getTomcatInstanceTestWebapp(false, true);
String path = "http://localhost:" + getPort() + "/test/valid.jspx";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setFeature("http://apache.org/xml/features/validation/schema", true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
});
Document document = db.parse(path);
Assert.assertEquals("urn:valid", document.getDocumentElement().getNamespaceURI());
Assert.assertEquals("root", document.getDocumentElement().getLocalName());
}
Aggregations