List of usage examples for javax.xml.bind Unmarshaller unmarshal
public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;
From source file:Main.java
/** * Unmarshal XML data from XML string using XSD string and return the resulting JAXB content tree * * @param dummyCtxObject/* w w w . j av a 2s. c om*/ * Dummy contect object for creating related JAXB context * @param strXML * XML * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshalling(Object dummyCtxObject, String strXML, String strXSD) throws Exception { if (dummyCtxObject == null) { throw new RuntimeException("No dummy context objekt (null)!"); } if (strXML == null) { throw new RuntimeException("No XML document (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD document (null)!"); } Object unmarshalledObject = null; StringReader readerXSD = null; StringReader readerXML = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); readerXSD = new StringReader(strXSD); readerXML = new StringReader(strXML); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(readerXSD)); unmarshaller.setSchema(schema); unmarshalledObject = unmarshaller.unmarshal(new StreamSource(readerXML)); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (readerXSD != null) { readerXSD.close(); readerXSD = null; } if (readerXML != null) { readerXML.close(); readerXML = null; } } return unmarshalledObject; }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
public static Object unmarshallFromString(Class destClass, String xmlStr) { try {/*from ww w .j a v a2 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:energy.usef.core.util.XMLUtil.java
/** * Converts xml to an object after optional validation against the xsd. * * @param xml xml string/*from w ww .j a va 2 s . c om*/ * @param validate true when the xml needs to be validated * @return object corresponding to this xml */ public static Object xmlToMessage(String xml, boolean validate) { try (InputStream is = IOUtils.toInputStream(xml, UTF_8)) { Unmarshaller unmarshaller = CONTEXT.createUnmarshaller(); if (validate) { // setup xsd validation SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(XMLUtil.class.getClassLoader().getResource(MESSAGING_XSD_FILE)); unmarshaller.setSchema(schema); } return unmarshaller.unmarshal(is); } catch (JAXBException | IOException e) { LOGGER.error(e.getMessage(), e); throw new TechnicalException("Invalid XML content: " + e.getMessage(), e); } catch (SAXException e) { LOGGER.error(e.getMessage(), e); throw new TechnicalException("Unable to read XSD schema", e); } }
From source file:com.sap.prd.mobile.ios.mios.VersionInfoManager.java
private static Dependency parseOldVersionsWithoutSchema(File f) throws JAXBException { final JAXBContext context = JAXBContext .newInstance(com.sap.prd.mobile.ios.mios.versioninfo.v_1_2_0.Versions.class); final Unmarshaller unmarshaller = context.createUnmarshaller(); final com.sap.prd.mobile.ios.mios.versioninfo.v_1_2_0.Versions versions = (com.sap.prd.mobile.ios.mios.versioninfo.v_1_2_0.Versions) unmarshaller .unmarshal(f);/* w ww . j a v a 2s.co m*/ final Coordinates coordinates = new Coordinates(); coordinates.setGroupId(versions.getCoordinates().getGroupId()); coordinates.setArtifactId(versions.getCoordinates().getArtifactId()); coordinates.setVersion(versions.getCoordinates().getVersion()); final SCM scm = new SCM(); scm.setConnection(versions.getScm().getConnection()); scm.setRevision(versions.getScm().getRevision()); final Dependency dependency = new Dependency(); dependency.setCoordinates(coordinates); dependency.setScm(scm); if (versions.getDependencies() != null) { for (com.sap.prd.mobile.ios.mios.versioninfo.v_1_2_0.Dependency d : versions.getDependencies()) { marshalDependenciesVersionsWithoutSchema(dependency, d); } } return dependency; }
From source file:Main.java
/** * Unmarshal XML data from XML file path using XSD from file path and return the resulting JAXB content tree * * @param dummyCtxObject/*from www . j a v a 2s.com*/ * Dummy contect object for creating related JAXB context * @param strXMLFilePath * XML file path * @param strXSDFilePath * XSD file path * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromFiles(Object dummyCtxObject, String strXMLFilePath, String strXSDFilePath) throws Exception { if (dummyCtxObject == null) { throw new RuntimeException("No dummy context object (null)!"); } if (strXMLFilePath == null) { throw new RuntimeException("No XML file path (null)!"); } if (strXSDFilePath == null) { throw new RuntimeException("No XSD file path (null)!"); } Object unmarshalledObject = null; FileInputStream fis = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); // unmarshaller.setValidating(true); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new java.io.File(strXSDFilePath)); unmarshaller.setSchema(schema); // register schema for validation fis = new FileInputStream(strXMLFilePath); unmarshalledObject = unmarshaller.unmarshal(fis); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (fis != null) { fis.close(); fis = null; } } return unmarshalledObject; }
From source file:com.sap.prd.mobile.ios.mios.VersionInfoManager.java
private static Dependency parseOldDependency(File f) throws JAXBException { final JAXBContext context = JAXBContext .newInstance(com.sap.prd.mobile.ios.mios.versioninfo.v_0_0_0.Versions.class); final Unmarshaller unmarshaller = context.createUnmarshaller(); final com.sap.prd.mobile.ios.mios.versioninfo.v_0_0_0.Versions versions = (com.sap.prd.mobile.ios.mios.versioninfo.v_0_0_0.Versions) unmarshaller .unmarshal(f);/*w ww . ja v a2 s . co m*/ final Coordinates coordinates = new Coordinates(); coordinates.setGroupId(versions.getCoordinates().getGroupId()); coordinates.setArtifactId(versions.getCoordinates().getArtifactId()); coordinates.setVersion(versions.getCoordinates().getVersion()); final SCM scm = new SCM(); scm.setConnection("scm:perforce:" + versions.getScm().getRepository() + ":" + getDepotPath(versions.getScm().getPath())); scm.setRevision(versions.getScm().getSnapshotId()); final Dependency dependency = new Dependency(); dependency.setCoordinates(coordinates); dependency.setScm(scm); if (versions.getDependencies() != null) { for (com.sap.prd.mobile.ios.mios.versioninfo.v_0_0_0.Dependency d : versions.getDependencies()) { marshalOldDependencies(dependency, d); } } return dependency; }
From source file:cosmos.results.integration.CosmosIntegrationSetup.java
@SuppressWarnings({ "rawtypes", "unchecked" }) protected static MediaWikiType loadWiki(int num) throws Exception { initializeJaxb();/* ww w .j av a 2 s . com*/ Unmarshaller unmarshaller = context.createUnmarshaller(); InputStream is = CosmosIntegrationSetup.class.getResourceAsStream(ARTICLE_BASE + num + ARTICLE_SUFFIX); Assert.assertNotNull(is); GZIPInputStream gzip = new GZIPInputStream(is); Object o = unmarshaller.unmarshal(gzip); Assert.assertEquals(JAXBElement.class, o.getClass()); Assert.assertEquals(MediaWikiType.class, ((JAXBElement) o).getDeclaredType()); JAXBElement<MediaWikiType> jaxb = (JAXBElement<MediaWikiType>) o; return jaxb.getValue(); }
From source file:edu.kit.dama.rest.util.RestClientUtils.java
/** * Deserializes an entity from a stream provided by a ClientResponse. * <b>Attention:</b>May throw a DeserializationException if the * deserialization fails for some reason. * * @param <C> entity class//from w ww . j a va 2s . c om * @param pEntityClass An array of classes needed to deserialize the entity. * @param pResponse The response which provides the entity input stream. * * @return The object. */ public static <C> C createObjectFromStream(final Class[] pEntityClass, final ClientResponse pResponse) { C returnValue = null; if (pEntityClass != null) { LOGGER.debug("createObjectFromStream"); try { Unmarshaller unmarshaller = org.eclipse.persistence.jaxb.JAXBContext.newInstance(pEntityClass) .createUnmarshaller(); returnValue = (C) unmarshaller.unmarshal(getInputStream(pResponse.getEntityInputStream())); if (LOGGER.isDebugEnabled()) { Marshaller marshaller = org.eclipse.persistence.jaxb.JAXBContext.newInstance(pEntityClass) .createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); marshaller.marshal(returnValue, sw); LOGGER.debug("createObjectFromStream: " + sw.toString()); } } catch (JAXBException ex) { throw new DeserializationException("Failed to deserialize object from response " + pResponse, ex); } } else { LOGGER.debug("No response expected!"); } return returnValue; }
From source file:com.hpe.application.automation.tools.octane.executor.UFTTestDetectionService.java
public static UFTTestDetectionResult readDetectionResults(Run run) { File file = getReportXmlFile(run); try {//www . j ava 2s. co m JAXBContext context = JAXBContext.newInstance(UFTTestDetectionResult.class); Unmarshaller m = context.createUnmarshaller(); return (UFTTestDetectionResult) m.unmarshal(new FileReader(file)); } catch (JAXBException | FileNotFoundException e) { return null; } }
From source file:com.indivica.olis.Driver.java
public static void readResponseFromXML(HttpServletRequest request, String olisResponse) { olisResponse = olisResponse.replaceAll("<Content", "<Content xmlns=\"\" "); olisResponse = olisResponse.replaceAll("<Errors", "<Errors xmlns=\"\" "); try {/*from w ww . j ava 2s. co m*/ DocumentBuilderFactory.newInstance().newDocumentBuilder(); SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Source schemaFile = new StreamSource( new File(OscarProperties.getInstance().getProperty("olis_response_schema"))); factory.newSchema(schemaFile); JAXBContext jc = JAXBContext.newInstance("ca.ssha._2005.hial"); Unmarshaller u = jc.createUnmarshaller(); @SuppressWarnings("unchecked") Response root = ((JAXBElement<Response>) u.unmarshal(new InputSource(new StringReader(olisResponse)))) .getValue(); if (root.getErrors() != null) { List<String> errorStringList = new LinkedList<String>(); // Read all the errors ArrayOfError errors = root.getErrors(); List<ca.ssha._2005.hial.Error> errorList = errors.getError(); for (ca.ssha._2005.hial.Error error : errorList) { String errorString = ""; errorString += "ERROR " + error.getNumber() + " (" + error.getSeverity() + ") : " + error.getMessage(); MiscUtils.getLogger().debug(errorString); ArrayOfString details = error.getDetails(); if (details != null) { List<String> detailList = details.getString(); for (String detail : detailList) { errorString += "\n" + detail; } } errorStringList.add(errorString); } if (request != null) request.setAttribute("errors", errorStringList); } else if (root.getContent() != null) { if (request != null) request.setAttribute("olisResponseContent", root.getContent()); } } catch (Exception e) { MiscUtils.getLogger().error("Couldn't read XML from OLIS response.", e); LoggedInInfo loggedInInfo = LoggedInInfo.getLoggedInInfoFromSession(request); notifyOlisError(loggedInInfo.getLoggedInProvider(), "Couldn't read XML from OLIS response." + "\n" + e); } }