use of org.xml.sax.ErrorHandler in project Mycat-Server by MyCATApache.
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 JGroups by belaban.
the class WriteVersionTo method parseVersionAndCodenameFromPOM.
public static String[] parseVersionAndCodenameFromPOM(String pom) throws IOException {
String version, codename;
try (InputStream in = Util.getResourceAsStream(pom, Version.class)) {
if (in == null)
throw new FileNotFoundException(pom);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
final AtomicReference<SAXParseException> ex = new AtomicReference<>();
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.err.printf(Util.getMessage("ParseFailure"), e);
}
public void fatalError(SAXParseException exception) throws SAXException {
ex.set(exception);
}
public void error(SAXParseException exception) throws SAXException {
ex.set(exception);
}
});
Document document = builder.parse(in);
if (ex.get() != null)
throw ex.get();
Element root_element = document.getDocumentElement();
String root_name = root_element.getNodeName().trim().toLowerCase();
if (!"project".equals(root_name))
throw new IOException("the POM does not start with a <project> element: " + root_name);
version = Util.getChild(root_element, "version");
codename = Util.getChild(root_element, "properties.codename");
return new String[] { version, codename };
} catch (Exception x) {
throw new IOException(String.format(Util.getMessage("ParseError"), x.getLocalizedMessage()));
}
}
use of org.xml.sax.ErrorHandler in project cxf by apache.
the class SoapHeaderInterceptor method validateHeader.
private void validateHeader(final SoapMessage message, MessagePartInfo mpi, Schema schema) {
Header param = findHeader(message, mpi);
if (param != null && param.getDataBinding() == null) {
Node source = (Node) param.getObject();
if (!(source instanceof Element)) {
return;
}
if (schema != null) {
final Element el = (Element) source;
DOMSource ds = new DOMSource(el);
try {
Validator v = schema.newValidator();
ErrorHandler errorHandler = new ErrorHandler() {
public void warning(SAXParseException exception) throws SAXException {
}
public void error(SAXParseException exception) throws SAXException {
String msg = exception.getMessage();
if (msg.contains(el.getLocalName()) && (msg.contains(":" + message.getVersion().getAttrNameRole()) || msg.contains(":" + message.getVersion().getAttrNameMustUnderstand()))) {
return;
}
throw exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
};
v.setErrorHandler(errorHandler);
v.validate(ds);
} catch (SAXException | IOException e) {
throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
}
}
}
}
use of org.xml.sax.ErrorHandler in project validator by validator.
the class SvgAnalyzer method main.
/**
* @param args
* @throws Exception
* @throws SAXException
*/
public static void main(String[] args) throws SAXException, Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
XMLReader parser = factory.newSAXParser().getXMLReader();
SvgAnalysisHandler analysisHandler = new SvgAnalysisHandler();
parser.setContentHandler(analysisHandler);
parser.setDTDHandler(analysisHandler);
parser.setProperty("http://xml.org/sax/properties/lexical-handler", analysisHandler);
parser.setProperty("http://xml.org/sax/properties/declaration-handler", analysisHandler);
parser.setFeature("http://xml.org/sax/features/string-interning", true);
parser.setEntityResolver(new NullEntityResolver());
parser.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
}
public void fatalError(SAXParseException exception) throws SAXException {
}
public void warning(SAXParseException exception) throws SAXException {
}
});
File dir = new File(args[0]);
File[] list = dir.listFiles();
int nsError = 0;
int encodingErrors = 0;
int otherIllFormed = 0;
double total = (double) list.length;
for (int i = 0; i < list.length; i++) {
File file = list[i];
try {
InputSource is = new InputSource(new FileInputStream(file));
is.setSystemId(file.toURL().toExternalForm());
parser.parse(is);
} catch (SAXParseException e) {
String msg = e.getMessage();
if (msg.startsWith("The prefix ")) {
nsError++;
} else if (msg.contains("Prefixed namespace bindings may not be empty.")) {
nsError++;
} else if (msg.startsWith("Invalid byte ")) {
encodingErrors++;
} else {
otherIllFormed++;
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
System.out.print("NS errors: ");
System.out.println(((double) nsError) / total);
System.out.print("Encoding errors: ");
System.out.println(((double) encodingErrors) / total);
System.out.print("Other WF errors: ");
System.out.println(((double) otherIllFormed) / total);
analysisHandler.print();
}
use of org.xml.sax.ErrorHandler in project validator by validator.
the class ValidationWorker method setupValidator.
private Validator setupValidator(Set<Schema> schemas) {
PropertyMapBuilder builder = new PropertyMapBuilder();
builder.put(ValidateProperty.ERROR_HANDLER, new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
validationErrors.add(replaceSpecificValues(exception.getMessage()));
}
public void fatalError(SAXParseException exception) throws SAXException {
// should not happen
validationErrors.add(replaceSpecificValues(exception.getMessage()));
}
public void warning(SAXParseException exception) throws SAXException {
}
});
PropertyMap map = builder.toPropertyMap();
Validator rv = null;
for (Schema schema : schemas) {
Validator v = schema.createValidator(map);
if (rv == null) {
rv = v;
} else {
rv = new CombineValidator(rv, v);
}
}
return rv;
}
Aggregations