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.bremersee.sms.ExtensionUtils.java

private static JAXBContext getJaxbContext(Class<?> valueType) throws JAXBException {
    JAXBContext jaxbContext = JAXB_CONTEXTS.get(valueType.getPackage());
    if (jaxbContext == null) {
        jaxbContext = JAXB_CONTEXTS.get(valueType);
    }/*from w  w  w.  j a v  a 2  s.  c om*/
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(valueType.getPackage().getName());
        } catch (JAXBException e) {
            jaxbContext = JAXBContext.newInstance(valueType);
            JAXB_CONTEXTS.put(valueType, jaxbContext);
        }
    }
    return jaxbContext;
}

From source file:demo.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, InputStream inputStream) throws JAXBException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder out = new StringBuilder();
    try {/*w w w  .  j a  va2  s. c om*/
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    String namespace = out.toString();

    System.out.println(namespace);
    namespace = namespace.replaceAll("xmlns=\"http://ws.wso2.org/dataservice\"", "");
    System.out.println(namespace);
    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    T doc = (T) unmarshaller.unmarshal(stream);
    return doc;
}

From source file:Main.java

/**
 *
 * @param fXMLFilePath/*  ww w.j  a  v  a 2  s . c om*/
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(File fXMLFilePath, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(fXMLFilePath);
}

From source file:Main.java

/**
 * Attempts to unmarshal an XML document from an InputStream.
 *
 * @param inputStream InputStream to unmarshal XML from.
 * @param type        JAXB class of the returned object. Must be in the same package as other relevant JAXB classes.
 * @param <T>         the type of the returned object
 * @return the unmarshalled JAXB object//w  w w .  j a v  a 2 s  .  com
 * @throws JAXBException if an unexpected error occurs while unmarshalling
 */
public static <T> T unmarshal(InputStream inputStream, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(type.getPackage().getName());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    @SuppressWarnings("unchecked")
    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(inputStream);
    return jaxbElement.getValue();
}

From source file:org.vincibean.salestaxes.jaxb.JaxbFactory.java

/**
 * Factory method, creates a {@link Marshaller} from the context given in the constructor; moreover, ensure that
 * the marshalled XML data is formatted with linefeeds and indentation.  
 * @return an {@link Optional} object which may or may not contain a {@link Marshaller}
 *///  w  ww .j  ava 2  s .co m
public static Optional<Marshaller> createMarshaller(final Class<?> context) {
    try {
        Marshaller marshaller = JAXBContext.newInstance(context).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new ClassPathResource("receipt/Poiuyt.xsd").getFile()));
        marshaller.setEventHandler(new FoobarValidationEventHandler());
        return Optional.fromNullable(marshaller);
    } catch (JAXBException | SAXException | IOException e) {
        logger.warn("Exception on jaxb factory creation: ", e);
        return Optional.absent();
    }
}

From source file:att.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, InputStream inputStream) {
    System.out.println("unmarshalling input stream");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder out = new StringBuilder();
    try {//w ww  . j  a  va2  s.  co m
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    String namespace = out.toString();

    namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, "");

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext;
    XmlRootElement doc = null;
    try {
        jaxbContext = JAXBContext.newInstance(xmlType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        doc = (XmlRootElement) unmarshaller.unmarshal(stream);
        System.out.println("unmarshall successful");
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return (T) doc;
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static String marshallFromBeanTree(final CXML cxml) {
    final StringWriter writer = new StringWriter();

    try {// ww w.  j  a  v a2s.c o  m
        final JAXBContext context = JAXBContext.newInstance(CXML.class);
        final Marshaller m = context.createMarshaller();
        removeStandalone(m);
        setHeader(m);
        m.marshal(cxml, writer);
    } catch (final JAXBException e) {
        throw new PunchOutException(e.getErrorCode(), e.getMessage());
    }

    final String xml = writer.toString();

    return xml;
}

From source file:edu.wustl.xipHost.avt2ext.RetrieveAIMTest.java

@BeforeClass
public static void setUp() throws Exception {
    adService = AVTFactory.getADServiceInstance();
    builder = new SAXBuilder();
    outToXMLFile = new XMLOutputter();
    try {//  w ww .ja  v  a  2 s  . co m
        jc = JAXBContext.newInstance("gme.cacore_cacore._3_2.edu_northwestern_radiology");
        u = jc.createUnmarshaller();
    } catch (JAXBException e) {
        logger.error(e, e);
    }
    DOMConfigurator.configure("log4j.xml");
}

From source file:org.pmedv.jake.JakeUtil.java

/**
 * Updates the {@link RecentFileList} with a new file
 * /* ww w. j a  va 2 s  .c om*/
 * @param filename the name to append to the list
 */
public static void updateRecentFiles(String filename) {

    RecentFileList fileList = null;

    try {

        String inputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String inputFileName = "recentFiles.xml";
        File inputFile = new File(inputDir + inputFileName);

        if (inputFile.exists()) {
            Unmarshaller u = JAXBContext.newInstance(RecentFileList.class).createUnmarshaller();
            fileList = (RecentFileList) u.unmarshal(inputFile);
        }

        if (fileList == null)
            fileList = new RecentFileList();

    } catch (JAXBException e) {
        e.printStackTrace();
    }

    if (fileList.getRecentFiles().size() >= 5) {
        fileList.getRecentFiles().remove(0);
    }

    if (!fileList.getRecentFiles().contains(filename))
        fileList.getRecentFiles().add(filename);

    Marshaller m;

    try {
        String outputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String outputFileName = "recentFiles.xml";
        m = JAXBContext.newInstance(RecentFileList.class).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        File output = new File(outputDir + outputFileName);
        m.marshal(fileList, output);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Get/*from   w w  w  .j  a va2  s. com*/
 * 
 * @param classType
 * @return
 * @throws JAXBException
 */
@SuppressWarnings("rawtypes")
private static synchronized JAXBContext getJaxbContext(Class... classTypes) throws JAXBException {
    JAXBContext retObj = null;

    String key = "";
    for (Class classType : classTypes) {
        if (!"".equals(key)) {
            key += ";";
        }
        key += classType.getName();
    }

    retObj = jaxbContextMap.get(key);
    if (retObj == null) {
        retObj = JAXBContext.newInstance(classTypes);
        jaxbContextMap.put(key, retObj);
    }

    return retObj;
}