public void error(TransformerException exception) throws TransformerException { log.error(exception.getMessage(), exception); // XSLT is not as draconian as XML. There are numerous errors // which the processor may but does not have to recover from; // e.g. multiple templates that match a node with the same // priority. If I do not want to allow that, I'd throw this // exception here. if (strict) { throw exception; } }
public void warning(TransformerException exception) { log.warn(exception.getMessage(), exception); // Don't throw an exception and stop the processor // just for a warning; but do log the problem }
public void fatalError(TransformerException exception) throws TransformerException { if (Docx4jProperties.getProperty("docx4j.openpackaging.exceptions.LogBeforeThrow", true)) { log.error(exception.getMessage(), exception); } // This is an error which the processor cannot recover from; // e.g. a malformed stylesheet or input document // (or a cancelled request) // so I must throw this exception here. throw exception; }
private void writeUpdatedTMX (TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException { Document doc; DocumentBuilder docBuilder; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(tmxFileHandle.read()); Node map = doc.getFirstChild(); while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") { if ((map = map.getNextSibling()) == null) { throw new GdxRuntimeException("Couldn't find map node!"); } } setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); outputDir.mkdirs(); StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name())); transformer.transform(source, result); } catch (ParserConfigurationException e) { throw new RuntimeException("ParserConfigurationException: " + e.getMessage()); } catch (SAXException e) { throw new RuntimeException("SAXException: " + e.getMessage()); } catch (TransformerConfigurationException e) { throw new RuntimeException("TransformerConfigurationException: " + e.getMessage()); } catch (TransformerException e) { throw new RuntimeException("TransformerException: " + e.getMessage()); } }
private void writeUpdatedTMX (TiledMap tiledMap, FileHandle tmxFileHandle) throws IOException { Document doc; DocumentBuilder docBuilder; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); try { docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(tmxFileHandle.read()); Node map = doc.getFirstChild(); while (map.getNodeType() != Node.ELEMENT_NODE || map.getNodeName() != "map") { if ((map = map.getNextSibling()) == null) { throw new GdxRuntimeException("Couldn't find map node!"); } } setProperty(doc, map, "atlas", settings.tilesetOutputDirectory + "/" + settings.atlasOutputName + ".atlas"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); outputDir.mkdirs(); StreamResult result = new StreamResult(new File(outputDir, tmxFileHandle.name())); transformer.transform(source, result); } catch (ParserConfigurationException e) { throw new RuntimeException("ParserConfigurationException: " + e.getMessage()); } catch (SAXException e) { throw new RuntimeException("SAXException: " + e.getMessage()); } catch (TransformerConfigurationException e) { throw new RuntimeException("TransformerConfigurationException: " + e.getMessage()); } catch (TransformerException e) { throw new RuntimeException("TransformerException: " + e.getMessage()); } }
private static void serialize(Source source, StreamResult target) { TransformerFactory factory = TransformerFactory.newInstance(); setIndent(factory, 2); try { Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); transformer.transform(source, target); } catch (TransformerException e) { throw new GroovyRuntimeException(e.getMessage()); } }
/** * Find the most contained message. * * @return The error message of the originating exception. */ public String getMessage() { String lastMessage = super.getMessage(); Throwable exception = m_exception; while (null != exception) { String nextMessage = exception.getMessage(); if (null != nextMessage) lastMessage = nextMessage; if (exception instanceof TransformerException) { TransformerException se = (TransformerException) exception; Throwable prev = exception; exception = se.getException(); if (prev == exception) break; } else { exception = null; } } return (null != lastMessage) ? lastMessage : ""; }
/** * Receive notification of the beginning of the document. * * <p>By default, do nothing. Application writers may override this * method in a subclass to take specific actions at the beginning * of a document (such as allocating the root node of a tree or * creating an output file).</p> * * @throws org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#startDocument * * @throws SAXException */ public void startDocument() throws SAXException { try { if (null == m_resultContentHandler) createResultContentHandler(m_result); } catch (TransformerException te) { throw new SAXException(te.getMessage(), te); } // Reset for multiple transforms with this transformer. m_flushedStartDoc = false; m_foundFirstElement = false; }
/** * Receive notification of the beginning of the document. * * <p>By default, do nothing. Application writers may override this * method in a subclass to take specific actions at the beginning * of a document (such as allocating the root node of a tree or * creating an output file).</p> * * @throws org.xml.sax.SAXException Any SAX exception, possibly * wrapping another exception. * @see org.xml.sax.ContentHandler#startDocument * * @throws SAXException */ public void startDocument() throws SAXException { try { if (null == m_resultContentHandler) createResultContentHandler(m_result); } catch (TransformerException te) { throw new SAXException(te.getMessage(), te); } // Reset for multiple transforms with this transformer. m_flushedStartDoc = false; m_foundFirstElement = false; }
/** * Receive notification of a warning. * * <p>SAX parsers will use this method to report conditions that * are not errors or fatal errors as defined by the XML 1.0 * recommendation. The default behaviour is to take no action.</p> * * <p>The SAX parser must continue to provide normal parsing events * after invoking this method: it should still be possible for the * application to process the document through to the end.</p> * * @param exception The warning information encapsulated in a * SAX parse exception. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly * wrapping another exception. * @see javax.xml.transform.TransformerException */ public void warning(TransformerException exception) throws TransformerException { PrintWriter pw = getErrorWriter(); printLocation(pw, exception); pw.println(exception.getMessage()); }
/** * Receive notification of a recoverable error. * * <p>The transformer must continue to try and provide normal transformation * after invoking this method. It should still be possible for the * application to process the document through to the end if no other errors * are encountered.</p> * * @param exception The error information encapsulated in a * transformer exception. * * @throws javax.xml.transform.TransformerException only if * setThrowOnError is true. * * @see javax.xml.transform.TransformerException */ public void error(TransformerException exception) throws TransformerException { logExceptionLocation(m_pw, exception); m_pw.println("error: " + exception.getMessage()); m_pw.flush(); if (getThrowOnError()) throw exception; }
/** * Receive notification of a non-recoverable error. * * <p>The transformer must continue to try and provide normal transformation * after invoking this method. It should still be possible for the * application to process the document through to the end if no other errors * are encountered, but there is no guarantee that the output will be * useable.</p> * * @param exception The error information encapsulated in a * transformer exception. * * @throws javax.xml.transform.TransformerException only if * setThrowOnError is true. * * @see javax.xml.transform.TransformerException */ public void fatalError(TransformerException exception) throws TransformerException { logExceptionLocation(m_pw, exception); m_pw.println("error: " + exception.getMessage()); m_pw.flush(); if (getThrowOnError()) throw exception; }
/** * Receive notification of a warning. * * <p>{@link javax.xml.transform.Transformer} can use this method to report * conditions that are not errors or fatal errors. The default behaviour * is to take no action.</p> * * <p>After invoking this method, the Transformer must continue with * the transformation. It should still be possible for the * application to process the document through to the end.</p> * * @param exception The warning information encapsulated in a * transformer exception. * * @throws javax.xml.transform.TransformerException only if * setThrowOnWarning is true. * * @see javax.xml.transform.TransformerException */ public void warning(TransformerException exception) throws TransformerException { logExceptionLocation(m_pw, exception); m_pw.println("warning: " + exception.getMessage()); m_pw.flush(); if (getThrowOnWarning()) throw exception; }
/** * Receive notification of a warning. * * <p>SAX parsers will use this method to report conditions that * are not errors or fatal errors as defined by the XML 1.0 * recommendation. The default behaviour is to take no action.</p> * * <p>The SAX parser must continue to provide normal parsing events * after invoking this method: it should still be possible for the * application to process the document through to the end.</p> * * @param exception The warning information encapsulated in a * SAX parse exception. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly * wrapping another exception. * @see javax.xml.transform.TransformerException */ public void warning(TransformerException exception) throws TransformerException { PrintWriter pw = getErrorWriter(); printLocation(pw, exception); pw.println(exception.getMessage()); }
pw.println(exception.getMessage());
@Override @Signature public Memory format(Environment environment, Memory... args) { if (!args[0].instanceOf(WrapDomDocument.class)) { throw new IllegalArgumentException( "Argument #1 must be instance of " + ReflectionUtils.getClassName(WrapDomDocument.class) ); } StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); try { transformer.transform(new DOMSource(args[0].toObject(WrapDomDocument.class).getWrappedObject()), result); return StringMemory.valueOf(writer.toString()); } catch (TransformerException e) { environment.exception(ProcessorException.class, e.getMessage()); return Memory.NULL; } }
/** * This method needs to override AxesWalker.acceptNode because FilterExprWalkers * don't need to, and shouldn't, do a node test. * @param n The node to check to see if it passes the filter or not. * @return a constant to determine whether the node is accepted, * rejected, or skipped, as defined above . */ public short acceptNode(int n) { try { if (getPredicateCount() > 0) { countProximityPosition(0); if (!executePredicates(n, m_lpi.getXPathContext())) return DTMIterator.FILTER_SKIP; } return DTMIterator.FILTER_ACCEPT; } catch (javax.xml.transform.TransformerException se) { throw new RuntimeException(se.getMessage()); } }
@Override @Signature public Memory formatTo(Environment environment, Memory... args) { if (!args[0].instanceOf(WrapDomDocument.class)) { throw new IllegalArgumentException( "Argument #1 must be instance of " + ReflectionUtils.getClassName(WrapDomDocument.class) ); } OutputStream output = Stream.getOutputStream(environment, args[1]); StreamResult result = new StreamResult(output); try { transformer.transform(new DOMSource(args[0].toObject(WrapDomDocument.class).getWrappedObject()), result); } catch (TransformerException e) { environment.exception(ProcessorException.class, e.getMessage()); return Memory.NULL; } finally { Stream.closeStream(environment, output); } return Memory.NULL; }
/** * This method needs to override AxesWalker.acceptNode because FilterExprWalkers * don't need to, and shouldn't, do a node test. * @param n The node to check to see if it passes the filter or not. * @return a constant to determine whether the node is accepted, * rejected, or skipped, as defined above . */ public short acceptNode(int n) { try { if (getPredicateCount() > 0) { countProximityPosition(0); if (!executePredicates(n, m_lpi.getXPathContext())) return DTMIterator.FILTER_SKIP; } return DTMIterator.FILTER_ACCEPT; } catch (javax.xml.transform.TransformerException se) { throw new RuntimeException(se.getMessage()); } }
/** * @see org.w3c.dom.xpath.XPathResult#getBooleanValue() */ public boolean getBooleanValue() throws XPathException { if (getResultType() != BOOLEAN_TYPE) { String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_BOOLEAN, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)}); throw new XPathException(XPathException.TYPE_ERR,fmsg); // "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a boolean." } else { try { return m_resultObj.bool(); } catch (TransformerException e) { // Type check above should prevent this exception from occurring. throw new XPathException(XPathException.TYPE_ERR,e.getMessage()); } } }