本文整理汇总了Java中javax.xml.parsers.SAXParserFactory.setFeature方法的典型用法代码示例。如果您正苦于以下问题:Java SAXParserFactory.setFeature方法的具体用法?Java SAXParserFactory.setFeature怎么用?Java SAXParserFactory.setFeature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.SAXParserFactory
的用法示例。
在下文中一共展示了SAXParserFactory.setFeature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retrieveJAXB
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Non-validating
*
* @param path path to input file
* @param _class class of xml root object
* @return root object
*/
public static Object retrieveJAXB(String path, Class _class) {
Object obj = null;
/*
Block parser from reaching out externally see:
https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#SAXTransformerFactory
*/
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(),
new InputSource(new FileReader(new File(path))));
JAXBContext jc = JAXBContext.newInstance(_class);
Unmarshaller um = jc.createUnmarshaller();
obj = um.unmarshal(xmlSource);
} catch (JAXBException | FileNotFoundException | SAXException | ParserConfigurationException e) {
e.printStackTrace();
}
return obj;
}
示例2: testValidMaxOccurLimitWithOutSecureProcessing
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public void testValidMaxOccurLimitWithOutSecureProcessing() {
if (isSecureMode())
return; // jaxp secure feature can not be turned off when security
// manager is present
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
spf.setValidating(true);
// Set the properties for Schema Validation
String SCHEMA_LANG = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
String SCHEMA_TYPE = "http://www.w3.org/2001/XMLSchema";
// Get the Schema location as a File object
File schemaFile = new File(this.getClass().getResource("toys3002.xsd").toURI());
// Get the parser
SAXParser parser = spf.newSAXParser();
parser.setProperty(SCHEMA_LANG, SCHEMA_TYPE);
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaFile);
InputStream is = this.getClass().getResourceAsStream("toys.xml");
MyErrorHandler eh = new MyErrorHandler();
parser.parse(is, eh);
Assert.assertFalse(eh.errorOccured, "Expected Error as maxOccurLimit is exceeded");
} catch (Exception e) {
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例3: testMaxOccurLimitPos
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Testing set MaxOccursLimit to 10000 in the secure processing enabled for
* SAXParserFactory.
*
* @throws Exception If any errors occur.
*/
@Test
public void testMaxOccurLimitPos() throws Exception {
String schema_file = XML_DIR + "toys.xsd";
String xml_file = XML_DIR + "toys.xml";
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setFeature(FEATURE_SECURE_PROCESSING, true);
setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000));
SAXParser parser = factory.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file));
try (InputStream is = new FileInputStream(xml_file)) {
MyErrorHandler eh = new MyErrorHandler();
parser.parse(is, eh);
assertFalse(eh.isAnyError());
}
}
示例4: parseXML
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**Parses the XML to fetch parameters.
* @param inputFile, source XML
* @return true, if XML is successfully parsed.
* @throws Exception
*/
public boolean parseXML(File inputFile,UIComponentRepo componentRepo) throws ParserConfigurationException, SAXException, IOException{
LOGGER.debug("Parsing target XML for separating Parameters");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser;
try {
factory.setFeature(Constants.DISALLOW_DOCTYPE_DECLARATION,true);
saxParser = factory.newSAXParser();
XMLHandler xmlhandler = new XMLHandler(componentRepo);
saxParser.parse(inputFile, xmlhandler);
return true;
} catch (ParserConfigurationException | SAXException | IOException exception) {
LOGGER.error("Parsing failed...",exception);
throw exception;
}
}
示例5: testXMLNoNsAwareStreamResult4
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Test an identity transform of an XML document with NS decls using a
* non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to
* TRUE and prefixes to TRUE.
*/
@Test
public void testXMLNoNsAwareStreamResult4() {
try {
// Create SAX parser *without* enabling ns
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); // Same as default
spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
SAXParser sp = spf.newSAXParser();
// Make sure that the output is well formed
String xml = runTransform(sp);
checkWellFormedness(xml);
} catch (Throwable ex) {
Assert.fail(ex.toString());
}
}
示例6: test
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
String invalidXml = "<a>";
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
SAXParser parser = saxParserFactory.newSAXParser();
parser.parse(new InputSource(new StringReader(invalidXml)), new DefaultHandler() {
@Override
public void fatalError(SAXParseException e) throws SAXException {
System.err.printf("%s%n", e.getMessage());
}
});
}
示例7: parse
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@NonNull
private Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom)
throws ParserConfigurationException, SAXException, IOException {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(NAMESPACE_FEATURE, true);
factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
SAXParser parser = factory.newSAXParser();
DomBuilder handler = new DomBuilder(xml);
parser.parse(input, handler);
return handler.getDocument();
} catch (SAXException e) {
if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
// Byte order mark in the string? Skip it. There are many markers
// (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
// just skip those up to the XML prolog beginning character, <
xml = xml.replaceFirst("^([\\W]+)<","<"); //$NON-NLS-1$ //$NON-NLS-2$
return parse(xml, new InputSource(new StringReader(xml)), false);
}
throw e;
}
}
示例8: testXMLNoNsAwareStreamResult3
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Test an identity transform of an XML document with NS decls using a
* non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to
* TRUE and prefixes to FALSE.
*/
@Test
public void testXMLNoNsAwareStreamResult3() {
try {
// Create SAX parser *without* enabling ns
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true); // Same as default
spf.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
SAXParser sp = spf.newSAXParser();
// Make sure that the output is well formed
String xml = runTransform(sp);
checkWellFormedness(xml);
} catch (Throwable ex) {
Assert.fail(ex.toString());
}
}
示例9: newSAXParserFactory
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public static SAXParserFactory newSAXParserFactory(boolean disableSecurity) {
SAXParserFactory factory = SAXParserFactory.newInstance();
String featureToSet = XMLConstants.FEATURE_SECURE_PROCESSING;
try {
boolean securityOn = !xmlSecurityDisabled(disableSecurity);
factory.setFeature(featureToSet, securityOn);
factory.setNamespaceAware(true);
if (securityOn) {
featureToSet = DISALLOW_DOCTYPE_DECL;
factory.setFeature(featureToSet, true);
featureToSet = EXTERNAL_GE;
factory.setFeature(featureToSet, false);
featureToSet = EXTERNAL_PE;
factory.setFeature(featureToSet, false);
featureToSet = LOAD_EXTERNAL_DTD;
factory.setFeature(featureToSet, false);
}
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support "+featureToSet+" feature!", new Object[]{factory.getClass().getName()});
}
return factory;
}
示例10: testXMLNoNsAwareStreamResult1
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Test an identity transform of an XML document with NS decls using a
* non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to
* FALSE and prefixes to FALSE.
*/
@Test
public void testXMLNoNsAwareStreamResult1() {
try {
// Create SAX parser *without* enabling ns
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(false); // Same as default
spf.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
SAXParser sp = spf.newSAXParser();
// Make sure that the output is well formed
String xml = runTransform(sp);
checkWellFormedness(xml);
} catch (Throwable ex) {
Assert.fail(ex.toString());
}
}
示例11: testConformantSAX
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testConformantSAX() throws ParserConfigurationException, SAXException, IOException {
InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml");
// Parse with SAX
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setSchema(schema);
saxFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", true);
SAXParser saxparser = saxFactory.newSAXParser();
sawIgnorable = false;
saxparser.parse(xmlFile, new MyHandler());
Assert.assertEquals(false, sawIgnorable);
}
示例12: testSaxExcludeHTMLReferences
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* <p><b>Description:</b> Exclude External references. Load files only once.<p>
* <p><b>Bug ID:</b> #9</p>
*
* @author adrian_sorop
*
* @throws Exception
*/
public void testSaxExcludeHTMLReferences() throws Exception {
File ditaFile = new File(rootDir,"topics/add-terms-list.dita");
assertTrue("UNABLE TO LOAD FILE", ditaFile.exists());
URL url = URLUtil.correct(ditaFile);
SAXParserFactory factory = SAXParserFactory.newInstance();
// Ignore the DTD declaration
factory.setValidating(false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
SAXParser parser = factory.newSAXParser();
SaxContentHandler handler= new SaxContentHandler(url);
parser.parse(ditaFile, handler);
List<ReferencedResource> referredFiles = new ArrayList<ReferencedResource>();
referredFiles.addAll(handler.getDitaMapHrefs());
for (ReferencedResource referencedResource : referredFiles) {
System.out.println(referencedResource);
}
assertEquals("Four files should have been referred.", 4, referredFiles.size());
assertTrue(referredFiles.toString().contains("issue-9/topics/dictionaries-preferences-page.dita"));
assertTrue(referredFiles.toString().contains("issue-9/topics/add-Hunspell-dictionary.dita"));
assertTrue(referredFiles.toString().contains("issue-9/topics/topic2.dita"));
assertTrue(referredFiles.toString().contains("issue-9/topics/topic1.dita"));
}
开发者ID:oxygenxml,项目名称:oxygen-dita-translation-package-builder,代码行数:35,代码来源:AttributesCollectorUsingSaxTest.java
示例13: testFeature10
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Test the functionality of setFeature method for
* external-parameter-entitie property.
* @throws Exception If any errors occur.
*/
@Test
public void testFeature10() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(EXTERNAL_P_ENTITIES, false);
assertFalse(spf.getFeature(EXTERNAL_P_ENTITIES));
}
示例14: createBuilder
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private SAXBuilder createBuilder()
{
return new SAXBuilder(new XMLReaderJDOMFactory()
{
@Override
public XMLReader createXMLReader() throws JDOMException
{
SAXParserFactory fac = SAXParserFactory.newInstance();
// All JDOM parsers are namespace aware.
fac.setNamespaceAware(true);
fac.setValidating(false);
try
{
fac.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
return fac.newSAXParser().getXMLReader();
}
catch (ParserConfigurationException | SAXException e)
{
throw new RuntimeException(e);
}
}
@Override
public boolean isValidating()
{
return false;
}
}, null, null);
}
示例15: testFeature06
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Test the functionality of setFeature and getFeature methods
* for validation property.
* @throws Exception If any errors occur.
*/
@Test
public void testFeature06() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(VALIDATION, true);
assertTrue(spf.getFeature(VALIDATION));
spf.setFeature(VALIDATION, false);
assertFalse(spf.getFeature(VALIDATION));
}