Search in sources :

Example 26 with JAXBContext

use of javax.xml.bind.JAXBContext in project quickstarts by jboss-switchyard.

the class JaxbTransformationTest method testJaxbOrderToXML.

@Test
public void testJaxbOrderToXML() throws Exception {
    Order order = new Order();
    order.setItemId("BUTTER");
    order.setOrderId("PO-19838-XYZ");
    order.setQuantity(200);
    JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { Order.class });
    StringWriter resultWriter = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(order, resultWriter);
    _testKit.compareXMLToResource(resultWriter.toString(), ORDER_XML);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 27 with JAXBContext

use of javax.xml.bind.JAXBContext 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 28 with JAXBContext

use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.

the class ConfigParser method marshal.

/**
     * This method saves List of Request objects into xml file
     *
     * @param requests
     *            object to be saved
     * @param xmlFileLocation
     *            file object to save the object into
     */
@SuppressWarnings("resource")
public void marshal(List<Request> requests, File xmlFileLocation) throws StiebelHeatPumpException {
    JAXBContext context;
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFileLocation), "UTF-8"));
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        context = JAXBContext.newInstance(Requests.class);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    Marshaller m;
    try {
        m = context.createMarshaller();
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    } catch (PropertyException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        m.marshal(new Requests(requests), writer);
    } catch (JAXBException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
    try {
        writer.close();
    } catch (IOException e) {
        throw new StiebelHeatPumpException(e.toString());
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Requests(org.openhab.binding.stiebelheatpump.protocol.Requests) BufferedWriter(java.io.BufferedWriter)

Example 29 with JAXBContext

use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.

the class FritzahaWebserviceUpdateXmlCallback method execute.

/**
     * {@inheritDoc}
     */
@Override
public void execute(int status, String response) {
    super.execute(status, response);
    if (validRequest) {
        logger.trace("Received State response " + response + " for item " + itemName);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
            Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();
            DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
            ArrayList<DeviceModel> list = model.getDevicelist();
            for (DeviceModel device : list) {
                if (device.getIdentifier().equals(this.deviceAin)) {
                    BigDecimal meterValueScaled = new BigDecimal(0);
                    switch(type) {
                        case POWER:
                            meterValueScaled = device.getPowermeter().getPower().scaleByPowerOfTen(-3);
                            break;
                        case ENERGY:
                            meterValueScaled = device.getPowermeter().getEnergy();
                            break;
                        case TEMPERATURE:
                            meterValueScaled = device.getTemperature().getCelsius().scaleByPowerOfTen(-1);
                            break;
                        default:
                            logger.warn("unknown meter type: " + type);
                            break;
                    }
                    logger.debug(device.toString());
                    webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
                } else {
                    logger.trace("device " + device.getIdentifier() + " was not requested");
                }
            }
        } catch (JAXBException e) {
            logger.error(e.getLocalizedMessage(), e);
        }
    }
}
Also used : DeviceModel(org.openhab.binding.fritzaha.internal.model.DeviceModel) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) DecimalType(org.openhab.core.library.types.DecimalType) JAXBContext(javax.xml.bind.JAXBContext) DevicelistModel(org.openhab.binding.fritzaha.internal.model.DevicelistModel) Unmarshaller(javax.xml.bind.Unmarshaller) BigDecimal(java.math.BigDecimal)

Example 30 with JAXBContext

use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.

the class SmarthomaticBinding method activate.

/**
     * activate binding
     *
     */
@Override
public void activate() {
    // log activate of binding
    if (baseStation != null) {
        logger.info("Smarthomatic Binding activated. BaseStation= {}", baseStation.toString());
    }
    Bundle bundle = SmarthomaticActivator.getContext().getBundle();
    URL fileURL = bundle.getEntry("packet_layout.xml");
    Packet packet = null;
    try {
        InputStream inputStream = fileURL.openConnection().getInputStream();
        JAXBContext jaxbContext = JAXBContext.newInstance(Packet.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        packet = (Packet) jaxbUnmarshaller.unmarshal(inputStream);
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    this.packet = packet;
}
Also used : Packet(org.openhab.binding.smarthomatic.internal.packetData.Packet) Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller) URL(java.net.URL)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)442 Unmarshaller (javax.xml.bind.Unmarshaller)240 Marshaller (javax.xml.bind.Marshaller)129 JAXBException (javax.xml.bind.JAXBException)128 Test (org.junit.Test)92 InputStream (java.io.InputStream)84 File (java.io.File)50 StringWriter (java.io.StringWriter)47 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 StringReader (java.io.StringReader)27 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)21 ArrayList (java.util.ArrayList)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 JAXBElement (javax.xml.bind.JAXBElement)19 FileOutputStream (java.io.FileOutputStream)18 Schema (javax.xml.validation.Schema)18 SchemaFactory (javax.xml.validation.SchemaFactory)17 SAXSource (javax.xml.transform.sax.SAXSource)14