下面列出了javax.xml.parsers.SAXParser#getProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void parseAndCheckReset(boolean setFeature, boolean value) throws Exception {
// Expected result based on system property and feature
boolean resetExpected = setFeature && value;
// Indicates if system property is set
boolean spSet = runWithAllPerm(() -> System.getProperty(RESET_FEATURE)) != null;
// Dummy xml input for parser
String input = "<dummy>Test</dummy>";
// Check if system property is set only when feature setting is not requested
// and estimate if reset of symbol table is expected
if (!setFeature && spSet) {
resetExpected = runWithAllPerm(() -> Boolean.getBoolean(RESET_FEATURE));
}
// Create SAXParser and set feature if it is requested
SAXParserFactory spf = SAXParserFactory.newInstance();
if (setFeature) {
spf.setFeature(RESET_FEATURE, value);
}
SAXParser p = spf.newSAXParser();
// First parse iteration
p.parse(new InputSource(new StringReader(input)), new DefaultHandler());
// Get first symbol table reference
Object symTable1 = p.getProperty(SYMBOL_TABLE_PROPERTY);
// reset parser
p.reset();
// Second parse iteration
p.parse(new InputSource(new StringReader(input)), new DefaultHandler());
// Get second symbol table reference
Object symTable2 = p.getProperty(SYMBOL_TABLE_PROPERTY);
// Check symbol table references after two subsequent parse operations
if (resetExpected) {
Assert.assertNotSame(symTable1, symTable2, "Symbol table references");
} else {
Assert.assertSame(symTable1, symTable2, "Symbol table references");
}
}
/**
* Test whether the xml-string property is not supported.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/
@Test(expectedExceptions = SAXNotSupportedException.class,
dataProvider = "parser-provider")
public void testProperty01(SAXParser saxparser) throws SAXException {
saxparser.getProperty(XML_STRING);
}
/**
* Test whether the dom-node property is not supported.
*
* @param saxparser a SAXParser instance.
* @throws SAXException If any parse errors occur.
*/
@Test(expectedExceptions = SAXNotSupportedException.class,
dataProvider = "parser-provider")
public void testProperty02(SAXParser saxparser) throws SAXException {
saxparser.getProperty(DOM_NODE);
}