1/* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.xml.internal.stream; 27 28 29 30import javax.xml.stream.Location; 31import javax.xml.stream.XMLInputFactory; 32import javax.xml.stream.XMLReporter; 33import javax.xml.stream.XMLStreamException; 34import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 35import com.sun.org.apache.xerces.internal.util.MessageFormatter; 36import com.sun.org.apache.xerces.internal.xni.XMLLocator; 37import com.sun.org.apache.xerces.internal.xni.XNIException; 38 39import com.sun.org.apache.xerces.internal.impl.PropertyManager; 40import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 41 42/** 43 * 44 * @author neeraj 45 */ 46 47public class StaxErrorReporter extends XMLErrorReporter { 48 49 protected XMLReporter fXMLReporter = null ; 50 51 /** Creates a new instance of StaxErrorReporter */ 52 public StaxErrorReporter(PropertyManager propertyManager) { 53 super(); 54 putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, new XMLMessageFormatter()); 55 reset(propertyManager); 56 } 57 58 /** Creates a new instance of StaxErrorReporter 59 * If this constructor is used to create the object, one must invoke reset() on this object. 60 */ 61 public StaxErrorReporter() { 62 super(); 63 putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, new XMLMessageFormatter()); 64 } 65 66 /** 67 *One must call reset before using any of the function. 68 */ 69 public void reset(PropertyManager propertyManager){ 70 fXMLReporter = (XMLReporter)propertyManager.getProperty(XMLInputFactory.REPORTER); 71 } 72 /** 73 * Reports an error at a specific location. 74 * 75 * @param location The error location. 76 * @param domain The error domain. 77 * @param key The key of the error message. 78 * @param arguments The replacement arguments for the error message, 79 * if needed. 80 * @param severity The severity of the error. 81 * 82 * @see #SEVERITY_WARNING 83 * @see #SEVERITY_ERROR 84 * @see #SEVERITY_FATAL_ERROR 85 */ 86 public String reportError(XMLLocator location, 87 String domain, String key, Object[] arguments, 88 short severity) throws XNIException { 89 // format error message and create parse exception 90 MessageFormatter messageFormatter = getMessageFormatter(domain); 91 String message; 92 if (messageFormatter != null) { 93 message = messageFormatter.formatMessage(fLocale, key, arguments); 94 } 95 else { 96 StringBuffer str = new StringBuffer(); 97 str.append(domain); 98 str.append('#'); 99 str.append(key); 100 int argCount = arguments != null ? arguments.length : 0; 101 if (argCount > 0) { 102 str.append('?'); 103 for (int i = 0; i < argCount; i++) { 104 str.append(arguments[i]); 105 if (i < argCount -1) { 106 str.append('&'); 107 } 108 } 109 } 110 message = str.toString(); 111 } 112 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, convertToStaxLocation(location) ); 128 } 129 }catch(XMLStreamException ex){ 130 //what we should be doing ?? if the user throws XMLStreamException 131 //REVISIT: 132 throw new XNIException(ex); 133 } 134 break; 135 } 136 case SEVERITY_ERROR: { 137 try{ 138 if(fXMLReporter!= null){ 139 fXMLReporter.report(message, "ERROR", null, convertToStaxLocation(location) ); 140 } 141 }catch(XMLStreamException ex){ 142 //what we should be doing ?? if the user throws XMLStreamException 143 //REVISIT: 144 throw new XNIException(ex); 145 } 146 break; 147 } 148 case SEVERITY_FATAL_ERROR: { 149 if (!fContinueAfterFatalError) { 150 throw new XNIException(message); 151 } 152 break; 153 } 154 } 155 return message; 156 } 157 158 159 Location convertToStaxLocation(final XMLLocator location){ 160 return new Location(){ 161 public int getColumnNumber(){ 162 return location.getColumnNumber(); 163 } 164 165 public int getLineNumber(){ 166 return location.getLineNumber(); 167 } 168 169 public String getPublicId(){ 170 return location.getPublicId(); 171 } 172 173 public String getSystemId(){ 174 return location.getLiteralSystemId(); 175 } 176 177 public int getCharacterOffset(){ 178 return location.getCharacterOffset(); 179 } 180 public String getLocationURI(){ 181 return ""; 182 } 183 184 }; 185 } 186 187} 188