📄 saxhandler.java
字号:
public void startEntity(String name) throws SAXException { if (D.ON && log.finerable()) log.finer("startEntity: " + name); //A "[dtd]" entity indicates the beginning of the external subset if (name.equals("[dtd]")) { _inInternSubset = false; return; } if (!isExpandEntityReferences() && !_inDTD && !entityToSkip(name)) { pushGroup(_factory.newEntityRef(name)); } } /** Tests whether the name is something that always expanded. */ private boolean entityToSkip(String name) { //To speed up the performance, we don't compare all strings //Rather, we use something similar to finite-state machine. switch (name.charAt(0)) { case 'a': return name.equals("amp") || name.equals("apos"); case 'g': return name.equals("gt"); case 'l': return name.equals("lt"); case 'q': return name.equals("quot"); } return false; } public void endEntity(String name) throws SAXException { if (D.ON && log.finerable()) log.finer("endEntity: " + name); if (name.equals("[dtd]")) { _inInternSubset = false; return; } if (!isExpandEntityReferences() && !_inDTD && !entityToSkip(name)) { popGroup(); } } public void startCDATA() throws SAXException { _inCData = true; } public void endCDATA() throws SAXException { _inCData = false; } public void comment(char[] ch, int start, int length) throws SAXException { if (length == 0 || isIgnoringComments()) return; //ignore zero length String data = new String(ch, start, length); if (_inDTD && _inInternSubset && !isExpandEntityReferences()) { _internSubset.append(" <!--").append(data).append("-->\n"); } if (!_inDTD && data.length() != 0) { addToCurrentGroup(_factory.newComment(data)); } } //-- org.xml.sax.ContentHandler --// public void startDocument() throws SAXException { _declNamespaces = new LinkedList(); _stack = new Stack(); _doc = _factory.newDocument(null, null); pushGroup(_doc); } public void endDocument() throws SAXException { popGroup(); assert(_stack.isEmpty()); _stack = null; _loc = null; _declNamespaces = null; } public void setDocumentLocator(Locator locator) { _loc = locator; } public void processingInstruction(String target, String data) throws SAXException { addToCurrentGroup(_factory.newProcessingInstruction(target, data)); } public void startPrefixMapping(String prefix, String uri) throws SAXException { if (D.ON && log.finerable()) log.finer("start prefix: " + prefix + ", " + uri + SimpleLocator.toString(_loc)); final Namespace ns = Namespace.getSpecial(prefix); if (ns == null || !ns.getURI().equals(uri)) _declNamespaces.add(0, new Namespace(prefix, uri)); //TY: add at the head to speed up the searching } public void endPrefixMapping(String prefix) throws SAXException { if (D.ON && log.finerable()) log.finer("end prefix: " + prefix + SimpleLocator.toString(_loc)); for (Iterator itr = _declNamespaces.iterator(); itr.hasNext();) { final Namespace ns = (Namespace) itr.next(); if (prefix.equals(ns.getPrefix())) { itr.remove(); break; } } } public void startElement(String nsURI, String lname, String tname, Attributes attrs) throws SAXException { if (D.ON && log.finerable()) log.finer("start element: nsURI=\"" + nsURI + "\", lname=" + lname + ", tname=" + tname + " attr#=" + attrs.getLength() + SimpleLocator.toString(_loc)); //create the element if (tname == null || tname.length() == 0) //just in case tname = lname; final Element element = newElement(nsURI, tname); //note: in crimson, lname might be empty, so... //Handle attributes for (int j=0, len=attrs.getLength(); j<len; j++) { String attQname = attrs.getQName(j); String attLname = attrs.getLocalName(j); if (attQname == null || attQname.length() == 0) //just in case attQname = attLname; final Attribute attr; int kp = attQname.indexOf(':'); if (kp >= 0) { final String prefix = attQname.substring(0, kp); final Namespace ns = element.getNamespace(prefix); if (ns == null) throw new SAXException("Unknown prefix: "+prefix+" at "+_loc+"\nDo you forget to turn on namespace-aware"); attr = _factory.newAttribute( ns, attQname.substring(kp + 1), attrs.getValue(j)); //if prefix, attLname might be empty so use attQname } else { attr = _factory.newAttribute( attLname == null || attLname.length() == 0 ? attQname: attLname, //crimson might use empty for AttLname attrs.getValue(j)); } attachLocator(attr); element.getAttributeItems().add(attr); //Don't use setAttribute, to can detect replicated attributes } } private Element newElement(String nsURI, String tname) throws SAXException { if (nsURI == null) nsURI = ""; final int j = tname.indexOf(':'); final String prefix = j >= 0 ? tname.substring(0, j): ""; final String lname = j >= 0 ? tname.substring(j + 1): tname; Namespace ns; final Group parent = getTopGroup(); if (parent instanceof Element) { ns = ((Element)parent).getNamespace(prefix); if (ns != null && !ns.getURI().equals(nsURI)) //override ns = null; //create a no-namespace element first } else { ns = Namespace.getSpecial(prefix); } if (ns == null) { if (_declNamespaces.size() > 0) { for (Iterator it = _declNamespaces.iterator(); it.hasNext();) { final Namespace n = (Namespace)it.next(); if (n.getPrefix().equals(prefix) && n.getURI().equals(nsURI)) { if (D.ON && log.finerable()) log.finer("Namespace found in _declNamespaces: "+n); ns = n; break; //found } } } if (ns == null && nsURI.length() > 0) { if (D.ON && log.finerable()) log.finer("Create namespace: "+prefix+" "+nsURI); ns = new Namespace(prefix, nsURI); } } final Element element = ns != null ? _factory.newElement(ns, lname): _factory.newElement(lname); //add to element's add. namespaces if (_declNamespaces.size() > 0) { for (Iterator it = _declNamespaces.iterator(); it.hasNext();) element.addDeclaredNamespace((Namespace)it.next()); _declNamespaces.clear(); } pushGroup(element); return element; } public void endElement (String nsURI, String lname, String tname) throws SAXException { if (D.ON && log.finerable()) log.finer("end element: " + nsURI + ", " + lname + ", " + tname); popGroup(); } public void characters(char[] ch, int start, int length) throws SAXException { if (length == 0) return; //ignore zero length //Note: Element's add will coalesce consecutive CDATA or Text final String data = new String(ch, start, length); if (getTopGroup() instanceof Document) { if (data.trim().length() > 0) throw new SAXException("Adding non-empty text to Document: "+data); return; //Under transforming, it is possible to have this case } if (_inCData && !isCoalescing()) addToCurrentGroup(_factory.newCData(data)); else addToCurrentGroup(_factory.newText(data)); } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (length == 0 || isIgnoringElementContentWhitespace()) return; addToCurrentGroup(_factory.newText(new String(ch, start, length))); } //-- org.xml.sax.DTDHandler --// public void notationDecl(String name, String publicID, String systemID) throws SAXException { if (!_inInternSubset) return; _internSubset.append(" <!NOTATION ").append(name) .append(" \"").append(systemID).append("\">\n"); } public void unparsedEntityDecl (String name, String pubId, String sysId, String notationName) throws SAXException { if (D.ON && log.finerable()) log.finer("externalEntityDecl: " + name + " p:" + pubId + " s:" + sysId + " n:" + notationName); if (!_inInternSubset) return; _internSubset.append(" <!ENTITY ").append(name); if (pubId != null) _internSubset.append(" PUBLIC \"").append(pubId).append("\" "); if (sysId != null) _internSubset.append(" SYSTEM \"").append(sysId).append("\" "); _internSubset.append(" NDATA ").append(notationName).append(">\n"); } //-- org.xml.sax.EntityResolver --// public InputSource resolveEntity(String publicId, String systemId) throws SAXException { if (D.ON && log.finerable()) log.finer("resolveEntity public="+publicId+" system="+systemId); EntityResolver er = getEntityResolver(); if (er != null) { try { return er.resolveEntity(publicId, systemId); } catch (IOException ex) { //unfortunately, DefaultHandler doesn't throw IOException, //so we have to wrap it throw new SAXException(ex); } } else { InputSource is = defaultResolveEntity(publicId, systemId); if (D.ON && is == null && log.finerable()) log.finer("Unable to resolve public="+publicId+" system="+systemId); return is; } } /** The default entity resolver. * It is used if {@link #setEntityResolver} is not called. * This implementation searches the class path under /metainfo/xml. * * <p>For example, http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd * is asked. * It searches from classpath * for /metainfo/xml/java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd * and /metainfo/xml/portlet-app_1_0.xsd */ protected InputSource defaultResolveEntity(String publicId, String systemId) throws SAXException { if (systemId == null) return null; //Try the original form first if "file:/" found (including jar:file:/) //We don't need to handle others, because http:// will be resolved //by parser. And, if we do it here, we lost the chance to improve //performance by loading it locally by below codes if (systemId.indexOf("file:/") >= 0) { try { final InputSource is = new InputSource(new URL(systemId).openStream()); is.setSystemId(systemId); if (D.ON && log.finerable()) log.finer("Entity found "+systemId); return is; } catch (Exception ex) { if (D.ON && log.finerable()) log.finer("Unable to open "+systemId); } } final String PREFIX = "/metainfo/xml"; final org.zkoss.util.resource.Locator loader = Locators.getDefault(); URL url = null; int j = systemId.indexOf("://"); if (j > 0) { final String resId = PREFIX + systemId.substring(j + 2); url = loader.getResource(resId); } if (url == null) { j = systemId.lastIndexOf('/'); final String resId = j >= 0 ? PREFIX + systemId.substring(j): PREFIX + '/' + systemId; url = loader.getResource(resId); } if (url != null) { if (D.ON && log.finerable()) log.finer("Entity resovled to "+url); try { final InputSource is = new InputSource(url.openStream()); is.setSystemId(url.toExternalForm()); return is; } catch (IOException ex) { throw new SAXException(ex); //not possible because Locator is used } } return null; } //-- org.xml.sax.ErrorHandler --// public void warning(SAXParseException ex) throws SAXException { ErrorHandler eh = getErrorHandler(); if (eh != null) { eh.warning(ex); } else { log.warning(ex.getMessage() + SimpleLocator.toString(_loc)); } } public void error(SAXParseException ex) throws SAXException { ErrorHandler eh = getErrorHandler(); if (eh != null) { eh.error(ex); } else { log.error(ex.getMessage() + SimpleLocator.toString(_loc)); throw ex; } } public void fatalError(SAXParseException ex) throws SAXException { ErrorHandler eh = getErrorHandler(); if (eh != null) { eh.fatalError(ex); } else { log.error(ex.getMessage() + SimpleLocator.toString(_loc)); throw ex; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -