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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
}
Aggregations