com.ctc.wstx.stax.WstxInputFactory Java Examples

The following examples show how to use com.ctc.wstx.stax.WstxInputFactory. 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: INRIALeMondeCorpusStaxHandler.java    From grobid-ner with Apache License 2.0 6 votes vote down vote up
/**
 * How to use it
 * <p>
 * This class require a single parameter which is the input file containng the french
 * corpus from Le Monde manually annotated.
 * <p>
 * The class will output the cONLL 2013 format in a file having the same name as the input
 * suffixed with .output.
 */
public static void main(String[] args) throws IOException, XMLStreamException {

    if (args.length == 0) {
        System.out.println("Missing input file. First parameter.");
        System.exit(-1);
    }

    WstxInputFactory inputFactory = new WstxInputFactory();

    Writer writer = new FileWriter(args[0] + ".output");
    INRIALeMondeCorpusStaxHandler inriaLeMondeCorpusStaxHandler = new INRIALeMondeCorpusStaxHandler(writer);

    InputStream is = new FileInputStream(args[0]);
    XMLStreamReader2 reader = (XMLStreamReader2) inputFactory.createXMLStreamReader(is);

    StaxUtils.traverse(reader, inriaLeMondeCorpusStaxHandler);

    writer.close();
}
 
Example #2
Source File: TestInputFactory.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testConfig()
    throws XMLStreamException
{
    XMLInputFactory2 f = getNewInputFactory();

    ReaderConfig cfg = ((WstxInputFactory) f).getConfig();
    assertNotNull(cfg);

    assertNull(f.getEventAllocator());
    assertNull(f.getXMLResolver());

    assertNull(f.getXMLReporter());
    MyReporter rep = new MyReporter();
    f.setXMLReporter(rep);
    assertEquals(rep, f.getXMLReporter());

    assertFalse(f.isPropertySupported("foobar"));
}
 
Example #3
Source File: TestLocation.java    From woodstox with Apache License 2.0 5 votes vote down vote up
public void doTestOffset(boolean coal, boolean readAll)
    throws XMLStreamException
{
    // First, let's create some input...
    StringBuffer inputBuf = new StringBuffer();
    StringBuffer expOut = new StringBuffer();
    generateData(new Random(123), inputBuf, expOut, true); 
    String inputStr = inputBuf.toString();

    WstxInputFactory f = getWstxInputFactory();
    // Should shrink it to get faster convergence
    f.getConfig().setInputBufferLength(17);
    f.getConfig().doCoalesceText(coal);
    XMLStreamReader2 sr = (XMLStreamReader2) f.createXMLStreamReader(new StringReader(inputStr));

    int lastLine = 0;
    int lastOffset = 0;

    while (sr.next() != XMLStreamConstants.END_DOCUMENT) {
        Location loc = sr.getLocation();
        int line = loc.getLineNumber();
        int offset = loc.getCharacterOffset();

        if (line < lastLine) {
            fail("Location.getLineNumber() should increase steadily, old value: "+lastLine+", new: "+line);
        }
        if (offset < lastOffset) {
            fail("Location.getCharacterOffset() should increase steadily, old value: "+lastOffset+", new: "+offset);
        }
        lastLine = line;
        lastOffset = offset;

        if (readAll) { // read it, or just skip?
            if (sr.hasText()) {
                /*String text =*/ sr.getText();
            }
        }
    }
}
 
Example #4
Source File: TestStreaming.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLStreamReader2 getReader(String contents, boolean coalesce)
    throws XMLStreamException
{
    WstxInputFactory f = getWstxInputFactory();
    f.getConfig().doSupportNamespaces(true);
    f.getConfig().doCoalesceText(coalesce);
    f.getConfig().setInputBufferLength(16);
    f.getConfig().setShortestReportedTextSegment(4);
    return constructStreamReader(f, contents);
}
 
Example #5
Source File: TestRandomStream.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Main branching point has settings for standard features; it
 * will further need to loop over Woodstox-specific settings.
 */
