本文整理汇总了Java中org.codehaus.stax2.XMLInputFactory2.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java XMLInputFactory2.newInstance方法的具体用法?Java XMLInputFactory2.newInstance怎么用?Java XMLInputFactory2.newInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.stax2.XMLInputFactory2
的用法示例。
在下文中一共展示了XMLInputFactory2.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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-&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 ) );
}
示例2: 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() );
}
示例3: 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-&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 ) );
}
示例4: 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-&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() );
}
示例5: 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-&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() );
}
示例6: 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);
}
}
示例7: 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);
}
示例8: 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));
}
示例9: 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)));
}
示例10: 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;
}
示例11: AbstractXMLTokenizer
import org.codehaus.stax2.XMLInputFactory2; //导入方法依赖的package包/类
/**
* @param inputReader a reader on the XML source to tokenized
*/
AbstractXMLTokenizer(Reader inputReader) {
XMLInputFactory2 inputFactory = (XMLInputFactory2) XMLInputFactory2.newInstance();
inputFactory.configureForConvenience();
inputFactory.setProperty(XMLInputFactory2.SUPPORT_DTD, false);
inputFactory.setProperty(XMLInputFactory2.IS_COALESCING, false);
try {
reader = (XMLStreamReader2) inputFactory.createXMLStreamReader(inputReader);
} catch (XMLStreamException e) {
throw new AgilentParseException("Cannot create StAX reader: " + e.getMessage(), e);
}
currentToken = getNextToken();
}
示例12: parse
import org.codehaus.stax2.XMLInputFactory2; //导入方法依赖的package包/类
public Feed parse(String datasource, String feedtype, InputStream is)
throws ParseFeedException, XMLStreamException, IOException {
Reader reader = new InputStreamReader(is);
XMLInputFactory2 xmlFactory = (XMLInputFactory2) XMLInputFactory2
.newInstance();
xmlFactory.configureForLowMemUsage();
xmlFactory.setProperty("javax.xml.stream.supportDTD", false);
XMLStreamReader2 xmlReader = (XMLStreamReader2) xmlFactory
.createXMLStreamReader(reader);
return parseGsafeedElement(datasource, feedtype, xmlReader);
}
示例13: createInputFactory
import org.codehaus.stax2.XMLInputFactory2; //导入方法依赖的package包/类
private static XMLInputFactory createInputFactory()
{
XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, true );
return inputFactory;
}
示例14: testLongProperties
import org.codehaus.stax2.XMLInputFactory2; //导入方法依赖的package包/类
/**
* 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.
*/
public void testLongProperties()
throws Exception
{
URL url = getClass().getResource( "PomHelperTest.testLongProperties.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 );
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 );
}
示例15: getResumptionToken
import org.codehaus.stax2.XMLInputFactory2; //导入方法依赖的package包/类
/**
* Get the oai:resumptionToken from the response
*
* @return the oai:resumptionToken value
* @throws TransformerException
* @throws NoSuchFieldException
*/
public String getResumptionToken()
throws TransformerException, NoSuchFieldException, ParserConfigurationException, SAXException, IOException, XMLStreamException {
String schemaLocation = getSchemaLocation();
if (schemaLocation.indexOf(SCHEMA_LOCATION_V2_0) != -1) {
if (hasDocument())
return getSingleString("/oai20:OAI-PMH/oai20:ListRecords/oai20:resumptionToken");
String token = null;
XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2.newInstance();
xmlif.configureForConvenience();
XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(getStream());
int state = 1; // 1:START 2:FOUND 0:STOP -1:ERROR
while (state > 0) {
int eventType = xmlr.getEventType();
switch (state) {
case 1://START
switch (eventType) {
case XMLEvent2.START_ELEMENT:
QName qn = xmlr.getName();
//logger.debug("finding token in the XML stream: node["+qn.getNamespaceURI()+"]["+qn.getLocalPart()+"]");
if (qn.getNamespaceURI().equals("http://www.openarchives.org/OAI/2.0/") && qn.getLocalPart().equals("resumptionToken"))
state = 2;//FOUND
break;
}
break;
case 2://FOUND
switch (eventType) {
case XMLEvent2.CHARACTERS:
token = xmlr.getText();
state = 0;//STOP
break;
default:
state = -1;//ERROR
break;
}
break;
}
if (xmlr.hasNext())
xmlr.next();
else
state = state == 1? 0: -1;// if START then STOP else ERROR
}
if (state < 0 || token == null) {
logger.warn("couldn't find token in the XML stream!");
return null;
}
logger.debug("found token["+token+"] in the XML stream!");
return token;
} else if (schemaLocation.indexOf(SCHEMA_LOCATION_V1_1_LIST_RECORDS) != -1) {
return getSingleString("/oai11_ListRecords:ListRecords/oai11_ListRecords:resumptionToken");
} else {
throw new NoSuchFieldException(schemaLocation);
}
}