Example usage for javax.xml.bind UnmarshalException getMessage

List of usage examples for javax.xml.bind UnmarshalException getMessage

Introduction

In this page you can find the example usage for javax.xml.bind UnmarshalException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:jails.http.converter.xml.Jaxb2RootElementHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {// ww  w. j  a  v  a 2  s . c  om
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(),
                ex);

    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:com.rabidgremlin.legalbeagle.util.HttpHelper.java

private JAXBElement<Model> unMarshallPom(String pom) {
    try {/*ww w  .jav a 2  s .c o  m*/
        return (JAXBElement<Model>) u.unmarshal(new StringReader(pom));
    } catch (javax.xml.bind.UnmarshalException e) {
        // System.out.println(e);
        // return null;

        if (e.getMessage().startsWith("unexpected element (uri:\"\", local:\"project\")")) {
            try {
                pom = pom.replaceFirst("<project>", "<project xmlns=\"http://maven.apache.org/POM/4.0.0\">");

                // System.out.println("***POM is " + pom);

                return (JAXBElement<Model>) u.unmarshal(new StringReader(pom));
            } catch (Exception ex) {
                e.printStackTrace();
                return null;
            }
        } else {
            e.printStackTrace();
            return null;
        }

    } catch (JAXBException e) {
        return null;
    }

}

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

@Override
protected Object readFromSource(Class<? extends Object> clazz, HttpHeaders headers, Source source)
        throws IOException {
    try {//from  w  w w  .  j  a  va  2  s . c o m
        Unmarshaller unmarshaller = createWrapperUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<? extends Object> jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(),
                ex);

    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.alfresco.repo.publishing.JaxbHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {/*from   w ww .  java2  s.  c om*/
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(source, clazz);
            return jaxbElement.getValue();
        }
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(),
                ex);

    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.docx4j.openpackaging.io.LoadFromZipNG.java

/**
 * Get a Part (except a relationships part), but not its relationships part
 * or related parts.  Useful if you need quick access to just this part.
 * This can be called directly from outside the library, in which case 
 * the Part will not be owned by a Package until the calling code makes it so.  
 * @see  To get a Part and all its related parts, and add all to a package, use
 * getPart.//from   www.  j a  v a  2  s . com
 * @param partByteArrays
 * @param ctm
 * @param resolvedPartUri
 * @param rel
 * @return
 * @throws Docx4JException including if result is null
 */
public static Part getRawPart(HashMap<String, ByteArray> partByteArrays, ContentTypeManager ctm,
        String resolvedPartUri, Relationship rel) throws Docx4JException {

    Part part = null;

    InputStream is = null;
    try {
        try {
            log.debug("resolved uri: " + resolvedPartUri);
            is = getInputStreamFromZippedPart(partByteArrays, resolvedPartUri);

            // Get a subclass of Part appropriate for this content type   
            // This will throw UnrecognisedPartException in the absence of
            // specific knowledge. Hence it is important to get the is
            // first, as we do above.
            part = ctm.getPart("/" + resolvedPartUri, rel);

            log.info("ctm returned " + part.getClass().getName());

            if (part instanceof org.docx4j.openpackaging.parts.ThemePart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).setJAXBContext(Context.jcThemePart);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsCorePart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).setJAXBContext(Context.jcDocPropsCore);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsCustomPart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).setJAXBContext(Context.jcDocPropsCustom);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.DocPropsExtendedPart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).setJAXBContext(Context.jcDocPropsExtended);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part)
                        .setJAXBContext(Context.jcCustomXmlProperties);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.digitalsignature.XmlSignaturePart) {

                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).setJAXBContext(Context.jcXmlDSig);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) {

                // MainDocument part, Styles part, Font part etc

                //((org.docx4j.openpackaging.parts.JaxbXmlPart)part).setJAXBContext(Context.jc);
                ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).unmarshal(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) {

                log.debug("Detected BinaryPart " + part.getClass().getName());
                ((BinaryPart) part).setBinaryData(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) {

                // Is it a part we know?
                try {

                    XMLInputFactory xif = XMLInputFactory.newInstance();
                    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
                    XMLStreamReader xsr = xif.createXMLStreamReader(is);

                    Unmarshaller u = Context.jc.createUnmarshaller();
                    Object o = u.unmarshal(xsr);
                    log.debug(o.getClass().getName());

                    PartName name = part.getPartName();

                    if (o instanceof CoverPageProperties) {

                        part = new DocPropsCoverPagePart(name);
                        ((DocPropsCoverPagePart) part).setJaxbElement((CoverPageProperties) o);

                    } else if (o instanceof org.opendope.conditions.Conditions) {

                        part = new ConditionsPart(name);
                        ((ConditionsPart) part).setJaxbElement((org.opendope.conditions.Conditions) o);

                    } else if (o instanceof org.opendope.xpaths.Xpaths) {

                        part = new XPathsPart(name);
                        ((XPathsPart) part).setJaxbElement((org.opendope.xpaths.Xpaths) o);

                    } else if (o instanceof org.opendope.questions.Questionnaire) {

                        part = new QuestionsPart(name);
                        ((QuestionsPart) part).setJaxbElement((org.opendope.questions.Questionnaire) o);

                    } else if (o instanceof org.opendope.answers.Answers) {

                        part = new StandardisedAnswersPart(name);
                        ((StandardisedAnswersPart) part).setJaxbElement((org.opendope.answers.Answers) o);

                    } else if (o instanceof org.opendope.components.Components) {

                        part = new ComponentsPart(name);
                        ((ComponentsPart) part).setJaxbElement((org.opendope.components.Components) o);

                    } else if (o instanceof JAXBElement<?>
                            && XmlUtils.unwrap(o) instanceof org.docx4j.bibliography.CTSources) {
                        part = new BibliographyPart(name);
                        ((BibliographyPart) part)
                                .setJaxbElement((JAXBElement<org.docx4j.bibliography.CTSources>) o);

                    } else {

                        log.error("TODO: handle known CustomXmlPart part  " + o.getClass().getName());

                        CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                        is.reset();
                        data.setDocument(is); // Not necessarily JAXB, that's just our method name
                        ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);

                    }

                } catch (javax.xml.bind.UnmarshalException ue) {

                    log.warn("No JAXB model for this CustomXmlDataStorage part; " + ue.getMessage());

                    CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                    is.reset();
                    data.setDocument(is); // Not necessarily JAXB, that's just our method name
                    ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);
                }

            } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart) {

                //               try {
                ((XmlPart) part).setDocument(is);

                // Experimental 22/6/2011; don't fall back to binary (which we used to) 

                //               } catch (Docx4JException d) {
                //                  // This isn't an XML part after all,
                //                  // even though ContentTypeManager detected it as such
                //                  // So get it as a binary part
                //                  part = getBinaryPart(partByteArrays, ctm, resolvedPartUri);
                //                  log.warn("Could not parse as XML, so using BinaryPart for " 
                //                        + resolvedPartUri);                  
                //                  ((BinaryPart)part).setBinaryData(is);
                //               }

            } else {
                // Shouldn't happen, since ContentTypeManagerImpl should
                // return an instance of one of the above, or throw an
                // Exception.

                log.error("No suitable part found for: " + resolvedPartUri);
                part = null;
            }

        } catch (PartUnrecognisedException e) {
            log.error("PartUnrecognisedException shouldn't happen anymore!", e);
            // Try to get it as a binary part
            part = getBinaryPart(partByteArrays, ctm, resolvedPartUri);
            log.warn("Using BinaryPart for " + resolvedPartUri);

            ((BinaryPart) part).setBinaryData(is);
        }
    } catch (Exception ex) {
        // IOException, URISyntaxException
        ex.printStackTrace();
        throw new Docx4JException("Failed to getPart", ex);

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
    }

    if (part == null) {
        throw new Docx4JException(
                "cannot find part " + resolvedPartUri + " from rel " + rel.getId() + "=" + rel.getTarget());
    }

    return part;
}

From source file:org.docx4j.openpackaging.io3.Load3.java

/**
 * Get a Part (except a relationships part), but not its relationships part
 * or related parts.  Useful if you need quick access to just this part.
 * This can be called directly from outside the library, in which case 
 * the Part will not be owned by a Package until the calling code makes it so.  
 * @see  To get a Part and all its related parts, and add all to a package, use
 * getPart.//www  .  j a va  2  s.co m
 * @param partByteArrays
 * @param ctm
 * @param resolvedPartUri
 * @param rel
 * @return
 * @throws Docx4JException including if result is null
 */
public Part getRawPart(ContentTypeManager ctm, String resolvedPartUri, Relationship rel)
        throws Docx4JException {

    Part part = null;

    InputStream is = null;
    try {
        try {
            log.debug("resolved uri: " + resolvedPartUri);

            // Get a subclass of Part appropriate for this content type   
            // This will throw UnrecognisedPartException in the absence of
            // specific knowledge. Hence it is important to get the is
            // first, as we do above.
            part = ctm.getPart("/" + resolvedPartUri, rel);

            log.debug("ctm returned " + part.getClass().getName());

            if (part instanceof org.docx4j.openpackaging.parts.ThemePart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsCorePart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsCustomPart
                    || part instanceof org.docx4j.openpackaging.parts.DocPropsExtendedPart
                    || part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart
                    || part instanceof org.docx4j.openpackaging.parts.digitalsignature.XmlSignaturePart
                    || part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) {

                // Nothing to do here

            } else if (part instanceof org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) {

                log.debug("Detected BinaryPart " + part.getClass().getName());

                // Note that this is done lazily, since the below lines are commented out

                //               is = partStore.loadPart( resolvedPartUri);
                //               ((BinaryPart)part).setBinaryData(is);

            } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) {
                // ContentTypeManager initially detects them as CustomXmlDataStoragePart;
                // the below changes as necessary 

                // Is it a part we know?
                is = partStore.loadPart(resolvedPartUri);
                try {

                    XMLInputFactory xif = XMLInputFactory.newInstance();
                    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
                    XMLStreamReader xsr = xif.createXMLStreamReader(is);

                    Unmarshaller u = Context.jc.createUnmarshaller();
                    Object o = u.unmarshal(xsr);
                    log.debug(o.getClass().getName());

                    PartName name = part.getPartName();
                    if (o instanceof CoverPageProperties) {

                        part = new DocPropsCoverPagePart(name);
                        ((DocPropsCoverPagePart) part).setJaxbElement((CoverPageProperties) o);

                    } else if (o instanceof org.opendope.conditions.Conditions) {

                        part = new ConditionsPart(name);
                        ((ConditionsPart) part).setJaxbElement((org.opendope.conditions.Conditions) o);

                    } else if (o instanceof org.opendope.xpaths.Xpaths) {

                        part = new XPathsPart(name);
                        ((XPathsPart) part).setJaxbElement((org.opendope.xpaths.Xpaths) o);

                    } else if (o instanceof org.opendope.questions.Questionnaire) {

                        part = new QuestionsPart(name);
                        ((QuestionsPart) part).setJaxbElement((org.opendope.questions.Questionnaire) o);

                    } else if (o instanceof org.opendope.answers.Answers) {

                        part = new StandardisedAnswersPart(name);
                        ((StandardisedAnswersPart) part).setJaxbElement((org.opendope.answers.Answers) o);

                    } else if (o instanceof org.opendope.components.Components) {

                        part = new ComponentsPart(name);
                        ((ComponentsPart) part).setJaxbElement((org.opendope.components.Components) o);

                    } else if (o instanceof JAXBElement<?>
                            && XmlUtils.unwrap(o) instanceof org.docx4j.bibliography.CTSources) {
                        part = new BibliographyPart(name);
                        ((BibliographyPart) part)
                                .setJaxbElement((JAXBElement<org.docx4j.bibliography.CTSources>) o);

                    } else {

                        log.error("TODO: handle known CustomXmlPart part  " + o.getClass().getName());

                        CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                        is.reset();
                        data.setDocument(is); // Not necessarily JAXB, that's just our method name
                        ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);

                    }

                } catch (javax.xml.bind.UnmarshalException ue) {

                    log.warn("No JAXB model for this CustomXmlDataStorage part; " + ue.getMessage());

                    CustomXmlDataStorage data = getCustomXmlDataStorageClass().factory();
                    is.reset();
                    data.setDocument(is); // Not necessarily JAXB, that's just our method name
                    ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part).setData(data);
                }

            } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart) {

                is = partStore.loadPart(resolvedPartUri);

                //               try {
                ((XmlPart) part).setDocument(is);

                // Experimental 22/6/2011; don't fall back to binary (which we used to) 

                //               } catch (Docx4JException d) {
                //                  // This isn't an XML part after all,
                //                  // even though ContentTypeManager detected it as such
                //                  // So get it as a binary part
                //                  part = getBinaryPart(partByteArrays, ctm, resolvedPartUri);
                //                  log.warn("Could not parse as XML, so using BinaryPart for " 
                //                        + resolvedPartUri);                  
                //                  ((BinaryPart)part).setBinaryData(is);
                //               }

            } else {
                // Shouldn't happen, since ContentTypeManagerImpl should
                // return an instance of one of the above, or throw an
                // Exception.

                log.error("No suitable part found for: " + resolvedPartUri);
                part = null;
            }

        } catch (PartUnrecognisedException e) {
            log.error("PartUnrecognisedException shouldn't happen anymore!", e);
            // Try to get it as a binary part
            part = getBinaryPart(ctm, resolvedPartUri);
            log.warn("Using BinaryPart for " + resolvedPartUri);

            //            is = partStore.loadPart( resolvedPartUri);
            //            ((BinaryPart)part).setBinaryData(is);
        }
    } catch (Exception ex) {
        // IOException, URISyntaxException
        ex.printStackTrace();
        throw new Docx4JException("Failed to getPart", ex);

    } finally {
        IOUtils.closeQuietly(is);
    }

    if (part == null) {
        throw new Docx4JException(
                "cannot find part " + resolvedPartUri + " from rel " + rel.getId() + "=" + rel.getTarget());
    }

    return part;
}

