下面列出了javax.xml.transform.TransformerException#getCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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));
}
@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());
}
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;
}
/**
* 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());
}
}
private static Document stripWhiteSpaceUsingXSLT(Document forDoc) {
try {
Transform whitespaceStripper = getStripWhitespaceTransform(forDoc);
return whitespaceStripper.getResultDocument();
} catch (TransformerException e) {
throw new XMLUnitRuntimeException(e.getMessage(), e.getCause());
}
}
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);
}
/**
* 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( );
}