Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:org.osmtools.api.SchemaService.java

public Unmarshaller createOsmUnmarshaller() {
    try {//w w w. ja  v a2  s .c o m
        return JAXBContext.newInstance(Osm.class).createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netsteadfast.greenstep.util.DynamicHqlUtils.java

public static DynamicHql loadResource(String resource) throws Exception {
    DynamicHql dynamicHql = resourceDataMap.get(resource);
    if (dynamicHql == null) {
        InputStream in = null;//from w w w  . ja  v  a 2  s  .  c  om
        try {
            in = DynamicHqlUtils.class.getResourceAsStream("/dynamichql/" + resource);
            byte[] xmlBytes = IOUtils.toByteArray(in);
            JAXBContext jaxbContext = JAXBContext.newInstance(DynamicHql.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            dynamicHql = (DynamicHql) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlBytes));
            resourceDataMap.put(resource, dynamicHql);
        } catch (IOException e) {
            logger.error(e.getMessage().toString());
            throw e;
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }
            in = null;
        }
    }
    return dynamicHql;
}

From source file:Main.java

/**
 *
 * @param obj// w w  w  .  j  a  va  2  s  . c om
 * @param filename
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static void serialize(Object obj, String filename) throws JAXBException, FileNotFoundException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, new FileOutputStream(filename));
}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {/*from www.j  a  va 2  s.  c  o m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
        jaxbMarshaller.marshal(content, osw);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return baos != null ? baos.toString() : null;
}

From source file:mx.bigdata.sat.cfdi.TFDv1.java

private static final JAXBContext createContext() {
    try {/*from  ww  w.  j a va  2s .  co  m*/
        return JAXBContext.newInstance("mx.bigdata.sat.cfdi.schema");
    } catch (Exception e) {
        throw new Error(e);
    }
}

From source file:at.ac.tuwien.dsg.quelle.cloudDescriptionParsers.impl.CloudFileDescriptionParser.java

public CloudProvider getCloudProviderDescription() {
    try {/*from w  w  w.j  a  v  a2 s .c o  m*/
        JAXBContext jAXBContext = JAXBContext.newInstance(CloudProvider.class);
        InputStream fileStream = context.getResource(descriptionFile).getInputStream();
        return (CloudProvider) jAXBContext.createUnmarshaller().unmarshal(fileStream);
    } catch (Exception ex) {
        log.error("Cannot unmarshall : {}", ex.getMessage());
        ex.printStackTrace();
        return new CloudProvider("empty");
    }
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

@SuppressWarnings("unchecked")
public static <T> T asObjectFromXml(String str, Class<T> clazz, Class<?>... otherClazz)
        throws JAXBException, IOException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(otherClazz));
    list.add(clazz);/*from w  ww.j a va2 s  .  com*/

    JAXBContext context = JAXBContext.newInstance(list.toArray(new Class[list.size()]));
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (T) unmarshaller.unmarshal(new StringReader(str));
}

From source file:com.photon.phresco.plugins.util.WarConfigProcessor.java

public WarConfigProcessor(File configFile) throws JAXBException, IOException {
    if (configFile.exists()) {
        JAXBContext jaxbContext = JAXBContext.newInstance(Assembly.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        assembly = (Assembly) jaxbUnmarshaller.unmarshal(configFile);
    } else {//from w  ww.j  a  v  a 2  s  .  co m
        configFile.createNewFile();
        assembly = new Assembly();
    }
    file = configFile;
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

public static String marshallToString(Class sourceClass, Object source) {
    try {//w ww. j  a  v  a 2s .c o m
        JAXBContext jaxbContext = JAXBContext.newInstance(sourceClass);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:eu.planets_project.tb.impl.serialization.JaxbUtil.java

/**
* Marshalling via Jaxb: Creates a String Serialization of the requested class for
* the content of Object objectToSerialize. The provided object and the requested class need to be of the same Type
* @param <T>//from ww  w. j  av  a  2s .  c om
* @param objectClass
* @param objectToSerialize
* @return
* @throws Exception
*/
public static <T> String marshallObjectwithJAXB(Class<T> objectClass, T objectToSerialize) throws Exception {
    JAXBContext context;
    try {
        context = JAXBContext.newInstance(objectClass);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        //now call the actual marshalling job
        m.marshal(objectToSerialize, sw);
        return sw.toString();
    } catch (Exception e) {
        log.error("marshalWorkflowResult failed for objectClass: " + objectClass + " with " + e);
        throw e;
    }
}