use of org.xml.sax.Attributes in project robovm by robovm.
the class ExpatSaxParserTest method testExceptions.
public void testExceptions() {
// From startElement().
ContentHandler contentHandler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
// From endElement().
contentHandler = new DefaultHandler() {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
// From characters().
contentHandler = new DefaultHandler() {
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
throw new SAXException();
}
};
try {
parse(SNIPPET, contentHandler);
fail();
} catch (SAXException checked) {
/* expected */
}
}
use of org.xml.sax.Attributes in project spring-framework by spring-projects.
the class AbstractStaxXMLReaderTestCase method mockContentHandler.
protected final ContentHandler mockContentHandler() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
return null;
}
}).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
return contentHandler;
}
use of org.xml.sax.Attributes in project robovm by robovm.
the class NamespacedAttributesLookupTest method getStartElements.
public List<String> getStartElements(String xml, final boolean namespace, boolean namespacePrefixes) throws Exception {
final List<String> result = new ArrayList<String>();
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
reader.setFeature(SAX_PROPERTY_NS, namespace);
reader.setFeature(SAX_PROPERTY_NS_PREFIXES, namespacePrefixes);
reader.setContentHandler(new DefaultHandler() {
@Override
public final void startElement(String uri, String localName, String qName, Attributes attributes) {
StringBuilder serialized = new StringBuilder();
/*
* Only supply the uri+localName or qname depending on whether namespaces are
* enabled. It's an optional parameter and the RI only supplies one or the other.
*/
if (namespace) {
serialized.append(uri).append(",");
serialized.append(localName);
} else {
serialized.append(qName);
}
for (int i = 0; i < attributes.getLength(); i++) {
serialized.append("\n ");
if (namespace) {
serialized.append(attributes.getURI(i)).append(",");
serialized.append(attributes.getLocalName(i));
} else {
serialized.append(attributes.getQName(i));
}
}
serialized.append("\n http://bar+c=").append(attributes.getValue("http://bar", "c")).append(",").append("\n bar:c=").append(attributes.getValue("bar:c")).append("\n");
result.add(serialized.toString());
}
});
reader.parse(new InputSource(new StringReader(xml)));
return result;
}
use of org.xml.sax.Attributes in project robovm by robovm.
the class SaxTest method testYesPrefixesYesNamespaces.
/**
* Android's Expat-based SAX parser fails this test because Expat doesn't
* supply us with our much desired {@code xmlns="http://..."} attributes.
*/
public void testYesPrefixesYesNamespaces() throws Exception {
parse(true, true, "<foo bar=\"baz\"/>", new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
assertEquals("", uri);
assertEquals("foo", localName);
assertEquals("foo", qName);
assertEquals(1, attributes.getLength());
assertEquals("", attributes.getURI(0));
assertEquals("bar", attributes.getLocalName(0));
assertEquals("bar", attributes.getQName(0));
}
});
parse(true, true, "<a:foo a:bar=\"baz\" xmlns:a=\"http://quux\"/>", new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
assertEquals("http://quux", uri);
assertEquals("foo", localName);
assertEquals("a:foo", qName);
assertEquals(2, attributes.getLength());
assertEquals("http://quux", attributes.getURI(0));
assertEquals("bar", attributes.getLocalName(0));
assertEquals("a:bar", attributes.getQName(0));
assertEquals("", attributes.getURI(1));
assertEquals("", attributes.getLocalName(1));
assertEquals("xmlns:a", attributes.getQName(1));
}
});
}
use of org.xml.sax.Attributes in project camel by apache.
the class ManualGenerator method grabBodyContent.
private String grabBodyContent() throws MalformedURLException, IOException {
URL url = new URL(page);
File file = new File(targetDir, ".manualCache-" + url.getFile().substring(1));
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
XMLReader parser = new Parser();
parser.setFeature(Parser.namespacesFeature, false);
parser.setFeature(Parser.namespacePrefixesFeature, false);
parser.setProperty(Parser.schemaProperty, new org.ccil.cowan.tagsoup.HTMLSchema() {
{
//problem with nested lists that the confluence {toc} macro creates
elementType("ul", M_LI, M_BLOCK | M_LI, 0);
}
});
StringWriter w = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(w) {
int inDiv = Integer.MAX_VALUE;
int count;
public void characters(char[] ch, int start, int len) throws SAXException {
if (inDiv <= count) {
super.characters(ch, start, len);
}
}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
count++;
if ("div".equalsIgnoreCase(qName) && "wiki-content maincontent".equalsIgnoreCase(atts.getValue("class"))) {
inDiv = count;
}
if (inDiv <= count) {
super.startElement(uri, localName, qName, atts);
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (inDiv <= count) {
super.endElement(uri, localName, qName);
}
count--;
if (inDiv > count) {
inDiv = Integer.MAX_VALUE;
}
}
};
xmlWriter.setOutputProperty(XMLWriter.OMIT_XML_DECLARATION, "yes");
xmlWriter.setOutputProperty(XMLWriter.METHOD, "html");
parser.setContentHandler(xmlWriter);
long date = con.getLastModified();
parser.parse(new InputSource(new BufferedInputStream(con.getInputStream())));
FileWriter writer = new FileWriter(file);
writer.write(Long.toString(date));
writer.close();
return w.toString();
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException("Failed", e);
}
}
Aggregations