Search in sources :

Example 76 with XMLInputFactory

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Reference(javax.naming.Reference) Stack(java.util.Stack) StartElement(javax.xml.stream.events.StartElement) Entry(java.util.Map.Entry) StringRefAddr(javax.naming.StringRefAddr) XMLEvent(javax.xml.stream.events.XMLEvent) Iterator(java.util.Iterator) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 77 with XMLInputFactory

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;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) Schema(javax.xml.validation.Schema) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) MalmoMod(com.microsoft.Malmo.MalmoMod) URL(java.net.URL) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 78 with XMLInputFactory

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);
}
Also used : StringReader(java.io.StringReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 79 with XMLInputFactory

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());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Marshaller(javax.xml.bind.Marshaller) QName(javax.xml.namespace.QName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBElement(javax.xml.bind.JAXBElement) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 80 with XMLInputFactory

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());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLEventWriter(javax.xml.stream.XMLEventWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)182 XMLStreamReader (javax.xml.stream.XMLStreamReader)114 XMLStreamException (javax.xml.stream.XMLStreamException)74 InputStream (java.io.InputStream)54 StringReader (java.io.StringReader)45 IOException (java.io.IOException)40 XMLEventReader (javax.xml.stream.XMLEventReader)36 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Test (org.junit.Test)29 InputStreamReader (java.io.InputStreamReader)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 XMLEvent (javax.xml.stream.events.XMLEvent)15 StAXSource (javax.xml.transform.stax.StAXSource)15 StreamSource (javax.xml.transform.stream.StreamSource)15 ArrayList (java.util.ArrayList)14 JAXBException (javax.xml.bind.JAXBException)14 HashMap (java.util.HashMap)13 DOMSource (javax.xml.transform.dom.DOMSource)12 StartElement (javax.xml.stream.events.StartElement)10 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10