📄 saxhandlerbase.java
字号:
/*------------------------------------------------------------------------------Name: SaxHandlerBase.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Default handling of Sax callbacksVersion: $Id: SaxHandlerBase.java 15623 2006-10-27 18:30:00Z laghi $------------------------------------------------------------------------------*/package org.xmlBlaster.util;import java.util.logging.Logger;import java.util.logging.Level;import java.io.*;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXParseException;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.Locator;import org.xml.sax.ContentHandler;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.ext.LexicalHandler;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.SAXParser;import org.xmlBlaster.util.def.ErrorCode;/** * Default xmlBlaster handling of Sax2 callbacks and errors. * <p /> * You may use this as a base class for your SAX2 handling. */public class SaxHandlerBase implements ContentHandler, ErrorHandler, LexicalHandler{ private String ME = "SaxHandlerBase"; protected final Global glob; private static Logger log = Logger.getLogger(SaxHandlerBase.class.getName()); // The current location protected Locator locator = null; // private static final String DEFAULT_PARSER_NAME = // com.ibm.xml.parsers.SAXParser // .sun.xml.parser.ValidatingParser protected StringBuffer character = new StringBuffer(); /** The xml file read for logging only */ protected String xmlSource; /** * The original XML string in ASCII representation, for example: * <code> <qos></qos>"</code> */ protected String xmlLiteral; private boolean useLexicalHandler = false; /** * Constructs an new object. * You need to call the init() method to parse the XML string. */ public SaxHandlerBase() { // TODO: use specific glob and not Global - set to deprecated this(null); } public SaxHandlerBase(Global glob) { this.glob = (glob == null) ? Global.instance() : glob; if (log.isLoggable(Level.FINER)) log.fine("Creating new SaxHandlerBase"); } /* * This method parses the XML InputSource using the SAX parser. * @param inputSource The XML string */ protected void init(InputSource inputSource) throws XmlBlasterException { parse(inputSource); } /* * This method parses the XML InputSource using the SAX parser. * Note that it is not synchronized and not thread safe. * The derived class should synchronize. * @param inputSource For logging only (e.g. the XML file) or null * @param xmlLiteral The XML string */ protected void init(String xmlSource, InputSource inputSource) throws XmlBlasterException { this.xmlSource = xmlSource; parse(inputSource); } /* * This method parses the XML string using the SAX parser. * @param xmlLiteral The XML string */ protected void init(String xmlLiteral) throws XmlBlasterException { if (xmlLiteral == null) xmlLiteral = ""; this.xmlLiteral = xmlLiteral; if (xmlLiteral.length() > 0) { parse(xmlLiteral); } } /** * activates/deactivates the lexical handler. This can be used to get also the CDATA events */ public void setUseLexicalHandler(boolean useLexicalHandler) { this.useLexicalHandler = useLexicalHandler; } public boolean getUseLexicalHandler() { return this.useLexicalHandler; } private void parse(String xmlData) throws XmlBlasterException { /* byte[] xmlRaw = new byte[0]; try { //xmlRaw = xmlData.getBytes("windows-1252"); //xmlRaw = xmlData.getBytes("UTF-8"); xmlRaw = xmlData.getBytes(); //xmlRaw = xmlData.getBytes("UTF-16"); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } InputStream inBytes = new ByteArrayInputStream(xmlRaw); InputSource inputSource = new InputSource(inBytes); parse(inputSource); */ parse(new InputSource(new StringReader(xmlData))); } /** * Does the actual parsing * @param xmlData Quality of service in XML notation */ private void parse(InputSource xmlData) throws XmlBlasterException { try { SAXParserFactory spf = glob.getSAXParserFactory(); boolean validate = glob.getProperty().get("javax.xml.parsers.validation", false); spf.setValidating(validate); //if (log.isLoggable(Level.FINE)) log.trace(ME, "XML-Validation 'javax.xml.parsers.validation' set to " + validate); SAXParser sp = spf.newSAXParser(); XMLReader parser = sp.getXMLReader(); //parser.setEntityResolver(EntityResolver resolver); //parser.setFeature("http://xml.org/sax/features/validation", true); //parser.setFeature("http://apache.org/xml/features/validation/schema", true); //parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); parser.setContentHandler(this); parser.setErrorHandler(this); // !!! new MyErrorHandler ()); /* final boolean useLexicalHandler = true; // switch on to get CDATA events */ if (this.useLexicalHandler) { try { parser.setProperty("http://xml.org/sax/properties/lexical-handler", this); // register for startCDATA() etc. events } catch (SAXNotRecognizedException e) { log.warning("The SAX parser does not support the LexicalHandler interface, CDATA sections can't be restored" + e.toString()); } catch (SAXNotSupportedException e) { log.warning("The SAX parser does not support the LexicalHandler interface, CDATA sections can't be restored" + e.toString()); } } parser.parse(xmlData); } catch (Throwable e) { // In startElement(), endElement() you can use directly // throw new org.xml.sax.SAXException("Can't parse it", e); if (e instanceof org.xmlBlaster.util.StopParseException) { // This inctanceOf / and cast does not seem to work: do we have different classloaders? StopParseException stop = (StopParseException)e; if (log.isLoggable(Level.FINE)) log.fine("StopParseException: Parsing execution stopped half the way"); if (stop.hasError()) { throw stop.getXmlBlasterException(); } else { log.severe("StopParseException without embedded XmlBlasterException: " + e.toString()); } return; } if (e instanceof SAXException) { // Try to find an encapsulated XmlBlasterException ... SAXException saxE = (SAXException)e; if (log.isLoggable(Level.FINE)) log.fine("SAXException: Parsing execution stopped half the way"); Exception exc = saxE.getException(); if (exc instanceof XmlBlasterException) { XmlBlasterException stop = (XmlBlasterException)exc; String txt = (stop.getMessage() != null && stop.getMessage().length() > 0) ? stop.getMessage() : "Error while SAX parsing"; throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, txt, e); } else if (exc instanceof StopParseException) { StopParseException stop = (StopParseException)exc; if (stop.hasError()) { throw stop.getXmlBlasterException(); } } } String location = (locator == null) ? "" : locator.toString(); if (e instanceof org.xml.sax.SAXParseException) { location = getLocationString((SAXParseException)e); } else if (this.xmlSource != null) { location = this.xmlSource; } if (e.getMessage() != null && e.getMessage().indexOf("org.xmlBlaster.util.StopParseException") > -1) { // org.xml.sax.SAXParseException if (log.isLoggable(Level.FINE)) log.fine(location + ": Parsing execution stopped half the way: " + e.getMessage() + ": " + e.toString()); return; } if (log.isLoggable(Level.FINE)) { log.fine("Error while SAX parsing: " + location + ": " + e.toString() + "\n" + xmlData); e.printStackTrace();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -