Coverage Report - org.webslinger.rules.CompiledRules
 
Classes in this File Line Coverage Branch Coverage Complexity
CompiledRules
65%
28/43
69%
11/16
0
CompiledRules$Entry
100%
6/6
N/A
0
 
 1  
 package org.webslinger.rules;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Collections;
 6  
 import java.util.List;
 7  
 import java.util.HashMap;
 8  
 import java.util.Map;
 9  
 
 10  
 import javax.servlet.ServletException;
 11  
 import javax.servlet.ServletRequest;
 12  
 
 13  
 import org.webslinger.Webslinger;
 14  
 
 15  
 public final class CompiledRules {
 16  
     public final CSSRules rules;
 17  
     public final Map<Action, Entry[]> rows;
 18  
 
 19  22
     public CompiledRules(CSSRules rules, Map<Action, List<Entry>> compiled) {
 20  22
         this.rules = rules;
 21  22
         Map<Action, Entry[]> rows = new HashMap<Action, Entry[]>(compiled.size());
 22  22
         for (Map.Entry<Action, List<Entry>> entry: compiled.entrySet()) {
 23  88
             List<Entry> entries = entry.getValue();
 24  88
             Collections.sort(entries);
 25  88
             Collections.reverse(entries);
 26  88
             rows.put(entry.getKey(), entries.toArray(new Entry[entries.size()]));
 27  88
         }
 28  22
         this.rows = Collections.unmodifiableMap(rows);
 29  22
     }
 30  
 
 31  
     public CSSAction[] findActions(Action action, ServletRequest request, Webslinger top, boolean all) throws IOException, ServletException {
 32  
         // FIXME: use specificity
 33  3495
         Entry[] entries = this.rows.get(action);
 34  3498
         if (entries == null) return null;
 35  2946
         ArrayList<CSSAction> found = all ? new ArrayList<CSSAction>() : null;
 36  6059
         for (Entry entry: entries) {
 37  3810
             if (entry.selector.matches(request, top)) {
 38  2283
                 if (all) {
 39  3223
                     for (CSSAction a: entry.actions) {
 40  1630
                         found.add(a);
 41  
                     }
 42  
                 } else {
 43  686
                     return entry.actions;
 44  
                 }
 45  
             }
 46  
         }
 47  2252
         return all ? found.toArray(new CSSAction[found.size()]) : null;
 48  
     }
 49  
 
 50  
     public static void showActions(CSSAction[] actions, ServletRequest request, Webslinger top) throws IOException, ServletException {
 51  0
         int actionCount = actions.length;
 52  0
         for (int i = 0; i < actionCount; i++) {
 53  0
             actions[i].dump(System.err, "A> ");
 54  0
             AbstractValue value = actions[i].getValue();
 55  0
             if (value instanceof AbstractListValue) {
 56  0
                 Object[] actionValues = ((AbstractListValue) value).getValue(request, top);
 57  0
                 if (true && actionValues != null) {
 58  0
                     for (int j = 0; j < actionValues.length; j++) {
 59  0
                         if (j != 0) System.err.print(", ");
 60  0
                         System.err.print("[" + j + "]=(" + actionValues[j] + ")");
 61  
                     }
 62  0
                     System.err.println();
 63  
                 }
 64  0
             } else {
 65  0
                 Object actionValue = ((AbstractSingleValue) value).getValue(request, top);
 66  0
                 if (true) System.err.println("v=" + actionValue);
 67  
             }
 68  
         }
 69  0
     }
 70  
 
 71  
     public static Object getFirstValue(CSSAction[] actions, ServletRequest request, Webslinger top) throws IOException, ServletException {
 72  302
         int actionCount = actions.length;
 73  486
         for (int i = 0; i < actionCount; i++) {
 74  281
             AbstractValue value = actions[i].getValue();
 75  281
             if (value == null) return null;
 76  281
             Object actionValue = value.getFirstValue(request, top);
 77  281
             if (actionValue != null) return actionValue;
 78  
         }
 79  205
         return null;
 80  
     }
 81  
 
 82  95
     public static final class Entry implements Comparable<Entry> {
 83  
         public final CSSSelector selector;
 84  
         public final CSSAction[] actions;
 85  
 
 86  140
         public Entry(CSSSelector selector, List<CSSAction> actions) {
 87  140
             this.selector = selector;
 88  140
             this.actions = actions.toArray(new CSSAction[actions.size()]);
 89  140
         }
 90  
 
 91  
         public int compareTo(Entry other) {
 92  95
             return selector.compareTo(other.selector);
 93  
         }
 94  
     }
 95  
 }