KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > CSSBank


1 package org.joshy.html;
2
3 import java.util.List JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.io.*;
7 import java.awt.Color JavaDoc;
8 import org.joshy.u;
9 import org.joshy.x;
10 import org.joshy.html.css.*;
11
12 import com.steadystate.css.*;
13 import com.steadystate.css.parser.*;
14 import org.w3c.dom.*;
15 import org.w3c.dom.css.*;
16 import org.w3c.css.sac.*;
17
18 public class CSSBank extends CSSAccessor {
19     /* internal vars */
20     
21     public List JavaDoc sheets;
22     public List JavaDoc style_nodes;
23     public List JavaDoc styles;
24     CSSParser parser;
25     
26
27     public CSSBank() {
28         sheets = new ArrayList JavaDoc();
29         style_nodes = new ArrayList JavaDoc();
30         styles = new ArrayList JavaDoc();
31         parser = new CSSParser(this);
32     }
33     
34     
35     public void parse(Reader reader) throws IOException {
36         parser.parse(reader);
37     }
38     public void parse(String JavaDoc reader) throws IOException {
39         parser.parse(reader);
40     }
41     
42     public void parseInlineStyles(Element elem) throws IOException {
43         parser.parseInlineStyles(elem);
44     }
45     private void pullOutStyles(CSSStyleSheet sheet) throws IOException {
46         parser.pullOutStyles(sheet);
47     }
48
49     
50     /* ========= property accessors ============ */
51     private Object JavaDoc getProperty(Node node, String JavaDoc prop) {
52         if(node.getNodeType() == node.TEXT_NODE) {
53             return getProperty(node.getParentNode(),prop);
54         }
55         if(node.getNodeType() == node.ELEMENT_NODE) {
56             return getProperty((Element)node,prop);
57         }
58         u.p("unknown node type: " + node);
59         u.p("type = " + node.getNodeType());
60         return null;
61     }
62     
63     public CSSValue getProperty(Element elem, String JavaDoc prop, boolean inherit) {
64         //u.p("looking at: " + elem.getNodeName() + " prop = " + prop + " inherit " + inherit);
65
RuleFinder rf = new RuleFinder(this.styles);
66         CSSStyleDeclaration style_dec = rf.findRule(elem,prop,inherit);
67         //u.p("got style: " + style_dec);
68
if(style_dec == null) {
69             //u.p("print there is no style declaration at all for: " + elem.getNodeName());
70
//u.p("looking for property: " + prop);
71
return null;
72         }
73         CSSValue val = style_dec.getPropertyCSSValue(prop);
74         if(val == null) {
75             //u.p("elem " + elem.getNodeName() + " doesn't have the property: " + prop);
76
if(elem.getParentNode() != null && inherit) {
77                 //u.p("going up: " + elem.getNodeName() + " -> " + elem.getParentNode().getNodeName() + " prop = " + prop);
78
if(elem.getParentNode() instanceof Element) {
79                     return getProperty((Element)elem.getParentNode(),prop,inherit);
80                 }
81             }
82             return null;
83         }
84         //u.p("returning: " + val);
85
return val;
86     }
87     
88     
89     
90
91 }
92
Popular Tags