KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > CssUtilities


1 package org.jahia.clipbuilder.html.util;
2 import java.io.*;
3 import java.io.*;
4 import com.steadystate.css.parser.CSSOMParser;
5 import com.steadystate.css.dom.*;
6 import org.w3c.dom.css.*;
7
8 import org.w3c.css.sac.InputSource;
9 import java.util.StringTokenizer JavaDoc;
10 import org.jahia.clipbuilder.html.util.*;
11 import org.jahia.clipbuilder.html.web.Url.Impl.*;
12 import org.w3c.dom.*;
13
14 /**
15  * Utilities for Css
16  *
17  *@author Tlili Khaled
18  */

19 public abstract class CssUtilities {
20     /**
21      * Div selector
22      */

23     public static String JavaDoc DIV_CLASS = "webClippingClassesDiv";
24     /**
25      * Description of the Field
26      */

27     public static String JavaDoc HTML_CLASS = "webClippingClasses";
28
29     /**
30      * Description of the Field
31      */

32     public static String JavaDoc BODY_CLASS = "webClippingClassesBody";
33     /**
34      * Description of the Field
35      */

36     public static String JavaDoc DIV_CLASS_DECLARATION = "." + DIV_CLASS;
37     /**
38      * Description of the Field
39      */

40     public static String JavaDoc HTML_CLASS_DECLARATION = "." + HTML_CLASS;
41
42     /**
43      * Description of the Field
44      */

45     public static String JavaDoc BODY_CLASS_DECLARATION = "." + BODY_CLASS;
46     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CssUtilities.class);
47
48
49     /**
50      * Gets the RefactoredCssContent attribute of the CssUtilities class
51      *
52      *@param baseUrl Description of Parameter
53      *@param cssContent Description of Parameter
54      *@param media Description of Parameter
55      *@return The RefactoredCssContent value
56      *@exception WebClippingException Description of Exception
57      */

58     public static String JavaDoc getRefactoredCssContent(String JavaDoc baseUrl, String JavaDoc cssContent, String JavaDoc media) throws WebClippingException {
59         try {
60             //Read the Css Content
61
java.io.InputStream JavaDoc in = new java.io.StringBufferInputStream JavaDoc(cssContent);
62             Reader r = new InputStreamReader(in);
63
64             //Parse the css
65
CSSOMParser parser = new CSSOMParser();
66             InputSource is = new InputSource(r);
67
68             CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
69
70             //Get the refactored css
71
String JavaDoc result = getRefactoredCssStyleSheet(baseUrl, stylesheet).toString();
72
73             // set the media
74
if (media != null && !media.equalsIgnoreCase("")) {
75                 return "\n@media " + media + "{\n" + result + "}";
76             }
77             return result;
78         }
79         catch (Exception JavaDoc ex) {
80             logger.error("Css Error ");
81             ex.printStackTrace();
82             //throw new WebClippingException("CSS Parser", ex);
83
return "";
84         }
85     }
86
87
88     /**
89      * Gets the RefactoredStyleRule attribute of the CssUtilities class
90      *
91      *@param baseUrl Description of Parameter
92      *@param cssContent Description of Parameter
93      *@return The RefactoredStyleRule value
94      *@exception WebClippingException Description of Exception
95      */

96     public static String JavaDoc getRefactoredStyleRule(String JavaDoc baseUrl, String JavaDoc cssContent) throws WebClippingException {
97         try {
98             //refactor rule
99
int[] urlIndex = getIndexUrlFromRuleText(cssContent);
100             if (urlIndex[0] > 0 && urlIndex[1] > 0) {
101                 //logger.debug("Rule is: " + ruleText);
102
//logger.debug("Index is: " + urlIndex[0] + "," + urlIndex[1]);
103
String JavaDoc url = cssContent.substring(urlIndex[0], urlIndex[1]);
104                 if (url != null) {
105                     String JavaDoc absoluteUrl = URLUtilities.getUrlAbsoluteValue(baseUrl, url);
106                     cssContent = cssContent.replaceAll(url, absoluteUrl);
107                 }
108             }
109
110             //Get the refactored css
111
String JavaDoc result = cssContent;
112
113             return result;
114         }
115         catch (Exception JavaDoc ex) {
116             logger.error("Css Error ");
117             ex.printStackTrace();
118             //throw new WebClippingException("CSS Parser", ex);
119
return "";
120         }
121     }
122
123
124
125     /**
126      * Gets the RefactoredCssStyleSheet attribute of the CssUtilities class
127      *
128      *@param baseUrl Description of Parameter
129      *@param stylesheet Description of Parameter
130      *@return The RefactoredCssStyleSheet value
131      */

132     private static CSSStyleSheet getRefactoredCssStyleSheet(String JavaDoc baseUrl, CSSStyleSheet stylesheet) {
133         CSSRuleList rules = stylesheet.getCssRules();
134         for (int i = 0; i < rules.getLength(); i++) {
135             try {
136                 CSSRule rule = rules.item(i);
137                 int typeRule = rules.item(i).getType();
138                 //logger.debug("[ Type is :" + typeRule + " ]");
139
if (typeRule == CSSRule.IMPORT_RULE) {
140                     importRule(baseUrl, (CSSImportRuleImpl) rule, i);
141                 }
142                 else if (typeRule == CSSRule.STYLE_RULE) {
143                     CSSStyleRuleImpl styleRule = (CSSStyleRuleImpl) rule;
144                     //relative url --> absolute url
145
relativeToAbsoluteUrlRessources(baseUrl, styleRule);
146
147                     // add class selector
148
//addClassSelector(styleRule);
149
}
150             }
151             catch (Exception JavaDoc ex) {
152                 logger.warn("[ Error has ocuured ]");
153                 ex.printStackTrace();
154             }
155         }
156         return stylesheet;
157     }
158
159
160     /**
161      * Gets the IndexUrlFromRuleText attribute of the CssUtilities class
162      *
163      *@param rule Description of Parameter
164      *@return The IndexUrlFromRuleText value
165      */

