| 1 | |
package org.webslinger.rules; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.ListIterator; |
| 6 | |
import javax.servlet.ServletException; |
| 7 | |
import javax.servlet.ServletRequest; |
| 8 | |
|
| 9 | |
import org.webslinger.Webslinger; |
| 10 | |
import org.webslinger.container.WebslingerContainer; |
| 11 | |
|
| 12 | |
public abstract class AbstractSelector extends SimpleNode { |
| 13 | |
protected int[] specificity; |
| 14 | 126 | protected AbstractRestriction[] restrictions = new AbstractRestriction[0]; |
| 15 | |
|
| 16 | |
public AbstractSelector(int id) { |
| 17 | 0 | super(id); |
| 18 | 0 | } |
| 19 | |
|
| 20 | |
public AbstractSelector(Rules p, int id) { |
| 21 | 126 | super(p, id); |
| 22 | 126 | } |
| 23 | |
|
| 24 | |
public boolean matches(ServletRequest request, Webslinger top) throws IOException, ServletException { |
| 25 | 3574 | if (!checkRestrictions(request, top)) return false; |
| 26 | 2595 | if (parent instanceof AbstractSelector) { |
| 27 | 164 | if (top.getParent() == null) return false; |
| 28 | 117 | return ((AbstractSelector) parent).matches(request, top.getParent()); |
| 29 | |
} |
| 30 | 2432 | return true; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public void updateSpecificity(int[] specificity, boolean[] isConstantHolder) { |
| 34 | 145 | if (parent instanceof AbstractSelector) ((AbstractSelector) parent).updateSpecificity(specificity, isConstantHolder); |
| 35 | 145 | for (AbstractRestriction restriction: restrictions) { |
| 36 | 0 | restriction.updateSpecificity(specificity, isConstantHolder); |
| 37 | |
} |
| 38 | 145 | } |
| 39 | |
|
| 40 | |
protected boolean checkRestrictions(ServletRequest request, Webslinger top) throws IOException, ServletException { |
| 41 | 4268 | for (AbstractRestriction restriction: restrictions) { |
| 42 | 1676 | if (!restriction.matches(request, top)) return false; |
| 43 | |
} |
| 44 | 2596 | return true; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void addRestriction(AbstractRestriction restriction) { |
| 48 | 80 | AbstractRestriction[] tmp = new AbstractRestriction[restrictions.length + 1]; |
| 49 | 80 | System.arraycopy(restrictions, 0, tmp, 0, restrictions.length); |
| 50 | 80 | tmp[restrictions.length] = restriction; |
| 51 | 80 | restrictions = tmp; |
| 52 | 80 | } |
| 53 | |
|
| 54 | |
public void compile(WebslingerContainer container) throws IOException { |
| 55 | 145 | ListIterator it = getChildrenIterator(); |
| 56 | 244 | while (it.hasNext()) { |
| 57 | 99 | Node child = (Node) it.next(); |
| 58 | 99 | if (child instanceof AbstractRestriction) { |
| 59 | 80 | addRestriction((AbstractRestriction) child); |
| 60 | 80 | it.remove(); |
| 61 | 19 | } else if (child instanceof AbstractSelector) { |
| 62 | 19 | ((AbstractSelector) child).compile(container); |
| 63 | |
} else { |
| 64 | 0 | throw new InternalError("Unknown child: " + child); |
| 65 | |
} |
| 66 | 99 | } |
| 67 | 145 | } |
| 68 | |
|
| 69 | |
public String toString() { |
| 70 | 0 | if (restrictions.length == 0) return super.toString(); |
| 71 | 0 | return super.toString() + Arrays.asList(restrictions).toString(); |
| 72 | |
} |
| 73 | |
} |