📄 xmlstreamreaderimpl.java
字号:
switch (eventType) { case XMLStreamConstants.START_ELEMENT: i = ((StartElement) currentEvent).getNamespaces(); break; case XMLStreamConstants.END_ELEMENT: i = ((EndElement) currentEvent).getNamespaces(); break; default: throw new IllegalStateException(); } int count = 0; while (i.hasNext()) { Namespace ns = (Namespace) i.next(); if (index == count) return ns.getNamespaceURI(); count++; } return null; } public NamespaceContext getNamespaceContext() { return this; } public int getEventType() { return eventType; } public String getText() { switch (eventType) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: case XMLStreamConstants.SPACE: return ((Characters) currentEvent).getData(); case XMLStreamConstants.COMMENT: return ((Comment) currentEvent).getText(); case XMLStreamConstants.ENTITY_REFERENCE: return ((EntityReference) currentEvent).getReplacementText(); case XMLStreamConstants.DTD: return ((DTD) currentEvent).getDocumentTypeDeclaration(); } return null; } public char[] getTextCharacters() { String text = getText(); return (text == null) ? null : text.toCharArray(); } public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { char[] source = getTextCharacters(); int len = Math.min(source.length, length); System.arraycopy(source, sourceStart, target, targetStart, len); return len; } public int getTextStart() { return 0; } public int getTextLength() { String text = getText(); return (text == null) ? 0 : text.length(); } public String getEncoding() { // XXX SAX doesn't provide this return null; } public boolean hasText() { return eventType == CHARACTERS || eventType == DTD || eventType == SPACE || eventType == ENTITY_REFERENCE || eventType == COMMENT || eventType == DTD; } public Location getLocation() { return currentEvent.getLocation(); } public QName getName() { switch (eventType) { case XMLStreamConstants.START_ELEMENT: return ((StartElement) currentEvent).getName(); case XMLStreamConstants.END_ELEMENT: return ((EndElement) currentEvent).getName(); case XMLStreamConstants.ATTRIBUTE: return ((Attribute) currentEvent).getName(); } return null; } public String getLocalName() { QName name = getName(); return (name == null) ? null : name.getLocalPart(); } public boolean hasName() { return getName() != null; } public String getNamespaceURI() { QName name = getName(); return (name == null) ? null : name.getNamespaceURI(); } public String getPrefix() { QName name = getName(); return (name == null) ? null : name.getPrefix(); } public String getVersion() { StartDocument sd = (StartDocument) currentEvent; return sd.getVersion(); } public boolean isStandalone() { StartDocument sd = (StartDocument) currentEvent; return sd.isStandalone(); } public boolean standaloneSet() { StartDocument sd = (StartDocument) currentEvent; return sd.standaloneSet(); } public String getCharacterEncodingScheme() { StartDocument sd = (StartDocument) currentEvent; return sd.getCharacterEncodingScheme(); } public String getPITarget() { ProcessingInstruction pi = (ProcessingInstruction) currentEvent; return pi.getTarget(); } public String getPIData() { ProcessingInstruction pi = (ProcessingInstruction) currentEvent; return pi.getData(); } /** * This class is used to construct the event series from SAX callbacks. */ class CallbackHandler implements ContentHandler, DTDHandler, LexicalHandler, DeclHandler, EntityResolver, ErrorHandler { XMLReader reader; Locator locator; Location location; private boolean inCDATA; private LinkedList namespaces = new LinkedList(); private LinkedList notations; private LinkedList entities; CallbackHandler(XMLReader reader) { this.reader = reader; } public void setDocumentLocator(Locator locator) { this.locator = locator; location = new LocationImpl(-1, locator.getColumnNumber(), locator.getLineNumber(), locator.getSystemId()); } public void startDocument() throws SAXException { String version = (locator instanceof Locator2) ? ((Locator2) locator).getXMLVersion() : null; String encoding = (locator instanceof Locator2) ? ((Locator2) locator).getEncoding() : null; boolean standalone = reader.getFeature("http://xml.org/sax/features/is-standalone"); boolean standaloneDeclared = standalone; boolean encodingDeclared = (encoding != null); events.add(new StartDocumentImpl(location, location.getLocationURI(), encoding, version, standalone, standaloneDeclared, encodingDeclared)); } public void endDocument() throws SAXException { events.add(new EndDocumentImpl(location)); } public void startPrefixMapping(String prefix, String uri) throws SAXException { namespaces.add(new NamespaceImpl(location, prefix, uri)); } public void endPrefixMapping(String prefix) throws SAXException { } public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { LinkedList ns = namespaces; namespaces = new LinkedList(); int ci = qName.indexOf(':'); String prefix = null; localName = qName; if (ci != -1) { prefix = qName.substring(0, ci); localName = qName.substring(ci + 1); } QName name = new QName(namespaceURI, localName, prefix); LinkedList attrs = new LinkedList(); StartElementImpl se = new StartElementImpl(location, name, attrs, ns, null); events.add(se); // Add namespaces //for (Iterator i = ns.iterator(); i.hasNext(); ) // events.add(i.next()); // Add attributes int len = atts.getLength(); for (int i = 0; i < len; i++) { String attURI = atts.getURI(i); String attQName = atts.getQName(i); String value = atts.getValue(i); QName type = QName.valueOf(atts.getType(i)); boolean specified = (atts instanceof Attributes2) && ((Attributes2) atts).isSpecified(i); ci = attQName.indexOf(':'); String attPrefix = null; String attLocalName = attQName; if (ci != -1) { attPrefix = attQName.substring(0, ci); attLocalName = attQName.substring(ci + 1); } if ("xmlns".equals(attPrefix) || "xmlns".equals(attQName)) continue; QName attrName = new QName(attURI, attLocalName, attPrefix); AttributeImpl attr = new AttributeImpl(location, attrName, value, type, specified); attrs.add(attr); //events.add(attr); } } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { int ci = qName.indexOf(':'); String prefix = null; localName = qName; if (ci != -1) { prefix = qName.substring(0, ci); localName = qName.substring(ci + 1); } QName name = new QName(namespaceURI, localName, prefix); events.add(new EndElementImpl(location, name, new LinkedList())); // TODO namespaces out of scope } public void characters(char[] ch, int start, int length) throws SAXException { boolean whitespace = isWhitespace(ch, start, length); events.add(new CharactersImpl(location, new String(ch, start, length), whitespace, inCDATA, false)); } public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { boolean whitespace = isWhitespace(ch, start, length); events.add(new CharactersImpl(location, new String(ch, start, length), whitespace, inCDATA, true)); } boolean isWhitespace(char[] ch, int start, int len) { int end = start + len; for (int i = start; i < end; i++) { char c = ch[i]; if (c != ' ' && c != '\t' && c != '\n' && c != '\r') return false; } return true; } public void processingInstruction(String target, String data) throws SAXException { events.add(new ProcessingInstructionImpl(location, target, data)); } public void skippedEntity(String name) throws SAXException { } public void startDTD(String name, String publicId, String systemId) throws SAXException { notations = new LinkedList(); entities = new LinkedList(); events.add(new DTDImpl(location, null, null, notations, entities)); } public void endDTD() throws SAXException { } public void startEntity(String name) throws SAXException { events.add(new StartEntityImpl(location, name)); } public void endEntity(String name) throws SAXException { events.add(new EndEntityImpl(location, name)); } 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 { events.add(new CommentImpl(location, new String(ch, start, length))); } public void notationDecl(String name, String publicId, String systemId) throws SAXException { Object n = new NotationDeclarationImpl(location, name, publicId, systemId); notations.add(n); //events.add(n); } public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException { Object e = new EntityDeclarationImpl(location, publicId, systemId, name, notationName, null, null); entities.add(e); //events.add(e); } public void elementDecl(String name, String model) throws SAXException { } public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException { } public void internalEntityDecl(String name, String value) throws SAXException { Object e = new EntityDeclarationImpl(location, null, null, name, null, value, null); entities.add(e); //events.add(e); } public void externalEntityDecl(String name, String publicId, String systemId) throws SAXException { Object e = new EntityDeclarationImpl(location, publicId, systemId, name, null, null, null); entities.add(e); //events.add(e); } public void warning(SAXParseException e) throws SAXException { if (reporter != null) { try { reporter.report(e.getMessage(), "warning", e, location); } catch (XMLStreamException e2) { SAXException e3 = new SAXException(e2.getMessage()); e3.initCause(e2); throw e3; } } } public void error(SAXParseException e) throws SAXException { if (reporter != null) { try { reporter.report(e.getMessage(), "error", e, location); } catch (XMLStreamException e2) { SAXException e3 = new SAXException(e2.getMessage()); e3.initCause(e2); throw e3; } } } public void fatalError(SAXParseException e) throws SAXException { if (reporter != null) { try { reporter.report(e.getMessage(), "fatal-error", e, location); } catch (XMLStreamException e2) { SAXException e3 = new SAXException(e2.getMessage()); e3.initCause(e2); throw e3; } } } public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (resolver != null) { try { InputStream in = resolver.resolve(systemId); if (in != null) { InputSource ret = new InputSource(in); ret.setPublicId(publicId); ret.setSystemId(systemId); return ret; } } catch (XMLStreamException e) { SAXException e2 = new SAXException(e.getMessage()); e2.initCause(e); throw e2; } } return null; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -