下面列出了怎么用org.xml.sax.ext.DefaultHandler2的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void docParseSAXTest() {
HtmlSAXSupport support = new HtmlSAXSupport(new DefaultHandler2() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
//System.out.println(localName);
}
}, new DefaultHandler2());
stream.forEach(support);
}
/**
* Creates a new {@link DocumentBuilder} instance that is secured against XXE attacks.
*
* Needed because DocumentBuilder is not thread-safe and crashes when different threads try to
* parse at the same time.
*
* @return
* @throws IOException
* if it fails to create a {@link DocumentBuilder}
*/
public static DocumentBuilder createDocumentBuilder() throws XMLParserException {
try {
synchronized (BUILDER_FACTORY) {
DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder();
// In contrast to the native default error handler,
// DefaultHandler2 does not print stuff to System.err
builder.setErrorHandler(new DefaultHandler2());
return builder;
}
} catch (ParserConfigurationException e) {
LogService.getRoot().log(Level.WARNING, "Unable to create document builder", e);
throw new XMLParserException(e);
}
}
static protected ContextsResult contextsResults(
final JettyResponseListener response) throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not "
+ contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final Collection<Resource> contexts =
Collections.synchronizedCollection(new LinkedList<Resource>());
/*
* For example:
* <contexts>
* <context uri="http://foo"/>
* <context uri="http://bar"/>
* </contexts>
*/
parser.parse(response.getInputStream(), new DefaultHandler2(){
@Override
public void startElement(final String uri,
final String localName, final String qName,
final Attributes attributes) {
if ("context".equals(qName))
contexts.add(new URIImpl(attributes.getValue("uri")));
}
});
// done.
return new ContextsResult(contexts);
} finally {
if (response != null) {
response.abort();
}
}
}
public void testDefaultHandler2() {
new DefaultHandler2();
}
static protected MutationResult mutationResults(final JettyResponseListener response)
throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not "
+ contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final AtomicLong mutationCount = new AtomicLong();
final AtomicLong elapsedMillis = new AtomicLong();
/*
* For example: <data modified="5" milliseconds="112"/>
*/
parser.parse(response.getInputStream(), new DefaultHandler2(){
@Override
public void startElement(final String uri,
final String localName, final String qName,
final Attributes attributes) {
if (!"data".equals(qName))
throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
+ ", localName=" + localName + ", qName="
+ qName);
mutationCount.set(Long.valueOf(attributes
.getValue("modified")));
elapsedMillis.set(Long.valueOf(attributes
.getValue("milliseconds")));
}
});
// done.
return new MutationResult(mutationCount.get(), elapsedMillis.get());
} finally {
if (response != null) {
response.abort();
}
}
}
/**
* Accept and parse a boolean response (NSS specific response type).
*/
static protected BooleanResult booleanResults(
final JettyResponseListener response) throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not "
+ contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final AtomicBoolean result = new AtomicBoolean();
final AtomicLong elapsedMillis = new AtomicLong();
/*
* For example: <data rangeCount="5" milliseconds="112"/>
*/
parser.parse(response.getInputStream(), new DefaultHandler2(){
@Override
public void startElement(final String uri,
final String localName, final String qName,
final Attributes attributes) {
if (!"data".equals(qName))
throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
+ ", localName=" + localName + ", qName="
+ qName);
result.set(Boolean.valueOf(attributes
.getValue("result")));
elapsedMillis.set(Long.valueOf(attributes
.getValue("milliseconds")));
}
});
// done.
return new BooleanResult(result.get(), elapsedMillis.get());
} finally {
if (response != null) {
response.abort();
}
}
}
/**
* Accept and parse a range count response (NSS specific response type).
*/
static protected RangeCountResult rangeCountResults(
final JettyResponseListener response) throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not "
+ contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final AtomicLong rangeCount = new AtomicLong();
final AtomicLong elapsedMillis = new AtomicLong();
/*
* For example: <data rangeCount="5" milliseconds="112"/>
*/
parser.parse(response.getInputStream(), new DefaultHandler2(){
@Override
public void startElement(final String uri,
final String localName, final String qName,
final Attributes attributes) {
if (!"data".equals(qName))
throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
+ ", localName=" + localName + ", qName="
+ qName);
rangeCount.set(Long.valueOf(attributes
.getValue("rangeCount")));
elapsedMillis.set(Long.valueOf(attributes
.getValue("milliseconds")));
}
});
// done.
return new RangeCountResult(rangeCount.get(), elapsedMillis.get());
} finally {
if (response != null) {
response.abort();
}
}
}
private IRemoteTxState0 singleTxResponse(final JettyResponseListener response)
throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not " + contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final AtomicLong txId = new AtomicLong();
final AtomicLong readsOnCommitTime = new AtomicLong();
/*
* For example: <tx txId="-512" readsOnCommitTime="11201201212"/>
*/
parser.parse(response.getInputStream(), new DefaultHandler2() {
@Override
public void startElement(final String uri, final String localName,
final String qName, final Attributes attributes) {
if ("response".equals(qName)) {
// This is the outer element.
return;
}
if (!"tx".equals(qName))
throw new RuntimeException("Expecting: 'tx', but have: uri="
+ uri + ", localName=" + localName + ", qName=" + qName);
txId.set(Long.valueOf(attributes.getValue("txId")));
readsOnCommitTime.set(Long.valueOf(attributes
.getValue("readsOnCommitTime")));
}
});
// done.
return new RemoteTxState0(txId.get(), readsOnCommitTime.get());
} finally {
if (response != null) {
response.abort();
}
}
}
private List<IRemoteTxState0> multiTxResponse(
final JettyResponseListener response) throws Exception {
try {
final String contentType = response.getContentType();
if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {
throw new RuntimeException("Expecting Content-Type of "
+ IMimeTypes.MIME_APPLICATION_XML + ", not " + contentType);
}
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final List<IRemoteTxState0> list = new LinkedList<IRemoteTxState0>();
/*
* For example: <tx txId="-512" readsOnCommitTime="11201201212"/>
*/
parser.parse(response.getInputStream(), new DefaultHandler2() {
@Override
public void startElement(final String uri, final String localName,
final String qName, final Attributes attributes) {
if ("response".equals(qName)) {
// This is the outer element.
return;
}
if (!"tx".equals(qName))
throw new RuntimeException("Expecting: 'tx', but have: uri="
+ uri + ", localName=" + localName + ", qName=" + qName);
final long txId = Long.valueOf(attributes.getValue("txId"));
final long readsOnCommitTime = Long.valueOf(attributes
.getValue("readsOnCommitTime"));
list.add(new RemoteTxState0(txId, readsOnCommitTime));
}
});
// done.
return list;
} finally {
if (response != null) {
response.abort();
}
}
}