1   /**
2    * Copyright (c) 2000-2008 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.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  
29  import java.util.Locale;
30  
31  import javax.xml.transform.ErrorListener;
32  import javax.xml.transform.SourceLocator;
33  import javax.xml.transform.TransformerException;
34  
35  import org.apache.xml.utils.SAXSourceLocator;
36  import org.apache.xml.utils.WrappedRuntimeException;
37  
38  import org.xml.sax.SAXException;
39  import org.xml.sax.SAXParseException;
40  
41  /**
42   * <a href="JournalXslErrorListener.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Raymond Augé
45   *
46   */
47  public class JournalXslErrorListener implements ErrorListener {
48  
49      public JournalXslErrorListener(long companyId, Locale locale) {
50          _companyId = companyId;
51          _locale = locale;
52      }
53  
54      public void error(TransformerException exception)
55          throws TransformerException {
56  
57          setLocation(exception);
58  
59          throw exception;
60      }
61  
62      public void fatalError(TransformerException exception)
63          throws TransformerException {
64  
65          setLocation(exception);
66  
67          throw exception;
68      }
69  
70      public void warning(TransformerException exception)
71          throws TransformerException {
72  
73          setLocation(exception);
74  
75          throw exception;
76      }
77  
78      public void setLocation(Throwable exception) {
79          SourceLocator locator = null;
80          Throwable cause = exception;
81          Throwable rootCause = null;
82  
83          while (cause != null) {
84              if (cause instanceof SAXParseException) {
85                  locator = new SAXSourceLocator((SAXParseException)cause);
86                  rootCause = cause;
87              }
88              else if (cause instanceof TransformerException) {
89                  SourceLocator causeLocator =
90                      ((TransformerException)cause).getLocator();
91  
92                  if (causeLocator != null) {
93                      locator = causeLocator;
94                      rootCause = cause;
95                  }
96              }
97  
98              if (cause instanceof TransformerException) {
99                  cause = ((TransformerException)cause).getCause();
100             }
101             else if (cause instanceof WrappedRuntimeException) {
102                 cause = ((WrappedRuntimeException)cause).getException();
103             }
104             else if (cause instanceof SAXException) {
105                 cause = ((SAXException)cause).getException();
106             }
107             else {
108                 cause = null;
109             }
110         }
111 
112         _message = rootCause.getMessage();
113 
114         if (locator != null) {
115             StringMaker sm = new StringMaker();
116 
117             sm.append(LanguageUtil.get(_companyId, _locale, "line"));
118             sm.append(" #");
119             sm.append(locator.getLineNumber());
120             sm.append("; ");
121             sm.append(LanguageUtil.get(_companyId, _locale, "column"));
122             sm.append(" #");
123             sm.append(locator.getColumnNumber());
124             sm.append("; ");
125 
126             _location = sm.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     private long _companyId;
146     private Locale _locale;
147     private String _location;
148     private String _message;
149 
150 }