001/* Handle MoML Parsing Errors. 002 003 Copyright (c) 2000-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.moml; 029 030import java.io.OutputStream; 031import java.io.PrintStream; 032 033import ptolemy.kernel.util.KernelException; 034import ptolemy.kernel.util.NamedObj; 035 036/////////////////////////////////////////////////////////////////// 037//// StreamErrorHandler 038 039/** 040 Basic error handler for the MoMLParser class. This error handler reports 041 errors to a stream or to standard error, and requests that parsing continue. 042 043 @see MoMLParser 044 @author Edward A. Lee 045 @version $Id$ 046 @since Ptolemy II 2.0 047 @Pt.ProposedRating Red (eal) 048 @Pt.AcceptedRating Red (reviewmoderator) 049 */ 050public class StreamErrorHandler implements ErrorHandler { 051 /////////////////////////////////////////////////////////////////// 052 //// constructors //// 053 054 /** Create an error handler that sends messages to the standard error. 055 */ 056 public StreamErrorHandler() { 057 _output = System.err; 058 } 059 060 /** Create an error handler that sends messages to the specified stream. 061 * @param out The OutputStream 062 */ 063 public StreamErrorHandler(OutputStream out) { 064 _output = new PrintStream(out); 065 } 066 067 /////////////////////////////////////////////////////////////////// 068 //// public methods //// 069 070 /** Enable or disable skipping of errors. This method does nothing. 071 * @param enable True to enable skipping, false to disable. 072 */ 073 @Override 074 public void enableErrorSkipping(boolean enable) { 075 } 076 077 /** Handle an error by printing a description of the error to 078 * the stream specified in the constructor. 079 * @param element The XML element that triggered the error. 080 * @param context The container object for the element. 081 * @param exception The exception that was thrown. 082 * @return CONTINUE to request skipping this element. 083 */ 084 @Override 085 public int handleError(String element, NamedObj context, 086 Throwable exception) { 087 _output.println("Error encountered in:\n" + element + "\n" 088 + KernelException.stackTraceToString(exception)); 089 return CONTINUE; 090 } 091 092 /////////////////////////////////////////////////////////////////// 093 //// private variables //// 094 private PrintStream _output; 095}