001    /*
002     * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia.util;
024    
025    import antlr.collections.AST;
026    
027    public class MatchPath {
028            RuleDefinition owner;
029            String tokenType;
030            String contextType;
031            boolean isNegation;
032            
033            /**
034             * @param owner
035             * @param node
036             */
037            public MatchPath(RuleDefinition owner, AST node) {
038                    super();
039                    this.owner = owner;
040                    for (AST child=node.getFirstChild(); child!=null; child=child.getNextSibling()) {
041                            switch (child.getType()) {
042                            case BnfTokenTypes.IDENT:
043                                    tokenType=child.getText();
044                                    owner.getModel().checkTokenName(tokenType);
045                                    break;
046                            case BnfTokenTypes.EXCL:
047                                    isNegation=true;
048                                    tokenType=child.getFirstChild().getText();
049                                    owner.getModel().checkTokenName(tokenType);
050                                    break;
051                            case BnfTokenTypes.CONTEXT:
052                                    StringBuffer csb=new StringBuffer();
053                                    Attribute.typeName(child.getFirstChild(), csb);
054                                    contextType=csb.toString();
055                                    if (contextType.indexOf('.')==-1) {
056                                            contextType=owner.getModel().getPackage()+"."+contextType;
057                                    }
058                                    break;
059                            default:
060                                    throw new IllegalStateException("Unexpected node: "+BnfRecognizer._tokenNames[child.getType()]);
061                            }
062                    }
063            }
064            
065            public boolean equals(Object obj) {
066                    if (obj instanceof MatchPath) {
067                            MatchPath omp = (MatchPath) obj;
068                            return 
069                                    tokenType.equals(omp.tokenType)
070                                    && isNegation==omp.isNegation
071                                    && (contextType==null ? omp.contextType==null : contextType.equals(omp.contextType));
072                    }
073                    return false;
074            }
075            
076            public int hashCode() {
077                    return tokenType.hashCode() ^ (contextType==null ? 0 : contextType.hashCode()) ^ (isNegation ? 0 : 1); 
078            }
079                    
080            public String getContextType() {
081                    return contextType;
082            }
083            
084            public String getTokenType() {
085                    return tokenType;
086            }
087            
088            public RuleDefinition getOwner() {
089                    return owner;
090            }
091            
092            public boolean isNegation() {
093                    return isNegation;
094            }
095            
096            @Override
097            public String toString() {
098                    return getClass().getName()+(isNegation? " [!" : " [")+tokenType+"@"+contextType+"]";
099            }
100    }