List of usage examples for javax.xml.bind JAXBContext newInstance
public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException
From source file:Main.java
public static <T> T unmarshal(Reader r, Class<T> clazz) throws JAXBException, XMLStreamException, FactoryConfigurationError { JAXBContext context = s_contexts.get(clazz); if (context == null) { context = JAXBContext.newInstance(clazz); s_contexts.put(clazz, context);/*from w w w . ja va 2 s .c o m*/ } ValidationEventCollector valEventHndlr = new ValidationEventCollector(); XMLStreamReader xmlsr = XMLInputFactory.newFactory().createXMLStreamReader(r); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setSchema(null); unmarshaller.setEventHandler(valEventHndlr); JAXBElement<T> elem = null; try { elem = unmarshaller.unmarshal(xmlsr, clazz); } catch (Exception e) { if (e instanceof JAXBException) { throw (JAXBException) e; } else { throw new UnmarshalException(e.getMessage(), e); } } if (valEventHndlr.hasEvents()) { for (ValidationEvent valEvent : valEventHndlr.getEvents()) { if (valEvent.getSeverity() != ValidationEvent.WARNING) { // throw a new Unmarshall Exception if there is a parsing error String msg = MessageFormat.format("Line {0}, Col: {1}: {2}", valEvent.getLocator().getLineNumber(), valEvent.getLocator().getColumnNumber(), valEvent.getLinkedException().getMessage()); throw new UnmarshalException(msg, valEvent.getLinkedException()); } } } return elem.getValue(); }
From source file:Main.java
public static <T> T fromXml(String xml, Class<T> type) { if (xml == null || xml.trim().equals("")) { return null; }/*from w ww . j a v a 2 s.c om*/ JAXBContext jc = null; Unmarshaller u = null; T object = null; try { jc = JAXBContext.newInstance(type); u = jc.createUnmarshaller(); object = (T) u.unmarshal(new ByteArrayInputStream(xml.getBytes(ENCODING))); } catch (JAXBException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } return object; }
From source file:Main.java
/** * Writes the content of the specified object into the specified XML file. * @param filename Path to the XML file. * @param content Content as an object of the specified class. * @param typeParameterClass Class of the object with the content. * @return File.//from w w w . j a va 2 s. c o m */ public static <T> File write(File file, T content, Class<T> typeParameterClass) { try { JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(content, file); } catch (JAXBException e) { e.printStackTrace(); } return file; }
From source file:Main.java
/** * Converts an input XML reader into the given type. * //w w w . j a v a 2 s. co m * @param <T> * the type to parse the XML into * @param xml * the XML to convert * @param clazz * the type to parse the XML into * @return the xml data converted into the specified type * @throws JAXBException * XML Exception thrown if the conversion failed */ @SuppressWarnings("unchecked") public static synchronized <T> T fromXml(Reader xml, Class<T> clazz) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(xml); }
From source file:net.firejack.aws.license.LicenseHelper.java
public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException { signature(license);/*from ww w. j av a2 s . c o m*/ File tmp = new File("/tmp/", license.getName() + ".xml"); tmp.getParentFile().mkdirs(); tmp.createNewFile(); FileOutputStream stream = new FileOutputStream(tmp); JAXBContext jaxbContext = JAXBContext.newInstance(License.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(license, stream); stream.close(); return tmp; }
From source file:Main.java
public static String jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) { // Make sure we are given the correct input. if (xmlClass == null || jaxbElement == null) { return null; }//w w w .ja va2 s .co m // We will write the XML encoding into a string. StringWriter writer = new StringWriter(); String result; try { // We will use JAXB to marshal the java objects. final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass); // Marshal the object. Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(jaxbElement, writer); result = writer.toString(); } catch (Exception e) { // Something went wrong so get out of here. return null; } finally { try { writer.close(); } catch (IOException ex) { } } // Return the XML string. return result; }
From source file:Main.java
/** * Marshal a object of//from ww w.j av a 2 s. co m * <code>classItem</code> from the xmlResponse * <code>String</code>. * * @param xmlResponse <code>String</code> that represents the object to be * marshal. * @param classItem <code>String</code> of the returns object. * @return a object of <code>classItem</code>. * @throws JAXBException throw trying to marshal. */ public static Object unmarshalFromString(String xmlResponse, String classItem) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(classItem); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = null; try { reader = new StringReader(xmlResponse); return jaxbUnmarshaller.unmarshal(reader); } finally { if (reader != null) reader.close(); } }
From source file:eu.modaclouds.sla.mediator.Utils.java
public static <E> E load(Class<E> clazz, InputStream is) throws JAXBException { JAXBContext jaxbContext;/*from w w w.j av a2 s . c o m*/ Unmarshaller jaxbUnmarshaller; jaxbContext = JAXBContext.newInstance(clazz); jaxbUnmarshaller = jaxbContext.createUnmarshaller(); @SuppressWarnings("unchecked") E result = (E) jaxbUnmarshaller.unmarshal(is); return result; }
From source file:io.apiman.test.common.util.TestUtil.java
/** * Loads a test plan from a classpath resource. * @param resourcePath//from ww w .j av a 2 s . c o m * @param cl */ public static final TestPlan loadTestPlan(String resourcePath, ClassLoader cl) { try { URL url = cl.getResource(resourcePath); if (url == null) throw new RuntimeException("Test Plan not found: " + resourcePath); //$NON-NLS-1$ JAXBContext jaxbContext = JAXBContext.newInstance(TestPlan.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); TestPlan plan = (TestPlan) jaxbUnmarshaller.unmarshal(url.openStream()); return plan; } catch (Throwable e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Method to write an XML object in the given filePath. * //from w w w.j a v a 2s . c o m * @param <S> * The type of object that needs to written in XML file. * @param filePath * Path of file where xml object gets written * @param object * The Object that needs to be written in XML file. * @throws Exception */ public static <S> void writeXMLObject(String filePath, S object) throws Exception { // java XML context object. JAXBContext jc = JAXBContext.newInstance(object.getClass()); // file. File file = new File(filePath); // Creating marshaller Marshaller marshaller = jc.createMarshaller(); // Setting output format marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshalling object. marshaller.marshal(object, file); }