use of javax.xml.stream.XMLInputFactory in project kernel by exoplatform.
the class InitialContextBinder method readBindings.
/**
* Import references from xml-file.
*
* @return map with bind name - references
*
* @throws XMLStreamException
* if errors occurs during import
* @throws FileNotFoundException
* if can't open input stream from file
*/
protected Map<String, Reference> readBindings() throws FileNotFoundException, XMLStreamException {
Stack<RefEntity> stack = new Stack<RefEntity>();
Map<String, Reference> importedRefs = new HashMap<String, Reference>();
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(PrivilegedFileHelper.fileInputStream(bindingsStorePath), "UTF-8");
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
Map<String, String> attr = new HashMap<String, String>();
Iterator attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next();
attr.put(attribute.getName().getLocalPart(), attribute.getValue());
}
String localName = startElement.getName().getLocalPart();
if (localName.equals(REFERENCE_ELEMENT)) {
String bindName = attr.get(BIND_NAME_ATTR);
String className = attr.get(CLASS_NAME_ATTR);
String factoryName = attr.get(FACTORY_ATTR);
String factoryLocation = attr.get(FACTORY_LOCATION_ATTR);
Reference reference = new Reference(className, factoryName, factoryLocation);
stack.push(new RefEntity(bindName, reference));
} else if (localName.equals(PROPERTY_ELEMENT)) {
RefEntity refEntity = stack.pop();
Reference reference = refEntity.getValue();
for (Entry<String, String> entry : attr.entrySet()) {
reference.add(new StringRefAddr(entry.getKey(), entry.getValue()));
}
refEntity.setValue(reference);
stack.push(refEntity);
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
localName = endElement.getName().getLocalPart();
if (localName.equals(REFERENCE_ELEMENT)) {
RefEntity refEntity = stack.pop();
importedRefs.put(refEntity.getKey(), refEntity.getValue());
}
break;
default:
break;
}
}
return importedRefs;
}
use of javax.xml.stream.XMLInputFactory in project malmo by Microsoft.
the class SchemaHelper method deserialiseObject.
/**
* Attempt to construct the specified object from this XML string
* @param xml the XML string to parse
* @param xsdFile the name of the XSD schema that defines the object
* @param objclass the class of the object requested
* @return if successful, an instance of class objclass that captures the data in the XML string
*/
public static Object deserialiseObject(String xml, String xsdFile, Class<?> objclass) throws JAXBException, SAXException, XMLStreamException {
Object obj = null;
JAXBContext jaxbContext = getJAXBContext(objclass);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final String schemaResourceFilename = new String(xsdFile);
URL schemaURL = MalmoMod.class.getClassLoader().getResource(schemaResourceFilename);
Schema schema = schemaFactory.newSchema(schemaURL);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
StringReader stringReader = new StringReader(xml);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader XMLreader = xif.createXMLStreamReader(stringReader);
obj = jaxbUnmarshaller.unmarshal(XMLreader);
return obj;
}
use of javax.xml.stream.XMLInputFactory in project cxf by apache.
the class Stax2ValidationUtilsTest method createReader.
private XMLStreamReader createReader(String message) throws XMLStreamException {
Reader reader = new StringReader(message);
XMLInputFactory factory = XMLInputFactory.newInstance();
return factory.createXMLStreamReader(reader);
}
use of javax.xml.stream.XMLInputFactory in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallIntoStaxEventWriter.
@Test
public void testMarshallIntoStaxEventWriter() 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);
FixNamespacesXMLEventWriter writer = new FixNamespacesXMLEventWriter(opFactory.createXMLEventWriter(baos));
assertNull(writer.getMarshaller());
// STARTDOCUMENT/ENDDOCUMENT is not required
// writer.add(eFactory.createStartDocument("utf-8", "1.0"));
Marshaller m = context.createMarshaller();
JAXBEncoderDecoder.marshall(m, obj, part, writer);
assertEquals(m, writer.getMarshaller());
// writer.add(eFactory.createEndDocument());
writer.flush();
writer.close();
// System.out.println(baos.toString());
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.XMLInputFactory in project cxf by apache.
the class JAXBEncoderDecoderTest method testMarshallWithoutQNameInfo.
@Test
public void testMarshallWithoutQNameInfo() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
XMLEventWriter writer = opFactory.createXMLEventWriter(baos);
// STARTDOCUMENT/ENDDOCUMENT is not required
// writer.add(eFactory.createStartDocument("utf-8", "1.0"));
JAXBEncoderDecoder.marshall(context.createMarshaller(), obj, null, writer);
// writer.add(eFactory.createEndDocument());
writer.flush();
writer.close();
// System.out.println(baos.toString());
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());
}
Aggregations