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.osmsurround.ae.osmrequest.SchemaService.java

public Unmarshaller createOsmUnmarshaller() {
    try {//from   w w  w  .j  a v a  2 s. c o  m
        return JAXBContext.newInstance(OsmRoot.class).createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.scape_project.tool.toolwrapper.data.tool_spec.utils.Utils.java

private static JAXBContext createContext() throws JAXBException {
    return JAXBContext.newInstance(Tool.class);
}

From source file:org.usd.edu.btl.cli.ConvertGalaxy.java

public void toBets(String inputS, String output) {
    System.out.println("Converting from GALAXY TO BETS");
    File input = new File(inputS);

    InputStream infile = null;//from w ww  .j av a 2 s.  c  o  m
    Tool myTool = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Tool.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //Unmarshalling  Conversion XML content into a Java Object.
        infile = new FileInputStream(input);
        Tool test_tool = (Tool) unmarshaller.unmarshal(infile);
        myTool = test_tool;
        //System.out.println(test_tool.toString()); //print the test_tool 

    } catch (FileNotFoundException | JAXBException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (infile != null) {
                infile.close();
            }
        } catch (IOException e) {
            System.out.println("You're rubbish, you can't even close a file");
            System.out.println(e.getMessage());
        }
    }

    //JAXB-Marshall Java back to XML
    //        try {
    //            JAXBContext jaxbContext = JAXBContext.newInstance(Tool.class);
    //            Marshaller marshaller = jaxbContext.createMarshaller(); //Marshalling  Conversion a Java object into a XML file.
    //            marshaller.marshal(myTool, System.out); //print XML out
    //        } catch (JAXBException e) {
    //            System.out.println("JAXB dun goofed");
    //            System.out.println(e.getMessage());
    //        }
    // RUN GALAXY TO BETS CONVERSION
    BETSV1 betsOutput = GalaxyConverter.toBETS(myTool);
    try {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println("************************************************\n"
                + "*********PRINTING OUT CONVERSION************\n" + "----------Galaxy --> Bets--------------\n"
                + "************************************************\n");
        //print objects as Json using jackson
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String betsJson = ow.writeValueAsString(betsOutput); //write Json as String
        System.out.println("=== GALAXY TO BETS JSON === \n" + betsJson);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T/*ww w  . j  a  va 2 s .c om*/
 * @param object the object to be transformed into XML
 * @param writer the writer object
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, Writer writer) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName());

    Marshaller m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(element, writer);
}

From source file:com.codercowboy.photo.organizer.service.LibraryMarshaller.java

private JAXBContext createContext() throws JAXBException {
    if (ctx == null) {
        ctx = JAXBContext.newInstance(Library.class.getPackage().getName());
    }/*from  w  w  w .  j a  v  a 2s.co  m*/
    return ctx;
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextCreationTest.java

@Test
public void useDefaultImplementation() throws Exception {
    JAXBContext context = JAXBContext.newInstance(String.class);
    assertThat(context.getClass().getName(),
            equalTo(com.sun.xml.bind.v2.runtime.JAXBContextImpl.class.getName()));

    context = JAXBContext.newInstance(new Class<?>[] { String.class }, new HashMap<String, Object>());
    assertThat(context.getClass().getName(),
            equalTo(com.sun.xml.bind.v2.runtime.JAXBContextImpl.class.getName()));
}

From source file:cn.hxh.springside.mapper.JaxbMapper.java

/**
 * @param rootTypes ??RootClass.// w w  w .  java  2s. co m
 */
public JaxbMapper(Class<?>... rootTypes) {
    try {
        jaxbContext = JAXBContext.newInstance(rootTypes);
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:Main.java

/**
 *
 * @param xml//www  . j  a v  a2s.c om
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(String xml, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(new ByteArrayInputStream(xml.getBytes()));
}

From source file:org.castor.jaxb.allannotations.XmlValueTest.java

@Test
public void testFullCycle() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(ForXmlValue.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    ForXmlValue fxv = new ForXmlValue();
    fxv.content = new BigDecimal(4711);
    m.marshal(fxv, sw);/*ww  w  .  j  av a2s. co m*/
    System.out.println(sw.toString());
}

From source file:it.polimi.modaclouds.qos_models.util.XMLHelper.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(URL xmlUrl, Class<T> targetClass) throws JAXBException, SAXException {
    // SchemaFactory schemaFactory =
    // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // Schema schema = schemaFactory.newSchema();
    Unmarshaller unmarshaller = JAXBContext.newInstance(targetClass).createUnmarshaller();
    // unmarshaller.setSchema(schema);
    T object = (T) unmarshaller.unmarshal(xmlUrl);
    return object;
}