use of org.xml.sax.SAXParseException in project robovm by robovm.
the class DocumentBuilderTest method testReset.
public void testReset() {
// Make sure EntityResolver gets reset
InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
InputStream entity = new ByteArrayInputStream("bar".getBytes());
MockResolver resolver = new MockResolver();
resolver.addEntity("foo", "foo", new InputSource(entity));
Document d;
try {
db = dbf.newDocumentBuilder();
db.setEntityResolver(resolver);
db.reset();
d = db.parse(source);
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
Element root = (Element) d.getElementsByTagName("a").item(0);
assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
// Make sure ErrorHandler gets reset
source = new ByteArrayInputStream("</a>".getBytes());
MethodLogger logger = new MethodLogger();
ErrorHandler handler = new MockHandler(logger);
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(handler);
db.reset();
d = db.parse(source);
} catch (SAXParseException e) {
// Expected
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals(0, logger.size());
}
use of org.xml.sax.SAXParseException in project robovm by robovm.
the class DocumentBuilderTest method testSetErrorHandler.
public void testSetErrorHandler() {
// Ordinary case
InputStream source = new ByteArrayInputStream("</a>".getBytes());
MethodLogger logger = new MethodLogger();
ErrorHandler handler = new MockHandler(logger);
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(handler);
db.parse(source);
} catch (SAXParseException e) {
// Expected, ErrorHandler does not mask exception
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals("error", logger.getMethod());
assertTrue(logger.getArgs()[0] instanceof SAXParseException);
// null case
source = new ByteArrayInputStream("</a>".getBytes());
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(null);
db.parse(source);
} catch (SAXParseException e) {
// Expected
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
}
use of org.xml.sax.SAXParseException in project j2objc by google.
the class DocumentBuilderTest method testSetErrorHandler.
public void testSetErrorHandler() {
// Ordinary case
InputStream source = new ByteArrayInputStream("</a>".getBytes());
MethodLogger logger = new MethodLogger();
ErrorHandler handler = new MockHandler(logger);
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(handler);
db.parse(source);
} catch (SAXParseException e) {
// Expected, ErrorHandler does not mask exception
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals("error", logger.getMethod());
assertTrue(logger.getArgs()[0] instanceof SAXParseException);
// null case
source = new ByteArrayInputStream("</a>".getBytes());
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(null);
db.parse(source);
} catch (SAXParseException e) {
// Expected
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
}
use of org.xml.sax.SAXParseException in project j2objc by google.
the class DocumentBuilderTest method testReset.
public void testReset() {
// Make sure EntityResolver gets reset
InputStream source = new ByteArrayInputStream("<a>&foo;</a>".getBytes());
InputStream entity = new ByteArrayInputStream("bar".getBytes());
MockResolver resolver = new MockResolver();
resolver.addEntity("foo", "foo", new InputSource(entity));
Document d;
try {
db = dbf.newDocumentBuilder();
db.setEntityResolver(resolver);
db.reset();
d = db.parse(source);
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
Element root = (Element) d.getElementsByTagName("a").item(0);
assertEquals("foo", ((EntityReference) root.getFirstChild()).getNodeName());
// Make sure ErrorHandler gets reset
source = new ByteArrayInputStream("</a>".getBytes());
MethodLogger logger = new MethodLogger();
ErrorHandler handler = new MockHandler(logger);
try {
db = dbf.newDocumentBuilder();
db.setErrorHandler(handler);
db.reset();
d = db.parse(source);
} catch (SAXParseException e) {
// Expected
} catch (Exception e) {
throw new RuntimeException("Unexpected exception", e);
}
assertEquals(0, logger.size());
}
use of org.xml.sax.SAXParseException in project gocd by gocd.
the class XmlSaxParserContext method parse_with.
@JRubyMethod
public IRubyObject parse_with(ThreadContext context, IRubyObject handlerRuby) {
Ruby ruby = context.getRuntime();
if (!invoke(context, handlerRuby, "respond_to?", ruby.newSymbol("document")).isTrue()) {
String msg = "argument must respond_to document";
throw ruby.newArgumentError(msg);
}
handler = new NokogiriHandler(ruby, handlerRuby);
preParse(context, handlerRuby, handler);
setContentHandler(handler);
setErrorHandler(handler);
try {
setProperty("http://xml.org/sax/properties/lexical-handler", handler);
} catch (Exception ex) {
throw ruby.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
}
try {
try {
do_parse();
} catch (SAXParseException spe) {
// A bad document (<foo><bar></foo>) should call the
// error handler instead of raising a SAX exception.
// However, an EMPTY document should raise a
// RuntimeError. This is a bit kludgy, but AFAIK SAX
// doesn't distinguish between empty and bad whereas
// Nokogiri does.
String message = spe.getMessage();
if ("Premature end of file.".matches(message) && stringDataSize < 1) {
throw ruby.newRuntimeError("couldn't parse document: " + message);
} else {
handler.error(spe);
}
}
} catch (SAXException se) {
throw RaiseException.createNativeRaiseException(ruby, se);
} catch (IOException ioe) {
throw ruby.newIOErrorFromException(ioe);
}
postParse(context, handlerRuby, handler);
return ruby.getNil();
}
Aggregations