From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java

/**
* Unmarshal XML data from the specified InputStream and return the
* resulting content tree. Validation event location information may be
* incomplete when using this form of the unmarshal API.
* 
* <p>//from  www . j a v  a  2 s  .  co m
* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
* 
* @param is
*            the InputStream to unmarshal XML data from
* @return the newly created root object of the java content tree
* 
* @throws JAXBException
*             If any unexpected errors occur while unmarshalling
*/
public E unmarshal(java.io.InputStream is) throws JAXBException {

    try {
        /* To avoid possible XML External Entity Injection attack,
         * we need to configure the processor.
         * 
         * We use STAX XMLInputFactory to do that.
         * 
         * createXMLStreamReader(is) is 40% slower than unmarshal(is).
         * 
         * But it seems to be the best we can do ... 
         * 
         *   org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is)
         *   unmarshal(doc)
         * 
         * ie DOM is 5x slower than unmarshal(is)
         * 
         */

        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
        XMLStreamReader xsr = xif.createXMLStreamReader(is);

        Unmarshaller u = jc.createUnmarshaller();

        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
        if (is.markSupported()) {
            // Only fail hard if we know we can restart
            eventHandler.setContinue(false);
        }
        u.setEventHandler(eventHandler);

        try {
            jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr));
        } catch (UnmarshalException ue) {

            if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) {

                /*
                   Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19]
                   Message: The entity "xxe" was referenced, but not declared.
                      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
                      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
                    */
                log.error(ue.getMessage(), ue);
                throw ue;
            }

            if (is.markSupported()) {
                // When reading from zip, we use a ByteArrayInputStream,
                // which does support this.

                log.info("encountered unexpected content; pre-processing");
                eventHandler.setContinue(true);

                try {
                    Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                    is.reset();
                    JAXBResult result = XmlUtils.prepareJAXBResult(jc);
                    XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result);
                    jaxbElement = (E) XmlUtils.unwrap(result.getResult());
                } catch (Exception e) {
                    throw new JAXBException("Preprocessing exception", e);
                }

            } else {
                log.error(ue.getMessage(), ue);
                log.error(".. and mark not supported");
                throw ue;
            }
        }

    } catch (XMLStreamException e1) {
        log.error(e1.getMessage(), e1);
        throw new JAXBException(e1);
    }

    return jaxbElement;

}

From source file:org.docx4j.XmlUtils.java

