|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream; |
|
|
|
|
|
|
|
import javax.xml.stream.Location; |
|
import javax.xml.stream.XMLInputFactory; |
|
import javax.xml.stream.XMLReporter; |
|
import javax.xml.stream.XMLStreamException; |
|
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
|
import com.sun.org.apache.xerces.internal.util.MessageFormatter; |
|
import com.sun.org.apache.xerces.internal.xni.XMLLocator; |
|
import com.sun.org.apache.xerces.internal.xni.XNIException; |
|
|
|
import com.sun.org.apache.xerces.internal.impl.PropertyManager; |
|
import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; |
|
|
|
/** |
|
* |
|
* @author neeraj |
|
*/ |
|
|
|
public class StaxErrorReporter extends XMLErrorReporter { |
|
|
|
protected XMLReporter fXMLReporter = null ; |
|
|
|
|
|
public StaxErrorReporter(PropertyManager propertyManager) { |
|
super(); |
|
putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, new XMLMessageFormatter()); |
|
reset(propertyManager); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public StaxErrorReporter() { |
|
super(); |
|
putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, new XMLMessageFormatter()); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void reset(PropertyManager propertyManager){ |
|
fXMLReporter = (XMLReporter)propertyManager.getProperty(XMLInputFactory.REPORTER); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String reportError(XMLLocator location, |
|
String domain, String key, Object[] arguments, |
|
short severity) throws XNIException { |
|
|
|
MessageFormatter messageFormatter = getMessageFormatter(domain); |
|
String message; |
|
if (messageFormatter != null) { |
|
message = messageFormatter.formatMessage(fLocale, key, arguments); |
|
} |
|
else { |
|
StringBuffer str = new StringBuffer(); |
|
str.append(domain); |
|
str.append('#'); |
|
str.append(key); |
|
int argCount = arguments != null ? arguments.length : 0; |
|
if (argCount > 0) { |
|
str.append('?'); |
|
for (int i = 0; i < argCount; i++) { |
|
str.append(arguments[i]); |
|
if (i < argCount -1) { |
|
str.append('&'); |
|
} |
|
} |
|
} |
|
message = str.toString(); |
|
} |
|
|
|
|
|
|
|
//no reporter was specified |
|
/** |
|
* if (reporter == null) { |
|
* reporter = new DefaultStaxErrorReporter(); |
|
* } |
|
*/ |
|
|
|
|
|
switch (severity) { |
|
case SEVERITY_WARNING: { |
|
try{ |
|
if(fXMLReporter!= null){ |
|
fXMLReporter.report(message, "WARNING", null, convertToStaxLocation(location) ); |
|
} |
|
}catch(XMLStreamException ex){ |
|
//what we should be doing ?? if the user throws XMLStreamException |
|
|
|
throw new XNIException(ex); |
|
} |
|
break; |
|
} |
|
case SEVERITY_ERROR: { |
|
try{ |
|
if(fXMLReporter!= null){ |
|
fXMLReporter.report(message, "ERROR", null, convertToStaxLocation(location) ); |
|
} |
|
}catch(XMLStreamException ex){ |
|
//what we should be doing ?? if the user throws XMLStreamException |
|
|
|
throw new XNIException(ex); |
|
} |
|
break; |
|
} |
|
case SEVERITY_FATAL_ERROR: { |
|
if (!fContinueAfterFatalError) { |
|
throw new XNIException(message); |
|
} |
|
break; |
|
} |
|
} |
|
return message; |
|
} |
|
|
|
|
|
Location convertToStaxLocation(final XMLLocator location){ |
|
return new Location(){ |
|
public int getColumnNumber(){ |
|
return location.getColumnNumber(); |
|
} |
|
|
|
public int getLineNumber(){ |
|
return location.getLineNumber(); |
|
} |
|
|
|
public String getPublicId(){ |
|
return location.getPublicId(); |
|
} |
|
|
|
public String getSystemId(){ |
|
return location.getLiteralSystemId(); |
|
} |
|
|
|
public int getCharacterOffset(){ |
|
return location.getCharacterOffset(); |
|
} |
|
public String getLocationURI(){ |
|
return ""; |
|
} |
|
|
|
}; |
|
} |
|
|
|
} |