throws ProtocolException {
super(value);
// Create the SAX and DOM Parsers, and link them.
MCSDOMContentHandler domParser = new MCSDOMContentHandler();
// Note: explicit package for SAXParser to be clear about it's source.
XMLReader saxParser =
new com.volantis.xml.xerces.parsers.SAXParser();
// No entity resolver, so SAX parser will do default entity resolution.
// Paul says this is OK.
// No dtd handler, so any dtd events will be ignored, which is fine
// since we do not expect to get any for XML "fragments".
// Use the DOM parsers nodeValue handler.
saxParser.setContentHandler(domParser);
// Use a specific error handler since we have specific needs here.
saxParser.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException e) {
// Log and continue.
logger.warn("parsing-problem", new Object[]{DOMScript.this},
adjust(e));
}
public void error(SAXParseException e) throws SAXException {
// Adjust and throw the exception, it will be logged above.
throw adjust(e);
}
public void fatalError(SAXParseException e) throws SAXException {
// Adjust and throw the exception, it will be logged above.
throw adjust(e);
}
/**
* Adjusts a SAXParseException so that it's column number does not
* include the synthetic root element that we add before parsing.
*
* @param e original exception
* @return the adjusted exception.
*/
private SAXParseException adjust(SAXParseException e) {
int colNo = e.getColumnNumber() - ROOT_ELEMENT.length() - 2;
return new ExtendedSAXParseException(e.getLocalizedMessage(), null, null,
e.getLineNumber(), colNo);
}
});
// Find the XML we need to parse. Note that we wrap the task in an
// element to convert even simple text into valid XML.
String validXml = "<" + ROOT_ELEMENT + ">" + value +
"</" + ROOT_ELEMENT + ">";
try {
// Parse the XML.
StringReader stringReader = new StringReader(validXml);
InputSource source = new InputSource(stringReader);
saxParser.parse(source);
} catch (SAXException e) {
throw new ProtocolException(
exceptionLocalizer.format("parsing-error", this), e);
} catch (IOException e) {
// NOTE: we are currently reading from a string, so this ought
// never to happen anyway.
throw new ProtocolException(
exceptionLocalizer.format("parsing-error", this), e);
}
// Add the parsed nodeValue into the dom, minus the root element.
Document document = domParser.getDocument();
Element root = document.getRootElement();
// Save our instance variables.
nodeValue = root.getHead();
}