001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.modeler.wsdl;
027:
028: import com.sun.tools.internal.xjc.api.ErrorListener;
029: import com.sun.tools.internal.ws.processor.util.ProcessorEnvironment;
030: import com.sun.xml.internal.ws.util.localization.LocalizableMessageFactory;
031: import org.xml.sax.SAXParseException;
032:
033: import java.util.ResourceBundle;
034: import java.text.MessageFormat;
035:
036: public class ConsoleErrorReporter implements ErrorListener {
037:
038: private LocalizableMessageFactory messageFactory;
039: private ProcessorEnvironment env;
040: private boolean printStackTrace;
041: private boolean hasError;
042:
043: public ConsoleErrorReporter(ProcessorEnvironment env,
044: boolean printStackTrace) {
045: this .env = env;
046: this .printStackTrace = printStackTrace;
047: messageFactory = new LocalizableMessageFactory(
048: "com.sun.tools.internal.ws.resources.model");
049: }
050:
051: public boolean hasError() {
052: return hasError;
053: }
054:
055: // will be null unless set in #error or #fatalError
056: //TODO: remove it after error handling is straightened
057: private Exception e;
058:
059: Exception getException() {
060: return e;
061: }
062:
063: public void error(SAXParseException e) {
064: hasError = true;
065: this .e = e;
066: if (printStackTrace)
067: e.printStackTrace();
068: env.error(messageFactory.getMessage(
069: "model.saxparser.exception", new Object[] {
070: e.getMessage(), getLocationString(e) }));
071: }
072:
073: public void fatalError(SAXParseException e) {
074: hasError = true;
075: this .e = e;
076: if (printStackTrace)
077: e.printStackTrace();
078:
079: env.error(messageFactory.getMessage(
080: "model.saxparser.exception", new Object[] {
081: e.getMessage(), getLocationString(e) }));
082: }
083:
084: public void warning(SAXParseException e) {
085: env.warn(messageFactory.getMessage("model.saxparser.exception",
086: new Object[] { e.getMessage(), getLocationString(e) }));
087: }
088:
089: /**
090: * Used to report possibly verbose information that
091: * can be safely ignored.
092: */
093: public void info(SAXParseException e) {
094: env.info(messageFactory.getMessage("model.saxparser.exception",
095: new Object[] { e.getMessage(), getLocationString(e) }));
096: }
097:
098: /**
099: * Returns the human readable string representation of the
100: * {@link org.xml.sax.Locator} part of the specified
101: * {@link SAXParseException}.
102: *
103: * @return non-null valid object.
104: */
105: protected final String getLocationString(SAXParseException e) {
106: if (e.getLineNumber() != -1 || e.getSystemId() != null) {
107: int line = e.getLineNumber();
108: return format("ConsoleErrorReporter.LineXOfY",
109: line == -1 ? "?" : Integer.toString(line),
110: getShortName(e.getSystemId()));
111: } else {
112: return format("ConsoleErrorReporter.UnknownLocation");
113: }
114: }
115:
116: /** Computes a short name of a given URL for display. */
117: private String getShortName(String url) {
118: if (url == null)
119: return format("ConsoleErrorReporter.UnknownLocation");
120: return url;
121: }
122:
123: private String format(String property, Object... args) {
124: String text = ResourceBundle.getBundle(
125: "com.sun.tools.internal.ws.resources.model").getString(
126: property);
127: return MessageFormat.format(text, args);
128: }
129:
130: }
|