166     private static int[] getIndexUrlFromRuleText(String JavaDoc rule) {
167         int[] res = new int[2];
168         int indexUrl = rule.indexOf("url");
169         if (indexUrl > 0) {
170             // 4 --> u r l (
171
indexUrl = indexUrl + 4;
172         }
173         else {
174             res[0] = -1;
175             res[1] = -1;
176             return res;
177         }
178         String JavaDoc url = rule.substring(indexUrl);
179         int indexEndUrl = indexUrl + url.indexOf(")");
180
181         res[0] = indexUrl;
182         res[1] = indexEndUrl;
183         return res;
184     }
185
186
187     /**
188      * Description of the Method
189      *
190      *@param baseUrl Description of Parameter
191      *@param importRule Description of Parameter
192      *@param position Description of Parameter
193      *@exception WebClippingException Description of Exception
194      *@exception Exception Description of Exception
195      */

196     private static void importRule(String JavaDoc baseUrl, CSSImportRuleImpl importRule, int position) throws Exception JavaDoc, WebClippingException {
197         // get the content from the url
198
String JavaDoc href = importRule.getHref();
199         String JavaDoc url = URLUtilities.getUrlAbsoluteValue(baseUrl, href);
200         String JavaDoc cssContent = org.jahia.clipbuilder.html.web.http.SimpleHttpSecureClient.getContentAsString(url);
201         logger.debug("[ Its an imported rule:" + url + " ]");
202
203         //Read the Css Content
204
java.io.InputStream JavaDoc in = new java.io.StringBufferInputStream JavaDoc(cssContent);
205         Reader r = new InputStreamReader(in);
206
207         //Parse the css
208
CSSOMParser parser = new CSSOMParser();
209         InputSource is = new InputSource(r);
210         CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
211         stylesheet = getRefactoredCssStyleSheet(baseUrl, stylesheet);
212
213         // add the rule of the imported css to the paretn url
214
CSSStyleSheet parentStyleSheet = importRule.getParentStyleSheet();
215         parentStyleSheet.deleteRule(position);
216         CSSRuleList rules = stylesheet.getCssRules();
217         for (int i = 0; i < rules.getLength(); i++) {
218             CSSRule ruleTo = rules.item(i);
219             parentStyleSheet.insertRule(ruleTo.toString(), position + i);
220         }
221     }
222
223
224     /**
225      * Description of the Method
226      *
227      *@param baseUrl Description of Parameter
228      *@param rule Description of Parameter
229      *@exception Exception Description of Exception
230      */

231     private static void relativeToAbsoluteUrlRessources(String JavaDoc baseUrl, CSSRule rule) throws Exception JavaDoc {
232         String JavaDoc ruleText = rule.getCssText();
233         int[] urlIndex = getIndexUrlFromRuleText(ruleText);
234         if (urlIndex[0] > 0 && urlIndex[1] > 0) {
235             //logger.debug("Rule is: " + ruleText);
236
//logger.debug("Index is: " + urlIndex[0] + "," + urlIndex[1]);
237
String JavaDoc url = ruleText.substring(urlIndex[0], urlIndex[1]);
238             if (url != null) {
239                 String JavaDoc absoluteUrl = URLUtilities.getUrlAbsoluteValue(baseUrl, url);
240                 ruleText = ruleText.replaceAll(url, absoluteUrl);
241                 rule.setCssText(ruleText);
242             }
243         }
244     }
245
246
247     /**
248      * Adds a feature to the ClassSelector attribute of the CssUtilities class
249      *
250      *@param rule The feature to be added to the ClassSelector
251      * attribute
252      *@exception DOMException Description of Exception
253      */

254     private static void addClassSelector(CSSStyleRule rule) throws DOMException {
255         String JavaDoc selector = rule.getSelectorText();
256         StringTokenizer JavaDoc s = new StringTokenizer JavaDoc(selector, ",");
257         String JavaDoc newSelector = null;
258
259         // add dic.webclipping class
260
while (s.hasMoreTokens()) {
261             String JavaDoc selectorToken = s.nextToken();
262             if (selectorToken.indexOf('.') > 0) {
263                 // do nothing
264
}
265             else if (selectorToken.indexOf('#') > 0) {
266
267             }
268             else {
269                 selectorToken = HTML_CLASS_DECLARATION + " " + selectorToken;
270             }
271             // change selector
272
selectorToken = selectorToken.replaceAll("body", BODY_CLASS_DECLARATION);
273
274             // Append
275
if (newSelector == null) {
276                 newSelector = selectorToken;
277             }
278             else {
279                 newSelector = newSelector + "," + selectorToken;
280             }
281
282             rule.setSelectorText(newSelector);
283         }
284     }
285
286 }
287
Popular Tags