Code example for LinkedList

Methods: removeLast, size

0
        direction = -1;
        final Handler handler = getHandler( uri,
                                            localName );
 
        if ( handler == null ) { 
            if ( this.configurationStack.size() >= 1 ) {
                endElementBuilder(); 
            } 
            return; 
        } 
 
        this.current = removeParent();
 
        this.peer = handler.end( uri,
                                 localName,
                                 this );
    } 
 
    public static class Null { 
        public static final Null instance = new Null();
    } 
 
    private void validate(final String uri,
                          final String localName,
                          final Handler handler) throws SAXParseException { 
        boolean validParent = false;
        boolean validPeer = false;
        boolean invalidNesting = false;
 
        final Set validParents = handler.getValidParents();
        final Set validPeers = handler.getValidPeers();
        boolean allowNesting = handler.allowNesting();
         
        if ( validParents == null || validPeers == null ) {
            return; 
        } 
 
        // 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 
        final Class nodeClass = getHandler( uri,
                                            localName ).generateNodeFor();
        if ( nodeClass != null && !nodeClass.isInstance( parent ) ) {
            Object allowedParent;
            final 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 [" + parent + "]",
                                             getLocator() ); 
            } 
        } 
 
        // check valid peers 
        // null peer means localname is rule-set 
        final 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: " + Handler.class.getName(),
                                         getLocator() ); 
        } 
 
        if ( nodeClass != null && !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 tagName 
     *            Tag name. 
     * @param attrs 
     *            Tag attributes. 
     */ 
    public void startElementBuilder(final String tagName,
                                    final Attributes attrs) {
        this.characters = new StringBuilder();
 
        final Element element = this.document.createElement( tagName );
 
        //final DefaultConfiguration config = new DefaultConfiguration( tagName ); 
 
        final int numAttrs = attrs.getLength();
 
        for ( int i = 0; i < numAttrs; ++i ) {
            element.setAttribute( attrs.getLocalName( i ),
                                  attrs.getValue( i ) );
        } 
 
        if ( this.configurationStack.isEmpty() ) {
            this.configurationStack.addLast( element );
        } else { 
            ((Element) this.configurationStack.getLast()).appendChild( element );
            this.configurationStack.addLast( element );
        } 
    } 
 
    Handler getHandler(final String uri,
                       final String localName) {
        SemanticModule module = this.modules.getSemanticModule( uri );
        if ( module != null ) {
            return module.getHandler( localName );
        } else { 
            return null; 
        } 
    } 
 
    /** 
     * @param chars 
     * @param start 
     * @param len 
     * @see org.xml.sax.ContentHandler 
     */ 
    public void characters(final char[] chars,
                           final int start,
                           final int len) {
        if ( this.characters != null ) {
            this.characters.append( chars,
                                    start,
                                    len );
        } 
    } 
 
    /** 
     * End a configuration node. 
     * 
     * @return The configuration. 
     */ 
    public Element endElementBuilder() {
        final Element element = (Element) this.configurationStack.removeLast();
        if ( this.characters != null ) {
            element.appendChild( this.document.createTextNode( this.characters.toString() ) );
        } 
 
        this.characters = null;