📄 saxdriver.java
字号:
if ((FEATURE + "namespace-prefixes").equals (featureId)) { // in this implementation, this only affects xmlns reporting xmlNames = state; return; } if ((FEATURE + "namespaces").equals (featureId)) { // XXX if not currently parsing ... if (true) { namespaces = state; return; } // if in mid-parse, critical info hasn't been computed/saved } // can't change builtins if (features == null || !features.containsKey (featureId)) throw new SAXNotSupportedException (featureId); } catch (SAXNotRecognizedException e) { // as-yet unknown features if (features == null) features = new Hashtable (5); } // record first value, or modify existing one features.put (featureId, state ? Boolean.TRUE : Boolean.FALSE); } /** * <b>SAX2</b>: Assigns the specified property. Like SAX1 handlers, * these may be changed at any time. */ public void setProperty (String propertyId, Object property) throws SAXNotRecognizedException, SAXNotSupportedException { Object value; try { // Properties with a defined value, we just change it if we can. value = getProperty (propertyId); if ((HANDLER + "declaration-handler").equals (propertyId)) { if (property == null) declHandler = base; else if (! (property instanceof DeclHandler)) throw new SAXNotSupportedException (propertyId); else declHandler = (DeclHandler) property; return ; } if ((HANDLER + "lexical-handler").equals (propertyId)) { if (property == null) lexicalHandler = base; else if (! (property instanceof LexicalHandler)) throw new SAXNotSupportedException (propertyId); else lexicalHandler = (LexicalHandler) property; return ; } // can't change builtins if (properties == null || !properties.containsKey (propertyId)) throw new SAXNotSupportedException (propertyId); } catch (SAXNotRecognizedException e) { // as-yet unknown properties if (properties == null) properties = new Hashtable (5); } // record first value, or modify existing one properties.put (propertyId, property); } // // This is where the driver receives AElfred callbacks and translates // them into SAX callbacks. Some more callbacks have been added for // SAX2 support. // // NOTE: in some cases, local copies of handlers are // created and used, to work around codegen bugs in at // least one snapshot version of GCJ. void startDocument () throws SAXException { contentHandler.setDocumentLocator (this); contentHandler.startDocument (); attributeNames.removeAllElements (); attributeValues.removeAllElements (); } void endDocument () throws SAXException { // SAX says endDocument _must_ be called (handy to close // files etc) so it's in a "finally" clause } Object resolveEntity (String publicId, String systemId) throws SAXException, IOException { InputSource source = entityResolver.resolveEntity (publicId, systemId); if (source == null) { return null; } else if (source.getCharacterStream () != null) { return source.getCharacterStream (); } else if (source.getByteStream () != null) { if (source.getEncoding () == null) return source.getByteStream (); else try { return new InputStreamReader ( source.getByteStream (), source.getEncoding () ); } catch (IOException e) { return source.getByteStream (); } } else { return source.getSystemId (); } // XXX no way to tell AElfred about new public // or system ids ... so relative URL resolution // through that entity could be less than reliable. } void startExternalEntity (String systemId) throws SAXException { entityStack.push (systemId); } void endExternalEntity (String systemId) throws SAXException { entityStack.pop (); } void doctypeDecl (String name, String publicId, String systemId) throws SAXException { LexicalHandler handler = lexicalHandler; handler.startDTD (name, publicId, systemId); // ... the "name" is a declaration and should be given // to the DeclHandler (but sax2 doesn't). // the IDs for the external subset are lexical details, // as are the contents of the internal subset; but sax2 // doesn't provide the internal subset "pre-parse" } void endDoctype () throws SAXException { // NOTE: some apps may care that comments and PIs, // are stripped from their DTD declaration context, // and that those declarations are themselves quite // thoroughly reordered here. deliverDTDEvents (); lexicalHandler.endDTD (); } void attribute (String aname, String value, boolean isSpecified) throws SAXException { if (attributeCount++ == 0) { if (namespaces) prefixStack.pushContext (); } // set nsTemp [0] == namespace URI (or empty) // set nsTemp [1] == local name (or empty) if (value != null) { if (namespaces) { int index = aname.indexOf (':'); // prefixed name? if (index > 0) { // prefix declaration? if (index == 5 && aname.startsWith ("xmlns")) { String prefix = aname.substring (index + 1); if (value.length () == 0) { errorHandler.error (new SAXParseException ( "missing URI in namespace decl attribute: " + aname, this)); } else { prefixStack.declarePrefix (prefix, value); contentHandler.startPrefixMapping (prefix, value); } if (!xmlNames) return; nsTemp [0] = ""; nsTemp [1] = aname; // prefix reference } else { if (prefixStack.processName (aname, nsTemp, true) == null) { // prefix declaration not yet seen nsTemp [0] = ""; nsTemp [1] = aname; nspending = true; } // else nsTemp [0, 1] received { uri, local } } // no prefix } else { // default declaration? if ("xmlns".equals (aname)) { prefixStack.declarePrefix ("", value); contentHandler.startPrefixMapping ("", value); if (!xmlNames) return; } nsTemp [0] = ""; nsTemp [1] = aname; } } else nsTemp [0] = nsTemp [1] = ""; attributeNamespaces.addElement (nsTemp [0]); attributeLocalNames.addElement (nsTemp [1]); attributeNames.addElement (aname); // attribute type comes from querying parser's DTD records attributeValues.addElement (value); } } void startElement (String elname) throws SAXException { ContentHandler handler = contentHandler; // // NOTE: this implementation of namespace support adds something // like six percent to parsing CPU time, in a large (~50 MB) // document that doesn't use namespaces at all. (Measured by PC // sampling.) // // It ought to become notably faster in such cases. Most // costs are the prefix stack calling Hashtable.get() (2%), // String.hashCode() (1.5%) and about 1.3% each for pushing // the context, and two chunks of name processing. // if (attributeCount == 0) prefixStack.pushContext (); else if (nspending) { for (int i = 0; i < attributeCount; i++) { String aname = (String) attributeNames.elementAt (i); if (aname.indexOf (':') > 0) { if (prefixStack.processName (aname, nsTemp, true) == null) errorHandler.error (new SAXParseException ( "undeclared attribute prefix in: " + aname, this)); else { attributeNamespaces.setElementAt (nsTemp [0], i); attributeLocalNames.setElementAt (nsTemp [1], i); } } } nspending = false; } // save element name so attribute callbacks work elementName = elname; if (namespaces) { if (prefixStack.processName (elname, nsTemp, false) == null) { errorHandler.error (new SAXParseException ( "undeclared element prefix in: " + elname, this)); nsTemp [0] = nsTemp [1] = ""; } handler.startElement (nsTemp [0], nsTemp [1], elname, this); } else handler.startElement ("", "", elname, this); // elementName = null; // elements with no attributes are pretty common! if (attributeCount != 0) { attributeNames.removeAllElements (); attributeNamespaces.removeAllElements (); attributeLocalNames.removeAllElements (); attributeValues.removeAllElements (); attributeCount = 0; } } void endElement (String elname) throws SAXException { ContentHandler handler = contentHandler; handler.endElement ("", "", elname); if (!namespaces) return; Enumeration prefixes = prefixStack.getDeclaredPrefixes (); while (prefixes.hasMoreElements ()) handler.endPrefixMapping ((String) prefixes.nextElement ()); prefixStack.popContext (); } void startCDATA () throws SAXException { LexicalHandler handler = lexicalHandler; handler.startCDATA (); } void charData (char ch[], int start, int length) throws SAXException { ContentHandler handler = contentHandler; handler.characters (ch, start, length); } void endCDATA () throws SAXException { lexicalHandler.endCDATA (); } void ignorableWhitespace (char ch[], int start, int length) throws SAXException { ContentHandler handler = contentHandler; handler.ignorableWhitespace (ch, start, length); } void processingInstruction (String target, String data) throws SAXException { ContentHandler handler = contentHandler; // XXX if within DTD, perhaps it's best to discard // PIs since the decls to which they (usually) // apply get significantly rearranged handler.processingInstruction (target, data); } void comment (char ch[], int start, int length) throws SAXException { // XXX if within DTD, perhaps it's best to discard // comments since the decls to which they (usually) // apply get significantly rearranged if (lexicalHandler != base) lexicalHandler.comment (ch, start, length); } // AElfred only has fatal errors void error (String message, String url, int line, int column) throws SAXException { SAXParseException fatal; fatal = new SAXParseException (message, null, url, line, column); errorHandler.fatalError (fatal); // Even if the application can continue ... we can't! throw fatal; } // // Before the endDtd event, deliver all non-PE declarations. // private void deliverDTDEvents () throws SAXException { String ename; String nname; String publicId; String systemId; // First, report all notations. if (dtdHandler != base) { Enumeration notationNames = parser.declaredNotations (); while (notationNames.hasMoreElements ()) { nname = (String) notationNames.nextElement (); publicId = parser.getNotationPublicId (nname); systemId = parser.getNotationSystemId (nname); dtdHandler.notationDecl (nname, publicId, systemId); } } // Next, report all entities. if (dtdHandler != base || declHandler != base) { Enumeration entityNames = parser.declaredEntities (); int type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -