📄 xmlstreamreaderimpl.java
字号:
* or the String value of the DTD * @return the current text or null * @throws java.lang.IllegalStateException if this state is not * a valid text state. */ public String getText() { if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){ //this requires creation of new string //fEventType == XMLEvent.ENTITY_REFERENCE return fScanner.getCharacterData().toString() ; } else if(fEventType == XMLEvent.ENTITY_REFERENCE){ String name = fScanner.getEntityName(); if(name != null){ if(fScanner.foundBuiltInRefs) return fScanner.getCharacterData().toString(); XMLEntityStorage entityStore = fEntityManager.getEntityStore(); Hashtable ht = entityStore.getDeclaredEntities(); Entity en = (Entity)ht.get(name); if(en == null) return null; if(en.isExternal()) return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId(); else return ((Entity.InternalEntity)en).text; }else return null; } else if(fEventType == XMLEvent.DTD){ if(fDTDDecl != null){ return fDTDDecl; } XMLStringBuffer tmpBuffer = fScanner.getDTDDecl(); fDTDDecl = tmpBuffer.toString(); return fDTDDecl; } else{ throw new IllegalStateException("Current state " + getEventTypeString(fEventType) + " is not among the states" + getEventTypeString(XMLEvent.CHARACTERS) + ", " + getEventTypeString(XMLEvent.COMMENT) + ", " + getEventTypeString(XMLEvent.CDATA) + ", " + getEventTypeString(XMLEvent.SPACE) + ", " + getEventTypeString(XMLEvent.ENTITY_REFERENCE) + ", " + getEventTypeString(XMLEvent.DTD) + " valid for getText() " ) ; } }//getText /** Test if the current event is of the given type and if the namespace and name match the current namespace and name of the current event. * If the namespaceURI is null it is not checked for equality, if the localName is null it is not checked for equality. * @param type the event type * @param namespaceURI the uri of the event, may be null * @param localName the localName of the event, may be null * @throws XMLStreamException if the required values are not matched. */ public void require(int type, String namespaceURI, String localName) throws XMLStreamException { if( type != fEventType) throw new XMLStreamException("Event type " + getEventTypeString(type) + " specified did " + "not match with current parser event " + getEventTypeString(fEventType)); if( namespaceURI != null && !namespaceURI.equals(getNamespaceURI()) ) throw new XMLStreamException("Namespace URI " + namespaceURI +" specified did not match " + "with current namespace URI"); if(localName != null && !localName.equals(getLocalName())) throw new XMLStreamException("LocalName " + localName +" specified did not match with " + "current local name"); return; } /** Gets the the text associated with a CHARACTERS, SPACE or CDATA event. * Text starting a "sourceStart" is copied into "destination" starting at "targetStart". * Up to "length" characters are copied. The number of characters actually copied is returned. * * The "sourceStart" argument must be greater or equal to 0 and less than or equal to * the number of characters associated with the event. Usually, one requests text starting at a "sourceStart" of 0. * If the number of characters actually copied is less than the "length", then there is no more text. * Otherwise, subsequent calls need to be made until all text has been retrieved. For example: * * <code> * int length = 1024; * char[] myBuffer = new char[ length ]; * * for ( int sourceStart = 0 ; ; sourceStart += length ) * { * int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length ); * * if (nCopied < length) * break; * } * </code> * XMLStreamException may be thrown if there are any XML errors in the underlying source. * The "targetStart" argument must be greater than or equal to 0 and less than the length of "target", * Length must be greater than 0 and "targetStart + length" must be less than or equal to length of "target". * * @param sourceStart the index of the first character in the source array to copy * @param target the destination array * @param targetStart the start offset in the target array * @param length the number of characters to copy * @return the number of characters actually copied * @throws XMLStreamException if the underlying XML source is not well-formed * @throws IndexOutOfBoundsException if targetStart < 0 or > than the length of target * @throws IndexOutOfBoundwhile(isCharacters()) ;sException if length < 0 or targetStart + length > length of target * @throws UnsupportedOperationException if this method is not supported * @throws NullPointerException is if target is null */ public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { if(target == null){ throw new NullPointerException("target char array can't be null") ; } if(targetStart < 0 || length < 0 || sourceStart < 0 || targetStart >= target.length || (targetStart + length ) > target.length) { throw new IndexOutOfBoundsException(); } //getTextStart() + sourceStart should not be greater than the lenght of number of characters //present int copiedLength = 0; //int presentDataLen = getTextLength() - (getTextStart()+sourceStart); int available = getTextLength() - sourceStart; if(available < 0){ throw new IndexOutOfBoundsException("sourceStart is greater than" + "number of characters associated with this event"); } if(available < length){ copiedLength = available; } else{ copiedLength = length; } System.arraycopy(getTextCharacters(), getTextStart() + sourceStart , target, targetStart, copiedLength); return copiedLength; } /** Return true if the current event has text, false otherwise * The following events have text: * CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT */ public boolean hasText() { if(DEBUG) pr("XMLReaderImpl#EVENT TYPE = " + fEventType ) ; if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA) { return fScanner.getCharacterData().length > 0 ? true : false; } else if(fEventType == XMLEvent.ENTITY_REFERENCE) { String name = fScanner.getEntityName(); if(name != null){ if(fScanner.foundBuiltInRefs) return true; XMLEntityStorage entityStore = fEntityManager.getEntityStore(); Hashtable ht = entityStore.getDeclaredEntities(); Entity en =(Entity)ht.get(name); if(en == null) return false; if(en.isExternal()){ return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId() != null ? true : false; } else{ return ((Entity.InternalEntity)en).text != null ? true : false ; } }else return false; } else { if(fEventType == XMLEvent.DTD) return fScanner.fSeenDoctypeDecl; } return false; } /** Returns a boolean which indicates if this * attribute was created by default * @param index the position of the attribute * @return true if this is a default attribute * @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE */ public boolean isAttributeSpecified(int index) { //check that current state should be either START_ELEMENT or ATTRIBUTE if( (fEventType == XMLEvent.START_ELEMENT) || (fEventType == XMLEvent.ATTRIBUTE)){ return fScanner.getAttributeIterator().isSpecified(index) ; } else{ throw new IllegalStateException("Current state is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) + " , " + getEventTypeString(XMLEvent.ATTRIBUTE) + "valid for isAttributeSpecified()") ; } } /** Returns true if the cursor points to a character data event * @return true if the cursor points to character data, false otherwise */ public boolean isCharacters() { return fEventType == XMLEvent.CHARACTERS ; } /** Skips any insignificant events (COMMENT and PROCESSING_INSTRUCTION) * until a START_ELEMENT or * END_ELEMENT is reached. If other than space characters are * encountered, an exception is thrown. This method should * be used when processing element-only content because * the parser is not able to recognize ignorable whitespace if * then DTD is missing or not interpreted. * @return the event type of the element read * @throws XMLStreamException if the current event is not white space */ public int nextTag() throws XMLStreamException { int eventType = next(); while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT ) { eventType = next(); } if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException( "found: " + getEventTypeString(eventType) + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT) + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT), getLocation()); } return eventType; } /** Checks if standalone was set in the document * @return true if standalone was set in the document, or false otherwise */ public boolean standaloneSet() { //xxx: it requires if the standalone was set in the document ? This is different that if the document // is standalone return fScanner.isStandAlone() ; } /** * @param qname * @return */ public javax.xml.namespace.QName convertXNIQNametoJavaxQName(com.sun.org.apache.xerces.internal.xni.QName qname){ //xxx: prefix definition ? if(qname.prefix == null){ return new javax.xml.namespace.QName(qname.uri, qname.localpart) ; } else{ return new javax.xml.namespace.QName(qname.uri, qname.localpart, qname.prefix) ; } } /** Return the uri for the given prefix. * The uri returned depends on the current state of the processor. * * <p><strong>NOTE:</strong>The 'xml' prefix is bound as defined in * <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a> * specification to "http://www.w3.org/XML/1998/namespace". * * <p><strong>NOTE:</strong> The 'xmlns' prefix must be resolved to following namespace * <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a> * @return the uri bound to the given prefix or null if it is not bound * @param prefix The prefix to lookup, may not be null */ public String getNamespaceURI(String prefix) { //first add the string to symbol table.. since internally identity comparisons are done. return fScanner.getNamespaceContext().getURI(fSymbolTable.addSymbol(prefix)) ; } //xxx: this function is not being used. protected void setPropertyManager(PropertyManager propertyManager){ fPropertyManager = propertyManager ; //REVISIT: we were supplying hashmap ealier fScanner.setProperty("stax-properties",propertyManager); fScanner.setPropertyManager(propertyManager) ; } /** * @return returns the reference to property manager. */ protected PropertyManager getPropertyManager(){ return fPropertyManager ; } static void pr(String str) { System.out.println(str) ; } protected List getEntityDecls(){ if(fEventType == XMLStreamConstants.DTD){ XMLEntityStorage entityStore = fEntityManager.getEntityStore(); Hashtable ht = entityStore.getDeclaredEntities(); ArrayList list = null; if(ht != null){ EntityDeclarationImpl decl = null; list = new ArrayList(ht.size()); Enumeration enu = ht.keys(); while(enu.hasMoreElements()){ String key = (String)enu.nextElement(); Entity en = (Entity)ht.get(key); decl = new EntityDeclarationImpl(); decl.setEntityName(key); if(en.isExternal()){ decl.setXMLResourceIdentifier(((Entity.ExternalEntity)en).entityLocation); decl.setNotationName(((Entity.ExternalEntity)en).notation); } else decl.setEntityReplacementText(((Entity.InternalEntity)en).text); list.add(decl); } } return list; } return null; } protected List getNotationDecls(){ if(fEventType == XMLStreamConstants.DTD){ if(fScanner.fDTDScanner == null) return null; DTDGrammar grammar = ((XMLDTDScannerImpl)(fScanner.fDTDScanner)).getGrammar(); if(grammar == null) return null; List notations = grammar.getNotationDecls(); Iterator it = notations.iterator(); ArrayList list = new ArrayList(); while(it.hasNext()){ XMLNotationDecl ni = (XMLNotationDecl)it.next(); if(ni!= null){ list.add(new NotationDeclarationImpl(ni)); } } return list; } return null; } }//XMLReaderImpl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -