WW-2561 Throws a Struts exception when there is a bad character in template
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/ef2939ff Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/ef2939ff Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/ef2939ff Branch: refs/heads/master Commit: ef2939ffd0663f8a5d86fafa105deb7183e2ac2d Parents: 8aba301 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Sun Nov 13 11:16:40 2016 +0100 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Sun Nov 13 11:16:40 2016 +0100 ---------------------------------------------------------------------- .../apache/struts2/views/xslt/XSLTResult.java | 42 ++++++++++---------- .../struts2/views/xslt/XSLTResultTest.java | 13 +++++- .../resources/XSLTResultTest.bad.character.xsl | 30 ++++++++++++++ 3 files changed, 61 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/ef2939ff/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java b/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java index 5dfd5fb..fb5068c 100644 --- a/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java +++ b/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java @@ -1,6 +1,4 @@ /* - * $Id$ - * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -330,26 +328,7 @@ public class XSLTResult implements Result { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setURIResolver(getURIResolver()); - transformer.setErrorListener(new ErrorListener() { - - public void error(TransformerException exception) - throws TransformerException { - throw new StrutsException("Error transforming result", exception); - } - - public void fatalError(TransformerException exception) - throws TransformerException { - throw new StrutsException("Fatal error transforming result", exception); - } - - public void warning(TransformerException exception) - throws TransformerException { - if (LOG.isWarnEnabled()) { - LOG.warn(exception.getMessage(), exception); - } - } - - }); + transformer.setErrorListener(buildErrorListener()); String mimeType; if (templates == null) @@ -385,6 +364,24 @@ public class XSLTResult implements Result { } } + protected ErrorListener buildErrorListener() { + return new ErrorListener() { + + public void error(TransformerException exception) throws TransformerException { + throw new StrutsException("Error transforming result", exception); + } + + public void fatalError(TransformerException exception) throws TransformerException { + throw new StrutsException("Fatal error transforming result", exception); + } + + public void warning(TransformerException exception) throws TransformerException { + LOG.warn(exception.getMessage(), exception); + } + + }; + } + protected AdapterFactory getAdapterFactory() { if (adapterFactory == null) adapterFactory = new AdapterFactory(); @@ -422,6 +419,7 @@ public class XSLTResult implements Result { TransformerFactory factory = TransformerFactory.newInstance(); factory.setURIResolver(getURIResolver()); + factory.setErrorListener(buildErrorListener()); templates = factory.newTemplates(new StreamSource(resource.openStream())); templatesCache.put(path, templates); } http://git-wip-us.apache.org/repos/asf/struts/blob/ef2939ff/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java b/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java index d1ad400..3e8c7f1 100644 --- a/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java +++ b/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java @@ -1,6 +1,4 @@ /* - * $Id$ - * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -192,6 +190,17 @@ public class XSLTResultTest extends StrutsInternalTestCase { } } + public void testTransformWithBadCharacter() throws Exception { + result = new XSLTResult(); + result.setStylesheetLocation("XSLTResultTest.bad.character.xsl"); + try { + result.execute(mai); + fail("Should have thrown an exception"); + } catch (Exception ex) { + assertEquals("Error transforming result", ex.getMessage()); + } + } + public void testStatusCode() throws Exception { result.setParse(false); result.setStylesheetLocation("XSLTResultTest.xsl"); http://git-wip-us.apache.org/repos/asf/struts/blob/ef2939ff/core/src/test/resources/XSLTResultTest.bad.character.xsl ---------------------------------------------------------------------- diff --git a/core/src/test/resources/XSLTResultTest.bad.character.xsl b/core/src/test/resources/XSLTResultTest.bad.character.xsl new file mode 100644 index 0000000..194b430 --- /dev/null +++ b/core/src/test/resources/XSLTResultTest.bad.character.xsl @@ -0,0 +1,30 @@ +<!-- +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +--> +<xsl:stylesheet version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns="http://www.w3.org/TR/xhtml1/strict"> + + <xsl:template match="result"> + <p></p> + <xsl:copy-of select="document('XSLTResultTest-val.xml')"/> + </xsl:template> + +</xsl:stylesheet> \ No newline at end of file