001    /*
002     *   Copyright (c) 2009 The JOMC Project
003     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
004     *   All rights reserved.
005     *
006     *   Redistribution and use in source and binary forms, with or without
007     *   modification, are permitted provided that the following conditions
008     *   are met:
009     *
010     *     o Redistributions of source code must retain the above copyright
011     *       notice, this list of conditions and the following disclaimer.
012     *
013     *     o Redistributions in binary form must reproduce the above copyright
014     *       notice, this list of conditions and the following disclaimer in
015     *       the documentation and/or other materials provided with the
016     *       distribution.
017     *
018     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
019     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
022     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
027     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     *
030     *   $Id: ModelExceptionErrorHandler.java 573 2009-09-25 16:05:05Z schulte2005 $
031     *
032     */
033    package org.jomc.model;
034    
035    import java.util.LinkedList;
036    import java.util.List;
037    import java.util.logging.Level;
038    import org.xml.sax.ErrorHandler;
039    import org.xml.sax.SAXException;
040    import org.xml.sax.SAXParseException;
041    
042    /**
043     * {@code ErrorHander} collecting {@code ModelException.Detail}s.
044     *
045     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a>
046     * @version $Id: ModelExceptionErrorHandler.java 573 2009-09-25 16:05:05Z schulte2005 $
047     *
048     * @see #getDetails()
049     */
050    final class ModelExceptionErrorHandler implements ErrorHandler
051    {
052    
053        /** The details of the instance. */
054        private final List<ModelException.Detail> details = new LinkedList<ModelException.Detail>();
055    
056        /** Creates a new {@code ModelExceptionErrorHandler} instance. */
057        ModelExceptionErrorHandler()
058        {
059            super();
060        }
061    
062        /**
063         * Gets the details of the instance.
064         *
065         * @return The details of the instance.
066         */
067        public List<ModelException.Detail> getDetails()
068        {
069            return this.details;
070        }
071    
072        public void warning( final SAXParseException exception ) throws SAXException
073        {
074            this.getDetails().add( new ModelException.Detail(
075                exception.getClass().getName(), Level.WARNING, exception.getMessage() ) );
076    
077        }
078    
079        public void error( final SAXParseException exception ) throws SAXException
080        {
081            this.getDetails().add( new ModelException.Detail(
082                exception.getClass().getName(), Level.SEVERE, exception.getMessage() ) );
083    
084            throw exception;
085        }
086    
087        public void fatalError( final SAXParseException exception ) throws SAXException
088        {
089            this.getDetails().add( new ModelException.Detail(
090                exception.getClass().getName(), Level.SEVERE, exception.getMessage() ) );
091    
092            throw exception;
093        }
094    
095    }