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


Java XMLInputFactory2类代码示例

本文整理汇总了Java中org.codehaus.stax2.XMLInputFactory2的典型用法代码示例。如果您正苦于以下问题:Java XMLInputFactory2类的具体用法?Java XMLInputFactory2怎么用?Java XMLInputFactory2使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


XMLInputFactory2类属于org.codehaus.stax2包,在下文中一共展示了XMLInputFactory2类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setUpXMLParser

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
private void setUpXMLParser(ReadableByteChannel channel, byte[] lookAhead) throws IOException {
  try {
    // We use Woodstox because the StAX implementation provided by OpenJDK reports
    // character locations incorrectly. Note that Woodstox still currently reports *byte*
    // locations incorrectly when parsing documents that contain multi-byte characters.
    XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory.newInstance();
    this.parser = xmlInputFactory.createXMLStreamReader(
        new SequenceInputStream(
            new ByteArrayInputStream(lookAhead), Channels.newInputStream(channel)),
        getCurrentSource().configuration.getCharset());

    // Current offset should be the offset before reading the record element.
    while (true) {
      int event = parser.next();
      if (event == XMLStreamConstants.START_ELEMENT) {
        String localName = parser.getLocalName();
        if (localName.equals(getCurrentSource().configuration.getRecordElement())) {
          break;
        }
      }
    }
  } catch (FactoryConfigurationError | XMLStreamException e) {
    throw new IOException(e);
  }
}
 
开发者ID:apache,项目名称:beam,代码行数:26,代码来源:XmlSource.java

示例2: testBasic

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
public void testBasic()
    throws Exception
{
    String input = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n"
        + "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n"
        + "    <artifactId>mojo-&amp;sandbox-parent</artifactId>\n" + "    <version>5-SNAPSHOT</version>\r"
        + "  </parent>\r" + "<build/></project>";

    byte[] rawInput = input.getBytes( "utf-8" );
    ByteArrayInputStream source = new ByteArrayInputStream( rawInput );
    ByteArrayOutputStream dest = new ByteArrayOutputStream();
    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader( source );
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter( dest, "utf-8" );
    while ( eventReader.hasNext() )
    {
        eventWriter.add( eventReader.nextEvent() );
    }

    String output = new String( dest.toByteArray(), "utf-8" );

    assertFalse( "StAX implementation is not good enough", input.equals( output ) );
}
 
开发者ID:mojohaus,项目名称:versions-maven-plugin,代码行数:26,代码来源:RewriteWithStAXTest.java

示例3: testImportedPOMsRetrievedFromDependencyManagement

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
/**
 * Tests if imported POMs are properly read from dependency management section. Such logic is required to resolve
 * <a href="https://github.com/mojohaus/versions-maven-plugin/issues/134">bug #134</a>
 *
 * @throws Exception if the test fails.
 */
public void testImportedPOMsRetrievedFromDependencyManagement()
    throws Exception
{
    URL url = getClass().getResource( "PomHelperTest.dependencyManagementBOMs.pom.xml" );
    StringBuilder input = PomHelper.readXmlFile( new File( url.getPath() ) );

    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );

    ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory );

    List<Dependency> dependencies = PomHelper.readImportedPOMsFromDependencyManagementSection( pom );

    assertNotNull( dependencies );
    assertEquals( 1, dependencies.size() );

    Dependency dependency = dependencies.get( 0 );
    assertEquals( "org.group1", dependency.getGroupId() );
    assertEquals( "artifact-pom", dependency.getArtifactId() );
    assertEquals( "1.0-SNAPSHOT", dependency.getVersion() );
    assertEquals( "import", dependency.getScope() );
    assertEquals( "pom", dependency.getType() );
}
 
开发者ID:mojohaus,项目名称:versions-maven-plugin,代码行数:30,代码来源:PomHelperTest.java

示例4: testBasic

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
public void testBasic()
    throws Exception
{
    String input = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n" +
        "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n" +
        "    <artifactId>mojo-&amp;sandbox-parent</artifactId>\n" + "    <version>5-SNAPSHOT</version>\r" +
        "  </parent>\r" + "<build/></project>";

    byte[] rawInput = input.getBytes( "utf-8" );
    ByteArrayInputStream source = new ByteArrayInputStream( rawInput );
    ByteArrayOutputStream dest = new ByteArrayOutputStream();
    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader( source );
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter( dest, "utf-8" );
    while ( eventReader.hasNext() )
    {
        eventWriter.add( eventReader.nextEvent() );
    }

    String output = new String( dest.toByteArray(), "utf-8" );

    assertFalse( "StAX implementation is not good enough", input.equals( output ) );
}
 
开发者ID:petr-ujezdsky,项目名称:versions-maven-plugin-svn-clone,代码行数:26,代码来源:RewriteWithStAXTest.java

