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