001    /*
002    @license.text@
003     */
004    package com.hammurapi.bnf2emf;
005    
006    import antlr.collections.AST;
007    
008    /**
009     * Parses attribute definition.
010     * 
011     * @author Pavel Vlasov
012     * @revision $Revision$
013     */
014    public class Attribute {
015            
016            public enum AttributeType { ATTRIBUTE, REFERENCE }
017            
018            private boolean isList;
019            private AttributeType attributeType;
020            private boolean isReference;
021            private String name;
022            private String type;
023            private Attribute opposite;
024            
025            public Attribute(AST node) {
026                    for (AST child=node.getFirstChild(); child!=null; child=child.getNextSibling()) {
027                            switch (child.getType()) {
028                            case BnfTokenTypes.LITERAL_attribute:
029                                    attributeType = AttributeType.ATTRIBUTE;
030                                    break;
031                            case BnfTokenTypes.LITERAL_reference:
032                                    attributeType = AttributeType.REFERENCE;
033                                    break;
034                            case BnfTokenTypes.LITERAL_opposite:
035                                    opposite = new Attribute(child);
036                                    break;
037                            case BnfTokenTypes.LITERAL_list:
038                                    isList=true;
039                                    break;
040                            case BnfTokenTypes.IDENT:
041                            case BnfTokenTypes.DOT:
042                                    StringBuffer nameBuf=new StringBuffer();
043                                    typeName(child, nameBuf);
044                                    if (name==null) {
045                                            name=nameBuf.toString();
046                                            type=name;
047                                    } else {
048                                            type=nameBuf.toString();
049                                    }
050                                    break;
051                            default:
052                                    throw new IllegalArgumentException("Unexpected node type: "+BnfRecognizer._tokenNames[child.getType()]);
053                            }
054                    }
055            }
056            
057            static void typeName(AST node, StringBuffer sb) {
058                    if (node.getType()==BnfTokenTypes.DOT) {
059                            typeName(node.getFirstChild(), sb);
060                            sb.append(".");
061                            typeName(node.getFirstChild().getNextSibling(), sb);
062                    } else {
063                            sb.append(node.getText());
064                    }
065            }       
066            
067            public String getName() {
068                    return name;
069            }
070            
071            public boolean isList() {
072                    return isList;
073            }
074    
075            public AttributeType getAttributeType() {
076                    return attributeType;
077            }
078                    
079            public String getType() {
080                    return type;
081            }
082            
083            public boolean isReference() {
084                    return isReference;
085            }
086            
087            public Attribute getOpposite() {
088                    return opposite;
089            }
090    }