001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringPool;
020
021 import java.util.Locale;
022
023 import javax.xml.transform.ErrorListener;
024 import javax.xml.transform.SourceLocator;
025 import javax.xml.transform.TransformerException;
026
027 import org.apache.xml.utils.SAXSourceLocator;
028 import org.apache.xml.utils.WrappedRuntimeException;
029
030 import org.xml.sax.SAXException;
031 import org.xml.sax.SAXParseException;
032
033
036 public class XSLErrorListener implements ErrorListener {
037
038 public XSLErrorListener(Locale locale) {
039 _locale = locale;
040 }
041
042 public void error(TransformerException exception)
043 throws TransformerException {
044
045 setLocation(exception);
046
047 throw exception;
048 }
049
050 public void fatalError(TransformerException exception)
051 throws TransformerException {
052
053 setLocation(exception);
054
055 throw exception;
056 }
057
058 public void warning(TransformerException exception)
059 throws TransformerException {
060
061 setLocation(exception);
062
063 throw exception;
064 }
065
066 public void setLocation(Throwable exception) {
067 SourceLocator locator = null;
068 Throwable cause = exception;
069 Throwable rootCause = null;
070
071 while (cause != null) {
072 if (cause instanceof SAXParseException) {
073 locator = new SAXSourceLocator((SAXParseException)cause);
074 rootCause = cause;
075 }
076 else if (cause instanceof TransformerException) {
077 SourceLocator causeLocator =
078 ((TransformerException)cause).getLocator();
079
080 if (causeLocator != null) {
081 locator = causeLocator;
082 rootCause = cause;
083 }
084 }
085
086 if (cause instanceof TransformerException) {
087 cause = ((TransformerException)cause).getCause();
088 }
089 else if (cause instanceof WrappedRuntimeException) {
090 cause = ((WrappedRuntimeException)cause).getException();
091 }
092 else if (cause instanceof SAXException) {
093 cause = ((SAXException)cause).getException();
094 }
095 else {
096 cause = null;
097 }
098 }
099
100 _message = rootCause.getMessage();
101
102 if (locator != null) {
103 _lineNumber = locator.getLineNumber();
104 _columnNumber = locator.getColumnNumber();
105
106 StringBundler sb = new StringBundler(8);
107
108 sb.append(LanguageUtil.get(_locale, "line"));
109 sb.append(" #");
110 sb.append(locator.getLineNumber());
111 sb.append("; ");
112 sb.append(LanguageUtil.get(_locale, "column"));
113 sb.append(" #");
114 sb.append(locator.getColumnNumber());
115 sb.append("; ");
116
117 _location = sb.toString();
118 }
119 else {
120 _location = StringPool.BLANK;
121 }
122 }
123
124 public String getLocation() {
125 return _location;
126 }
127
128 public String getMessage() {
129 return _message;
130 }
131
132 public String getMessageAndLocation() {
133 return _message + " " + _location;
134 }
135
136 public int getLineNumber() {
137 return _lineNumber;
138 }
139
140 public int getColumnNumber() {
141 return _columnNumber;
142 }
143
144 private Locale _locale;
145 private String _location;
146 private String _message;
147 private int _lineNumber;
148 private int _columnNumber;
149
150 }