1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.util.Locale;
29  
30  import javax.xml.transform.ErrorListener;
31  import javax.xml.transform.SourceLocator;
32  import javax.xml.transform.TransformerException;
33  
34  import org.apache.xml.utils.SAXSourceLocator;
35  import org.apache.xml.utils.WrappedRuntimeException;
36  
37  import org.xml.sax.SAXException;
38  import org.xml.sax.SAXParseException;
39  
40  /**
41   * <a href="XSLErrorListener.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Raymond Augé
44   */
45  public class XSLErrorListener implements ErrorListener {
46  
47      public XSLErrorListener(Locale locale) {
48          _locale = locale;
49      }
50  
51      public void error(TransformerException exception)
52          throws TransformerException {
53  
54          setLocation(exception);
55  
56          throw exception;
57      }
58  
59      public void fatalError(TransformerException exception)
60          throws TransformerException {
61  
62          setLocation(exception);
63  
64          throw exception;
65      }
66  
67      public void warning(TransformerException exception)
68          throws TransformerException {
69  
70          setLocation(exception);
71  
72          throw exception;
73      }
74  
75      public void setLocation(Throwable exception) {
76          SourceLocator locator = null;
77          Throwable cause = exception;
78          Throwable rootCause = null;
79  
80          while (cause != null) {
81              if (cause instanceof SAXParseException) {
82                  locator = new SAXSourceLocator((SAXParseException)cause);
83                  rootCause = cause;
84              }
85              else if (cause instanceof TransformerException) {
86                  SourceLocator causeLocator =
87                      ((TransformerException)cause).getLocator();
88  
89                  if (causeLocator != null) {
90                      locator = causeLocator;
91                      rootCause = cause;
92                  }
93              }
94  
95              if (cause instanceof TransformerException) {
96                  cause = ((TransformerException)cause).getCause();
97              }
98              else if (cause instanceof WrappedRuntimeException) {
99                  cause = ((WrappedRuntimeException)cause).getException();
100             }
101             else if (cause instanceof SAXException) {
102                 cause = ((SAXException)cause).getException();
103             }
104             else {
105                 cause = null;
106             }
107         }
108 
109         _message = rootCause.getMessage();
110 
111         if (locator != null) {
112             _lineNumber = locator.getLineNumber();
113             _columnNumber = locator.getColumnNumber();
114 
115             StringBuilder sb = new StringBuilder();
116 
117             sb.append(LanguageUtil.get(_locale, "line"));
118             sb.append(" #");
119             sb.append(locator.getLineNumber());
120             sb.append("; ");
121             sb.append(LanguageUtil.get(_locale, "column"));
122             sb.append(" #");
123             sb.append(locator.getColumnNumber());
124             sb.append("; ");
125 
126             _location = sb.toString();
127         }
128         else {
129             _location = StringPool.BLANK;
130         }
131     }
132 
133     public String getLocation() {
134         return _location;
135     }
136 
137     public String getMessage() {
138         return _message;
139     }
140 
141     public String getMessageAndLocation() {
142         return _message + " " + _location;
143     }
144 
145     public int getLineNumber() {
146         return _lineNumber;
147     }
148 
149     public int getColumnNumber() {
150         return _columnNumber;
151     }
152 
153     private Locale _locale;
154     private String _location;
155     private String _message;
156     private int _lineNumber;
157     private int _columnNumber;
158 
159 }