Example usage for javax.xml.stream XMLInputFactory newInstance

List of usage examples for javax.xml.stream XMLInputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory newInstance.

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:org.sonar.api.server.rule.RulesDefinitionXmlLoader.java

/**
 * Loads rules by reading the XML input stream. The reader is not closed by the method, so it
 * should be handled by the caller.//w w w  . j a va  2s  .co m
 * @since 4.3
 */
public void load(RulesDefinition.NewRepository repo, Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    try {
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <rules>

        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            processRule(repo, rulesC);
        }

    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}

From source file:org.sonar.cxx.sensors.utils.StaxParser.java

/**
 * StaxParser for a given stream handler and ISO control chars set awareness to on. The ISO control chars in the XML
 * file will be replaced by simple spaces, useful for potentially bogus XML files to parse, this has a small perfs
 * overhead so use it only when necessary
 *
 * @param streamHandler the XML stream handler
 * @param isoControlCharsAwareParser true or false
 *//*from   w ww.j  a v  a  2 s.  c  o m*/
public StaxParser(XmlStreamHandler streamHandler, boolean isoControlCharsAwareParser) {
    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, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    this.isoControlCharsAwareParser = isoControlCharsAwareParser;
    inf = new SMInputFactory(xmlFactory);
}

From source file:org.sonar.plugins.android.lint.AndroidLintProfileExporter.java

private void loadRuleKeys() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    InputStream inputStream = getClass().getResourceAsStream(AndroidLintRulesDefinition.RULES_XML_PATH);
    InputStreamReader reader = new InputStreamReader(inputStream, Charsets.UTF_8);
    try {//ww w.ja  v a 2s .c  o m
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <rules>

        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            SMInputCursor cursor = rulesC.childElementCursor();
            while (cursor.getNext() != null) {
                if (StringUtils.equalsIgnoreCase("key", cursor.getLocalName())) {
                    String key = StringUtils.trim(cursor.collectDescendantText(false));
                    ruleKeys.add(key);
                }
            }
        }

    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}

From source file:org.sonar.plugins.csharp.gallio.results.execution.GallioResultParser.java

public Set<UnitTestReport> parse(File report) {
    try {/* w w  w .jav  a2  s  . co m*/
        testCaseDetailsByTestIds = new HashMap<String, TestCaseDetail>();
        SMInputFactory inf = new SMInputFactory(XMLInputFactory.newInstance());
        SMHierarchicCursor rootCursor = inf.rootElementCursor(report);
        advanceCursor(rootCursor);
        LOG.debug("rootCursor is at : {}", findElementName(rootCursor));

        // We first get the tests ids and put them in a map to get the details later
        Map<String, TestDescription> testsDetails = new HashMap<String, TestDescription>();

        QName testModelTag = new QName(GALLIO_URI, "testModel");
        SMInputCursor testModelCursor = descendantElements(rootCursor);
        testModelCursor.setFilter(SMFilterFactory.getElementOnlyFilter(testModelTag));
        advanceCursor(testModelCursor);
        LOG.debug("TestModelCursor initialized at : {}", findElementName(testModelCursor));
        testsDetails = recursiveParseTestsIds(testModelCursor, testsDetails, null, null);

        QName testPackageRunTag = new QName(GALLIO_URI, "testPackageRun");
        testModelCursor.setFilter(SMFilterFactory.getElementOnlyFilter(testPackageRunTag));
        advanceCursor(testModelCursor);
        String testId = "";
        recursiveParseTestsResults(testModelCursor, testId);

        // Finally, we fill the reports
        final Set<UnitTestReport> reports = createUnitTestsReport(testsDetails);
        rootCursor.getStreamReader().closeCompletely();
        LOG.debug("Parsing ended");

        return reports;
    } catch (XMLStreamException e) {
        throw new SonarException(GALLIO_REPORT_PARSING_ERROR, e);
    }

}

From source file:org.sonar.plugins.javascript.jslint.JsLintXmlRuleParser.java

public List<JsLintRule> parse(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    try {//from  www. java  2s .c  om
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        rootC.advance(); // <rules>
        List<JsLintRule> rules = new ArrayList<JsLintRule>();

        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            JsLintRule rule = new JsLintRule();
            rules.add(rule);

            processRule(rule, rulesC);
        }
        return rules;

    } catch (XMLStreamException e) {
        throw new SonarException("XML is not valid", e);
    }
}

From source file:org.sonar.plugins.ndepend.QueryLoader.java

public ImmutableList<NdependQuery> getQueries(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    ImmutableList.Builder<NdependQuery> builder = new Builder<NdependQuery>();
    try {//from ww  w. jav  a2s .c o  m
        SMHierarchicCursor root = inputFactory.rootElementCursor(reader);
        root.advance();

        SMInputCursor rules = root.childElementCursor("rule");
        while (rules.getNext() != null) {
            builder.add(processRule(rules));
        }
        return builder.build();

    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}

From source file:org.sonar.server.duplication.ws.DuplicationsParser.java

private static SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    return new SMInputFactory(xmlFactory);
}

From source file:org.springframework.batch.item.xml.StaxEventItemReader.java

@Override
protected void doOpen() throws Exception {
    Assert.notNull(resource, "The Resource must not be null.");

    noInput = true;//from   w  ww .j a  v  a2  s.c o  m
    if (!resource.exists()) {
        if (strict) {
            throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");
        }
        logger.warn("Input resource does not exist " + resource.getDescription());
        return;
    }
    if (!resource.isReadable()) {
        if (strict) {
            throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)");
        }
        logger.warn("Input resource is not readable " + resource.getDescription());
        return;
    }

    inputStream = resource.getInputStream();
    eventReader = XMLInputFactory.newInstance().createXMLEventReader(inputStream);
    fragmentReader = new DefaultFragmentEventReader(eventReader);
    noInput = false;

}

From source file:org.springframework.ws.soap.axiom.AxiomSoapMessageFactory.java

/**
 * Create a {@code XMLInputFactory} that this resolver will use to create {@link XMLStreamReader} objects.
 * <p/>/* w w  w  . j  a  v a 2 s  .  c om*/
 * Can be overridden in subclasses, adding further initialization of the factory. The resulting factory is cached,
 * so this method will only be called once.
 *
 * @return the created factory
 */
protected XMLInputFactory createXmlInputFactory() {
    return XMLInputFactory.newInstance();
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new XML stream reader from the given InputStream.
 * /*  ww  w  . j a v  a 2  s. c  o  m*/
 * @param inputStream
 *          the InputStream used as source.
 * @return the created XML stream reader.
 * @throws XmlRpcParsingException
 *           if there are any errors during the parsing.
 */
protected final XMLStreamReader loadXmlReader(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (logger.isDebugEnabled()) {
        logger.debug("Using StAX implementation [" + factory + "]");
    }

    XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
    return reader;
}