001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.xsl;
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    /**
034     * @author Raymond Aug??
035     */
036    public class XSLErrorListener implements ErrorListener {
037    
038            public XSLErrorListener(Locale locale) {
039                    _locale = locale;
040            }
041    
042            @Override
043            public void error(TransformerException exception)
044                    throws TransformerException {
045    
046                    setLocation(exception);
047    
048                    throw exception;
049            }
050    
051            @Override
052            public void fatalError(TransformerException exception)
053                    throws TransformerException {
054    
055                    setLocation(exception);
056    
057                    throw exception;
058            }
059    
060            public int getColumnNumber() {
061                    return _columnNumber;
062            }
063    
064            public int getLineNumber() {
065                    return _lineNumber;
066            }
067    
068            public String getLocation() {
069                    return _location;
070            }
071    
072            public String getMessage() {
073                    return _message;
074            }
075    
076            public String getMessageAndLocation() {
077                    return _message + " " + _location;
078            }
079    
080            public void setLocation(Throwable exception) {
081                    SourceLocator locator = null;
082                    Throwable cause = exception;
083                    Throwable rootCause = null;
084    
085                    while (cause != null) {
086                            if (cause instanceof SAXParseException) {
087                                    locator = new SAXSourceLocator((SAXParseException)cause);
088                                    rootCause = cause;
089                            }
090                            else if (cause instanceof TransformerException) {
091                                    SourceLocator causeLocator =
092                                            ((TransformerException)cause).getLocator();
093    
094                                    if (causeLocator != null) {
095                                            locator = causeLocator;
096                                            rootCause = cause;
097                                    }
098                            }
099    
100                            if (cause instanceof TransformerException) {
101                                    cause = ((TransformerException)cause).getCause();
102                            }
103                            else if (cause instanceof WrappedRuntimeException) {
104                                    cause = ((WrappedRuntimeException)cause).getException();
105                            }
106                            else if (cause instanceof SAXException) {
107                                    cause = ((SAXException)cause).getException();
108                            }
109                            else {
110                                    cause = null;
111                            }
112                    }
113    
114                    _message = rootCause.getMessage();
115    
116                    if (locator != null) {
117                            _lineNumber = locator.getLineNumber();
118                            _columnNumber = locator.getColumnNumber();
119    
120                            StringBundler sb = new StringBundler(8);
121    
122                            sb.append(LanguageUtil.get(_locale, "line"));
123                            sb.append(" #");
124                            sb.append(locator.getLineNumber());
125                            sb.append("; ");
126                            sb.append(LanguageUtil.get(_locale, "column"));
127                            sb.append(" #");
128                            sb.append(locator.getColumnNumber());
129                            sb.append("; ");
130    
131                            _location = sb.toString();
132                    }
133                    else {
134                            _location = StringPool.BLANK;
135                    }
136            }
137    
138            @Override
139            public void warning(TransformerException exception)
140                    throws TransformerException {
141    
142                    setLocation(exception);
143    
144                    throw exception;
145            }
146    
147            private int _columnNumber;
148            private int _lineNumber;
149            private Locale _locale;
150            private String _location;
151            private String _message;
152    
153    }