01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.css;
06:
07: import java.io.*;
08: import java.util.*;
09:
10: import jaxx.*;
11: import jaxx.compiler.*;
12:
13: public class Rule implements Serializable, Comparable {
14: public static final String INLINE_ATTRIBUTE = "<inline attribute>";
15: public static final String DATA_BINDING = "<data binding>";
16:
17: private Selector[] selectors;
18: private Map/*<String, String>*/properties;
19:
20: public Rule(Selector[] selectors,
21: Map/*<String, String>*/properties) {
22: this .selectors = selectors;
23: Arrays.sort(selectors);
24: this .properties = properties;
25: }
26:
27: public Rule(Selector[] selectors, String[] keys, String[] values) {
28: this .selectors = selectors;
29: Arrays.sort(selectors);
30: this .properties = new HashMap/*<String, String>*/();
31: if (keys.length != values.length)
32: throw new IllegalArgumentException(
33: "keys and values must have the same number of entries");
34: for (int i = 0; i < keys.length; i++)
35: properties.put(keys[i], values[i]);
36: }
37:
38: public static Rule inlineAttribute(CompiledObject object,
39: String propertyName, boolean dataBinding) {
40: Map/*<String, String>*/properties = new HashMap/*<String, String>*/();
41: properties.put(propertyName, dataBinding ? DATA_BINDING
42: : INLINE_ATTRIBUTE);
43: return new Rule(new Selector[] { new Selector(null, null, null,
44: object.getId(), true) }, properties);
45: }
46:
47: public Selector[] getSelectors() {
48: return selectors;
49: }
50:
51: public Map/*<String, String>*/getProperties() {
52: return properties;
53: }
54:
55: public int appliesTo(CompiledObject object)
56: throws CompilerException {
57: int appliesTo = Selector.NEVER_APPLIES;
58: for (int i = 0; i < selectors.length; i++) {
59: appliesTo = Math.max(selectors[i].appliesTo(object),
60: appliesTo);
61: if (appliesTo == Selector.ALWAYS_APPLIES
62: || appliesTo == Selector.ALWAYS_APPLIES_INHERIT_ONLY)
63: break;
64: }
65: return appliesTo;
66: }
67:
68: public int compareTo(Object o) {
69: Rule r = (Rule) o;
70: return selectors[0].compareTo(r.selectors[0]); // they are already sorted so we only need to compare the highest-ranked from each one
71: }
72:
73: public String toString() {
74: return "Rule[" + Arrays.asList(selectors) + ", " + properties
75: + "]";
76: }
77: }
|