001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.event;
015
016import static ch.qos.logback.core.CoreConstants.XML_PARSING;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.xml.parsers.ParserConfigurationException;
024import javax.xml.parsers.SAXParser;
025import javax.xml.parsers.SAXParserFactory;
026
027import org.xml.sax.Attributes;
028import org.xml.sax.InputSource;
029import org.xml.sax.Locator;
030import org.xml.sax.SAXException;
031import org.xml.sax.SAXParseException;
032import org.xml.sax.helpers.DefaultHandler;
033
034import ch.qos.logback.core.Context;
035import ch.qos.logback.core.joran.spi.ElementPath;
036import ch.qos.logback.core.joran.spi.JoranException;
037import ch.qos.logback.core.spi.ContextAware;
038import ch.qos.logback.core.spi.ContextAwareImpl;
039import ch.qos.logback.core.status.Status;
040
041public class SaxEventRecorder extends DefaultHandler implements ContextAware {
042
043    final ContextAwareImpl contextAwareImpl;
044    final ElementPath elementPath;
045    List<SaxEvent> saxEventList = new ArrayList<SaxEvent>();
046    Locator locator;
047
048    public SaxEventRecorder(Context context) {
049        this(context, new ElementPath());
050    }
051
052    public SaxEventRecorder(Context context, ElementPath elementPath) {
053        contextAwareImpl = new ContextAwareImpl(context, this);
054        this.elementPath = elementPath;
055    }
056
057    final public void recordEvents(InputStream inputStream) throws JoranException {
058        recordEvents(new InputSource(inputStream));
059    }
060
061    public void recordEvents(InputSource inputSource) throws JoranException {
062        SAXParser saxParser = buildSaxParser();
063        try {
064            saxParser.parse(inputSource, this);
065            return;
066        } catch (IOException ie) {
067            handleError("I/O error occurred while parsing xml file", ie);
068        } catch (SAXException se) {
069            // Exception added into StatusManager via Sax error handling. No need to add it again
070            throw new JoranException("Problem parsing XML document. See previously reported errors.", se);
071        } catch (Exception ex) {
072            handleError("Unexpected exception while parsing XML document.", ex);
073        }
074        throw new IllegalStateException("This point can never be reached");
075    }
076
077    private void handleError(String errMsg, Throwable t) throws JoranException {
078        addError(errMsg, t);
079        throw new JoranException(errMsg, t);
080    }
081
082    private SAXParser buildSaxParser() throws JoranException {
083        try {
084            SAXParserFactory spf = SAXParserFactory.newInstance();
085            spf.setValidating(false);
086            // spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
087            // See LOGBACK-1465
088            spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
089            spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
090            spf.setNamespaceAware(true);
091            return spf.newSAXParser();
092        } catch (ParserConfigurationException pce) {
093            String errMsg = "Error during SAX paser configuration. See https://logback.qos.ch/codes.html#saxParserConfiguration";
094            addError(errMsg, pce);
095            throw new JoranException(errMsg, pce);
096        }  catch (SAXException pce) {
097            String errMsg = "Error during parser creation or parser configuration";
098            addError(errMsg, pce);
099            throw new JoranException(errMsg, pce);
100        } 
101    }
102
103    public void startDocument() {
104    }
105
106    public Locator getLocator() {
107        return locator;
108    }
109
110    public void setDocumentLocator(Locator l) {
111        locator = l;
112    }
113
114    protected boolean shouldIgnoreForElementPath(String tagName) {
115        return false;
116    }
117
118    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
119
120        String tagName = getTagName(localName, qName);
121        if (!shouldIgnoreForElementPath(tagName)) {
122            elementPath.push(tagName);
123        }
124        ElementPath current = elementPath.duplicate();
125        saxEventList.add(new StartEvent(current, namespaceURI, localName, qName, atts, getLocator()));
126    }
127
128    public void characters(char[] ch, int start, int length) {
129        String bodyStr = new String(ch, start, length);
130        SaxEvent lastEvent = getLastEvent();
131        if (lastEvent instanceof BodyEvent) {
132            BodyEvent be = (BodyEvent) lastEvent;
133            be.append(bodyStr);
134        } else {
135            // ignore space only text if the previous event is not a BodyEvent
136            if (!isSpaceOnly(bodyStr)) {
137                saxEventList.add(new BodyEvent(bodyStr, getLocator()));
138            }
139        }
140    }
141
142    boolean isSpaceOnly(String bodyStr) {
143        String bodyTrimmed = bodyStr.trim();
144        return (bodyTrimmed.length() == 0);
145    }
146
147    SaxEvent getLastEvent() {
148        if (saxEventList.isEmpty()) {
149            return null;
150        }
151        int size = saxEventList.size();
152        return saxEventList.get(size - 1);
153    }
154
155    public void endElement(String namespaceURI, String localName, String qName) {
156        saxEventList.add(new EndEvent(namespaceURI, localName, qName, getLocator()));
157        String tagName = getTagName(localName, qName);
158        if (!shouldIgnoreForElementPath(tagName)) {
159            elementPath.pop();
160        }
161    }
162
163    String getTagName(String localName, String qName) {
164        String tagName = localName;
165        if ((tagName == null) || (tagName.length() < 1)) {
166            tagName = qName;
167        }
168        return tagName;
169    }
170
171    public void error(SAXParseException spe) throws SAXException {
172        addError(XML_PARSING + " - Parsing error on line " + spe.getLineNumber() + " and column "
173                + spe.getColumnNumber());
174        addError(spe.toString());
175    }
176
177    public void fatalError(SAXParseException spe) throws SAXException {
178        addError(XML_PARSING + " - Parsing fatal error on line " + spe.getLineNumber() + " and column "
179                + spe.getColumnNumber());
180        addError(spe.toString());
181    }
182
183    public void warning(SAXParseException spe) throws SAXException {
184        addWarn(XML_PARSING + " - Parsing warning on line " + spe.getLineNumber() + " and column "
185                + spe.getColumnNumber(), spe);
186    }
187
188    public void addError(String msg) {
189        contextAwareImpl.addError(msg);
190    }
191
192    public void addError(String msg, Throwable ex) {
193        contextAwareImpl.addError(msg, ex);
194    }
195
196    public void addInfo(String msg) {
197        contextAwareImpl.addInfo(msg);
198    }
199
200    public void addInfo(String msg, Throwable ex) {
201        contextAwareImpl.addInfo(msg, ex);
202    }
203
204    public void addStatus(Status status) {
205        contextAwareImpl.addStatus(status);
206    }
207
208    public void addWarn(String msg) {
209        contextAwareImpl.addWarn(msg);
210    }
211
212    public void addWarn(String msg, Throwable ex) {
213        contextAwareImpl.addWarn(msg, ex);
214    }
215
216    public Context getContext() {
217        return contextAwareImpl.getContext();
218    }
219
220    public void setContext(Context context) {
221        contextAwareImpl.setContext(context);
222    }
223
224    public List<SaxEvent> getSaxEventList() {
225        return saxEventList;
226    }
227
228}