📄 saxfilter.java
字号:
} if (!qName.equals(elInfo.qName)) { _rdfParser.sendFatalError("expected end tag </'" + elInfo.qName + ">, " + "found </" + qName + ">"); } } if (!_inRdfContext) { _elInfoStack.pop(); _charBuf.setLength(0); return; } if (_deferredElement == null && _rdfContextStackHeight == 0) { // This end tag removes the element that signaled the start // of the RDF context (i.e. <rdf:RDF>) from the stack. _inRdfContext = false; _elInfoStack.pop(); _charBuf.setLength(0); return; } // We're still in RDF context. if (_parseLiteralMode) { if (_xmlLiteralStackHeight > 0) { _appendEndTag(qName); _xmlLiteralStackHeight--; return; } else { // This tag is no longer part of the XML literal // and it ends the parseLiteral mode. _parseLiteralMode = false; // Insert any used namespace prefixes from the XML literal's // context that are not defined in the XML literal itself. _insertUsedContextPrefixes(); // Continue after this if-statement to process // the end tag as a normal end tag } } // Check for any deferred start elements if (_deferredElement != null) { // Start element still deferred, this is an empty element _rdfParser.setBaseURI(_deferredElement.baseURI); _rdfParser.setXmlLang(_deferredElement.xmlLang); _rdfParser.emptyElement( _deferredElement.namespaceURI, _deferredElement.localName, _deferredElement.qName, _deferredElement.atts); _deferredElement = null; } else { // Check if any character data has been collected in the _charBuf String s = _charBuf.toString().trim(); if (s.length() > 0) { _rdfParser.text(s); } _charBuf.setLength(0); _elInfoStack.pop(); _rdfContextStackHeight--; _rdfParser.endElement(namespaceURI, localName, qName); } } public void characters(char[] ch, int start, int length) throws SAXException { if (_inRdfContext) { if (_deferredElement != null) { _reportDeferredStartElement(); } if (_parseLiteralMode) { // Characters like '<', '>', and '&' must be escaped to // prevent breaking the XML text. String s = new String(ch, start, length); s = XmlUtil.escapeChars(s); _charBuf.append(s); } else { _charBuf.append(ch, start, length); } } } public void ignorableWhitespace(char[] ch, int start, int length) { if (_parseLiteralMode) { _charBuf.append(ch, start, length); } } public void processingInstruction(String target, String data) { // ignore } public void skippedEntity(String name) { // ignore } private void _checkAndCopyAttributes(Attributes attributes, ElementInfo elInfo) throws SAXException { Atts atts = new Atts(attributes.getLength()); int attCount = attributes.getLength(); for (int i = 0; i < attCount; i++) { String qName = attributes.getQName(i); String value = attributes.getValue(i); // attributes starting with "xml" should be ignored, except for the // ones that are handled by this parser (xml:lang and xml:base). if (qName.startsWith("xml")) { if (qName.equals("xml:lang")) { elInfo.xmlLang = value; } else if (qName.equals("xml:base")) { elInfo.setBaseURI(value); } } else { String namespace = attributes.getURI(i); String localName = attributes.getLocalName(i); if (_rdfParser._verifyData) { if ("".equals(namespace)) { _rdfParser.sendError("unqualified attribute '" + qName + "' not allowed"); } else if (qName.indexOf(":") == -1) { _rdfParser.sendWarning("unqualified attribute '" + qName + "' not allowed"); } } Att att = new Att(namespace, localName, qName, value); atts.addAtt(att); } } elInfo.atts = atts; } public void setParseLiteralMode() { _parseLiteralMode = true; _xmlLiteralStackHeight = 0; // All currently known namespace prefixes are // new for this XML literal. _xmlLiteralPrefixes.clear(); _unknownPrefixesInXmlLiteral.clear(); } private URI _createBaseURI(String uriString) { if (uriString.length() > 4 && uriString.substring(0, 4).equalsIgnoreCase("jar:")) { // uriString is e.g. jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class // Treat the part up to and including the exclamation mark as the scheme and // the rest as the path to enable 'correct' resolving of relative URIs int idx = uriString.indexOf('!'); if (idx != -1) { String scheme = uriString.substring(0, idx + 1); String path = uriString.substring(idx + 1); return new URI(scheme, null, path, null, null); } } URI uri = new URI(uriString); uri.normalize(); return uri; }/*--------------------------------+| Methods related to XML literals |+--------------------------------*/ /** * Appends a start tag to _charBuf. This method is used during the * parsing of an XML Literal. **/ private void _appendStartTag(String qName, Attributes attributes) { // Write start of start tag _charBuf.append("<" + qName); // Write any new namespace prefix definitions Iterator prefixes = _newNamespaceMappings.keySet().iterator(); while (prefixes.hasNext()) { String prefix = (String)prefixes.next(); String namespace = (String)_newNamespaceMappings.get(prefix); _appendNamespaceDecl(_charBuf, prefix, namespace); } // Write attributes int attCount = attributes.getLength(); for (int i = 0; i < attCount; i++) { _appendAttribute(_charBuf, attributes.getQName(i), attributes.getValue(i)); } // Write end of start tag _charBuf.append(">"); // Check for any used prefixes that are not // defined in the XML literal itself int colonIdx = qName.indexOf(':'); String prefix = (colonIdx > 0) ? qName.substring(0, colonIdx) : ""; if (!_xmlLiteralPrefixes.contains(prefix) && !_unknownPrefixesInXmlLiteral.contains(prefix)) { _unknownPrefixesInXmlLiteral.add(prefix); } } /** * Appends an end tag to _charBuf. This method is used during the * parsing of an XML Literal. **/ private void _appendEndTag(String qName) { _charBuf.append("</" + qName + ">"); } /** * Inserts prefix mappings from an XML Literal's context for all prefixes * that are used in the XML Literal and that are not defined in the XML * Literal itself. **/ private void _insertUsedContextPrefixes() { int unknownPrefixesCount = _unknownPrefixesInXmlLiteral.size(); if (unknownPrefixesCount > 0) { // Create a String with all needed context prefixes StringBuffer contextPrefixes = new StringBuffer(1024); ElementInfo topElement = _peekStack(); for (int i = 0; i < unknownPrefixesCount; i++) { String prefix = (String)_unknownPrefixesInXmlLiteral.get(i); String namespace = topElement.getNamespace(prefix); if (namespace != null) { _appendNamespaceDecl(contextPrefixes, prefix, namespace); } } // Insert this String before the first '>' character // StringBuffer.indexOf(String) requires JDK1.4 or newer //int endOfFirstStartTag = _charBuf.indexOf(">"); int endOfFirstStartTag = 0; while (_charBuf.charAt(endOfFirstStartTag) != '>') { endOfFirstStartTag++; } _charBuf.insert(endOfFirstStartTag, contextPrefixes.toString()); } _unknownPrefixesInXmlLiteral.clear(); } private void _appendNamespaceDecl(StringBuffer sb, String prefix, String namespace) { String attName = "xmlns"; if (!"".equals(prefix)) { attName += ":" + prefix; } _appendAttribute(sb, attName, namespace); } private void _appendAttribute(StringBuffer sb, String name, String value) { sb.append(" "); sb.append(name); sb.append("=\""); sb.append(XmlUtil.escapeChars(value)); sb.append("\""); }/*-----------------------------------------+| Methods related to the ElementInfo stack |+-----------------------------------------*/ private ElementInfo _peekStack() { ElementInfo result = null; if (!_elInfoStack.empty()) { result = (ElementInfo)_elInfoStack.peek(); } return result; }/*---------------------------+| Internal class ElementInfo |+---------------------------*/ private class ElementInfo { public String qName; public String namespaceURI; public String localName; public Atts atts; public ElementInfo parent; private Map _namespaceMap; public URI baseURI; public String xmlLang; public ElementInfo(String qName, String namespaceURI, String localName) { this(null, qName, namespaceURI, localName); } public ElementInfo(ElementInfo parent, String qName, String namespaceURI, String localName) { this.parent = parent; this.qName = qName; this.namespaceURI = namespaceURI; this.localName = localName; if (parent != null) { // Inherit baseURI and xmlLang from parent this.baseURI = parent.baseURI; this.xmlLang = parent.xmlLang; } else { this.baseURI = _documentURI; this.xmlLang = ""; } } public void setBaseURI(String uriString) { // Resolve the specified base URI against the inherited base URI baseURI = baseURI.resolve( _createBaseURI(uriString) ); } public void setNamespaceMappings(Map namespaceMappings) { if (namespaceMappings.isEmpty()) { _namespaceMap = null; } else { _namespaceMap = new HashMap(namespaceMappings); } } public String getNamespace(String prefix) { String result = null; if (_namespaceMap != null) { result = (String)_namespaceMap.get(prefix); } if (result == null && parent != null) { result = parent.getNamespace(prefix); } return result; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -