Java Code Examples for javax.xml.transform.TransformerException#getCause()

The following examples show how to use javax.xml.transform.TransformerException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MCRXMLHelper.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
/**
 * validates <code>doc</code> using XML Schema defined <code>schemaURI</code>
 * @param doc document to be validated
 * @param schemaURI URI of XML Schema document
 * @throws SAXException if validation fails
 * @throws IOException if resolving resources fails
 */
public static void validate(Document doc, String schemaURI) throws SAXException, IOException {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    sf.setResourceResolver(MCREntityResolver.instance());
    Schema schema;
    try {
        schema = sf.newSchema(MCRURIResolver.instance().resolve(schemaURI, null));
    } catch (TransformerException e) {
        Throwable cause = e.getCause();
        if (cause == null) {
            throw new IOException(e);
        }
        if (cause instanceof SAXException) {
            throw (SAXException) cause;
        }
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        throw new IOException(e);
    }
    Validator validator = schema.newValidator();
    validator.setResourceResolver(MCREntityResolver.instance());
    validator.validate(new JDOMSource(doc));
}
 
Example 2
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 5 votes vote down vote up
@Test
void testEDIReporterUnset() throws Exception {
    EDIInputFactory ediFactory = EDIInputFactory.newFactory();
    InputStream stream = getClass().getResourceAsStream("/x12/invalid999.edi");
    SchemaFactory schemaFactory = SchemaFactory.newFactory();
    Schema schema = schemaFactory.createSchema(getClass().getResource("/x12/EDISchema999.xml"));

    EDIStreamReader ediReader = ediFactory.createEDIStreamReader(stream);
    ediReader = ediFactory.createFilteredReader(ediReader, (reader) -> {
        if (reader.getEventType() == EDIStreamEvent.START_TRANSACTION) {
            reader.setTransactionSchema(schema);
        }
        return true;
    });

    XMLStreamReader xmlReader = new StaEDIXMLStreamReader(ediReader);

    xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter result = new StringWriter();
    TransformerException thrown = assertThrows(TransformerException.class,
                                               () -> transformer.transform(new StAXSource(xmlReader), new StreamResult(result)));
    Throwable cause = thrown.getCause();
    assertTrue(cause instanceof XMLStreamException);
    javax.xml.stream.Location l = ((XMLStreamException) cause).getLocation();
    assertEquals("ParseError at [row,col]:[" + l.getLineNumber() + "," +
            l.getColumnNumber() + "]\n" +
            "Message: " + "Segment IK5 has error UNEXPECTED_SEGMENT", cause.getMessage());
}
 
Example 3
Source File: MCRErrorListener.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static TransformerException unwrapException(TransformerException exception) {
    Throwable cause = exception.getCause();
    while (cause != null) {
        if (cause instanceof TransformerException) {
            return unwrapException((TransformerException) cause);
        }
        if (cause instanceof WrappedRuntimeException) {
            cause = ((WrappedRuntimeException) cause).getException();
        } else {
            cause = cause.getCause();
        }
    }
    return exception;
}
 
Example 4
Source File: Diff.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all comment nodes if {@link XMLUnit#getIgnoreComments
 * comments are ignored}.
 *     
 * @param orig a document making up one half of this difference
 * @return manipulated doc
 */
private Document getCommentlessDocument(Document orig) {
    if (!XMLUnit.getIgnoreComments()) {
        return orig;
    }
    try {
        Transform commentStripper = XMLUnit.getStripCommentsTransform(orig);
        return commentStripper.getResultDocument();
    } catch (TransformerException e) {
        throw new XMLUnitRuntimeException(e.getMessage(), e.getCause());
    }
}
 
Example 5
Source File: XMLUnit.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
private static Document stripWhiteSpaceUsingXSLT(Document forDoc) {
    try {
        Transform whitespaceStripper = getStripWhitespaceTransform(forDoc);
        return whitespaceStripper.getResultDocument();
    } catch (TransformerException e) {
        throw new XMLUnitRuntimeException(e.getMessage(), e.getCause());
    }
}
 
Example 6
Source File: SaxExceptionTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
public void inspectViaTransformerException(TransformerException e, SAXException originalException, String expectedInSaxExceptionMessage, Locator locator) {
		System.out.println("TransformerException getMessage() ["+e.getMessage()+"]");
		System.out.println("TransformerException toString() ["+e.toString()+"]");
		//e.printStackTrace();
//		if (locator!=null) {
//			assertThat(e.getMessage(), StringContains.containsString(EXPECTED_LOCATION_MESSAGE_PART));
//		}
		Throwable cause = e.getCause();
		assertThat(cause, IsInstanceOf.instanceOf(SAXException.class));
		SAXException saxCause = (SAXException)cause;
		inspectSAXException(saxCause, expectedInSaxExceptionMessage, locator);
	}
 
Example 7
Source File: XmlTransformer.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Transform XML documents using XSLT with cache
 * 
 * @param source
 *            The XML document content
 * @param stylesheet
 *            The XSL source
 * @param strStyleSheetId
 *            The StyleSheet Id
 * @param params
 *            Parameters that can be used by the XSL StyleSheet
 * @param outputProperties
 *            Properties to use for the XSL transform. Will overload the XSL output definition.
 * @return The output document
 * @throws TransformerException
 *             The exception
 */
public String transform( Source source, Source stylesheet, String strStyleSheetId, Map<String, String> params, Properties outputProperties )
        throws TransformerException
{
    Templates templates = this.getTemplates( stylesheet, strStyleSheetId );
    Transformer transformer = templates.newTransformer( );

    if ( outputProperties != null )
    {
        transformer.setOutputProperties( outputProperties );
    }

    if ( params != null )
    {
        transformer.clearParameters( );

        for ( Entry<String, String> entry : params.entrySet( ) )
        {
            transformer.setParameter( entry.getKey( ), entry.getValue( ) );
        }
    }

    StringWriter sw = new StringWriter( );
    Result result = new StreamResult( sw );

    try
    {
        transformer.transform( source, result );
    }
    catch( TransformerException e )
    {
        String strMessage = "strStyleSheetId = " + strStyleSheetId + " " + e.getMessage( );

        if ( e.getLocationAsString( ) != null )
        {
            strMessage += ( " - location : " + e.getLocationAsString( ) );
        }

        throw new TransformerException( ERROR_MESSAGE_XLST + strMessage, e.getCause( ) );
    }
    finally
    {
        this.releaseTemplates( templates, strStyleSheetId );
    }

    return sw.toString( );
}