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