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