Search in sources :

Example 21 with XMLStreamException

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

the class Stax2JettisonFactory method ensureNonEmptyReader.

private static Reader ensureNonEmptyReader(Reader reader) throws XMLStreamException {
    try {
        Reader mr = reader.markSupported() ? reader : new BufferedReader(reader);
        mr.mark(1);
        if (mr.read() == -1) {
            throw new XMLStreamException("JSON expression can not be empty!");
        }
        mr.reset();
        return mr;
    } catch (IOException ex) {
        throw new XMLStreamException(ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) MappedXMLStreamReader(org.codehaus.jettison.mapped.MappedXMLStreamReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) BufferedReader(java.io.BufferedReader) BadgerFishXMLStreamReader(org.codehaus.jettison.badgerfish.BadgerFishXMLStreamReader) IOException(java.io.IOException)

Example 22 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 23 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project hibernate-orm by hibernate.

the class XmlParserHelper method getJaxbRoot.

public <T> T getJaxbRoot(InputStream stream, Class<T> clazz, Schema schema) throws XmlParsingException {
    XMLEventReader staxEventReader;
    try {
        staxEventReader = createXmlEventReader(stream);
    } catch (XMLStreamException e) {
        throw new XmlParsingException("Unable to create stax reader", e);
    }
    ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
    try {
        staxEventReader = new JpaNamespaceTransformingEventReader(staxEventReader);
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(handler);
        return clazz.cast(unmarshaller.unmarshal(staxEventReader));
    } catch (JAXBException e) {
        StringBuilder builder = new StringBuilder();
        builder.append("Unable to perform unmarshalling at line number ");
        builder.append(handler.getLineNumber());
        builder.append(" and column ");
        builder.append(handler.getColumnNumber());
        builder.append(". Message: ");
        builder.append(handler.getMessage());
        throw new XmlParsingException(builder.toString(), e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 24 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project databus by linkedin.

the class StaxBuilderTest method processXml.

public void processXml() throws Exception {
    try {
        //wrap the stream with fake root
        String xmlStartTag = "<?xml version=\"" + DbusConstants.XML_VERSION + "\" encoding=\"" + DbusConstants.ISO_8859_1 + "\"?><root>";
        String xmlEndTag = "</root>";
        List xmlTagsList = Arrays.asList(new InputStream[] { new ByteArrayInputStream(xmlStartTag.getBytes(DbusConstants.ISO_8859_1)), _fileInputStream, new ByteArrayInputStream(xmlEndTag.getBytes(DbusConstants.ISO_8859_1)) });
        Enumeration<InputStream> streams = Collections.enumeration(xmlTagsList);
        SequenceInputStream seqStream = new SequenceInputStream(streams);
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(seqStream);
        //TODO replace _tableToNamespace this by physical sources config
        XmlParser parser = new XmlParser(xmlStreamReader, _schemaRegistry, _tableToNamespace, _tableToSourceId, _transactionSuccessCallBack, true, _staticReplicationConfig, seqStream);
        parser.start();
    } catch (XMLStreamException e) {
        LOG.error("Unable to parse the given xml stream", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SequenceInputStream(java.io.SequenceInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) List(java.util.List) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 25 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project databus by linkedin.

the class XmlFormatTrailParser method run.

/**
   * @see java.lang.Runnable#run()
   */
@Override
public void run() {
    long lastReportTs = 0;
    long lastReportedPosition = _lastPosition;
    long savePos = _lastPosition;
    try {
        while (!_shutdownRequested.get() && _xmlStreamReader.hasNext()) {
            int eventType = _xmlStreamReader.next();
            switch(eventType) {
                case XMLStreamConstants.START_ELEMENT:
                    processStartElement();
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    processEndElement();
                    break;
                //do nothing -- just log progress
                default:
                    break;
            }
            File curFile = _inputStream.getCurrentFile();
            if (null != curFile && !_inputStream.isClosed()) {
                String curFileName = curFile.getName();
                long currentTs = System.currentTimeMillis();
                long curPos = _inputStream.getCurrentPosition();
                if (curPos != savePos) {
                    //the XML reader seems to do internal buffering which makes it harder to
                    //guess the position where a problem happened.
                    //At any point the parser is processing between the bytes [_lastPosition, curPos).
                    _lastPosition = savePos;
                    savePos = curPos;
                }
                if (!curFileName.equals(_lastFileName) || (curPos - lastReportedPosition) >= POSITION_REPORT_GAP || (currentTs - lastReportTs) >= POSITION_REPORT_TIME_MS) {
                    lastReportedPosition = curPos;
                    lastReportTs = currentTs;
                    _log.info("file: " + curFileName + "; pos: " + curPos);
                    logTablesSeen();
                }
                _lastFileName = curFileName;
            }
        }
    } catch (XMLStreamException e) {
        _log.error("xml stream error: " + e, e);
        _lastError = e;
    } catch (RuntimeException e) {
        _log.error("runtime error: " + e, e);
        _lastError = e;
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)274 IOException (java.io.IOException)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)74 XMLInputFactory (javax.xml.stream.XMLInputFactory)60 InputStream (java.io.InputStream)43 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)34 XMLEvent (javax.xml.stream.events.XMLEvent)24 StringReader (java.io.StringReader)22 JAXBException (javax.xml.bind.JAXBException)22 XMLEventReader (javax.xml.stream.XMLEventReader)19 ModelNode (org.jboss.dmr.ModelNode)18 ArrayList (java.util.ArrayList)16 XMLExtendedStreamReader (org.jboss.staxmapper.XMLExtendedStreamReader)15 ModelElement (org.wildfly.extension.picketlink.common.model.ModelElement)15 SAXException (org.xml.sax.SAXException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 StringWriter (java.io.StringWriter)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Characters (javax.xml.stream.events.Characters)11