当前位置: 首页>>代码示例>>Java>>正文


Java XMLInputFactory.setProperty方法代码示例

本文整理汇总了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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:DefaultAttributeTest.java

示例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;
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:ValidatorTest.java

示例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;
}
 
开发者ID:Yarichi,项目名称:Proyecto-DASI,代码行数:28,代码来源:SchemaHelper.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:NamespaceTest.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:NamespaceTest.java

示例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);
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:18,代码来源:FeatureConfigSnapshotHolder.java

示例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
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:StreamReaderTest.java

示例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);
    }
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:18,代码来源:Config.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:StAXSourceTest.java

示例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>");
            }
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:StreamReaderTest.java

示例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);
}
 
开发者ID:apache,项目名称:nifi-registry,代码行数:11,代码来源:XmlUtils.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:CDataChunkSizeTest.java

示例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;
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:BaseStAXUT.java

示例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;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:BaseStAXUT.java

示例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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:Bug8153781.java


注:本文中的javax.xml.stream.XMLInputFactory.setProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。