Example usage for javax.xml.bind Unmarshaller setProperty

List of usage examples for javax.xml.bind Unmarshaller setProperty

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller setProperty.

Prototype

public void setProperty(String name, Object value) throws PropertyException;

Source Link

Document

Set the particular property in the underlying implementation of Unmarshaller .

Usage

From source file:com.dnastack.bob.rest.BasicTest.java

public static Object readObject(Class c, String url) throws JAXBException, MalformedURLException {
    JAXBContext jc = JAXBContext.newInstance(c);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
    unmarshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    StreamSource source = new StreamSource(url);
    JAXBElement jaxbElement = unmarshaller.unmarshal(source, c);

    return jaxbElement.getValue();
}

From source file:Main.java

private static Unmarshaller getJaxbUnmarshaller(Class<?> classesToBeBound,
        Map<String, Object> unmarshallerProps) throws Exception {
    Unmarshaller unmarshaller = JAXB_UNMARSHALLER_CACHE.get(classesToBeBound);
    if (unmarshaller == null) {
        JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
        unmarshaller = jaxbContext.createUnmarshaller();
        if (unmarshallerProps != null && unmarshallerProps.size() > 0) {
            for (Map.Entry<String, Object> prop : unmarshallerProps.entrySet()) {
                unmarshaller.setProperty(prop.getKey(), prop.getValue());
            }//from  www. j a  v  a2  s. c  o m
        }
        JAXB_UNMARSHALLER_CACHE.put(classesToBeBound, unmarshaller);
    }
    return unmarshaller;
}

From source file:org.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

