📄 saxunmarshallerhandlerimpl.java
字号:
if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) ) // make sure that this prefix is still effective. a.add(nsBind[i]); return a; } public String getPrefix(String uri) { if( uri.equals(XMLConstants.XML_NS_URI) ) return XMLConstants.XML_NS_PREFIX; if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) ) return XMLConstants.XMLNS_ATTRIBUTE; if( uri==null ) throw new IllegalArgumentException(); for( int i=idxStack[stackTop]-2; i>=0; i-=2 ) if(uri.equals(nsBind[i+1])) if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) ) // make sure that this prefix is still effective. return nsBind[i]; return null; } public String getNamespaceURI(String prefix) { if( prefix.equals(XMLConstants.XMLNS_ATTRIBUTE) ) return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; if( prefix==null ) throw new IllegalArgumentException(); return resolveNamespacePrefix(prefix); }////// Attribute handling//// /** * Attributes stack. */ private AttributesImpl[] attStack = new AttributesImpl[16]; /** * Element nesting level. */ private int elementDepth; /** * Always {@link #elementDepth}-1. */ private int stackTop; /** * Stack of collectText flag. * False means text can be ignored for this element. * * Use {@link #elementDepth} and {@link #stackTop} to access the array. */ private boolean[] collectText = new boolean[16]; public void pushAttributes( Attributes atts, boolean collectTextFlag ) { if( attStack.length==elementDepth ) { // reallocate the buffer AttributesImpl[] buf1 = new AttributesImpl[attStack.length*2]; System.arraycopy(attStack,0,buf1,0,attStack.length); attStack = buf1; int[] buf2 = new int[idxStack.length*2]; System.arraycopy(idxStack,0,buf2,0,idxStack.length); idxStack = buf2; boolean[] buf3 = new boolean[collectText.length*2]; System.arraycopy(collectText,0,buf3,0,collectText.length); collectText = buf3; } elementDepth++; stackTop++; // push the stack AttributesImpl a = attStack[stackTop]; if( a==null ) attStack[stackTop] = a = new AttributesImpl(); else a.clear(); // since Attributes object is mutable, it is criticall important // to make a copy. // also symbolize attribute names for( int i=0; i<atts.getLength(); i++ ) { String auri = atts.getURI(i); String alocal = atts.getLocalName(i); String avalue = atts.getValue(i); String aqname = atts.getQName(i); // work gracefully with misconfigured parsers that don't support namespaces if( auri==null ) auri=""; if( alocal==null || alocal.length()==0 ) alocal=aqname; if( aqname==null || aqname.length()==0 ) aqname=alocal; // <foo xsi:nil="false">some value</foo> is a valid fragment, however // we need a look ahead to correctly handle this case. // (because when we process @xsi:nil, we don't know what the value is, // and by the time we read "false", we can't cancel this attribute anymore.) // // as a quick workaround, we remove @xsi:nil if the value is false. if( auri=="http://www.w3.org/2001/XMLSchema-instance" && alocal=="nil" ) { String v = avalue.trim(); if(v.equals("false") || v.equals("0")) continue; // skip this attribute } // otherwise just add it. a.addAttribute( auri, alocal, aqname, atts.getType(i), avalue ); } // start a new namespace scope idxStack[stackTop] = nsLen; collectText[stackTop] = collectTextFlag; } public void popAttributes() { stackTop--; elementDepth--; } public Attributes getUnconsumedAttributes() { return attStack[stackTop]; } /** * @param uri,local * has to be interned. */ public int getAttribute( String uri, String local ) { return attStack[stackTop].getIndexFast(uri,local); } public void consumeAttribute( int idx ) throws SAXException { AttributesImpl a = attStack[stackTop]; String uri = a.getURI(idx); String local = a.getLocalName(idx); String qname = a.getQName(idx); String value = a.getValue(idx); // mark the attribute as consumed // we need to remove the attribute before we process it // because the event handler might access attributes. a.removeAttribute(idx); getCurrentHandler().enterAttribute(uri,local,qname); consumeText(value,false); getCurrentHandler().leaveAttribute(uri,local,qname); } public String eatAttribute( int idx ) throws SAXException { AttributesImpl a = attStack[stackTop]; String value = a.getValue(idx); // mark the attribute as consumed a.removeAttribute(idx); return value; }////// ID/IDREF related code//// /** * Submitted patchers in the order they've submitted. * Many XML vocabulary doesn't use ID/IDREF at all, so we * initialize it with null. */ private Runnable[] patchers = null; private int patchersLen = 0; public void addPatcher( Runnable job ) { // re-allocate buffer if necessary if( patchers==null ) patchers = new Runnable[32]; if( patchers.length == patchersLen ) { Runnable[] buf = new Runnable[patchersLen*2]; System.arraycopy(patchers,0,buf,0,patchersLen); patchers = buf; } patchers[patchersLen++] = job; } /** Executes all the patchers. */ private void runPatchers() { if( patchers!=null ) { for( int i=0; i<patchersLen; i++ ) patchers[i].run(); } } /** Records ID->Object map. */ private Hashtable idmap = null; public String addToIdTable( String id ) { if(idmap==null) idmap = new Hashtable(); idmap.put( id, getCurrentHandler().owner() ); return id; } public Object getObjectFromId( String id ) { if(idmap==null) return null; return idmap.get(id); } ////// Other SAX callbacks//// public void skippedEntity( String name ) { } public void processingInstruction( String target, String data ) { // just ignore } public void setDocumentLocator( Locator loc ) { locator = loc; } public Locator getLocator() { return locator; } private Locator locator;////// error handling//// private final UnmarshallerImpl parent; private boolean aborted = false; public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException { ValidationEventHandler eventHandler; try { eventHandler = parent.getEventHandler(); } catch( JAXBException e ) { // impossible. throw new JAXBAssertionError(); } boolean recover = eventHandler.handleEvent(event); // if the handler says "abort", we will not return the object // from the unmarshaller.getResult() if(!recover) aborted = true; if( !canRecover || !recover ) throw new SAXException( new UnmarshalException( event.getMessage(), event.getLinkedException() ) ); } ////// ValidationContext implementation//// public String getBaseUri() { return null; } public boolean isUnparsedEntity(String s) { return true; } public boolean isNotation(String s) { return true; }////// debug trace methods//// private Tracer tracer; public void setTracer( Tracer t ) { this.tracer = t; } public Tracer getTracer() { if(tracer==null) tracer = new Tracer.Standard(); return tracer; } /** * Computes the names of possible root elements for a better error diagnosis. */ private String computeExpectedRootElements() { String r = ""; String[] probePoints = grammarInfo.getProbePoints(); for( int i=0; i<probePoints.length; i+=2 ) { if( grammarInfo.recognize(probePoints[i],probePoints[i+1]) ) { if(r.length()!=0) r+=','; r += "<{"+probePoints[i]+"}"+probePoints[i+1]+">"; } } return r; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -