Search in sources :

Example 31 with JAXBContext

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

the class TestSHCMessage method setUp.

/**
     * common setup
     *
     * @throws Exception
     */
@Before
public void setUp() throws Exception {
    File file = new File("src/main/resources/packet_layout.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Packet.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    packet = (Packet) jaxbUnmarshaller.unmarshal(file);
}
Also used : JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) Before(org.junit.Before)

Example 32 with JAXBContext

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

the class LgTvAppSet method loadapps.

/**
     * read applications out of string into a list
     * 
     * @param s
     * @throws JAXBException
     */
public void loadapps(String s) throws JAXBException {
    JAXBContext jc;
    jc = JAXBContext.newInstance(envelope.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    int start = s.indexOf("<envelope>");
    int stop = s.indexOf("</envelope>") + "</envelope>".length();
    String t = s.substring(start, stop);
    StringReader reader = new StringReader(t);
    envel = null;
    envel = (envelope) unmarshaller.unmarshal(reader);
}
Also used : StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 33 with JAXBContext

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

the class LgTvChannelSet method savetofile.

/**
     * Save Channel List to File f
     * 
     * @param f
     */
public void savetofile(String f) {
    Writer writer = null;
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(envelope.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        marshaller.marshal(envel, writer);
    } catch (PropertyException e) {
        logger.error("error in savetofile", e);
    } catch (JAXBException e) {
        logger.error("error in savetofile", e);
    } catch (IOException ex) {
        logger.error("error in savetofile", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
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) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter)

Example 34 with JAXBContext

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

the class LgTvEventChannelChanged method readevent.

public String readevent(String s) throws JAXBException {
    JAXBContext jc;
    jc = JAXBContext.newInstance(envelope.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    int start = s.indexOf("<envelope>");
    int stop = s.indexOf("</envelope>") + "</envelope>".length();
    String t = s.substring(start, stop);
    // System.out.println(t);
    StringReader reader = new StringReader(t);
    envel = null;
    envel = (envelope) unmarshaller.unmarshal(reader);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter sw = new StringWriter();
    marshaller.marshal(envel, sw);
    return new String(sw.toString());
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 35 with JAXBContext

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

the class DenonConnector method getDocument.

private <T> T getDocument(String uri, Class<T> response) {
    try {
        String result = doHttpRequest("GET", uri, null);
        logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);
        if (StringUtils.isNotBlank(result)) {
            JAXBContext jc = JAXBContext.newInstance(response);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
            xsr = new PropertyRenamerDelegate(xsr);
            @SuppressWarnings("unchecked") T obj = (T) jc.createUnmarshaller().unmarshal(xsr);
            return obj;
        }
    } catch (UnmarshalException e) {
        logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
    } catch (JAXBException e) {
        logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
    } catch (XMLStreamException e) {
        logger.debug("Communication error: {}", e.getMessage());
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

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