/**
 * Creates a new {@link Unmarshaller} for the given class.
 *
 * @param clazz the class to create the unmarshaller for
 * @return the {@code Unmarshaller}/*from   w  ww  .j  a  v  a2s. co  m*/
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Unmarshaller createWrapperUnmarshaller(Class clazz) throws JAXBException {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setProperty("com.sun.xml.bind.ObjectFactory", new ObjectFactoryWrapper());
        return unmarshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException(
                "Could not create Unmarshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}

From source file:com.bitplan.jaxb.JaxbFactory.java

/**
 * get an instance of T for the given xml string
 * //from  ww w. j a va  2 s.c om
 * @param xml
 *          - the xml representation of the <T> instance
 * @return T
 * @throws Exception
 *           - if the conversion fails
 */
public T fromXML(String xml) throws Exception {
    Unmarshaller u = this.getUnmarshaller();
    u.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    T result = this.fromString(u, xml);
    return result;
}

From source file:com.bitplan.jaxb.JaxbFactory.java

/**
 * get an instance of T for the given json string
 * //from   w w  w  .  j a  v  a2  s.com
 * @param json
 *          - the json representation of the <T> instance
 * @return T
 * @throws Exception
 */
public T fromJson(String json) throws Exception {
    Unmarshaller u = this.getUnmarshaller();
    u.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    T result = this.fromString(u, json);
    return result;
}

From source file:com.qmetry.qaf.automation.integration.qmetry.qmetry6.scheduler.Qmetry6SchedulerFilter.java

public List<IMethodInstance> applyQmetrySecheduledTCsFilter(List<IMethodInstance> list, ITestContext context,
        String jsonFile) {/*  ww w.  ja v a 2 s .  c o  m*/

    JAXBContext jc;

    List<IMethodInstance> filteredList = new ArrayList<IMethodInstance>();

    tcLst = new ArrayList<Testcase>();
    try {

        jc = JAXBContext.newInstance(Schedule.class);

        Unmarshaller unmarshell = jc.createUnmarshaller();

        // Set the Unmarshaller media type to JSON or XML
        unmarshell.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");

        // Set it to true if you need to include the JSON root element in
        // the
        // JSON input
        unmarshell.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);

        JAXBElement<Schedule> scheduleUnmarshal = null;

        StreamSource json = new StreamSource(new File(jsonFile));
        scheduleUnmarshal = unmarshell.unmarshal(json, Schedule.class);

        Schedule schedules = scheduleUnmarshal.getValue();

        List<Testcase> testcase = schedules.getTestcases();

        @SuppressWarnings("unused")
        Qmetry6RestClient wsUtil = Qmetry6RestClient.getInstance();
        QMetryRestWebservice integration = Qmetry6RestClient.getIntegration();
        integration.setPlatform(String.valueOf(schedules.getPlatformId()));
        integration.setSuite(String.valueOf(schedules.getTestsuiteId()));

        tcMap = new HashMap<String, Testcase>();

        ConfigurationManager.getBundle().setProperty(ApplicationProperties.INTEGRATION_PARAM_QMETRY_PRJ.key,
                String.valueOf(schedules.getProjectId()));
        ConfigurationManager.getBundle().setProperty(ApplicationProperties.INTEGRATION_PARAM_QMETRY_REL.key,
                String.valueOf(schedules.getReleaseId()));
        ConfigurationManager.getBundle().setProperty(ApplicationProperties.INTEGRATION_PARAM_QMETRY_CYCLE.key,
                String.valueOf(schedules.getBuildId()));
        ConfigurationManager.getBundle().setProperty(ApplicationProperties.INTEGRATION_PARAM_QMETRY_SUIT.key,
                String.valueOf(schedules.getTestsuiteId()));
        ConfigurationManager.getBundle().setProperty(
                ApplicationProperties.INTEGRATION_PARAM_QMETRY_SUITERUNID.key,
                String.valueOf(schedules.getTestsuiteRunId()));
        ConfigurationManager.getBundle().setProperty(ApplicationProperties.INTEGRATION_PARAM_QMETRY_DROP.key,
                String.valueOf(schedules.getDropId()));
        ConfigurationManager.getBundle().setProperty(
                ApplicationProperties.INTEGRATION_PARAM_QMETRY_PLATFORM.key,
                String.valueOf(schedules.getPlatformId()));

        Iterator<Testcase> testcaseIterator = testcase.iterator();
        while (testcaseIterator.hasNext()) {
            Testcase tc = testcaseIterator.next();
            String xmltcid = String.valueOf(tc.getTestcaseId());
            String xmlscriptname = tc.getTestcaseName();
            logger.info("Qmetry6 scheduled TC: " + xmltcid + " " + xmlscriptname);
            tcLst.add(tc);
            Iterator<IMethodInstance> iter = list.iterator();

            while (iter.hasNext()) {
                IMethodInstance iMethodInstance = iter.next();
                TestNGScenario method = (TestNGScenario) iMethodInstance.getMethod();
                logger.debug("SchedulerFilter testNG method: " + method);
                if (isScriptNameMaching(method, tc) || isRunIdMaching(method, tc)
                        || isTCIDMaching(method, tc)) {

                    logger.info("SchedulerFilter including testNG method: " + method);
                    filteredList.add(iMethodInstance);
                    tcMap.put(method.getSignature(), tc);
                    break;
                }
            }
        }

        Map<String, String> params = context.getCurrentXmlTest().getAllParameters();
        int platform = Integer.parseInt(String.valueOf(schedules.getPlatformId()));
        String qmetryplatform = platform == 116 ? "*iehta" : platform == 118 ? "*googlechrome" : "*firefox";
        params.put("browser", qmetryplatform);
        context.getCurrentXmlTest().setParameters(params);
        return filteredList;
    } catch (Exception e) {
        logger.error(e);
    }
    for (IMethodInstance l : list) {
        logger.info(l.getMethod());
    }
    return list;
}

From source file:org.codice.ddf.parser.xml.XmlParser.java

private <T> T unmarshal(ParserConfigurator configurator, Function<Unmarshaller, T> func)
        throws ParserException {
    JAXBContext jaxbContext = getContext(configurator.getContextPath(), configurator.getClassLoader());

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {/* w w  w . ja  va  2 s. co  m*/
        Thread.currentThread().setContextClassLoader(configurator.getClassLoader());

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        if (configurator.getAdapter() != null) {
            unmarshaller.setAdapter(configurator.getAdapter());
        }
        if (configurator.getHandler() != null) {
            unmarshaller.setEventHandler(configurator.getHandler());
        }
        for (Map.Entry<String, Object> propRow : configurator.getProperties().entrySet()) {
            unmarshaller.setProperty(propRow.getKey(), propRow.getValue());
        }

        return func.apply(unmarshaller);
    } catch (RuntimeException e) {
        LOGGER.error("Error unmarshalling ", e);
        throw new ParserException("Error unmarshalling", e);
    } catch (JAXBException e) {
        LOGGER.error("Error unmarshalling ", e);
        throw new ParserException("Error unmarshalling", e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
}

From source file:org.eclipse.om2m.datamapping.jaxb.Mapper.java

/**
 * Converts a resource XML representation data into resource Java object.
 * //from  w  w  w.jav  a2  s  .c  o  m
 * @param representation
 *            - resource XML representation
 * @return resource Java object
 */
@Override
public Object stringToObj(String representation) {
    if (representation.isEmpty()) {
        return null;
    }
    StringReader stringReader = new StringReader(representation);
    try {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, mediaType);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);

        return unmarshaller.unmarshal(stringReader);
    } catch (JAXBException e) {
        LOGGER.error("JAXB unmarshalling error!", e);
    }
    return null;
}

From source file:org.geoserver.wfs.notification.TriggerFileWatcher.java

@Override
protected Map<QName, List<Trigger>> doLoad(URLConnection conn) {
    Triggers triggers = null;//  w  w  w . ja v a2 s . c  o m
    InputStream inputStream = null;
    try {
        Unmarshaller unm = CTX.createUnmarshaller();
        try {
            unm.setProperty("com.sun.xml.bind.ObjectFactory", new ObjectFactoryEx());
        } catch (PropertyException pe) {
            try {
                unm.setProperty("com.sun.xml.internal.bind.ObjectFactory", new ObjectFactoryEx());
            } catch (PropertyException pe2) {

            }
        }
        inputStream = conn.getInputStream();
        triggers = unm.unmarshal(new StreamSource(inputStream), Triggers.class).getValue();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    if (triggers == null)
        return Collections.EMPTY_MAP;

    final HashMap<QName, List<Trigger>> map = new HashMap<QName, List<Trigger>>(triggers.getFeature().size());
    for (Feature f : triggers.getFeature())
        map.put(f.getType(), f.getTrigger());
    return Collections.unmodifiableMap(map);
}