use of org.xml.sax.ErrorHandler in project kie-wb-common by kiegroup.
the class PersistenceDescriptorXMLMarshaller method fromXML.
public static PersistenceDescriptorModel fromXML(InputStream xmlStream, boolean validate) throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlStream);
if (validate) {
Validator validator = getPersistenceSchema().newValidator();
validator.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException e) throws SAXException {
// TODO add fine grained error processing if needed
logger.warn("PersistenceDescriptorModel parsing error: ", e);
}
@Override
public void error(SAXParseException e) throws SAXException {
// TODO add fine grained error processing if needed
logger.error("PersistenceDescriptorModel parsing error: ", e);
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
// TODO add fine grained error processing if needed
logger.error("PersistenceDescriptorModel parsing error: ", e);
throw e;
}
});
validator.validate(new DOMSource(document));
}
DOM2PersistenceDescriptorVisitor visitor = new DOM2PersistenceDescriptorVisitor(document);
return visitor.visit();
}
use of org.xml.sax.ErrorHandler in project tomee by apache.
the class SuperProperties method getDocumentBuilder.
private DocumentBuilder getDocumentBuilder() {
if (builder == null) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try {
builder = factory.newDocumentBuilder();
} catch (final ParserConfigurationException e) {
throw new Error(e);
}
builder.setErrorHandler(new ErrorHandler() {
public void warning(final SAXParseException e) throws SAXException {
throw e;
}
public void error(final SAXParseException e) throws SAXException {
throw e;
}
public void fatalError(final SAXParseException e) throws SAXException {
throw e;
}
});
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException {
if (systemId.equals(PROP_DTD_NAME)) {
final InputSource result = new InputSource(new StringReader(PROP_DTD));
result.setSystemId(PROP_DTD_NAME);
return result;
}
throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
}
});
}
return builder;
}
use of org.xml.sax.ErrorHandler in project jena by apache.
the class NTriple method processOpts.
private static int processOpts(String opts, String nextArg) {
boolean usedNext = false;
ARPOptions options = arp.getOptions();
for (int i = 0; i < opts.length(); i++) {
char opt = opts.charAt(i);
if ("beiwD".indexOf(opt) != -1) {
if (usedNext)
usage();
usedNext = true;
}
switch(opt) {
case 'D':
final int nStatements = Integer.parseInt(nextArg);
rt.gc();
rt.gc();
startMem = (int) (rt.totalMemory() - rt.freeMemory());
arp.getHandlers().setStatementHandler(new StatementHandler() {
int debugC = 0;
@Override
public void statement(AResource subj, AResource pred, AResource obj) {
statement(null, null, (ALiteral) null);
}
@Override
public void statement(AResource subj, AResource pred, ALiteral lit) {
if (++debugC % 100 == 0) {
System.out.println("T: " + debugC);
rt.gc();
System.out.println("M1: " + (rt.totalMemory() - rt.freeMemory() - startMem));
rt.gc();
System.out.println("M2: " + (rt.totalMemory() - rt.freeMemory() - startMem));
}
if (debugC == 1) {
rt.gc();
rt.gc();
startMem = (int) (rt.totalMemory() - rt.freeMemory());
}
if (debugC == nStatements) {
rt.gc();
System.err.println("Kill me now.");
try {
Thread.sleep(200000);
} catch (Exception e) {
// ignore
}
}
}
});
break;
case 'x':
options.setLaxErrorMode();
break;
case 's':
options.setStrictErrorMode();
break;
case 't':
arp.getHandlers().setStatementHandler(getSH(false));
break;
case 'r':
options.setEmbedding(false);
break;
case 'R':
options.setEmbedding(true);
break;
case 'n':
numbers = true;
break;
case 'E':
arp.getHandlers().setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
/* ignore */
}
@Override
public void error(SAXParseException exception) {
/* ignore */
}
@Override
public void fatalError(SAXParseException exception) {
/* ignore */
}
});
arp.setBadStatementHandler(new SH(System.err));
break;
case 'b':
xmlBase = nextArg;
break;
case 'e':
setErrorMode(nextArg, EM_ERROR);
break;
case 'i':
setErrorMode(nextArg, EM_IGNORE);
break;
case 'w':
setErrorMode(nextArg, EM_WARNING);
break;
case 'f':
for (int j = 0; j < 400; j++) {
if (options.setErrorMode(j, -1) == EM_ERROR)
options.setErrorMode(j, EM_FATAL);
}
break;
case 'u':
options.setErrorMode(WARN_UNQUALIFIED_ATTRIBUTE, EM_IGNORE);
options.setErrorMode(WARN_UNQUALIFIED_RDF_ATTRIBUTE, EM_IGNORE);
break;
default:
usage();
}
}
return usedNext ? 1 : 0;
}
use of org.xml.sax.ErrorHandler in project jena by apache.
the class ARPHandlers method setErrorHandler.
/**
* Sets the error handler, for both XML and RDF parse errors. XML errors are
* reported by Xerces, as instances of SAXParseException; the RDF errors are
* reported from ARP as instances of ParseException. Code that needs to
* distingusih between them may look like:
*
* <pre>
* void error( SAXParseException e ) throws SAXException {
* if ( e instanceof com.hp.hpl.jena.rdf.arp.ParseException ) {
* ...
* } else {
* ...
* }
* }
* </pre>
*
* <p>
* See the ARP documentation for ErrorHandler for details of the
* ErrorHandler semantics (in particular how to upgrade a warning to an
* error, and an error to a.errorError).
* </p>
* <p>
* The Xerces/SAX documentation for ErrorHandler is available on the web.
* </p>
*
* @param eh
* The error handler to use.
* @return The previous error handler.
*/
public ErrorHandler setErrorHandler(ErrorHandler eh) {
ErrorHandler old = errorHandler;
errorHandler = eh;
return old;
}
use of org.xml.sax.ErrorHandler in project jena by apache.
the class TestPropEltErrorMsg method runTest.
@Override
protected void runTest() {
Attributes noAtts = new Atts();
final StringBuffer buf = new StringBuffer();
XMLHandler arp = new XMLHandler();
arp.getHandlers().setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
buf.append(exception.getMessage());
buf.append("\n");
}
@Override
public void error(SAXParseException e) {
warning(e);
}
@Override
public void fatalError(SAXParseException e) {
warning(e);
}
});
try {
arp.initParse("http://example.org/", "");
arp.startElement(RDF.getURI(), "RDF", "rdf:RDF", noAtts);
arp.startElement(RDF.getURI(), "Description", "rdf:Description", noAtts);
arp.startElement(RDF.getURI(), "value", "rdf:value", testAtts);
} catch (SAXException e) {
}
// System.err.println("===");
// System.err.println("\""+getName()+"\",");
// System.err.println("---");
String contents = buf.toString();
// System.err.println("\""+(contents.length()>7?contents.substring(7).replace("\n","\\n"):"")+"\",");
assertEquals("test data muddled", rslts[n * 2], getName());
assertTrue("error message has changed.", contents.endsWith(rslts[n * 2 + 1]));
contents = null;
}
Aggregations