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="JournalXslErrorListener.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Raymond Augé
44   */
45  public class JournalXslErrorListener implements ErrorListener {
46  
47      public JournalXslErrorListener(long companyId, Locale locale) {
48          _companyId = companyId;
49          _locale = locale;
50      }
51  
52      public void error(TransformerException exception)
53          throws TransformerException {
54  
55          setLocation(exception);
56  
57          throw exception;
58      }
59  
60      public void fatalError(TransformerException exception)
61          throws TransformerException {
62  
63          setLocation(exception);
64  
65          throw exception;
66      }
67  
68      public void warning(TransformerException exception)
69          throws TransformerException {
70  
71          setLocation(exception);
72  
73          throw exception;
74      }
75  
76      public void setLocation(Throwable exception) {
77          SourceLocator locator = null;
78          Throwable cause = exception;
79          Throwable rootCause = null;
80  
81          while (cause != null) {
82              if (cause instanceof SAXParseException) {
83                  locator = new SAXSourceLocator((SAXParseException)cause);
84                  rootCause = cause;
85              }
86              else if (cause instanceof TransformerException) {
87                  SourceLocator causeLocator =
88                      ((TransformerException)cause).getLocator();
89  
90                  if (causeLocator != null) {
91                      locator = causeLocator;
92                      rootCause = cause;
93                  }
94              }
95  
96              if (cause instanceof TransformerException) {
97                  cause = ((TransformerException)cause).getCause();
98              }
99              else if (cause instanceof WrappedRuntimeException) {
100                 cause = ((WrappedRuntimeException)cause).getException();
101             }
102             else if (cause instanceof SAXException) {
103                 cause = ((SAXException)cause).getException();
104             }
105             else {
106                 cause = null;
107             }
108         }
109 
110         _message = rootCause.getMessage();
111 
112         if (locator != null) {
113             _lineNumber = locator.getLineNumber();
114             _columnNumber = locator.getColumnNumber();
115 
116             StringBuilder sb = new StringBuilder();
117 
118             sb.append(LanguageUtil.get(_companyId, _locale, "line"));
119             sb.append(" #");
120             sb.append(locator.getLineNumber());
121             sb.append("; ");
122             sb.append(LanguageUtil.get(_companyId, _locale, "column"));
123             sb.append(" #");
124             sb.append(locator.getColumnNumber());
125             sb.append("; ");
126 
127             _location = sb.toString();
128         }
129         else {
130             _location = StringPool.BLANK;
131         }
132     }
133 
134     public String getLocation() {
135         return _location;
136     }
137 
138     public String getMessage() {
139         return _message;
140     }
141 
142     public String getMessageAndLocation() {
143         return _message + " " + _location;
144     }
145 
146     public int getLineNumber() {
147         return _lineNumber;
148     }
149 
150     public int getColumnNumber() {
151         return _columnNumber;
152     }
153 
154     private long _companyId;
155     private Locale _locale;
156     private String _location;
157     private String _message;
158     private int _lineNumber;
159     private int _columnNumber;
160 
161 }