private void doTest(boolean ns, boolean coalescing, boolean autoEntity)
    throws Exception
{
    /* Let's generate seed from args so it's reproducible; String hash
     * code only depend on text it contains, so it'll be fixed for
     * specific String.
     */
    String baseArgStr = "ns: "+ns+", coalesce: "+coalescing+", entityExp: "+autoEntity;
    long seed = baseArgStr.hashCode();

    WstxInputFactory f = (WstxInputFactory) getInputFactory();
    ReaderConfig cfg = f.getConfig();

    // Settings we always need:
    cfg.doSupportDTDs(true);
    cfg.doValidateWithDTD(false);

    // Then variable ones we got settings for:
    cfg.doSupportNamespaces(ns);
    cfg.doCoalesceText(coalescing);
    cfg.doReplaceEntityRefs(autoEntity);

    /* How many random permutations do we want to try?
     */
    final int ROUNDS = 5;

    for (int round = 0; round < ROUNDS; ++round) {
        Random r = new Random(seed+round);
        StringBuffer inputBuf = new StringBuffer(1000);
        StringBuffer expOutputBuf = new StringBuffer(1000);

        generateData(r, inputBuf, expOutputBuf, autoEntity);

        mInput = inputBuf.toString();
        normalizeLFs(expOutputBuf);
        mExpOutputNorm = expOutputBuf.toString();
        mConfigs.iterate(f, this);
    }
}
 
Example #6
Source File: TestAttr.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLStreamReader getValidatingReader(String contents, boolean nsAware)
    throws XMLStreamException
{
    WstxInputFactory f = getWstxInputFactory();
    f.getConfig().doSupportNamespaces(nsAware);
    f.getConfig().doSupportDTDs(true);
    f.getConfig().doValidateWithDTD(true);
    return constructStreamReader(f, contents);
}
 
Example #7
Source File: TestAttr.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLStreamReader getReader(String contents, boolean nsAware)
    throws XMLStreamException
{
    WstxInputFactory f = getWstxInputFactory();
    f.getConfig().doValidateWithDTD(false);
    f.getConfig().doSupportNamespaces(nsAware);
    return constructStreamReader(f, contents);
}
 
Example #8
Source File: WordPressXMLReader.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public void getNext(CAS aCAS) throws IOException, CollectionException {
  JCas jcas;
  try {
    jcas = aCAS.getJCas();
  } catch (CASException var6) {
    throw new CollectionException(var6);
  }

  try {
    if (this.xmlReader == null) {
      WstxInputFactory e = new WstxInputFactory();
      this.xmlReader = e.createXMLStreamReader((File) this.xmlFiles.get(this.currentParsedFile));
      this.iDoc = 0;
    }

    this.parseSubDocument(jcas);
    System.out.println(jcas.getDocumentText());
    ++this.iDoc;
    if (this.xmlReader.getDepth() < 2) {
      this.xmlReader.closeCompletely();
      this.xmlReader = null;
      ++this.currentParsedFile;
    }

  } catch (XMLStreamException var4) {
    var4.printStackTrace();
    throw new CollectionException(var4);
  } catch (Exception var5) {
    var5.printStackTrace();
    throw new CollectionException(var5);
  }
}
 
Example #9
Source File: TestPrologWS.java    From woodstox with Apache License 2.0 5 votes vote down vote up
private XMLStreamReader getReader(String contents, boolean prologWS,
                                  boolean lazyParsing)
    throws XMLStreamException
{
    WstxInputFactory f = (WstxInputFactory) getInputFactory();
    ReaderConfig cfg = f.getConfig();
    cfg.doReportPrologWhitespace(prologWS);
    cfg.doParseLazily(lazyParsing);
    return constructStreamReader(f, contents);
}
 
Example #10
Source File: WstxSAXParserFactory.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * @since 4.0.8
 */
public WstxSAXParserFactory(WstxInputFactory f)
{
    mStaxFactory = f;
    /* defaults should be fine... except that for some weird
     * reason, by default namespace support is defined to be off
     */
    setNamespaceAware(true);
}
 
Example #11
Source File: WstxSAXParserFactory.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
     * @since 5.3
     */
//    protected boolean mSecureProcessing = false;
    
    public WstxSAXParserFactory()
    {
        this(new WstxInputFactory());
    }
 
Example #12
Source File: WstxSAXParser.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 *<p>
 * NOTE: this was a protected constructor for versions 4.0
 * and 3.2; changed to public in 4.1
 */
public WstxSAXParser(WstxInputFactory sf, boolean nsPrefixes)
{
    mStaxFactory = sf;
    mFeatNsPrefixes = nsPrefixes;
    mConfig = sf.createPrivateConfig();
    mConfig.doSupportDTDs(true);
    /* Lazy parsing is a tricky thing: although most of the time
     * it's useless with SAX, it is actually necessary to be able
     * to properly model internal DTD subsets, for example. So,
     * we can not really easily determine defaults.
     */
    ResolverProxy r = new ResolverProxy();
    /* SAX doesn't distinguish between DTD (ext. subset, PEs) and
     * entity (external general entities) resolvers, so let's
     * assign them both:
     */
    mConfig.setDtdResolver(r);
    mConfig.setEntityResolver(r);
    mConfig.setDTDEventListener(this);

    /* These settings do NOT make sense as generic defaults, but
     * are helpful when using some test frameworks. Specifically,
     * - DTD caching may remove calls to resolvers, changing
     *   observed behavior
     * - Using min. segment length of 1 will force flushing of
     *   all content before entity expansion, which will
     *   completely serialize entity resolution calls wrt.
     *   CHARACTERS events.
     */
    // !!! ONLY for testing; never remove for prod use
    //mConfig.setShortestReportedTextSegment(1);
    //mConfig.doCacheDTDs(false);
}
 
Example #13
Source File: BasicSerializableRepository.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private XmlMapper createXMLMapper() {
    final XMLInputFactory ifactory = new WstxInputFactory();
    ifactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);
    ifactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

    final XMLOutputFactory ofactory = new WstxOutputFactory();
    ofactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);
    ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final XmlFactory xf = new XmlFactory(ifactory, ofactory);

    final XmlMapper mapper = new XmlMapper(xf);
    mapper.registerModules(new JavaTimeModule());
    return mapper;
}
 
Example #14
Source File: JacksonXML.java    From dropwizard-xml with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link com.fasterxml.jackson.dataformat.xml.XmlMapper} using Woodstox
 * with Logback and Joda Time support.
 * Also includes all {@link io.dropwizard.jackson.Discoverable} interface implementations.
 *
 * @return XmlMapper
 */
public static XmlMapper newXMLMapper(JacksonXmlModule jacksonXmlModule) {

    final XmlFactory woodstoxFactory = new XmlFactory(new WstxInputFactory(), new WstxOutputFactory());
    final XmlMapper mapper = new XmlMapper(woodstoxFactory, jacksonXmlModule);

    mapper.registerModule(new GuavaModule());
    mapper.registerModule(new GuavaExtrasModule());
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new FuzzyEnumModule());
    mapper.setPropertyNamingStrategy(new AnnotationSensitivePropertyNamingStrategy());
    mapper.setSubtypeResolver(new DiscoverableSubtypeResolver());

    return mapper;
}
 
Example #15
Source File: StaxParser.java    From sonar-clover with Apache License 2.0 5 votes vote down vote up
StaxParser(XmlStreamHandler streamHandler) {
    this.streamHandler = streamHandler;
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    if (xmlFactory instanceof WstxInputFactory) {
        WstxInputFactory wstxInputfactory = (WstxInputFactory) xmlFactory;
        wstxInputfactory.configureForLowMemUsage();
        wstxInputfactory.getConfig().setUndeclaredEntityResolver(new UndeclaredEntitiesXMLResolver());
    }
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    inf = new SMInputFactory(xmlFactory);
}
 
Example #16
Source File: BasicSerializableRepository.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
private XmlMapper createXMLMapper() {
    final XMLInputFactory ifactory = new WstxInputFactory();
    ifactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);
    ifactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);

    final XMLOutputFactory ofactory = new WstxOutputFactory();
    ofactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);
    ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final XmlFactory xf = new XmlFactory(ifactory, ofactory);

    final XmlMapper mapper = new XmlMapper(xf);
    mapper.registerModules(new JavaTimeModule());
    return mapper;
}
 
Example #17
Source File: POM.java    From pomutils with Apache License 2.0 4 votes vote down vote up
private static XMLInputFactory initializeXmlInputFactory()
        throws FactoryConfigurationError {
	XMLInputFactory inputFactory = new WstxInputFactory();
	inputFactory.setProperty(XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE);
	return inputFactory;
}
 
Example #18
Source File: PomMergeDriverTest.java    From pomutils with Apache License 2.0 4 votes vote down vote up
public void testAutoMergeFailed() throws Exception {
	String myTestSubFolder = "merge/autoMergeFailed";

	TestUtils.prepareTestFolder(myTestSubFolder);

	String basePomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/base.pom.xml";
	String ourPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/our.pom.xml";
	String theirPomFile = TestUtils.resourceBaseTestFolder + "/" + myTestSubFolder + "/their.pom.xml";

	Ruleset ruleset = new Ruleset(SelectionStrategy.OUR);

	PomMergeDriver pomMergeDriver = new PomMergeDriver(ruleset, basePomFile, ourPomFile, theirPomFile);
	int mergeReturnValue = pomMergeDriver.merge();

	assertTrue("merge conflict", mergeReturnValue == 1);

	POM theirPom = new POM(theirPomFile);

	StringBuilder ourPomString = new StringBuilder(FileUtils.readFileToString(new File(ourPomFile)));
	String ourProjectVersion = PomHelper.getProjectVersion(new ModifiedPomXMLEventReader(ourPomString, new WstxInputFactory()));

	assertEquals("same version now", ourProjectVersion, theirPom.getProjectVersion());
}
 
Example #19
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected static XMLInputFactory2 getNewInputFactory() {
    return new WstxInputFactory();
}
 
Example #20
Source File: BaseWstxTest.java    From woodstox with Apache License 2.0 4 votes vote down vote up
protected WstxInputFactory getWstxInputFactory() {
    return (WstxInputFactory) getInputFactory();
}
 
Example #21
Source File: WoodstoxHelper.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static XMLInputFactory createInputFactory() {
    return new WstxInputFactory();
}
 
Example #22
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory ) f).getConfig().setShortestReportedTextSegment(mSizes[index]);
}
 
Example #23
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory) f).getConfig().setInputBufferLength(mSizes[index]);
}
 
Example #24
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory) f).getConfig().doParseLazily(booleanFromInt(index));
}
 
Example #25
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory) f).getConfig().doReplaceEntityRefs(booleanFromInt(index));
}
 
Example #26
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory) f).getConfig().doCoalesceText(booleanFromInt(index));
}
 
Example #27
Source File: Configs.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public void config(XMLInputFactory f, int index) {
    ((WstxInputFactory) f).getConfig().doSupportNamespaces(booleanFromInt(index));
}
 
Example #28
Source File: WstxSAXParser.java    From woodstox with Apache License 2.0 4 votes vote down vote up
public WstxSAXParser()
{
    this(new WstxInputFactory(), FEAT_DEFAULT_NS_PREFIXES);
}
 
Example #29
Source File: InputFactoryProviderImpl.java    From woodstox with Apache License 2.0 4 votes vote down vote up
@Override
public XMLInputFactory2 createInputFactory() {
    return new WstxInputFactory();
}
 
Example #30
Source File: XMLAnalyzer.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public XMLAnalyzer() {
    factory = WstxInputFactory.newInstance();
}