View Javadoc

1   /*
2    * Copyright 2005-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.test.support;
18  
19  import javax.xml.transform.OutputKeys;
20  import javax.xml.transform.Source;
21  import javax.xml.transform.Transformer;
22  import javax.xml.transform.TransformerConfigurationException;
23  import javax.xml.transform.TransformerException;
24  
25  import org.springframework.xml.transform.StringResult;
26  import org.springframework.xml.transform.TransformerHelper;
27  
28  /**
29   * Subclass of {@link AssertionError} that also contains a {@link Source} for more context.
30   *
31   * @author Lukas Krecan
32   * @author Arjen Poutsma
33   * @since 2.0.1 
34   */
35  public class SourceAssertionError extends AssertionError {
36  
37      private final String sourceLabel;
38  
39      private final Source source;
40  
41      private final TransformerHelper transformerHelper = new TransformerHelper();
42  
43      /**
44       * Creates a new instance of the {@code SourceAssertionError} class with the given parameters.
45       */
46      public SourceAssertionError(String detailMessage, String sourceLabel, Source source) {
47          super(detailMessage);
48          this.sourceLabel = sourceLabel;
49          this.source = source;
50      }
51  
52      /**
53       * Returns the source context of this error.
54       * @return the source
55       */
56      public Source getSource() {
57          return source;
58      }
59  
60      @Override
61      public String getMessage() {
62          StringBuilder builder = new StringBuilder();
63          builder.append(super.getMessage());
64          String sourceString = getSourceString();
65          if (sourceString != null) {
66              String newLine = System.getProperty("line.separator");
67              builder.append(newLine);
68              String label = sourceLabel != null ? sourceLabel : "Source";
69              builder.append(label);
70              builder.append(": ");
71              builder.append(sourceString);
72          }
73          return builder.toString();
74      }
75  
76      private String getSourceString() {
77          if (source != null) {
78              try {
79                  StringResult result = new StringResult();
80                  Transformer transformer = createNonIndentingTransformer();
81                  transformer.transform(source, result);
82                  return result.toString();
83              }
84              catch (TransformerException ex) {
85                  // Ignore
86              }
87          }
88          return null;
89      }
90  
91      private Transformer createNonIndentingTransformer() throws TransformerConfigurationException {
92          Transformer transformer = transformerHelper.createTransformer();
93          transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
94          transformer.setOutputProperty(OutputKeys.INDENT, "no");
95          return transformer;
96      }
97  
98  
99  }