Example usage for javax.xml.bind JAXBContext createUnmarshaller

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

Introduction

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

Prototype

public abstract Unmarshaller createUnmarshaller() throws JAXBException;

Source Link

Document

Create an Unmarshaller object that can be used to convert XML data into a java content tree.

Usage

From source file:net.ageto.gyrex.impex.console.internal.ImpexConsoleCommands.java

/**
 * Adds xml process configuration from file
 *
 * @param interpreter//from w  ww. ja v  a  2  s  .c om
 * @param context
 * @param processXmlFileName
 */
public static boolean addProcessFromFile(IRuntimeContext context, final String processXmlFileName) {
    final File processXmlFile = new File(processXmlFileName);
    if (!processXmlFile.isFile()) {
        System.err.println("ERROR: file does not exist " + processXmlFileName);
        return false;
    }

    ProcessConfig process = null;
    if (processXmlFileName != null) {

        try {
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(ProcessConfig.class);
            final Unmarshaller u = jaxbContext.createUnmarshaller();
            // create process instance from xml
            process = (ProcessConfig) u.unmarshal(processXmlFile);

            process.getId();
        } catch (final JAXBException e) {
            System.err.println(e);
            return false;
        }
    }

    // store process into database
    final IProcessConfigManager processManager = context.get(IProcessConfigManager.class);
    processManager.persist(process);

    System.out.println(processXmlFileName + " file processed successfully.");

    return true;
}

From source file:org.bremersee.sms.ExtensionUtils.java

/**
 * Transforms a XML node into an object.
 * /*w w  w  . j  ava  2s . co m*/
 * @param node
 *            the XML node
 * @param valueType
 *            the class of the target object
 * @param jaxbContext
 *            the {@link JAXBContext} (can be null)
 * @return the target object
 * @throws JAXBException
 *             if transformation fails
 */
public static <T> T xmlNodeToObject(Node node, Class<T> valueType, JAXBContext jaxbContext)
        throws JAXBException {
    if (node == null) {
        return null;
    }
    Validate.notNull(valueType, "valueType must not be null");
    if (jaxbContext == null) {
        jaxbContext = getJaxbContext(valueType);
    }
    return valueType.cast(jaxbContext.createUnmarshaller().unmarshal(node));
}

From source file:org.eclipse.mylyn.internal.hudson.core.client.RestfulHudsonClient.java

public static <T> T unmarshal(Node node, Class<T> clazz) throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(clazz);

    Unmarshaller unmarshaller = ctx.createUnmarshaller();

    JAXBElement<T> hudsonElement = unmarshaller.unmarshal(node, clazz);
    return hudsonElement.getValue();
}

From source file:com.evolveum.midpoint.pwdfilter.opendj.PasswordPusher.java

@SuppressWarnings("unchecked")
private static <T> T unmarshallResouce(String path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = ModelClientUtil.instantiateJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    InputStream is = null;//from   w w w. j av  a 2 s  . c o  m
    JAXBElement<T> element = null;
    try {
        is = PasswordPusher.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<T>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}

From source file:Main.java

public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    Object obj = null;//from w ww. ja  v a2 s  .  c o  m
    JAXBContext jc = JAXBContext.newInstance(cls);

    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));

        JAXBResult result = new JAXBResult(jc);
        transformer.transform(source, result);
        obj = result.getResult();
    } else {
        obj = jc.createUnmarshaller().unmarshal(source);
    }
    return obj;
}

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

public static Object unmarshallFromString(Class destClass, String xmlStr) {
    try {//w  ww .ja v  a  2 s  .  c o m
        JAXBContext jaxbContext = JAXBContext.newInstance(destClass);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(xmlStr);
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

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

/**
 * JAXB object translation methods      
 *///from   w w w. j  a v  a2s .  c o m

public static Object transformXmlObj2XmlObj(Class sourceClass, Class destClass, Object source) {
    try {
        JAXBContext sourceContext = JAXBContext.newInstance(sourceClass);
        JAXBSource jaxbSource = new JAXBSource(sourceContext, source);
        JAXBContext destContext = JAXBContext.newInstance(destClass);
        Unmarshaller unmarshaller = destContext.createUnmarshaller();
        return unmarshaller.unmarshal(jaxbSource);
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:com.impetus.ankush.agent.utils.XMLUtils.java

/**
 * Method to get an XML Object for the given class type from the provided
 * XML configuration file .// w  w  w  .j  a  va  2  s .c o  m
 * 
 * @param <S>
 *            The class name for which XML object needs to be returned.
 * @param filePath
 *            File Path of XML file.
 * @param className
 *            Class name of returning object.
 * @return The Object of provided class
 * @throws Exception
 */
public static <S> S getXMLObject(String filePath, Class<S> className) throws Exception {
    // java XML context object.
    JAXBContext jc = JAXBContext.newInstance(className);
    // file.
    File file = new File(filePath);
    if (file.exists()) {
        // if file exists read the file content.
        String fileContent = FileUtils.readFileToString(file);
        // if not empty then unmarshal the object.
        if (!fileContent.isEmpty()) {
            // Creating unmarshaller
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            // Getting component services
            S object = (S) unmarshaller.unmarshal(file);
            // returning object.
            return object;
        }
    }
    return null;
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(docClass);
    Unmarshaller u = jc.createUnmarshaller();
    return (T) u.unmarshal(inputStream);
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static Object unmarshal(InputStream inputStream, Class... docClass) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(docClass);
    Unmarshaller u = jc.createUnmarshaller();
    return u.unmarshal(inputStream);
}