001: /*
002: * $Id: StaxErrorReporter.java,v 1.2 2006/04/01 06:01:47 jeffsuttor Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream;
030:
031: import javax.xml.stream.Location;
032: import javax.xml.stream.XMLInputFactory;
033: import javax.xml.stream.XMLReporter;
034: import javax.xml.stream.XMLStreamException;
035: import com.sun.xml.stream.xerces.impl.msg.XMLMessageFormatter;
036: import com.sun.xml.stream.xerces.util.MessageFormatter;
037: import com.sun.xml.stream.xerces.xni.XMLLocator;
038: import com.sun.xml.stream.xerces.xni.XNIException;
039:
040: /**
041: *
042: * @author neeraj
043: */
044:
045: public class StaxErrorReporter extends XMLErrorReporter {
046:
047: protected XMLReporter fXMLReporter = null;
048:
049: /** Creates a new instance of StaxErrorReporter */
050: public StaxErrorReporter(PropertyManager propertyManager) {
051: super ();
052: putMessageFormatter(XMLMessageFormatter.XML_DOMAIN,
053: new XMLMessageFormatter());
054: reset(propertyManager);
055: }
056:
057: /** Creates a new instance of StaxErrorReporter
058: * If this constructor is used to create the object, one must invoke reset() on this object.
059: */
060: public StaxErrorReporter() {
061: super ();
062: putMessageFormatter(XMLMessageFormatter.XML_DOMAIN,
063: new XMLMessageFormatter());
064: }
065:
066: /**
067: *One must call reset before using any of the function.
068: */
069: public void reset(PropertyManager propertyManager) {
070: fXMLReporter = (XMLReporter) propertyManager
071: .getProperty(XMLInputFactory.REPORTER);
072: }
073:
074: /**
075: * Reports an error at a specific location.
076: *
077: * @param location The error location.
078: * @param domain The error domain.
079: * @param key The key of the error message.
080: * @param arguments The replacement arguments for the error message,
081: * if needed.
082: * @param severity The severity of the error.
083: *
084: * @see #SEVERITY_WARNING
085: * @see #SEVERITY_ERROR
086: * @see #SEVERITY_FATAL_ERROR
087: */
088: public void reportError(XMLLocator location, String domain,
089: String key, Object[] arguments, short severity)
090: throws XNIException {
091: // format error message and create parse exception
092: MessageFormatter messageFormatter = getMessageFormatter(domain);
093: String message;
094: if (messageFormatter != null) {
095: message = messageFormatter.formatMessage(fLocale, key,
096: arguments);
097: } else {
098: StringBuffer str = new StringBuffer();
099: str.append(domain);
100: str.append('#');
101: str.append(key);
102: int argCount = arguments != null ? arguments.length : 0;
103: if (argCount > 0) {
104: str.append('?');
105: for (int i = 0; i < argCount; i++) {
106: str.append(arguments[i]);
107: if (i < argCount - 1) {
108: str.append('&');
109: }
110: }
111: }
112: message = str.toString();
113: }
114:
115: //no reporter was specified
116: /**
117: * if (reporter == null) {
118: * reporter = new DefaultStaxErrorReporter();
119: * }
120: */
121:
122: // call error handler
123: switch (severity) {
124: case SEVERITY_WARNING: {
125: try {
126: if (fXMLReporter != null) {
127: fXMLReporter.report(message, "WARNING", null,
128: convertToStaxLocation(location));
129: }
130: } catch (XMLStreamException ex) {
131: //what we should be doing ?? if the user throws XMLStreamException
132: //REVISIT:
133: throw new XNIException(ex);
134: }
135: break;
136: }
137: case SEVERITY_ERROR: {
138: try {
139: if (fXMLReporter != null) {
140: fXMLReporter.report(message, "ERROR", null,
141: convertToStaxLocation(location));
142: }
143: } catch (XMLStreamException ex) {
144: //what we should be doing ?? if the user throws XMLStreamException
145: //REVISIT:
146: throw new XNIException(ex);
147: }
148: break;
149: }
150: case SEVERITY_FATAL_ERROR: {
151: if (!fContinueAfterFatalError) {
152: throw new XNIException(message);
153: }
154: break;
155: }
156: }
157:
158: }
159:
160: Location convertToStaxLocation(final XMLLocator location) {
161: return new Location() {
162: public int getColumnNumber() {
163: return location.getColumnNumber();
164: }
165:
166: public int getLineNumber() {
167: return location.getLineNumber();
168: }
169:
170: public String getPublicId() {
171: return location.getPublicId();
172: }
173:
174: public String getSystemId() {
175: return location.getLiteralSystemId();
176: }
177:
178: public int getCharacterOffset() {
179: return location.getCharacterOffset();
180: }
181:
182: };
183: }
184:
185: }
|