KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > css > RuleFinder


1 package org.joshy.html.css;
2
3 import org.joshy.u;
4 import java.io.*;
5 import java.util.List JavaDoc;
6 import org.w3c.dom.*;
7 import org.w3c.dom.css.*;
8 import org.w3c.css.sac.*;
9 import com.steadystate.css.*;
10 import com.steadystate.css.parser.*;
11
12 /* ============ CSS searching and cascading code ================ */
13 public class RuleFinder {
14     List JavaDoc styles;
15     public RuleFinder(List JavaDoc styles) {
16         this.styles = styles;
17     }
18     
19     
20     
21     public CSSStyleDeclaration findRule(Node elem, String JavaDoc property, boolean inherit) {
22         // loop through the styles in reverse order
23
for(int s=styles.size()-1; s>=0; s--) {
24             JStyle style = (JStyle)styles.get(s);
25             // loop through each selector for the style
26
for(int i=0; i<style.selector_list.getLength(); i++) {
27                 Selector sel = style.selector_list.item(i);
28                 // if the selector matches
29
if(matchSelector(sel,elem)) {
30                     CSSStyleDeclaration dec = style.declaration;
31                     // if the style has the property we want
32
if(dec != null && dec.getPropertyValue(property) != null &&
33                             !dec.getPropertyValue(property).equals("")) {
34                         return dec;
35                     }
36                 }
37             }
38         }
39         
40         // since we didn't find anything, recurse up the chain
41
if(inherit) {
42             if(elem.getParentNode() != null) {
43                 return findRule(elem.getParentNode(),property,inherit);
44             }
45         }
46         
47         //u.p("no style found at all " + elem.getNodeName() + " prop = " + property);
48
return null;
49     }
50     
51     
52     
53     public boolean match(String JavaDoc selector, Node node) {
54         try {
55             CSSOMParser parser = new CSSOMParser();
56             SelectorList list = parser.parseSelectors(new InputSource(new StringReader(selector)));
57             for(int i=0; i<list.getLength(); i++) {
58                 Selector sel = list.item(i);
59                 if(matchSelector(sel,node)) {
60                     return true;
61                 }
62             }
63         } catch (Exception JavaDoc ex) {
64             u.p(ex);
65         }
66         return false;
67     }
68     
69     
70     
71     private boolean matchSelector(Selector selector, Node node) {
72         if(selector.getSelectorType() == selector.SAC_ELEMENT_NODE_SELECTOR) {
73             return matchElement((ElementSelector)selector,node);
74         }
75         if(selector.getSelectorType() == selector.SAC_CONDITIONAL_SELECTOR) {
76             ConditionalSelector cond_sel = (ConditionalSelector)selector;
77             return matchConditional(cond_sel,node);
78         }
79         if(selector.getSelectorType() == selector.SAC_DESCENDANT_SELECTOR) {
80             DescendantSelector desc_sel = (DescendantSelector)selector;
81             return matchDescendant(desc_sel,node);
82         }
83         u.p("unrecognized selector type: " + selector + " node = " + node.getNodeName());
84         return false;
85     }
86     
87     
88     
89     private boolean matchElement(ElementSelector selector, Node node) {
90         // null = any element (wildcard = *);
91
if(selector.getLocalName() == null) {
92             return true;
93         }
94         if(selector.getLocalName().equals(node.getNodeName())) {
95             return true;
96         }
97         return false;
98     }
99
100
101     private boolean matchConditional(ConditionalSelector selector, Node node) {
102         Condition cond = selector.getCondition();
103         if(cond.getConditionType() == cond.SAC_CLASS_CONDITION) {
104             return matchClassConditional((AttributeCondition)cond,selector.getSimpleSelector(),node);
105         }
106         if(cond.getConditionType() == cond.SAC_ID_CONDITION) {
107             return matchIDConditional((AttributeCondition)cond,selector.getSimpleSelector(),node);
108         }
109         return false;
110     }
111     
112
113     private boolean matchDescendant(DescendantSelector selector, Node node) {
114         SimpleSelector child = selector.getSimpleSelector();
115         Selector parent = selector.getAncestorSelector();
116         if(matchSelector(child,node)) {
117         //if(matchElement((ElementSelector)child, node)) {
118
Node current_node = node;
119             while(true) {
120                 Node parent_node = current_node.getParentNode();
121                 if(parent_node == null) {
122                     return false;
123                 }
124                 if(matchSelector(parent,parent_node)) {
125                     return true;
126                 }
127                 current_node = parent_node;
128             }
129         }
130         return false;
131     }
132     
133     
134     private boolean matchClassConditional(AttributeCondition cond, SimpleSelector sel, Node node) {
135         // if it's an element
136
if(!(node.getNodeType() == node.ELEMENT_NODE)) {
137             return false;
138         }
139         Element elem = (Element)node;
140         // if it has a class attribute
141
if(!elem.hasAttribute("class")) {
142             return false;
143         }
144         // if the class attribute matches the condition's class
145
if(!elem.getAttribute("class").equals(cond.getValue())) {
146             return false;
147         }
148         
149         // if local name = null then it's the 'any' selector (*)
150
// so it matches
151
ElementSelector es = (ElementSelector)sel;
152         if(es.getLocalName() == null) {
153             return true;
154         }
155         if(!((ElementSelector)sel).getLocalName().equals(node.getNodeName())) {
156             return false;
157         }
158         return true;
159     }
160
161     private boolean matchIDConditional(AttributeCondition cond, SimpleSelector sel, Node node) {
162         // if it's an element
163
if(!(node.getNodeType() == node.ELEMENT_NODE)) { return false; }
164         Element elem = (Element)node;
165         // if it has a id attribute
166
if(!elem.hasAttribute("id")) { return false; }
167         // if the id attribute matches the condition's id
168
if(!elem.getAttribute("id").equals(cond.getValue())) { return false; }
169         
170         // if the node names match up
171
ElementSelector es = (ElementSelector)sel;
172         // if local name = null then it's the 'any' selector (*)
173
// so it matches
174
if(es.getLocalName() == null) {
175             return true;
176         }
177         if(!((ElementSelector)sel).getLocalName().equals(node.getNodeName())) { return false; }
178         return true;
179     }
180 }
181
Popular Tags