public static Object unmarshal(InputStream is, JAXBContext jc) throws JAXBException {

    // Guard against XXE
    XMLInputFactory xif = XMLInputFactory.newInstance();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
    XMLStreamReader xsr = null;//from   ww w.jav a 2s.  com
    try {
        xsr = xif.createXMLStreamReader(is);
    } catch (XMLStreamException e) {
        throw new JAXBException(e);
    }

    Object o = null;
    Unmarshaller u = jc.createUnmarshaller();

    JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
    //      if (is.markSupported()) {
    //         // Only fail hard if we know we can restart
    //         eventHandler.setContinue(false);
    //      }
    u.setEventHandler(eventHandler);
    try {
        o = u.unmarshal(xsr);
        return o;
    } catch (UnmarshalException ue) {

        if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) {

            /*
               Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19]
               Message: The entity "xxe" was referenced, but not declared.
                  at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
                  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
                */
            log.error(ue.getMessage(), ue);
            throw ue;
        }

        if (is.markSupported()) {
            // When reading from zip, we use a ByteArrayInputStream,
            // which does support this.

            log.info("encountered unexpected content; pre-processing");
            eventHandler.setContinue(true);

            try {
                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                is.reset();
                JAXBResult result = XmlUtils.prepareJAXBResult(jc);
                XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result);
                return //XmlUtils.unwrap(
                result.getResult();
            } catch (Exception e) {
                throw new JAXBException("Preprocessing exception", e);
            }

        } else {
            log.error(ue.getMessage(), ue);
            log.error(".. and mark not supported");
            throw ue;
        }
    }

}

From source file:org.intermine.modelviewer.jaxb.ConfigParser.java

/**
 * This method is a legacy of trying to persuade JAXB to read Intermine
 * files of all types, using all sorts of tricks to get it to work with
 * documents without schema declarations, documents with schema declarations,
 * documents without proper namespace declarations etc.
 * <p>It is no longer used, but remains here for the lessons learned.</p>
 * //from  ww  w .  ja va  2 s.co  m
 * @param context The type of objects expected from the XML file.
 * @param file The file to load.
 * 
 * @return The Object created as a result of reading the file.
 * Its type will depend on <code>context</code>.
 * 
 * @throws JAXBException if there is a problem when unmarshalling.
 * @throws SAXException if there is a problem parsing with SAX.
 * @throws ParserConfigurationException if there is a configuration problem with
 * the SAX system.
 * @throws IOException if there is a low level I/O problem.
 */
protected Object twoPassUnmarshall(Context context, File file)
        throws JAXBException, SAXException, ParserConfigurationException, IOException {
    Unmarshaller unmarshall = jaxbContexts.get(context).createUnmarshaller();
    //unmarshall.setSchema(schemas.get(context));

    try {
        Source source = getSource(file, null);
        return unmarshall.unmarshal(source);
    } catch (UnmarshalException e) {
        if (e.getCause() == null) {
            logger.warn("Failed to unmarshall " + file + ": " + e.getMessage());
        } else {
            try {
                throw e.getCause();
            } catch (SAXParseException e2) {
                logger.warn("Failed to unmarshall " + file + ": " + e2.getMessage());
                logger.debug("", e2);
            } catch (Throwable e2) {
                logger.warn("Unexpected root exception while unmarshalling " + file + ": "
                        + e2.getClass().getName());
                throw e;
            }
        }

        /*
         * This one would try to replace namespaces for JAXB. Unfortunately this
         * too is too strict.
         * 
        String namespace = namespaces.get(context);
                
        // Try filtering the XML by adding the appropriate namespace.
        try {
        Source source = getSource(file, namespace);
        return unmarshall.unmarshal(source);
        } catch (UnmarshalException e2) {
        // Throw the original exception - it's really the one that nags
        // about the namespace.
        throw e;
        }
        */

        return saxParse(context, file);
    }
}

From source file:org.openhab.binding.denonmarantz.internal.connector.http.DenonMarantzHttpConnector.java

@Nullable
private <T> T getDocument(String uri, Class<T> response) throws IOException {
    try {//from www . j  a  va 2  s  .  c  o  m
        String result = HttpUtil.executeUrl("GET", uri, REQUEST_TIMEOUT_MS);
        logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);

        if (StringUtils.isNotBlank(result)) {
            JAXBContext jc = JAXBContext.newInstance(response);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
            xsr = new PropertyRenamerDelegate(xsr);

            @SuppressWarnings("unchecked")
            T obj = (T) jc.createUnmarshaller().unmarshal(xsr);

            return obj;
        }
    } catch (UnmarshalException e) {
        logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
    } catch (JAXBException e) {
        logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
    } catch (XMLStreamException e) {
        logger.debug("Communication error: {}", e.getMessage());
    }

    return null;
}