📄 xmlstreamreaderimpl.java
字号:
return convertXNIQNametoJavaxQName(fScanner.getAttributeIterator().getQualifiedName(index)) ; } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeName()") ; } }//getAttributeName /** * @param index * @return */ public String getAttributeLocalName(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getLocalName(index) ; } else{ throw new java.lang.IllegalStateException() ; } }//getAttributeName /** Returns the namespace of the attribute at the provided * index * @param index the position of the attribute * @return the namespace URI (can be null) * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public String getAttributeNamespace(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getURI(index); } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeNamespace()") ; } }//getAttributeNamespace /** Returns the prefix of this attribute at the * provided index * @param index the position of the attribute * @return the prefix of the attribute * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public String getAttributePrefix(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getPrefix(index); } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributePrefix()") ; } }//getAttributePrefix /** Returns the qname of the attribute at the provided index * * @param index the position of the attribute * @return the QName of the attribute * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public javax.xml.namespace.QName getAttributeQName(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { // create new object at runtime.. String localName = fScanner.getAttributeIterator().getLocalName(index) ; String uri = fScanner.getAttributeIterator().getURI(index) ; return new javax.xml.namespace.QName(uri, localName) ; } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeQName()") ; } }//getAttributeQName /** Returns the XML type of the attribute at the provided * index * @param index the position of the attribute * @return the XML type of the attribute * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public String getAttributeType(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getType(index) ; } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeType()") ; } }//getAttributeType /** Returns the value of the attribute at the * index * @param index the position of the attribute * @return the attribute value * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public String getAttributeValue(int index) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getValue(index) ; } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeValue()") ; } }//getAttributeValue /** * @param namespaceURI * @param localName * @return */ public String getAttributeValue(String namespaceURI, String localName) { //State should be either START_ELEMENT or ATTRIBUTE if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { return fScanner.getAttributeIterator().getValue(namespaceURI, localName) ; } else{ throw new java.lang.IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for getAttributeValue()") ; } } /** Reads the content of a text-only element. Precondition: * the current event is START_ELEMENT. Postcondition: * The current event is the corresponding END_ELEMENT. * @throws XMLStreamException if the current event is not a START_ELEMENT or if * a non text element is encountered */ public String getElementText() throws XMLStreamException { if(getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", getLocation()); } int eventType = next(); StringBuffer content = new StringBuffer(); while(eventType != XMLStreamConstants.END_ELEMENT ) { if(eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) { content.append(getText()); } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) { // skipping } else if(eventType == XMLStreamConstants.END_DOCUMENT) { throw new XMLStreamException("unexpected end of document when reading element text content"); } else if(eventType == XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException( "elementGetText() function expects text only elment but START_ELEMENT was encountered.", getLocation()); } else { throw new XMLStreamException( "Unexpected event type "+ eventType, getLocation()); } eventType = next(); } return content.toString(); } /** Return the current location of the processor. * If the Location is unknown the processor should return * an implementation of Location that returns -1 for the * location and null for the publicId and systemId. * The location information is only valid until next() is * called. */ public Location getLocation() { return new Location() { public String getLocationURI(){ return fEntityScanner.getExpandedSystemId(); } public int getCharacterOffset(){ return fEntityScanner.getCharacterOffset(); } public int getColumnNumber() { return fEntityScanner.getColumnNumber(); } public int getLineNumber(){ return fEntityScanner.getLineNumber(); } public String getPublicId(){ return fEntityScanner.getPublicId(); } public String getSystemId(){ return fEntityScanner.getExpandedSystemId(); } public String toString(){ StringBuffer sbuffer = new StringBuffer() ; sbuffer.append("Line number = " + getLineNumber()); sbuffer.append("\n") ; sbuffer.append("Column number = " + getColumnNumber()); sbuffer.append("\n") ; sbuffer.append("System Id = " + getSystemId()); sbuffer.append("\n") ; sbuffer.append("Public Id = " + getPublicId()); sbuffer.append("\n") ; sbuffer.append("Location Uri= " + getLocationURI()); sbuffer.append("\n") ; sbuffer.append("CharacterOffset = " + getCharacterOffset()); sbuffer.append("\n") ; return sbuffer.toString(); } } ; } /** Returns a QName for the current START_ELEMENT or END_ELEMENT event * @return the QName for the current START_ELEMENT or END_ELEMENT event */ public javax.xml.namespace.QName getName() { if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT) return convertXNIQNametoJavaxQName(fScanner.getElementQName()); else throw new java.lang.IllegalArgumentException("Illegal to call getName() "+ "when event type is "+ getEventTypeString(fEventType) + "." + " Valid states are " + getEventTypeString(XMLEvent.START_ELEMENT) + ", " + getEventTypeString(XMLEvent.END_ELEMENT)); } /** Returns a read only namespace context for the current * position. The context is transient and only valid until * a call to next() changes the state of the reader. * @return return a namespace context */ public NamespaceContext getNamespaceContext() { return fNamespaceContextWrapper ; } /** Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT, * this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. On * an END_ELEMENT the count is of the namespaces that are about to go * out of scope. This is the equivalent of the information reported * by SAX callback for an end element event. * @return returns the number of namespace declarations on this specific element * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE */ public int getNamespaceCount() { //namespaceContext is dynamic object. //REVISIT: check if it specifies all conditions mentioned in the javadoc if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ return fScanner.getNamespaceContext().getDeclaredPrefixCount() ; } else{ throw new IllegalStateException("Current event state is " + getEventTypeString(fEventType) + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " + getEventTypeString(XMLEvent.NAMESPACE) + " valid for getNamespaceCount()." ); } } /** Returns the prefix for the namespace declared at the * index. Returns null if this is the default namespace * declaration * * @param index the position of the namespace declaration * @return returns the namespace prefix * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE */ public String getNamespacePrefix(int index) { if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ //namespaceContext is dynamic object. String prefix = fScanner.getNamespaceContext().getDeclaredPrefixAt(index) ; return prefix.equals("") ? null : prefix ; } else{ throw new IllegalStateException("Current state " + getEventTypeString(fEventType) + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " + getEventTypeString(XMLEvent.NAMESPACE) + " valid for getNamespacePrefix()." ); } } /** Returns the uri for the namespace declared at the * index. * * @param index the position of the namespace declaration * @return returns the namespace uri * @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE */ public String getNamespaceURI(int index) { if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ //namespaceContext is dynamic object. return fScanner.getNamespaceContext().getURI(fScanner.getNamespaceContext().getDeclaredPrefixAt(index)); } else{ throw new IllegalStateException("Current state " + getEventTypeString(fEventType) + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " + getEventTypeString(XMLEvent.NAMESPACE) + " valid for getNamespaceURI()." ); } } /** Get the value of a feature/property from the underlying implementation * @param name The name of the property, may not be null * @return The value of the property * @throws IllegalArgumentException if name is null */ public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { if(name == null) throw new java.lang.IllegalArgumentException() ; if (fPropertyManager != null ){ if(name.equals(fPropertyManager.STAX_NOTATIONS)){ return getNotationDecls(); }else if(name.equals(fPropertyManager.STAX_ENTITIES)){ return getEntityDecls(); }else return fPropertyManager.getProperty(name); } return null; } /** Returns the current value of the parse event as a string, * this returns the string value of a CHARACTERS event, * returns the value of a COMMENT, the replacement value * for an ENTITY_REFERENCE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -