Search in sources :

Example 31 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project jersey by jersey.

the class JettisonListElementProvider method writeCollection.

@Override
public final void writeCollection(Class<?> elementType, Collection<?> t, MediaType mediaType, Charset c, Marshaller m, OutputStream entityStream) throws JAXBException, IOException {
    final OutputStreamWriter osw = new OutputStreamWriter(entityStream, c);
    JettisonConfig origJsonConfig = JettisonConfig.DEFAULT;
    if (m instanceof JettisonConfigured) {
        origJsonConfig = ((JettisonConfigured) m).getJSONConfiguration();
    }
    final JettisonConfig unwrappingJsonConfig = JettisonConfig.createJSONConfiguration(origJsonConfig);
    final XMLStreamWriter jxsw = Stax2JettisonFactory.createWriter(osw, unwrappingJsonConfig);
    final String invisibleRootName = getRootElementName(elementType);
    try {
        jxsw.writeStartDocument();
        jxsw.writeStartElement(invisibleRootName);
        for (Object o : t) {
            m.marshal(o, jxsw);
        }
        jxsw.writeEndElement();
        jxsw.writeEndDocument();
        jxsw.flush();
    } catch (XMLStreamException ex) {
        Logger.getLogger(JettisonListElementProvider.class.getName()).log(Level.SEVERE, null, ex);
        throw new JAXBException(ex.getMessage(), ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) JettisonConfigured(org.glassfish.jersey.jettison.JettisonConfigured) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBException(javax.xml.bind.JAXBException) JettisonConfig(org.glassfish.jersey.jettison.JettisonConfig) OutputStreamWriter(java.io.OutputStreamWriter)

Example 32 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project hadoop by apache.

the class OfflineImageReconstructor method loadNodeChildrenHelper.

private void loadNodeChildrenHelper(Node parent, String expected, String[] terminators) throws IOException {
    XMLEvent ev = null;
    while (true) {
        try {
            ev = events.peek();
            switch(ev.getEventType()) {
                case XMLEvent.END_ELEMENT:
                    if (terminators.length != 0) {
                        return;
                    }
                    events.nextEvent();
                    return;
                case XMLEvent.START_ELEMENT:
                    String key = ev.asStartElement().getName().getLocalPart();
                    for (String terminator : terminators) {
                        if (terminator.equals(key)) {
                            return;
                        }
                    }
                    events.nextEvent();
                    Node node = new Node();
                    parent.addChild(key, node);
                    loadNodeChildrenHelper(node, expected, new String[0]);
                    break;
                case XMLEvent.CHARACTERS:
                    String val = XMLUtils.unmangleXmlString(ev.asCharacters().getData(), true);
                    parent.setVal(val);
                    events.nextEvent();
                    break;
                case XMLEvent.ATTRIBUTE:
                    throw new IOException("Unexpected XML event " + ev);
                default:
                    // Ignore other event types like comment, etc.
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Skipping XMLEvent " + ev);
                    }
                    events.nextEvent();
                    break;
            }
        } catch (XMLStreamException e) {
            throw new IOException("Expecting " + expected + ", but got XMLStreamException", e);
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 33 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project sonarqube by SonarSource.

the class XMLProfileParser method parse.

public RulesProfile parse(Reader reader, ValidationMessages messages) {
    RulesProfile profile = RulesProfile.create();
    SMInputFactory inputFactory = initStax();
    try {
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <profile>
        rootC.advance();
        SMInputCursor cursor = rootC.childElementCursor();
        while (cursor.getNext() != null) {
            String nodeName = cursor.getLocalName();
            if (StringUtils.equals("rules", nodeName)) {
                SMInputCursor rulesCursor = cursor.childElementCursor("rule");
                processRules(rulesCursor, profile, messages);
            } else if (StringUtils.equals("name", nodeName)) {
                profile.setName(StringUtils.trim(cursor.collectDescendantText(false)));
            } else if (StringUtils.equals("language", nodeName)) {
                profile.setLanguage(StringUtils.trim(cursor.collectDescendantText(false)));
            }
        }
    } catch (XMLStreamException e) {
        messages.addErrorText("XML is not valid: " + e.getMessage());
    }
    checkProfile(profile, messages);
    return profile;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SMInputFactory(org.codehaus.staxmate.SMInputFactory)

Example 34 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project sonarqube by SonarSource.

the class RulesDefinitionXmlLoader method load.

/**
   * Loads rules by reading the XML input stream. The reader is not closed by the method, so it
   * should be handled by the caller.
   * @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);
        // <rules>
        rootC.advance();
        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            processRule(repo, rulesC);
        }
    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SMInputFactory(org.codehaus.staxmate.SMInputFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 35 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project sonarqube by SonarSource.

the class DebtRulesXMLImporter method importXML.

public List<RuleDebt> importXML(Reader xml, ValidationMessages validationMessages) {
    List<RuleDebt> ruleDebts = newArrayList();
    try {
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml);
        // advance to <sqale>
        cursor.advance();
        SMInputCursor rootCursor = cursor.childElementCursor(CHARACTERISTIC);
        while (rootCursor.getNext() != null) {
            process(ruleDebts, validationMessages, rootCursor);
        }
        cursor.getStreamReader().closeCompletely();
    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
    return ruleDebts;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SMInputFactory(org.codehaus.staxmate.SMInputFactory) RuleDebt(org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)442 XMLStreamReader (javax.xml.stream.XMLStreamReader)137 IOException (java.io.IOException)126 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)75 XMLInputFactory (javax.xml.stream.XMLInputFactory)69 InputStream (java.io.InputStream)66 Document (org.w3c.dom.Document)36 JAXBException (javax.xml.bind.JAXBException)35 Fault (org.apache.cxf.interceptor.Fault)34 Element (org.w3c.dom.Element)32 StringReader (java.io.StringReader)30 XMLEvent (javax.xml.stream.events.XMLEvent)28 DOMSource (javax.xml.transform.dom.DOMSource)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)23 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)23 Node (org.w3c.dom.Node)22 StringWriter (java.io.StringWriter)21 XMLEventReader (javax.xml.stream.XMLEventReader)20