Java Code Examples for org.codehaus.stax2.XMLInputFactory2#newInstance()

The following examples show how to use org.codehaus.stax2.XMLInputFactory2#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RewriteWithStAXTest.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
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 ) );
}
 
Example 2
Source File: PomHelperTest.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 */
@Test
public void testImportedPOMsRetrievedFromDependencyManagement()
    throws Exception
{
    URL url = getClass().getResource( "PomHelperTest.dependencyManagementBOMs.pom.xml" );
    File file = new File( url.getPath() );
    StringBuilder input = PomHelper.readXmlFile( file );

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

    ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory, file.getAbsolutePath() );

    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() );
}
 
Example 3
Source File: PomHelperTest.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Tests what happens when changing a long property substitution pattern, e.g.
 * <a href="http://jira.codehaus.org/browse/MVERSIONS-44">MVERSIONS-44</a>
 *
 * @throws Exception if the test fails.
 */
@Test
public void testLongProperties()
    throws Exception
{
    URL url = getClass().getResource( "PomHelperTest.testLongProperties.pom.xml" );
    File file = new File( url.getPath() );
    StringBuilder input = PomHelper.readXmlFile( file );

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

    ModifiedPomXMLEventReader pom = new ModifiedPomXMLEventReader( input, inputFactory, file.getAbsolutePath() );

    String oldVersion = PomHelper.getProjectVersion( pom );

    String newVersion = "1";

    assertTrue( "The pom has been modified", PomHelper.setProjectVersion( pom, newVersion ) );

    assertEquals( newVersion, PomHelper.getProjectVersion( pom ) );

    assertNotSame( oldVersion, newVersion );
}
 
Example 4
Source File: RewriteWithStAXTest.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
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, null );
    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() );
}
 
Example 5
Source File: XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source File: XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Standalone constructor
 * @param xmlInput the stream to read from
 */
public XmlDumpParser(InputStream xmlInput) {
    try {
        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);
        this.header = readHeader(xmlReader);
    } catch (XMLStreamException e) {
        throw new IOError(e);
    }
}
 
Example 7
Source File: MultistreamBzip2XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: XMLEventReaderFactory.java    From tidy-maven-plugin with Apache License 2.0 4 votes vote down vote up
private static XMLInputFactory createInputFactory()
{
    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, true );
    return inputFactory;
}