001/* 002 * ============================================================================ 003 * Copyright © 2002-2023 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 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, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.xml.parse; 019 020import static java.lang.String.format; 021import static java.lang.System.err; 022import static org.apiguardian.api.API.Status.STABLE; 023import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 024 025import org.apiguardian.api.API; 026import org.tquadrat.foundation.annotation.ClassVersion; 027import org.xml.sax.ErrorHandler; 028import org.xml.sax.SAXParseException; 029 030/** 031 * <p>{@summary This implementation for a 032 * {@linkplain ErrorHandler XML Error handler} 033 * will write the error messages to 034 * {@link System#err System.err}.} 035 * The one and only instance for this class can be obtained using the 036 * {@link #INSTANCE} 037 * constant.</p> 038 * <p>This simple initialisation pattern was chosen instead of a full 039 * Singleton setup because the error handler does not maintain a state.</p> 040 * 041 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 042 * @version $Id: DefaultErrorHandler.java 1071 2023-09-30 01:49:32Z tquadrat $ 043 * @since 0.0.5 044 * 045 * @UMLGraph.link 046 */ 047@SuppressWarnings( "UseOfSystemOutOrSystemErr" ) 048@ClassVersion( sourceVersion = "$Id: DefaultErrorHandler.java 1071 2023-09-30 01:49:32Z tquadrat $" ) 049@API( status = STABLE, since = "0.0.5" ) 050public final class DefaultErrorHandler implements ErrorHandler 051{ 052 /*-----------*\ 053 ====** Constants **======================================================== 054 \*-----------*/ 055 /** 056 * The Error Message. 057 */ 058 public static final String MSG_XMLError = "XML Parsing Error: %5$s - SystemId %1$s, PublicId %2$s, Line %3$d, Column %4$d"; 059 060 /** 061 * The Fatal Error Message. 062 */ 063 public static final String MSG_XMLFatal = "Fatal XML Parsing Error: %5$s - SystemId %1$s, PublicId %2$s, Line %3$d, Column %4$d"; 064 065 /** 066 * The Warning Message. 067 */ 068 public static final String MSG_XMLWarning = "XML Parsing Warning: %5$s - SystemId %1$s, PublicId %2$s, Line %3$d, Column %4$d"; 069 070 /*------------------------*\ 071 ====** Static Initialisations **=========================================== 072 \*------------------------*/ 073 /** 074 * The one and only instance of this class. 075 */ 076 public static final DefaultErrorHandler INSTANCE = new DefaultErrorHandler(); 077 078 /*--------------*\ 079 ====** Constructors **===================================================== 080 \*--------------*/ 081 /** 082 * Creates a new {@code DefaultErrorHandler} instance. 083 */ 084 private DefaultErrorHandler() { /* Does nothing! */ } 085 086 /*---------*\ 087 ====** Methods **========================================================== 088 \*---------*/ 089 /** 090 * Receives and processes notification of a recoverable parser error. This 091 * implementation will just print the error to {@code System.out}.. 092 * 093 * @param exception The error exception. 094 */ 095 @Override 096 public final void error( final SAXParseException exception ) 097 { 098 final var publicId = requireNonNullArgument( exception, "exception" ).getPublicId(); 099 final var systemId = exception.getSystemId(); 100 final var column = exception.getColumnNumber(); 101 final var line = exception.getLineNumber(); 102 //noinspection RedundantStringFormatCall 103 err.println( format( MSG_XMLError, systemId, publicId, line, column, exception.getMessage() ) ); 104 } // error() 105 106 /** 107 * Receives and processes notification of a fatal parser error. This 108 * implementation will just print the error to {@code System.out}.. 109 * 110 * @param exception The error exception. 111 */ 112 @Override 113 public final void fatalError( final SAXParseException exception ) 114 { 115 final var publicId = requireNonNullArgument( exception, "exception" ).getPublicId(); 116 final var systemId = exception.getSystemId(); 117 final var column = exception.getColumnNumber(); 118 final var line = exception.getLineNumber(); 119 //noinspection RedundantStringFormatCall 120 err.println( format( MSG_XMLFatal, systemId, publicId, line, column, exception.getLocalizedMessage() ) ); 121 } // fatalError() 122 123 /** 124 * Receives and processes notification of a parser warning. This 125 * implementation will just print the warning to {@code System.out}.. 126 * 127 * @param exception The error exception. 128 */ 129 @Override 130 public final void warning( final SAXParseException exception ) 131 { 132 final var publicId = requireNonNullArgument( exception, "exception" ).getPublicId(); 133 final var systemId = exception.getSystemId(); 134 final var column = exception.getColumnNumber(); 135 final var line = exception.getLineNumber(); 136 //noinspection RedundantStringFormatCall 137 err.println( format( MSG_XMLWarning, systemId, publicId, line, column, exception.getLocalizedMessage() ) ); 138 } // warning() 139} 140// class DefaultErrorHandler 141 142/* 143 * End of File 144 */