001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.runtime.support.stubgen.parser;
018    
019    import org.codehaus.gmaven.runtime.support.stubgen.UnexpectedNodeException;
020    
021    /**
022     * Support for {@link Node} implementations.
023     *
024     * @version $Id: NodeSupport.java 18 2009-07-16 09:39:40Z user57 $
025     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
026     */
027    public abstract class NodeSupport
028        implements Node
029    {
030        protected abstract int type();
031    
032        protected abstract Tokens tokens();
033    
034        protected abstract String name();
035    
036        protected abstract int childCount();
037    
038        public boolean isLeaf() {
039            return childCount() == 0;
040        }
041        
042        public String toString() {
043            String name = name();
044            String text = text();
045            String position = line() + ":" + column();
046    
047            if (name.equals(text)) {
048                return "Node[" + position + "," + type() + "," + name + "]";
049            }
050            else {
051                return "Node[" + position + "," + type() + "," + name + "=" + text + "]";
052            }
053        }
054    
055        public void dump(final String pad) {
056            // FIXME: Use logging
057            System.out.println(pad + this);
058            
059            Node child = firstChild();
060            if (child != null) {
061                child.dump(pad + "    ");
062            }
063    
064            Node sibling = nextSibling();
065            if (sibling != null) {
066                sibling.dump(pad);
067            }
068        }
069    
070        public void dump() {
071            dump("");
072            System.out.println();
073        }
074    
075        public boolean is(final String name) {
076            assert name != null;
077    
078            return type() == tokens().value(name);
079        }
080    
081        public boolean is(final String[] names) {
082            assert names != null;
083            assert names.length != 0;
084    
085            for (int i=0; i<names.length; i++) {
086                if (is(names[i])) {
087                    return true;
088                }
089            }
090    
091            return false;
092        }
093    
094        public void ensure(final String name) throws UnexpectedNodeException {
095            if (!is(name)) {
096                throw new UnexpectedNodeException(this);
097            }
098        }
099    
100        public Node skip(final String name) {
101            if (is(name)) {
102                return nextSibling();
103            }
104    
105            return this;
106        }
107    }