📄 rulesetreader.java
字号:
} /** * @see org.xml.sax.ContentHandler */ public void startElement( String uri, String localName, String qname, Attributes attrs ) throws SAXException { //going down so no peer if ( !this.lastWasEndElement ) { this.peer = null; } Handler handler = getHandler( localName ); validate( localName, handler ); Object node = handler.start( uri, localName, attrs ); if ( node != null ) { parents.add( node ); this.current = node; } this.lastWasEndElement = false; } /** * @see org.xml.sax.ContentHandler */ public void endElement( String uri, String localName, String qname ) throws SAXException { Handler handler = getHandler( localName ); this.current = getParent( handler.generateNodeFor( ) ); Object node = handler.end( uri, localName ); //next if ( node != null && !this.lastWasEndElement ) { this.peer = node; } //up or no children else if ( this.lastWasEndElement || ( this.parents.getLast( ) ).getClass( ) .isInstance( this.current ) ) { this.peer = this.parents.removeLast( ); } this.lastWasEndElement = true; } private void validate( String localName, Handler handler ) throws SAXParseException { boolean validParent = false; boolean validPeer = false; boolean invalidNesting = false; Set validParents = handler.getValidParents( ); Set validPeers = handler.getValidPeers( ); boolean allowNesting = handler.allowNesting( ); //get parent Object parent; if ( this.parents.size( ) != 0 ) { parent = this.parents.getLast( ); } else { parent = null; } // check valid parents // null parent means localname is rule-set // dont process if elements are the same // instead check for allowed nesting Class nodeClass = getHandler( localName ).generateNodeFor( ); if ( !nodeClass.isInstance( parent ) ) { Object allowedParent; Iterator it = validParents.iterator( ); while ( !validParent && it.hasNext( ) ) { allowedParent = it.next( ); if ( parent == null && allowedParent == null ) { validParent = true; } else if ( allowedParent != null && ( ( Class ) allowedParent).isInstance( parent ) ) { validParent = true; } } if ( !validParent ) { throw new SAXParseException( "<" + localName + "> has an invalid parent element", getLocator( ) ); } } // check valid peers // null peer means localname is rule-set Object peer = this.peer; Object allowedPeer; Iterator it = validPeers.iterator( ); while ( !validPeer && it.hasNext( ) ) { allowedPeer = it.next( ); if ( peer == null && allowedPeer == null ) { validPeer = true; } else if ( allowedPeer != null && ( ( Class ) allowedPeer).isInstance( peer ) ) { validPeer = true; } } if ( !validPeer ) { throw new SAXParseException( "<" + localName + "> is after an invalid element", getLocator( ) ); } if ( !allowNesting ) { it = this.parents.iterator( ); while ( !invalidNesting && it.hasNext( ) ) { if ( nodeClass.isInstance( it.next( ) ) ) { invalidNesting = true; } } } if ( invalidNesting ) { throw new SAXParseException( "<" + localName + "> may not be nested", getLocator( ) ); } } /** * Start a configuration node. * * @param name * Tag name. * @param attrs * Tag attributes. * * @throws SAXException * If an error occurs during parse. */ protected void startConfiguration( String name, Attributes attrs ) throws SAXException { this.characters2 = new StringBuffer( ); DefaultConfiguration config = new DefaultConfiguration( name ); int numAttrs = attrs.getLength( ); for ( int i = 0; i < numAttrs; ++i ) { config.setAttribute( attrs.getLocalName( i ), attrs.getValue( i ) ); } if ( this.configurationStack.isEmpty( ) ) { this.configurationStack.addLast( config ); } else { ((DefaultConfiguration) this.configurationStack.getLast( )) .addChild( config ); } } /** * @see org.xml.sax.ContentHandler */ public void characters( char[] chars, int start, int len ) throws SAXException { if ( this.characters2 != null ) { this.characters2.append( chars, start, len ); } } /** * End a configuration node. * * @return The configuration. */ protected Configuration endConfiguration() { DefaultConfiguration config = (DefaultConfiguration) this.configurationStack .removeLast( ); config.setText( this.characters2.toString( ) ); this.characters2 = null; return config; } SemanticModule lookupSemanticModule( String uri, String localName ) throws SAXParseException { SemanticModule module; try { module = this.repo.lookupSemanticModule( uri ); } catch ( NoSuchSemanticModuleException e ) { throw new SAXParseException( "no semantic module for namespace '" + uri + "' (" + localName + ")", getLocator( ) ); } return module; } private Handler getHandler( String localName ) throws SAXParseException { Handler handler = (Handler) this.localNameMap.get( localName ); if ( handler == null ) { throw new SAXParseException( "unable to handle element <" + localName + ">", getLocator( ) ); } return handler; } LinkedList getParents() { return this.parents; } Object getParent( Class parent ) { ListIterator it = this.parents.listIterator( parents.size( ) ); Object node = null; while ( it.hasPrevious( ) ) { node = it.previous( ); if ( parent.isInstance( node ) ) break; } return node; } Object getPeer() { return this.peer; } Object getCurrent() { return this.current; } public InputSource resolveEntity(String publicId, String systemId) throws SAXException { //Schema files must end with xsd if (!systemId.toLowerCase( ).endsWith("xsd")) { return null; } //try the actual location given by systemId try { URL url = new URL(systemId); return new InputSource(url.openStream()); } catch (Exception e) { } //Try and get the index for the filename, else return null String xsd; int index = systemId.lastIndexOf("/"); if (index == -1) { index = systemId.lastIndexOf("\\"); } if (index != -1) { xsd = systemId.substring(index+1); } else { xsd = systemId; } ClassLoader cl = Thread.currentThread( ).getContextClassLoader( ); if ( cl == null ) { cl = RuleSetReader.class.getClassLoader( ); } //Try looking in META-INF try { return new InputSource(cl.getResourceAsStream("META-INF/" + xsd)); } catch (Exception e) { } //Try looking in /META-INF try { return new InputSource(cl.getResourceAsStream("/META-INF/" + xsd)); } catch (Exception e) { } //Try looking at root of classpath try { return new InputSource(cl.getResourceAsStream("/" + xsd)); } catch (Exception e) { } //Try current working directory try { return new InputSource(new BufferedInputStream(new FileInputStream(xsd))); } catch (Exception e) { } return null; } private void print( SAXParseException x ) { String msg = message.format( new Object[]{x.getSystemId( ), new Integer( x.getLineNumber( ) ), new Integer( x.getColumnNumber( ) ), x.getMessage( )} ); System.out.println( msg ); } public void warning( SAXParseException x ) { print( x ); } public void error( SAXParseException x ) { print( x ); } public void fatalError( SAXParseException x ) throws SAXParseException { print( x ); throw x; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -