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: */
017: package org.apache.cocoon.util;
018:
019: import org.apache.avalon.framework.logger.Logger;
020:
021: import javax.xml.transform.ErrorListener;
022: import javax.xml.transform.SourceLocator;
023: import javax.xml.transform.TransformerException;
024:
025: /**
026: * This ErrorListener simply logs the exception and in
027: * case of an fatal-error the exception is rethrown.
028: * Warnings and errors are ignored.
029: *
030: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
031: * @version CVS $Id: TraxErrorHandler.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public class TraxErrorHandler implements ErrorListener {
034:
035: private StringBuffer warnings = new StringBuffer(
036: "Errors in XSLT transformation:\n");
037: private Logger logger = null;
038:
039: public TraxErrorHandler(Logger logger) {
040: this .logger = logger;
041: }
042:
043: public void warning(TransformerException exception)
044: throws TransformerException {
045: final String message = getMessage(exception);
046: if (this .logger != null) {
047: this .logger.warn(message);
048: } else {
049: System.out.println("WARNING: " + message);
050: }
051: warnings.append("Warning: ");
052: warnings.append(message);
053: warnings.append("\n");
054: }
055:
056: public void error(TransformerException exception)
057: throws TransformerException {
058: final String message = getMessage(exception);
059: if (this .logger != null) {
060: this .logger.error(message, exception);
061: } else {
062: System.out.println("ERROR: " + message);
063: }
064: warnings.append("Error: ");
065: warnings.append(message);
066: warnings.append("\n");
067: }
068:
069: public void fatalError(TransformerException exception)
070: throws TransformerException {
071: final String message = getMessage(exception);
072: if (this .logger != null) {
073: this .logger.fatalError(message, exception);
074: } else {
075: System.out.println("FATAL-ERROR: " + message);
076: }
077: warnings.append("Fatal: ");
078: warnings.append(message);
079: warnings.append("\n");
080: try {
081: throw new TransformerException(warnings.toString());
082: } finally {
083: warnings = new StringBuffer();
084: }
085: }
086:
087: private String getMessage(TransformerException exception) {
088:
089: SourceLocator locator = exception.getLocator();
090: if (null != locator) {
091: String id = (!locator.getPublicId().equals(
092: locator.getPublicId())) ? locator.getPublicId()
093: : (null != locator.getSystemId()) ? locator
094: .getSystemId() : "SystemId Unknown";
095: return "File " + id + "; Line " + locator.getLineNumber()
096: + "; Column " + locator.getColumnNumber() + "; "
097: + exception.getMessage();
098: }
099: return exception.getMessage();
100: }
101: }
|