List of usage examples for javax.xml.bind JAXBException JAXBException
public JAXBException(String message, Throwable exception)
From source file:org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.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 */ @Override public E unmarshal(java.io.InputStream is) throws JAXBException { try { log.debug("For " + this.getClass().getName() + ", unmarshall via binder"); // InputStream to Document org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is); // binder = jc.createBinder(); log.debug("info: " + binder.getClass().getName()); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); eventHandler.setContinue(false); binder.setEventHandler(eventHandler); try { unwrapUsually(binder, doc); // unlikely to need this in the code below } catch (Exception ue) { if (ue instanceof UnmarshalException) { // Usually.. } else { // eg java.lang.NumberFormatException log.warn(ue.getMessage(), ue); } if (is.markSupported()) { // When reading from zip, we use a ByteArrayInputStream, // which does support this. log.info("encountered unexpected content in " + this.getPartName() + "; pre-processing"); /* Always try our preprocessor, since if what is first encountered is * eg: * <w14:glow w14:rad="101600"> ... * * the error would be: * * unexpected element (uri:"http://schemas.microsoft.com/office/word/2010/wordml", local:"glow") * * but there could well be mc:AlternateContent somewhere * further down in the document. */ // mimic docx4j 2.7.0 and earlier behaviour; this will // drop w14:glow etc; the preprocessor doesn't need to // do that eventHandler.setContinue(true); // There is no JAXBResult(binder), // so use a DOMResult result = new DOMResult(); Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); XmlUtils.transform(doc, mcPreprocessorXslt, null, result); doc = (org.w3c.dom.Document) result.getNode(); try { jaxbElement = (E) XmlUtils.unwrap(binder.unmarshal(doc)); } catch (ClassCastException cce) { /* * Work around for issue with JAXB binder, in Java 1.6 * encountered with /src/test/resources/jaxb-binder-issue.docx * See http://old.nabble.com/BinderImpl.associativeUnmarshal-ClassCastException-casting-to-JAXBElement-td32456585.html * and http://java.net/jira/browse/JAXB-874 * * java.lang.ClassCastException: org.docx4j.wml.PPr cannot be cast to javax.xml.bind.JAXBElement at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$IntercepterLoader.intercept(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.endElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source) at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.BinderImpl.associativeUnmarshal(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.BinderImpl.unmarshal(Unknown Source) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.unmarshal(MainDocumentPart.java:321) */ log.warn("Binder not available for this docx"); Unmarshaller u = jc.createUnmarshaller(); jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(doc)); } } else { log.warn("problem in " + this.getPartName()); log.warn(ue.getMessage(), ue); log.warn(".. and mark not supported"); throw ue; } } return jaxbElement; } catch (Exception e) { // e.printStackTrace(); // return null; /* java.lang.NullPointerException at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor.startDTD(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDInternalSubset(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) */ for (int i = 0; i < e.getStackTrace().length; i++) { if (e.getStackTrace()[i].getClassName().contains("DTD") || e.getStackTrace()[i].getMethodName().contains("DTD")) { // Mimic Word 2010 message throw new JAXBException("DTD is prohibited", e); } } throw new JAXBException(e.getMessage(), e); } }
From source file:org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.java
/** * @since 2.7.1/* w w w .jav a 2 s .co m*/ */ @Override public E unmarshal(org.w3c.dom.Element el) throws JAXBException { try { log.debug("For " + this.getClass().getName() + ", unmarshall via binder"); binder = jc.createBinder(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); eventHandler.setContinue(false); binder.setEventHandler(eventHandler); try { // jaxbElement = (E) XmlUtils.unwrap(binder.unmarshal( el )); unwrapUsually(binder, el); // unlikely to need this in the code below } catch (Exception ue) { if (ue instanceof UnmarshalException) { // Usually.. } else { // eg java.lang.NumberFormatException log.warn(ue.getMessage(), ue); log.info(".. can recover if problem is w:tblW/@w:w"); } log.info("encountered unexpected content; pre-processing"); org.w3c.dom.Document doc = null; try { if (el instanceof org.w3c.dom.Document) { doc = (org.w3c.dom.Document) el; } else { // Hope for the best. Dodgy though; what if this is // being used on something deep in the tree? // TODO: revisit doc = el.getOwnerDocument(); } eventHandler.setContinue(true); DOMResult result = new DOMResult(); Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); XmlUtils.transform(doc, mcPreprocessorXslt, null, result); doc = (org.w3c.dom.Document) result.getNode(); jaxbElement = (E) XmlUtils.unwrap(binder.unmarshal(doc)); } catch (ClassCastException cce) { log.warn("Binder not available for this docx"); Unmarshaller u = jc.createUnmarshaller(); jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(doc)); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } return jaxbElement; } catch (JAXBException e) { log.error(e.getMessage(), e); throw e; } }
From source file:org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart.java
/** * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output * stream/*from ww w.j a va 2 s . c o m*/ * * @param os * XML will be added to this stream. * @param namespacePrefixMapper * namespacePrefixMapper * * @throws JAXBException * If any unexpected problem occurs during the marshalling. */ @Override public void marshal(java.io.OutputStream os, Object namespacePrefixMapper) throws JAXBException { // Add xmlns:v="urn:schemas-microsoft-com:vml" eg in // <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> // <mc:Choice xmlns:v="urn:schemas-microsoft-com:vml" Requires="v"> // How? Could marshall to a DOM doc, but there is no way to force the xmlns to be included // where it is not required. Well, JAXB namespace prefix mapping stuff promises a way, but it is buggy. // So do string manipulation // @since 3.2.1 String xmlString = XmlUtils.marshaltoString(getJaxbElement(), false, true, jc); // include the XML declaration; it'll be UTF-8 int pos = xmlString.indexOf(":sldLayout "); //11 int closeTagPos = xmlString.indexOf(">", pos); if (xmlString.substring(pos, closeTagPos).contains(VML_DECL)) { // nothing to do; vml namespace is already declared } else { xmlString = xmlString.substring(0, pos + 11) + VML_DECL + " " + xmlString.substring(pos + 11); } try { IOUtils.write(xmlString, os, "UTF-8"); // be sure to write UTF-8 irrespective of default encoding /* FIX confirmed by running a presentation containing eg mg * through RoundTripTest, * with run configuration setting -Dfile.encoding=ISO-8859-1, * verified Powerpoint (2010) can open it. */ } catch (IOException e) { throw new JAXBException(e.getMessage(), e); } }
From source file:org.docx4j.openpackaging.parts.PresentationML.SlideMasterPart.java
/** * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output * stream//w w w.j ava2 s. c om * * @param os * XML will be added to this stream. * @param namespacePrefixMapper * namespacePrefixMapper * * @throws JAXBException * If any unexpected problem occurs during the marshalling. */ @Override public void marshal(java.io.OutputStream os, Object namespacePrefixMapper) throws JAXBException { // Add xmlns:v="urn:schemas-microsoft-com:vml" eg in // <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> // <mc:Choice xmlns:v="urn:schemas-microsoft-com:vml" Requires="v"> // How? Could marshall to a DOM doc, but there is no way to force the xmlns to be included // where it is not required. Well, JAXB namespace prefix mapping stuff promises a way, but it is buggy. // So do string manipulation // @since 3.2.1 String xmlString = XmlUtils.marshaltoString(getJaxbElement(), false, true, jc); // include the XML declaration; it'll be UTF-8 int pos = xmlString.indexOf(":sldMaster "); //11 int closeTagPos = xmlString.indexOf(">", pos); if (xmlString.substring(pos, closeTagPos).contains(VML_DECL)) { // nothing to do; vml namespace is already declared } else { xmlString = xmlString.substring(0, pos + 11) + VML_DECL + " " + xmlString.substring(pos + 11); } try { IOUtils.write(xmlString, os, "UTF-8"); // be sure to write UTF-8 irrespective of default encoding /* FIX confirmed by running a presentation containing eg mg * through RoundTripTest, * with run configuration setting -Dfile.encoding=ISO-8859-1, * verified Powerpoint (2010) can open it. */ } catch (IOException e) { throw new JAXBException(e.getMessage(), e); } }
From source file:org.docx4j.openpackaging.parts.PresentationML.SlidePart.java
/** * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output * stream/*from ww w .j a v a 2s .c o m*/ * * @param os * XML will be added to this stream. * @param namespacePrefixMapper * namespacePrefixMapper * * @throws JAXBException * If any unexpected problem occurs during the marshalling. */ @Override public void marshal(java.io.OutputStream os, Object namespacePrefixMapper) throws JAXBException { // Add xmlns:v="urn:schemas-microsoft-com:vml" eg in // <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> // <mc:Choice xmlns:v="urn:schemas-microsoft-com:vml" Requires="v"> // How? Could marshall to a DOM doc, but there is no way to force the xmlns to be included // where it is not required. Well, JAXB namespace prefix mapping stuff promises a way, but it is buggy. // So do string manipulation String xmlString = XmlUtils.marshaltoString(getJaxbElement(), false, true, jc); // include the XML declaration; it'll be UTF-8 int pos = xmlString.indexOf(":sld "); int closeTagPos = xmlString.indexOf(">", pos); if (xmlString.substring(pos, closeTagPos).contains(VML_DECL)) { // nothing to do; vml namespace is already declared } else { xmlString = xmlString.substring(0, pos + 5) + VML_DECL + " " + xmlString.substring(pos + 5); } try { IOUtils.write(xmlString, os, "UTF-8"); // be sure to write UTF-8 irrespective of default encoding /* FIX confirmed by running a presentation containing eg mg * through RoundTripTest, * with run configuration setting -Dfile.encoding=ISO-8859-1, * verified Powerpoint (2010) can open it. */ } catch (IOException e) { throw new JAXBException(e.getMessage(), e); } }
From source file:org.docx4j.openpackaging.parts.PresentationML.SlidePart.java
public Sld unmarshal(org.w3c.dom.Element el) throws JAXBException { // Note comments above about AlternateContent. // unmarshalling here from an Element doesn't implement that fix, so beware. try {// w w w. j av a2s . co m binder = jc.createBinder(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); eventHandler.setContinue(false); binder.setEventHandler(eventHandler); try { jaxbElement = (Sld) binder.unmarshal(el); } catch (UnmarshalException ue) { log.info("encountered unexpected content; pre-processing"); try { org.w3c.dom.Document doc; if (el instanceof org.w3c.dom.Document) { doc = (org.w3c.dom.Document) el; } else { // Hope for the best. Dodgy though; what if this is // being used on something deep in the tree? // TODO: revisit doc = el.getOwnerDocument(); } eventHandler.setContinue(true); DOMResult result = new DOMResult(); Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); XmlUtils.transform(doc, mcPreprocessorXslt, null, result); doc = (org.w3c.dom.Document) result.getNode(); jaxbElement = (Sld) binder.unmarshal(doc); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } return jaxbElement; } catch (JAXBException e) { log.error(e.getMessage(), e); throw e; } }
From source file:org.excalibur.core.util.JAXBContextFactory.java
private static JAXBException throwAsJAXBException(Throwable e) { return new JAXBException(e.getMessage(), e); }
From source file:org.intermine.install.project.source.SourceInfoLoader.java
/** * Initialise this object by creating the JAXB context and the XML validation * schema for loading source descriptors, then loading each available data source * metadata./* ww w.jav a2 s. c om*/ * <p>This method should be called exactly once at application start up.</p> * * @throws JAXBException if there is a problem preparing the JAXB system, or * reading any of the source descriptors. * @throws IOException if there is an I/O error while loading the configuration. * @throws IllegalStateException if this object has already been initialised. */ public synchronized void initialise() throws JAXBException, IOException { if (initialised) { throw new IllegalStateException("SourceInfoLoader already initialised"); } SchemaFactory sfact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { schema = sfact.newSchema(new StreamSource(getClass().getResourceAsStream("/xsd/sourcedescriptor.xsd"))); } catch (SAXException e) { throw new JAXBException("Could not parse sourcedescriptor.xsd", e); } jaxbContext = JAXBContext.newInstance("org.intermine.install.project.source"); // Initialise known types list. knownTypes = new ArrayList<String>(); String resourceName = RESOURCE_PATH + "sourcetypes.txt"; InputStream in = getClass().getResourceAsStream(resourceName); if (in == null) { throw new FileNotFoundException(resourceName); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { knownTypes.add(line.trim()); } } finally { in.close(); } knownTypes = Collections.unmodifiableList(knownTypes); // Initialise information for these known types. for (String type : knownTypes) { String descriptorFile = RESOURCE_PATH + type + ".xml"; InputStream descriptorStream = getClass().getResourceAsStream(descriptorFile); if (descriptorStream == null) { logger.warn("There is no source descriptor file for the type " + type); continue; } // Load the source descriptor. SourceDescriptor source; try { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(schema); JAXBElement<?> result = (JAXBElement<?>) unmarshaller.unmarshal(descriptorStream); source = (SourceDescriptor) result.getValue(); } finally { descriptorStream.close(); } // Load default property values, if any are present. Properties defaults = new Properties(); String defaultsFile = RESOURCE_PATH + type + "_defaults.properties"; InputStream defaultsStream = getClass().getResourceAsStream(defaultsFile); if (defaultsStream != null) { try { defaults.load(defaultsStream); } finally { defaultsStream.close(); } } SourceInfo info = new SourceInfo(type, source, defaults); knownSources.put(type, info); if (info.getSource().getDerivation() != null) { derivatedSources.put(info.getSource().getDerivation().getAntTask(), info); } } initialised = true; }