001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env;
018
019import java.io.Serializable;
020
021import javax.xml.stream.Location;
022import javax.xml.stream.XMLReporter;
023import javax.xml.stream.XMLStreamException;
024
025import org.apache.commons.scxml2.ErrorReporter;
026import org.apache.commons.scxml2.SCXMLListener;
027import org.apache.commons.scxml2.model.EnterableState;
028import org.apache.commons.scxml2.model.Transition;
029import org.apache.commons.scxml2.model.TransitionTarget;
030import org.xml.sax.ErrorHandler;
031import org.xml.sax.SAXException;
032import org.xml.sax.SAXParseException;
033
034/**
035 * A simple tracer connected to Apache Commons Logging.
036 *
037 */
038public class Tracer implements ErrorHandler, ErrorReporter,
039                               SCXMLListener, Serializable, XMLReporter {
040
041    /** Serial version UID. */
042    private static final long serialVersionUID = 1L;
043    /** ErrorHandler delegate. */
044    private ErrorHandler errHandler;
045    /** ErrorReporter delegate. */
046    private ErrorReporter errReporter;
047    /** SCXMLListener delegate. */
048    private SCXMLListener scxmlListener;
049    /** XMLReporter delegate. */
050    private XMLReporter xmlReporter;
051
052    /**
053     * Constructor.
054     */
055    public Tracer() {
056        super();
057        errHandler = new SimpleErrorHandler();
058        errReporter = new SimpleErrorReporter();
059        scxmlListener = new SimpleSCXMLListener();
060        xmlReporter = new SimpleXMLReporter();
061    }
062
063    /**
064     * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
065     */
066    public void warning(final SAXParseException exception)
067    throws SAXException {
068        errHandler.warning(exception);
069    }
070
071    /**
072     * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
073     */
074    public void error(final SAXParseException exception)
075    throws SAXException {
076        errHandler.error(exception);
077    }
078
079    /**
080     * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
081     */
082    public void fatalError(final SAXParseException exception)
083    throws SAXException {
084        errHandler.fatalError(exception);
085    }
086
087    /**
088     * @see ErrorReporter#onError(String, String, Object)
089     */
090    public void onError(final String errCode, final String errDetail,
091            final Object errCtx) {
092        errReporter.onError(errCode, errDetail, errCtx);
093    }
094
095    /**
096     * @see SCXMLListener#onEntry(EnterableState)
097     */
098    public void onEntry(final EnterableState state) {
099        scxmlListener.onEntry(state);
100    }
101
102    /**
103     * @see SCXMLListener#onExit(EnterableState)
104     */
105    public void onExit(final EnterableState state) {
106        scxmlListener.onExit(state);
107    }
108
109    /**
110     * @see SCXMLListener#onTransition(TransitionTarget,TransitionTarget,Transition,String)
111     */
112    public void onTransition(final TransitionTarget from,
113            final TransitionTarget to, final Transition transition, final String event) {
114        scxmlListener.onTransition(from, to, transition, event);
115    }
116
117    /**
118     * @see XMLReporter#report(String, String, Object, Location)
119     */
120        public void report(final String message, final String errorType, final Object relatedInformation,
121                        final Location location)
122        throws XMLStreamException {
123                xmlReporter.report(message, errorType, relatedInformation, location);
124        }
125
126}
127