1
14
15 package com.liferay.portlet.journal.util;
16
17 import com.liferay.portal.kernel.language.LanguageUtil;
18 import com.liferay.portal.kernel.util.StringBundler;
19 import com.liferay.portal.kernel.util.StringPool;
20
21 import java.util.Locale;
22
23 import javax.xml.transform.ErrorListener;
24 import javax.xml.transform.SourceLocator;
25 import javax.xml.transform.TransformerException;
26
27 import org.apache.xml.utils.SAXSourceLocator;
28 import org.apache.xml.utils.WrappedRuntimeException;
29
30 import org.xml.sax.SAXException;
31 import org.xml.sax.SAXParseException;
32
33
38 public class XSLErrorListener implements ErrorListener {
39
40 public XSLErrorListener(Locale locale) {
41 _locale = locale;
42 }
43
44 public void error(TransformerException exception)
45 throws TransformerException {
46
47 setLocation(exception);
48
49 throw exception;
50 }
51
52 public void fatalError(TransformerException exception)
53 throws TransformerException {
54
55 setLocation(exception);
56
57 throw exception;
58 }
59
60 public void warning(TransformerException exception)
61 throws TransformerException {
62
63 setLocation(exception);
64
65 throw exception;
66 }
67
68 public void setLocation(Throwable exception) {
69 SourceLocator locator = null;
70 Throwable cause = exception;
71 Throwable rootCause = null;
72
73 while (cause != null) {
74 if (cause instanceof SAXParseException) {
75 locator = new SAXSourceLocator((SAXParseException)cause);
76 rootCause = cause;
77 }
78 else if (cause instanceof TransformerException) {
79 SourceLocator causeLocator =
80 ((TransformerException)cause).getLocator();
81
82 if (causeLocator != null) {
83 locator = causeLocator;
84 rootCause = cause;
85 }
86 }
87
88 if (cause instanceof TransformerException) {
89 cause = ((TransformerException)cause).getCause();
90 }
91 else if (cause instanceof WrappedRuntimeException) {
92 cause = ((WrappedRuntimeException)cause).getException();
93 }
94 else if (cause instanceof SAXException) {
95 cause = ((SAXException)cause).getException();
96 }
97 else {
98 cause = null;
99 }
100 }
101
102 _message = rootCause.getMessage();
103
104 if (locator != null) {
105 _lineNumber = locator.getLineNumber();
106 _columnNumber = locator.getColumnNumber();
107
108 StringBundler sb = new StringBundler(8);
109
110 sb.append(LanguageUtil.get(_locale, "line"));
111 sb.append(" #");
112 sb.append(locator.getLineNumber());
113 sb.append("; ");
114 sb.append(LanguageUtil.get(_locale, "column"));
115 sb.append(" #");
116 sb.append(locator.getColumnNumber());
117 sb.append("; ");
118
119 _location = sb.toString();
120 }
121 else {
122 _location = StringPool.BLANK;
123 }
124 }
125
126 public String getLocation() {
127 return _location;
128 }
129
130 public String getMessage() {
131 return _message;
132 }
133
134 public String getMessageAndLocation() {
135 return _message + " " + _location;
136 }
137
138 public int getLineNumber() {
139 return _lineNumber;
140 }
141
142 public int getColumnNumber() {
143 return _columnNumber;
144 }
145
146 private Locale _locale;
147 private String _location;
148 private String _message;
149 private int _lineNumber;
150 private int _columnNumber;
151
152 }