tree grammar DmaplTree; options{ tokenVocab = Dmapl; ASTLabelType = CommonTree; } @header{ package pt.ist.dmapl; import pt.ist.dmapl.*; import pt.ist.dmapl.model.*; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; import java.util.Map; import java.util.HashMap; import java.lang.String; } @members{ private Set policyRules = new HashSet(); private Map tickets = new HashMap(); private UserFinder userFinder; private RoleFinder roleFinder; private boolean injectionMode = false; public void setFinder(UserFinder userFinder, RoleFinder roleFinder) { this.userFinder = userFinder; this.roleFinder = roleFinder; } public void setInjectionMode() { this.injectionMode = true; } } policy returns [ArrayList policyAndTickets] : rule* {$policyAndTickets = new ArrayList(); $policyAndTickets.add(policyRules); $policyAndTickets.add(tickets);} ; rule : r=posAuthRule { policyRules.add(r); } | rn=negAuthRule { policyRules.add(rn);} | a=amplificationRule { policyRules.add(a); } | d=delegRule { policyRules.add(d); } | t = ticketDecl { tickets.put(t.getName(), t); } | crr=changeRelationRule { policyRules.add(crr); } | arr=addRelationRule { policyRules.add(arr); } | rrr=removeRelationRule { policyRules.add(rrr); } ; changeRelationRule returns [RelationRule rule] : ^(CHANGE_RELATION_RULE IDENTIFIER s=subject t=targetRelation c=constraint?) { $rule = new RelationRule($IDENTIFIER.text, s, t, c, Rule.CHANGE_RELATION_RULE); } ; addRelationRule returns [RelationRule rule] : ^(ADD_RELATION_RULE IDENTIFIER s=subject t=targetRelation c=constraint?) { $rule = new RelationRule($IDENTIFIER.text, s, t, c, Rule.ADD_RELATION_RULE); } ; removeRelationRule returns [RelationRule rule] : ^(REMOVE_RELATION_RULE IDENTIFIER s=subject t=targetRelation c=constraint?) { $rule = new RelationRule($IDENTIFIER.text, s, t, c, Rule.REMOVE_RELATION_RULE); } ; posAuthRule returns [AuthRule rule] : ^(ALLOW IDENTIFIER s=subject t=target c=constraint?) { $rule = new AuthRule($IDENTIFIER.text, s, t, c, Rule.POS_AUTH_RULE); } ; negAuthRule returns [AuthRule rule] : ^(DENY IDENTIFIER s=subject t=target c=constraint?) { $rule = new AuthRule($IDENTIFIER.text, s, t, c, Rule.NEG_AUTH_RULE); } ; subject returns [Subject subject] : ^(SUBJECT_ROLE role=IDENTIFIER) {if(injectionMode) { $subject = new RoleSubject(null); }else { $subject = new RoleSubject(this.roleFinder.getRoleByName(role.getText())); } } | ^(SUBJECT_USER user=IDENTIFIER) {if(injectionMode) { $subject = new UserSubject(null); }else { $subject = new UserSubject(this.userFinder.getUserByUserId(user.getText())); } } ; target returns [Target target] : m=targetMethod { $target = m; } | a=targetAnnotation { $target = a; } ; targetRelation returns [RelationTarget target] : ^(TARGET_RELATION r=relationName) {$target = new RelationTarget(r);} ; targetMethod returns [MethodTarget target] : ^(TARGET_METHOD c=className m=methodName a=argumentList) {$target = new MethodTarget(c, m, a);} ; targetAnnotation returns [AnnotationTarget target] : ^(TARGET_ANNOTATION annotation=ANNOTATION_LITERAL) {$target = new AnnotationTarget($annotation.text);} ; className returns [String className] : ^(CLASS_NAME i+=IDENTIFIER (i+='.' i+=IDENTIFIER)*) { $className = new String(); for(CommonTree id : (ArrayList)$i) { $className = $className.concat(id.getText()); } } ; relationName returns [String relationName] : ^(RELATION_NAME i+=IDENTIFIER (i+='.' i+=IDENTIFIER)*) { $relationName = new String(); for(CommonTree id : (ArrayList)$i) { $relationName = $relationName.concat(id.getText()); } } ; methodName returns [String methodName] : ^(METHOD_NAME i=IDENTIFIER) {$methodName = $i.text;} ; /* argumentList returns [ArrayList argList] : ^(ARG_LIST argument*) {$argList = null;} ; */ argumentList returns [List argList] : ^(ARG_LIST (args+=IDENTIFIER args+=IDENTIFIER)*) {$argList = new ArrayList(); if($args != null) { for(int i = 0; i < $args.size(); i += 2) { $argList.add(new Parameter(((CommonTree)$args.get(i)).getText(), ((CommonTree)$args.get(i + 1)).getText())); } } } ; constraint returns [Constraint constraint] : ^(CONSTRAINT c=CONSTRAINT_LITERAL) {$constraint = new BeanShellConstraint($c.text); } ; amplificationRule returns [AmplificationRule amplificationRule] : ^(AMP_RULE n=IDENTIFIER? m=targetMethod s=subject? t=ticketInvocation[m]+) {List ticketList = new ArrayList(); ticketList.add(t); /* TODO: Multiple Tickets */ if(n == null) { $amplificationRule = new AmplificationRule(null, s, m, ticketList); } else { $amplificationRule = new AmplificationRule($n.text, s, m, ticketList); } } ; ticketDecl returns [TicketPrototype ticket] : ^(TICKET_DECL id=IDENTIFIER a=argumentList t=target c=constraint?) { $ticket = new TicketPrototype($id.text, t, c, a); } ; ticketInvocation [MethodTarget methodTarget] returns[TicketPrototype ticket] : ^(TICKET_INV id=IDENTIFIER args=ticketArgs) { $ticket = tickets.get($id.text); if(args.size() != $ticket.getParameters().size()) { /* error */ } for(int i = 0; i < args.size(); i++) { if(args.get(i).matches("receiver")) { $ticket.getParameters().get(i).setValue(new Integer(-1)); } else { boolean found = false; for(int j = 0; j < methodTarget.getParameters().size(); j++) { if(args.get(i).matches(methodTarget.getParameters().get(j).getName())) { $ticket.getParameters().get(i).setValue(new Integer(i)); found = true; break; } } if(!found) { /* error */ } } } /** TODO: Type Checking, param checking*/ } ; ticketArgs returns [List arguments] : ^(TICKET_ARG_LIST id+=IDENTIFIER*) { $arguments = new ArrayList(); if($id != null) { for(int i = 0; i < $id.size(); i++) { $arguments.add(((CommonTree)$id.get(i)).getText()); } } } ; delegRule returns [DelegRule delegRule] : ^(DELEG n=IDENTIFIER? a=IDENTIFIER tor=grantor? tee=grantee t=targetMethod? c=constraint? v=validity?) { /*boolean found = false; String name = null; if(n != null) { name = $n.text; } for(Rule rule : policyRules) { if((rule.getRuleType() == Rule.AUTH_RULE) && rule.getName().matches($a.text)) { $delegRule = new DelegRule(name, DelegRule.POSITIVE_DELEG_RULE, (AuthRule)rule, tor, tee, t, c, v, h); found = true; } } if(!found) { System.out.println("Rule " + a + " does not exist!"); }*/ $delegRule = null; } ; grantor returns [Subject grantor] : ^(GRANTOR s=subject) { $grantor = s; } ; grantee returns [Subject grantee] : ^(GRANTEE s=subject) { $grantee = s; } ; validity returns [Constraint constraint] : ^(VALID c=CONSTRAINT_LITERAL) { $constraint = new BeanShellConstraint($c.text); } ;