1
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
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 }