本文整理匯總了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));
}