本文整理匯總了Java中javax.xml.stream.XMLInputFactory.setProperty方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLInputFactory.setProperty方法的具體用法?Java XMLInputFactory.setProperty怎麽用?Java XMLInputFactory.setProperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.stream.XMLInputFactory
的用法示例。
在下文中一共展示了XMLInputFactory.setProperty方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testStreamReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public void testStreamReader() {
XMLInputFactory ifac = XMLInputFactory.newInstance();
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
try {
ifac.setProperty(ifac.IS_REPLACING_ENTITY_REFERENCES, new Boolean(false));
XMLStreamReader re = ifac.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE));
while (re.hasNext()) {
int event = re.next();
if (event == XMLStreamConstants.START_ELEMENT && re.getLocalName().equals("bookurn")) {
Assert.assertTrue(re.getAttributeCount() == 0, "No attributes are expected for <bookurn> ");
Assert.assertTrue(re.getNamespaceCount() == 2, "Two namespaces are expected for <bookurn> ");
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例2: getXMLEventReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
private XMLEventReader getXMLEventReader(final String filename) {
XMLInputFactory xmlif = null;
XMLEventReader xmlr = null;
try {
xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
// FileInputStream fis = new FileInputStream(filename);
FileInputStream fis = new FileInputStream(new File(ValidatorTest.class.getResource(filename).toURI()));
xmlr = xmlif.createXMLEventReader(filename, fis);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail("Exception : " + ex.getMessage());
}
return xmlr;
}
示例3: deserialiseObject
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/** Attempt to construct the specified object from this XML string
* @param xml the XML string to parse
* @param xsdFile the name of the XSD schema that defines the object
* @param objclass the class of the object requested
* @return if successful, an instance of class objclass that captures the data in the XML string
*/
static public Object deserialiseObject(String xml, String xsdFile, Class<?> objclass) throws JAXBException, SAXException, XMLStreamException
{
Object obj = null;
JAXBContext jaxbContext = getJAXBContext(objclass);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final String schemaResourceFilename = new String(xsdFile);
URL schemaURL = MalmoMod.class.getClassLoader().getResource(schemaResourceFilename);
Schema schema = schemaFactory.newSchema(schemaURL);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
StringReader stringReader = new StringReader(xml);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader XMLreader = xif.createXMLStreamReader(stringReader);
obj = jaxbUnmarshaller.unmarshal(XMLreader);
return obj;
}
示例4: testNamespaceCount
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public void testNamespaceCount() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
XMLStreamReader sr = xif.createXMLStreamReader(is);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(rootElement)) {
int count = sr.getNamespaceCount();
Assert.assertTrue(count == 1);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例5: testNamespaceContext
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public void testNamespaceContext() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
InputStream is = new java.io.ByteArrayInputStream(getXML().getBytes());
XMLStreamReader sr = xif.createXMLStreamReader(is);
while (sr.hasNext()) {
int eventType = sr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (sr.getLocalName().equals(childElement)) {
NamespaceContext context = sr.getNamespaceContext();
Assert.assertTrue(context.getPrefix(namespaceURI).equals(prefix));
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例6: FeatureConfigSnapshotHolder
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
public FeatureConfigSnapshotHolder(final ConfigFileInfo fileInfo,
final Feature feature) throws JAXBException, XMLStreamException {
Preconditions.checkNotNull(fileInfo);
Preconditions.checkNotNull(fileInfo.getFinalname());
Preconditions.checkNotNull(feature);
this.fileInfo = fileInfo;
this.featureChain.add(feature);
// TODO extract utility method for umarshalling config snapshots
JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File(fileInfo.getFinalname())));
unmarshalled = (ConfigSnapshot) um.unmarshal(xsr);
}
示例7: testHasNameOnEntityEvent
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/**
* CR 6631264 / sjsxp Issue 45:
* https://sjsxp.dev.java.net/issues/show_bug.cgi?id=45
* XMLStreamReader.hasName() should return false for ENTITY_REFERENCE
*/
@Test
public void testHasNameOnEntityEvent() throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader r = xif.createXMLStreamReader(
this.getClass().getResourceAsStream("ExternalDTD.xml"));
while (r.next() != XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("event type: " + r.getEventType());
continue;
}
if (r.hasName()) {
System.out.println("hasName returned true on ENTITY_REFERENCE event.");
}
Assert.assertFalse(r.hasName()); // fails
}
示例8: fromXml
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
public static Config fromXml(final File from) {
if(isEmpty(from)) {
return new Config();
}
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(from));
return (Config) um.unmarshal(xsr);
} catch (JAXBException | XMLStreamException e) {
throw new PersistException("Unable to restore configuration", e);
}
}
示例9: testStAXSource2
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public final void testStAXSource2() throws XMLStreamException {
XMLInputFactory ifactory = XMLInputFactory.newInstance();
ifactory.setProperty("javax.xml.stream.supportDTD", Boolean.TRUE);
StAXSource ss = new StAXSource(ifactory.createXMLStreamReader(getClass().getResource("5368141.xml").toString(),
getClass().getResourceAsStream("5368141.xml")));
DOMResult dr = new DOMResult();
TransformerFactory tfactory = TransformerFactory.newInstance();
try {
Transformer transformer = tfactory.newTransformer();
transformer.transform(ss, dr);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例10: testSwitchXMLVersions
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/**
* Verifies that after switching to a different XML Version (1.1), the parser
* is initialized properly (the listener was not registered in this case).
*
* @param path the path to XML source
* @throws Exception
*/
@Test(dataProvider = "getPaths")
public void testSwitchXMLVersions(String path) throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(
this.getClass().getResourceAsStream(path));
while (xmlStreamReader.hasNext()) {
int event = xmlStreamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
if (xmlStreamReader.getLocalName().equals("body")) {
String elementText = xmlStreamReader.getElementText();
Assert.assertTrue(!elementText.contains("</body>"),
"Fail: elementText contains </body>");
}
}
}
}
示例11: createSafeReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
public static XMLStreamReader createSafeReader(StreamSource source) throws XMLStreamException {
if (source == null) {
throw new IllegalArgumentException("The provided source cannot be null");
}
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
return xif.createXMLStreamReader(source);
}
示例12: testStAX
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test(dataProvider = "xml-data")
public void testStAX(String xml, int chunkSize, int expectedNumOfChunks, boolean withinLimit) throws Exception {
XMLInputFactory xifactory = XMLInputFactory.newInstance();
xifactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", true);
if (chunkSize > 0) {
xifactory.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
XMLStreamReader streamReader = xifactory.createXMLStreamReader(new StringReader(xml));
StringBuilder cdata = new StringBuilder();
int numOfChunks = 0;
boolean isWithinLimit = true;
while (streamReader.hasNext()) {
int eventType = streamReader.next();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
debugPrint("\nElement: " + streamReader.getLocalName());
break;
case XMLStreamConstants.CDATA:
String text = streamReader.getText();
numOfChunks++;
if (text.length() > chunkSize) {
isWithinLimit = false;
}
debugPrint("\nCDATA: " + text.length());
cdata.append(text);
break;
case XMLStreamConstants.CHARACTERS:
debugPrint("\nCharacters: " + streamReader.getText().length());
break;
}
}
debugPrint("CData in single chunk:" + cdata.toString().length());
Assert.assertEquals(numOfChunks, expectedNumOfChunks);
Assert.assertEquals(isWithinLimit, withinLimit);
}
示例13: setReportCData
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
protected static boolean setReportCData(XMLInputFactory f, boolean state) throws XMLStreamException {
Boolean b = state ? Boolean.TRUE : Boolean.FALSE;
if (f.isPropertySupported(PROP_REPORT_CDATA)) {
f.setProperty(PROP_REPORT_CDATA, b);
return true;
}
return false;
}
示例14: setSupportExternalEntities
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
protected static boolean setSupportExternalEntities(XMLInputFactory f, boolean state) throws XMLStreamException {
Boolean b = state ? Boolean.TRUE : Boolean.FALSE;
try {
f.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, b);
Object act = f.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES);
return (act instanceof Boolean) && ((Boolean) act).booleanValue() == state;
} catch (IllegalArgumentException e) {
/*
* Let's assume, then, that the property (or specific value for it)
* is NOT supported...
*/
return false;
}
}
示例15: test
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public void test() {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
for (int i = 0; i < 3; i++) {
runReader(factory, i);
}
} catch (XMLStreamException xe) {
xe.printStackTrace();
Assert.fail(xe.getMessage());
}
}