use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.
the class AbstractDom4jImporterExporterTest method testDom4jCommentFiltering.
@Test
public void testDom4jCommentFiltering() throws Exception {
final TestDom4jImporter importer = new TestDom4jImporter();
final TestDom4jExporter exporter = new TestDom4jExporter();
exporter.setXmlUtilities(new XmlUtilitiesImpl());
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final InputStream resource = this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml");
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(resource);
final Tuple<String, Element> result = importer.unmarshal(new StAXSource(xmlEventReader));
assertNotNull(result);
final Element document = result.getSecond();
final List<org.dom4j.Node> comments = document.selectNodes("//comment()");
for (final org.dom4j.Node comment : comments) {
comment.detach();
}
final StringWriter writer = new StringWriter();
exporter.marshal(result, new StreamResult(writer));
final String marshalResult = writer.toString();
assertNotNull(marshalResult);
XMLUnit.setIgnoreWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/filtered-pilot-lo.fragment-layout.xml")), new StringReader(marshalResult));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
}
}
use of javax.xml.stream.XMLEventReader in project cxf by apache.
the class ContextUtils method getDataReader.
public static DataReader<XMLEventReader> getDataReader(CorbaMessage message) {
Service service = ServiceModelUtil.getService(message.getExchange());
DataReader<XMLEventReader> dataReader = service.getDataBinding().createReader(XMLEventReader.class);
if (dataReader == null) {
// throw a fault
// throw new Fault(new org.apache.cxf.common.i18n.Message("NO_DATAREADER", BUNDLE, service
// .getName()));
}
return dataReader;
}
use of javax.xml.stream.XMLEventReader in project cxf by apache.
the class JAXBEncoderDecoder method doUnmarshal.
private static Object doUnmarshal(final Unmarshaller u, final Object source, final QName elName, final Class<?> clazz, final boolean unwrap) throws Exception {
Object obj = null;
boolean unmarshalWithClass = true;
if (clazz == null || (!clazz.isPrimitive() && !clazz.isArray() && !clazz.isEnum() && !clazz.equals(Calendar.class) && (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())))) {
unmarshalWithClass = false;
}
if (clazz != null && (clazz.getName().equals("javax.xml.datatype.XMLGregorianCalendar") || clazz.getName().equals("javax.xml.datatype.Duration"))) {
// special treat two jaxb defined built-in abstract types
unmarshalWithClass = true;
}
if (source instanceof Node) {
obj = unmarshalWithClass ? u.unmarshal((Node) source, clazz) : u.unmarshal((Node) source);
} else if (source instanceof DepthXMLStreamReader) {
// JAXB optimizes a ton of stuff depending on the StreamReader impl. Thus,
// we REALLY want to pass the original reader in. This is OK with JAXB
// as it doesn't read beyond the end so the DepthXMLStreamReader state
// would be OK when it returns. The main winner is FastInfoset where parsing
// a testcase I have goes from about 300/sec to well over 1000.
DepthXMLStreamReader dr = (DepthXMLStreamReader) source;
XMLStreamReader reader = dr.getReader();
// allows the XML Stream Reader to adjust it's behaviour based on the state of the unmarshaller
if (reader instanceof UnmarshallerAwareXMLReader) {
((UnmarshallerAwareXMLReader) reader).setUnmarshaller(u);
}
if (u.getSchema() != null) {
// validating, but we may need more namespaces
reader = findExtraNamespaces(reader);
}
obj = unmarshalWithClass ? u.unmarshal(reader, clazz) : u.unmarshal(dr.getReader());
} else if (source instanceof XMLStreamReader) {
XMLStreamReader reader = (XMLStreamReader) source;
// allows the XML Stream Reader to adjust it's behaviour based on the state of the unmarshaller
if (reader instanceof UnmarshallerAwareXMLReader) {
((UnmarshallerAwareXMLReader) reader).setUnmarshaller(u);
}
if (u.getSchema() != null) {
// validating, but we may need more namespaces
reader = findExtraNamespaces(reader);
}
obj = unmarshalWithClass ? u.unmarshal(reader, clazz) : u.unmarshal(reader);
} else if (source instanceof XMLEventReader) {
// allows the XML Event Reader to adjust it's behaviour based on the state of the unmarshaller
if (source instanceof UnmarshallerAwareXMLReader) {
((UnmarshallerAwareXMLReader) source).setUnmarshaller(u);
}
obj = unmarshalWithClass ? u.unmarshal((XMLEventReader) source, clazz) : u.unmarshal((XMLEventReader) source);
} else if (source == null) {
throw new Fault(new Message("UNKNOWN_SOURCE", LOG, "null"));
} else {
throw new Fault(new Message("UNKNOWN_SOURCE", LOG, source.getClass().getName()));
}
return unwrap ? getElementValue(obj) : obj;
}
use of javax.xml.stream.XMLEventReader in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallIntoStaxStreamWriter.
@Test
public void testMarshallIntoStaxStreamWriter() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
FixNamespacesXMLStreamWriter writer = new FixNamespacesXMLStreamWriter(opFactory.createXMLStreamWriter(baos));
assertNull(writer.getMarshaller());
Marshaller m = context.createMarshaller();
JAXBEncoderDecoder.marshall(m, obj, part, writer);
assertEquals(m, writer.getMarshaller());
writer.flush();
writer.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
XMLInputFactory ipFactory = XMLInputFactory.newInstance();
XMLEventReader reader = ipFactory.createXMLEventReader(bais);
Unmarshaller um = context.createUnmarshaller();
Object val = um.unmarshal(reader, GreetMe.class);
assertTrue(val instanceof JAXBElement);
val = ((JAXBElement<?>) val).getValue();
assertTrue(val instanceof GreetMe);
assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
use of javax.xml.stream.XMLEventReader in project Payara by payara.
the class GenericSniffer method readDeploymentConfig.
private String readDeploymentConfig(final InputStream is) throws IOException {
String encoding = null;
XMLEventReader rdr = null;
try {
is.mark(Integer.MAX_VALUE);
rdr = xmlInputFactory.createXMLEventReader(new InputStreamReader(is));
while (rdr.hasNext()) {
final XMLEvent ev = rdr.nextEvent();
if (ev.isStartDocument()) {
final StartDocument sd = (StartDocument) ev;
encoding = sd.getCharacterEncodingScheme();
break;
}
}
} catch (XMLStreamException e) {
if (rdr != null) {
try {
rdr.close();
} catch (XMLStreamException inner) {
throw new IOException(e);
}
}
throw new IOException(e);
}
if (encoding == null) {
encoding = "UTF-8";
}
is.reset();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
final byte[] buffer = new byte[1024];
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
try {
rdr.close();
} catch (XMLStreamException ex) {
throw new IOException(ex);
}
is.close();
return new String(baos.toByteArray(), encoding);
}
Aggregations