| 1 | |
package org.webslinger.rules; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import javax.servlet.ServletRequest; |
| 5 | |
|
| 6 | |
import org.webslinger.Webslinger; |
| 7 | |
|
| 8 | |
public class CSSAttributeRestriction extends AbstractRestriction { |
| 9 | |
private String name; |
| 10 | |
private String value; |
| 11 | 62 | private Operator operator = Operator.HAS; |
| 12 | |
|
| 13 | |
public CSSAttributeRestriction(int id) { |
| 14 | 0 | super(id); |
| 15 | 0 | } |
| 16 | |
|
| 17 | |
public CSSAttributeRestriction(Rules p, int id) { |
| 18 | 62 | super(p, id); |
| 19 | 62 | } |
| 20 | |
|
| 21 | |
|
| 22 | |
public Object jjtAccept(RulesVisitor visitor, Object data) { |
| 23 | 0 | return visitor.visit(this, data); |
| 24 | |
} |
| 25 | |
|
| 26 | |
void setName(String name) { |
| 27 | 62 | this.name = name; |
| 28 | 62 | } |
| 29 | |
|
| 30 | |
public String getName() { |
| 31 | 1501 | return name; |
| 32 | |
} |
| 33 | |
|
| 34 | |
void setOperator(Operator operator) { |
| 35 | 61 | this.operator = operator; |
| 36 | 61 | } |
| 37 | |
|
| 38 | |
public Operator getOperator() { |
| 39 | 0 | return operator; |
| 40 | |
} |
| 41 | |
|
| 42 | |
void setValue(String value) { |
| 43 | 61 | this.value = value; |
| 44 | 61 | } |
| 45 | |
|
| 46 | |
public String getValue() { |
| 47 | 1417 | return value; |
| 48 | |
} |
| 49 | |
|
| 50 | |
public void updateSpecificity(int[] specificity, boolean[] isConstantHolder) { |
| 51 | 0 | isConstantHolder[0] = false; |
| 52 | 0 | if ("type".equals(getName())) { |
| 53 | 0 | specificity[Selector.TYPE.ordinal()]++; |
| 54 | 0 | } else if ("class".equals(getName())) { |
| 55 | 0 | specificity[Selector.CLASS.ordinal()]++; |
| 56 | |
} else { |
| 57 | 0 | specificity[Selector.ATTRIBUTE.ordinal()]++; |
| 58 | |
} |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public boolean matches(ServletRequest request, Webslinger top) throws IOException { |
| 62 | 1493 | if (top == null) return false; |
| 63 | 1490 | switch (operator) { |
| 64 | |
case HAS: |
| 65 | 28 | return top.getAttribute(getName()) != null; |
| 66 | |
case EQUALS: |
| 67 | 1408 | return getValue().equals(top.getAttribute(getName())); |
| 68 | |
case CONTAINS: |
| 69 | 56 | String[] parts = top.getSplitAttribute(getName()); |
| 70 | 56 | if (parts == null) return false; |
| 71 | 0 | for (String part: parts) { |
| 72 | 0 | if (part.equals(getValue())) return true; |
| 73 | |
} |
| 74 | 0 | return false; |
| 75 | |
} |
| 76 | 0 | return false; |
| 77 | |
} |
| 78 | |
|
| 79 | |
public String toString() { |
| 80 | 0 | return "AttributeRestriction(" + getName() + ":" + getOperator() + ":" + getValue() + ")"; |
| 81 | |
} |
| 82 | |
} |