2 * Copyright (C) 2015 IBM, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.neutron.spi;
11 import java.io.StringReader;
13 import java.util.HashMap;
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.Unmarshaller;
20 import javax.xml.transform.stream.StreamSource;
22 import org.eclipse.persistence.jaxb.JAXBContextProperties;
23 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
25 public class JaxbTestHelper {
27 @SuppressWarnings({ "rawtypes", "unchecked" })
28 public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
29 Class c = schemaObject.getClass();
30 Class[] types = new Class[1];
32 Map<String, String> namespacePrefixMapper = new HashMap<String, String>(3);
33 namespacePrefixMapper.put("router", "router");
34 namespacePrefixMapper.put("provider", "provider");
35 namespacePrefixMapper.put("binding", "binding");
36 Map<String, Object> jaxbProperties = new HashMap<String, Object>(2);
37 jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
38 jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
39 jaxbProperties.put(JAXBContextProperties.JSON_NAMESPACE_SEPARATOR, ':');
40 jaxbProperties.put(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
41 JAXBContext jc = JAXBContext.newInstance(types, jaxbProperties);
43 Unmarshaller unmarshaller = jc.createUnmarshaller();
44 unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
46 StringReader reader = new StringReader(json);
47 StreamSource stream = new StreamSource(reader);
48 return unmarshaller.unmarshal(stream, c).getValue();