示例5: testCreateUsingSystemId

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
/**
 * This test is related to problem reported as [WSTX-182], inability
 * to use SystemId alone as source.
 */
public void testCreateUsingSystemId()
    throws IOException, XMLStreamException
{
    File tmpF = File.createTempFile("staxtest", ".xml");
    tmpF.deleteOnExit();

    XMLOutputFactory f = getOutputFactory();
    StreamResult dst = new StreamResult();
    dst.setSystemId(tmpF);
    XMLStreamWriter sw = f.createXMLStreamWriter(dst);

    sw.writeStartDocument();
    sw.writeEmptyElement("root");
    sw.writeEndDocument();
    sw.close();

    // plus let's read and check it
    XMLInputFactory2 inf = getInputFactory();
    XMLStreamReader sr = inf.createXMLStreamReader(tmpF);
    assertTokenType(START_ELEMENT, sr.next());
    assertTokenType(END_ELEMENT, sr.next());
    sr.close();
}
 
开发者ID:FasterXML,项目名称:woodstox,代码行数:28,代码来源:TestStreamResult.java

示例6: getReader

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
private XMLStreamReader2 getReader(String contents,
                                  boolean xmlidEnabled,
                                  boolean nsAware, boolean coal)
    throws XMLStreamException
{
    XMLInputFactory2 f = getInputFactory();
    setSupportDTD(f, true);
    setValidating(f, false);
    setCoalescing(f, coal);
    setNamespaceAware(f, nsAware);
    f.setProperty(XMLInputFactory2.XSP_SUPPORT_XMLID,
                  xmlidEnabled
                  ? XMLInputFactory2.XSP_V_XMLID_TYPING
                  : XMLInputFactory2.XSP_V_XMLID_NONE);
    return constructStreamReader(f, contents);
}
 
开发者ID:FasterXML,项目名称:woodstox,代码行数:17,代码来源:TestXmlId.java

示例7: testWsdlValidation

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
public void testWsdlValidation() throws Exception {
	String runMe = System.getProperty("testWsdlValidation");
	if (runMe == null || "".equals(runMe)) {
		return;
	}
	XMLInputFactory2 factory = getInputFactory();
	XMLStreamReader2 reader = (XMLStreamReader2) factory.createXMLStreamReader(getClass().getResourceAsStream("test-message.xml"), "utf-8");
	QName msgQName = new QName("http://server.hw.demo/", "sayHi");
	while (true) {
		int what = reader.nextTag();
		if (what == XMLStreamConstants.START_ELEMENT) {
			if (reader.getName().equals(msgQName)) {
				reader.validateAgainst(schema);
			}
		} else if (what == XMLStreamConstants.END_ELEMENT) {
			if (reader.getName().equals(msgQName)) {
				reader.stopValidatingAgainst(schema);
			}
		} else if (what == XMLStreamConstants.END_DOCUMENT) {
			break;
		}
	}
}
 
开发者ID:FasterXML,项目名称:woodstox,代码行数:24,代码来源:TestWsdlValidation.java

示例8: testReplace

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
public void testReplace()
    throws Exception
{
    String input = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n"
        + "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n"
        + "    <artifactId>mojo-&amp;sandbox-parent</artifactId>\n" + "    <version>5-SNAPSHOT</version>\r"
        + "  </parent>\r" + "<build/></project>";
    String expected = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n"
        + "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n" + "    <artifactId>my-artifact</artifactId>\n"
        + "    <version>5-SNAPSHOT</version>\r" + "  </parent>\r" + "<build/></project>";

    StringBuilder output = new StringBuilder( input );

    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
    ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory );
    while ( eventReader.hasNext() )
    {
        XMLEvent event = eventReader.nextEvent();
        if ( event instanceof StartElement
            && event.asStartElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 0 );
        }
        if ( event instanceof EndElement && event.asEndElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 1 );
            if ( eventReader.hasMark( 0 ) )
            {
                eventReader.replaceBetween( 0, 1, "my-artifact" );
            }
        }
    }

    assertEquals( expected, output.toString() );
}
 
开发者ID:mojohaus,项目名称:versions-maven-plugin,代码行数:37,代码来源:RewriteWithStAXTest.java

示例9: testReplace

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
public void testReplace()
    throws Exception
{
    String input = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n" +
        "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n" +
        "    <artifactId>mojo-&amp;sandbox-parent</artifactId>\n" + "    <version>5-SNAPSHOT</version>\r" +
        "  </parent>\r" + "<build/></project>";
    String expected = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n" +
        "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n" + "    <artifactId>my-artifact</artifactId>\n" +
        "    <version>5-SNAPSHOT</version>\r" + "  </parent>\r" + "<build/></project>";

    StringBuilder output = new StringBuilder( input );

    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
    ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory );
    while ( eventReader.hasNext() )
    {
        XMLEvent event = eventReader.nextEvent();
        if ( event instanceof StartElement &&
            event.asStartElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 0 );
        }
        if ( event instanceof EndElement && event.asEndElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 1 );
            if ( eventReader.hasMark( 0 ) )
            {
                eventReader.replaceBetween( 0, 1, "my-artifact" );
            }
        }
    }

    assertEquals( expected, output.toString() );
}
 
