本文整理汇总了Java中org.xml.sax.XMLReader.setFeature方法的典型用法代码示例。如果您正苦于以下问题:Java XMLReader.setFeature方法的具体用法?Java XMLReader.setFeature怎么用?Java XMLReader.setFeature使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xml.sax.XMLReader
的用法示例。
在下文中一共展示了XMLReader.setFeature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Parse the given XML string an create/update the corresponding entities
*
* @param xml
* the XML string
* @return the parse return code
* @throws Exception
*/
public int parse(byte[] xml) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try (InputStream inputStream = ResourceLoader.getResourceAsStream(
getClass(), getSchemaName())) {
Schema schema = sf.newSchema(new StreamSource(inputStream));
spf.setSchema(schema);
}
SAXParser saxParser = spf.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setFeature(Constants.XERCES_FEATURE_PREFIX
+ Constants.DISALLOW_DOCTYPE_DECL_FEATURE, true);
reader.setContentHandler(this);
reader.parse(new InputSource(new ByteArrayInputStream(xml)));
return 0;
}
示例2: doTests
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
public void doTests() throws Exception {
String fileName ;
fileName = "torture.xml";
/* The tests do not check for content length */
StringMsgParser.setComputeContentLengthFromMessage(true);
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
XMLReader saxParser = saxParserFactory.newSAXParser()
.getXMLReader();
saxParser.setEntityResolver(new MyEntityResolver());
saxParser.setContentHandler(this);
saxParser
.setFeature("http://xml.org/sax/features/validation", true);
saxParser.parse(new InputSource ( Torture.class.getResourceAsStream(fileName)));
System.out.println("Elapsed time = "
+ (System.currentTimeMillis() - startTime) / counter);
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
示例3: processSource
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
protected Source processSource(Source source) {
if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
InputSource inputSource = new InputSource(streamSource.getInputStream());
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
String featureName = "http://xml.org/sax/features/external-general-entities";
xmlReader.setFeature(featureName, isProcessExternalEntities());
if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
}
return new SAXSource(xmlReader, inputSource);
}
catch (SAXException ex) {
logger.warn("Processing of external entities could not be disabled", ex);
return source;
}
}
else {
return source;
}
}
示例4: testWithTrueTrue
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* namespace processing is enabled. namespace-prefix is also is enabled.
* So it is a True-True combination.
* The test is to test XMLReader with these conditions.
*
* @throws Exception If any errors occur.
*/
public void testWithTrueTrue() throws Exception {
String outputFile = USER_DIR + "XRNSTableTT.out";
String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true);
try (FileInputStream fis = new FileInputStream(xmlFile);
MyNSContentHandler handler = new MyNSContentHandler(outputFile);) {
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(fis));
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例5: xrNSTable01
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Here namespace processing and namespace-prefixes are enabled.
* The testcase tests XMLReader for this.
*
* @throws Exception If any errors occur.
*/
@Test
public void xrNSTable01() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlReader.getFeature(NAMESPACES));
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
示例6: receiveXMLStreamDoctypeDisabled
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
public static void receiveXMLStreamDoctypeDisabled(final InputStream inStream)
throws ParserConfigurationException, SAXException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
// Secure processing enabled
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
reader.setContentHandler(new PrintHandler());
reader.parse(new InputSource(inStream));
}
示例7: parse
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Parses the document.
* @param provider an implementation of the XMLProvider interface
* @param source a SAX input source
* @param context a parsing context
* @param rootParser the root parser to start with; if null,
* a root parser will be derived based on the rootClass
* requested in the constructor.
* @return an object that is the result of parsing.
*/
public Object parse(
XMLProvider provider,
InputSource source,
ParseContext context,
NodeParser rootParser) throws IOException, SAXException
{
if ((_rootClass == null) && (rootParser == null))
throw new NullPointerException(_LOG.getMessage(
"NULL_ROOTCLASS_ROOTPARSER"));
if (provider == null)
provider = new JaxpXMLProvider();
ParseContextWrapper wrappedContext = new ParseContextWrapper(context,
_manager,
provider);
Handler handler = new Handler(wrappedContext, rootParser);
XMLReader reader = provider.getXMLReader();
// Force these two features to be set the way we want.
// These are the default values, but we'd crash and burn
// if they're wrong.
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
if (getEntityResolver() != null)
reader.setEntityResolver(getEntityResolver());
reader.parse(source);
return handler.getRoot();
}
示例8: getHtmlAsXml
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
@Override
public XmlScriptType getHtmlAsXml()
{
try
{
XMLReader htmlParser = new Parser();
htmlParser.setFeature(Parser.namespacesFeature, false);
htmlParser.setFeature(Parser.namespacePrefixesFeature, false);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
transformer.transform(new SAXSource(htmlParser, new InputSource(new StringReader(getAsText()))),
result);
Node node = result.getNode();
if( node.getNodeType() == Node.DOCUMENT_NODE )
{
node = node.getFirstChild();
}
return new PropBagWrapper(new PropBagEx(node));
}
catch( Exception ex )
{
throw new RuntimeException("Response received from external URL could not be tidied into XML", ex);
}
}
示例9: readSAXSource
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
private SAXSource readSAXSource(InputStream body) throws IOException {
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
byte[] bytes = StreamUtils.copyToByteArray(body);
if (!isProcessExternalEntities()) {
reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
}
return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
}
catch (SAXException ex) {
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
}
}
示例10: decode
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
if (response.status() == 404) return Util.emptyValueOf(type);
if (response.body() == null) return null;
ContentHandlerWithResult.Factory<?> handlerFactory = handlerFactories.get(type);
checkState(handlerFactory != null, "type %s not in configured handlers %s", type,
handlerFactories.keySet());
ContentHandlerWithResult<?> handler = handlerFactory.create();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
xmlReader.setFeature("http://xml.org/sax/features/validation", false);
/* Explicitly control sax configuration to prevent XXE attacks */
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
xmlReader.setContentHandler(handler);
InputStream inputStream = response.body().asInputStream();
try {
xmlReader.parse(new InputSource(inputStream));
} finally {
ensureClosed(inputStream);
}
return handler.result();
} catch (SAXException e) {
throw new DecodeException(e.getMessage(), e);
}
}
示例11: getXMLReader
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Returns an instance of XMLReader with a catalog if one is provided.
*
* @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
* through the factory
* @param useCatalog the value of USE_CATALOG
* @param catalog a catalog
* @return an instance of XMLReader
* @throws ParserConfigurationException
* @throws SAXException
*/
XMLReader getXMLReader(boolean setUseCatalog, boolean useCatalog, String catalog)
throws ParserConfigurationException, SAXException {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader reader = spf.newSAXParser().getXMLReader();
if (setUseCatalog) {
reader.setFeature(XMLConstants.USE_CATALOG, useCatalog);
}
reader.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
return reader;
}
示例12: featureNS03
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Obtain http://xml.org/sax/features/namespaces feature name after it's
* just set. Expect it's same as set value.
*
* @throws Exception If any errors occur.
*/
@Test
public void featureNS03() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACES, true);
assertTrue(xmlReader.getFeature(NAMESPACES));
xmlReader.setFeature(NAMESPACES, false);
assertFalse(xmlReader.getFeature(NAMESPACES));
}
示例13: featureNSP03
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Obtain http://xml.org/sax/features/namespaces-prefixes feature name after
* it's just set. Expect it's same as set value.
*
* @throws Exception If any errors occur.
*/
@Test
public void featureNSP03() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(NAMESPACE_PREFIXES, true);
assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
xmlReader.setFeature(NAMESPACE_PREFIXES, false);
assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
}
示例14: featureV02
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* getFeature returns the value that a feature has been preset as when
* namespace awareness is set.
*
* @throws Exception If any errors occur.
*/
@Test
public void featureV02() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(VALIDATION, true);
assertTrue(xmlReader.getFeature(VALIDATION));
xmlReader.setFeature(VALIDATION, false);
assertFalse(xmlReader.getFeature(VALIDATION));
}
示例15: featureEGE02
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* getFeature returns false if a feature has been preset as false when
* namespace awareness is set.
*
* @throws Exception If any errors occur.
*/
@Test
public void featureEGE02() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setFeature(EXTERNAL_G_ENTITIES, false);
assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES));
}