001    /**
002     * Copyright (c) 2000-2012 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.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    /**
034     * @author Raymond Augé
035     */
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 int getColumnNumber() {
059                    return _columnNumber;
060            }
061    
062            public int getLineNumber() {
063                    return _lineNumber;
064            }
065    
066            public String getLocation() {
067                    return _location;
068            }
069    
070            public String getMessage() {
071                    return _message;
072            }
073    
074            public String getMessageAndLocation() {
075                    return _message + " " + _location;
076            }
077    
078            public void setLocation(Throwable exception) {
079                    SourceLocator locator = null;
080                    Throwable cause = exception;
081                    Throwable rootCause = null;
082    
083                    while (cause != null) {
084                            if (cause instanceof SAXParseException) {
085                                    locator = new SAXSourceLocator((SAXParseException)cause);
086                                    rootCause = cause;
087                            }
088                            else if (cause instanceof TransformerException) {
089                                    SourceLocator causeLocator =
090                                            ((TransformerException)cause).getLocator();
091    
092                                    if (causeLocator != null) {
093                                            locator = causeLocator;
094                                            rootCause = cause;
095                                    }
096                            }
097    
098                            if (cause instanceof TransformerException) {
099                                    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                            _lineNumber = locator.getLineNumber();
116                            _columnNumber = locator.getColumnNumber();
117    
118                            StringBundler sb = new StringBundler(8);
119    
120                            sb.append(LanguageUtil.get(_locale, "line"));
121                            sb.append(" #");
122                            sb.append(locator.getLineNumber());
123                            sb.append("; ");
124                            sb.append(LanguageUtil.get(_locale, "column"));
125                            sb.append(" #");
126                            sb.append(locator.getColumnNumber());
127                            sb.append("; ");
128    
129                            _location = sb.toString();
130                    }
131                    else {
132                            _location = StringPool.BLANK;
133                    }
134            }
135    
136            public void warning(TransformerException exception)
137                    throws TransformerException {
138    
139                    setLocation(exception);
140    
141                    throw exception;
142            }
143    
144            private int _columnNumber;
145            private int _lineNumber;
146            private Locale _locale;
147            private String _location;
148            private String _message;
149    
150    }