开发者ID:petr-ujezdsky,项目名称:versions-maven-plugin-svn-clone,代码行数:37,代码来源:RewriteWithStAXTest.java

示例10: XmlDumpParser

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
/**
 * Constructor used by Multistream parser
 * @param header   parsed header
 * @param xmlInput parallel input stream
 */
public XmlDumpParser(Header header, InputStream xmlInput) {
    try {
        this.header = header;

        XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory2.newInstance();
        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        factory.setProperty(WstxInputProperties.P_INPUT_PARSING_MODE, WstxInputProperties.PARSING_MODE_FRAGMENT);

        xmlReader = (XMLStreamReader2)factory.createXMLStreamReader(xmlInput);

    } catch (XMLStreamException e) {
        throw new IOError(e);
    }
}
 
开发者ID:marcusklang,项目名称:wikiforia,代码行数:20,代码来源:XmlDumpParser.java

示例11: parseHeader

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
/**
 * Header parsing code
 * @param xml the header xml
 * @return true if match
 * @throws javax.xml.stream.XMLStreamException
 */
public static Header parseHeader(String xml) throws XMLStreamException
{
    XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory2.newInstance();
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    BufferedReader buffreader = new BufferedReader(new StringReader(xml));

    XMLStreamReader2 xmlReader = (XMLStreamReader2)factory.createXMLStreamReader(buffreader);
    return XmlDumpParser.readHeader(xmlReader);
}
 
开发者ID:marcusklang,项目名称:wikiforia,代码行数:16,代码来源:MultistreamBzip2XmlDumpParser.java

示例12: run

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
@Override
public void run(InputStream stream) throws Exception {
    XMLInputFactory factory = XMLInputFactory2.newInstance();
    factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);

    read(factory.createXMLStreamReader(stream));
}
 
开发者ID:atlanmod,项目名称:NeoEMF,代码行数:8,代码来源:XmiStAXCursorReader.java

示例13: run

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
@Override
public void run(InputStream stream) throws Exception {
    XMLInputFactory factory = XMLInputFactory2.newInstance();
    factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);

    read(factory.createXMLEventReader(factory.createXMLStreamReader(stream)));
}
 
开发者ID:atlanmod,项目名称:NeoEMF,代码行数:8,代码来源:XmiStAXIteratorReader.java

示例14: getSchemaLocation

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
/**
 * Get the xsi:schemaLocation for the OAI response
 * 
 * @return the xsi:schemaLocation value
 */
public String getSchemaLocation() throws TransformerException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
    if (this.schemaLocation == null) {
        if (hasDocument()) {
            this.schemaLocation = getSingleString("/*/@xsi:schemaLocation");
            logger.debug("found schemaLocation["+schemaLocation+"] in the XML tree");
        } else {
            XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2.newInstance();
            xmlif.configureForConvenience();
            XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(getStream());
            int state = 1; // 1:START 0:STOP -1:ERROR
            while (state > 0) {
                int eventType = xmlr.getEventType();
                switch (eventType) {
                    case XMLEvent2.START_ELEMENT:
                        schemaLocation = xmlr.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance","schemaLocation");
                        if (schemaLocation != null)
                            state = 0;
                        break;
                }
                if (xmlr.hasNext())
                    xmlr.next();
                else
                    state = state == 1? 0: -1;// if START then STOP else ERROR
            }
            xmlr.close();
            logger.debug("found schemaLocation["+schemaLocation+"] in the XML stream");
        }

        // The URIs in xsi:schemaLocation are separated by (any kind
        // of) white space. Normalize it to a single space.
        this.schemaLocation = schemaLocation.trim().replaceAll("\\s+", " ");
    }
    return schemaLocation;
}
 
开发者ID:clarin-eric,项目名称:oai-harvest-manager,代码行数:40,代码来源:HarvesterVerb.java

示例15: getReader

import org.codehaus.stax2.XMLInputFactory2; //导入依赖的package包/类
private XMLStreamReader2 getReader(String contents, boolean nsAware)
    throws XMLStreamException
{
    XMLInputFactory2 f = getInputFactory();
    setCoalescing(f, true);
    setSupportDTD(f, true);
    setNamespaceAware(f, nsAware);
    setValidating(f, false);
    return constructStreamReader(f, contents);
}
 
开发者ID:FasterXML,项目名称:woodstox,代码行数:11,代码来源:TestReaderWithDTD.java


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