本文整理汇总了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);
}
}
示例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-&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 ) );
}
示例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() );
}
示例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-&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 ) );
}
示例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();
}
示例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);
}
示例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;
}
}
}
示例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-&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() );
}
示例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-&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() );
}
示例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);
}
}
示例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);
}
示例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));
}
示例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)));
}
示例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;
}
示例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);
}