KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > style > CSSUtilities


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21
22 package org.lobobrowser.html.style;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.*;
30 import java.util.logging.*;
31 import java.security.*;
32
33 import org.w3c.css.sac.InputSource;
34 import org.w3c.dom.css.CSSStyleSheet;
35 import org.w3c.dom.stylesheets.*;
36 import org.lobobrowser.html.*;
37 import org.lobobrowser.html.domimpl.HTMLDocumentImpl;
38 import org.lobobrowser.util.*;
39
40 import com.steadystate.css.dom.CSSStyleSheetImpl;
41 import com.steadystate.css.parser.CSSOMParser;
42
43 public class CSSUtilities {
44     private static final Logger logger = Logger.getLogger(CSSUtilities.class.getName());
45
46     private CSSUtilities() {
47     }
48
49     public static String JavaDoc preProcessCss(String JavaDoc text) {
50         //TODO: Fix parser to optimize away this
51
try {
52             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new StringReader JavaDoc(text));
53             String JavaDoc line;
54             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55             while((line = reader.readLine()) != null) {
56                 if(!line.trim().startsWith("//")) {
57                     sb.append(line);
58                     sb.append("\r\n");
59                 }
60             }
61             return sb.toString();
62         } catch(IOException JavaDoc ioe) {
63             // not possible
64
throw new IllegalStateException JavaDoc(ioe.getMessage());
65         }
66     }
67     
68     public static InputSource getCssInputSourceForStyleSheet(String JavaDoc text) {
69         java.io.Reader JavaDoc reader = new StringReader JavaDoc(text);
70         InputSource is = new InputSource(reader);
71         return is;
72     }
73         
74     public static CSSStyleSheet parse(String JavaDoc href, HTMLDocumentImpl doc, String JavaDoc baseUri, boolean considerDoubleSlashComments) throws MalformedURLException JavaDoc {
75         UserAgentContext bcontext = doc.getUserAgentContext();
76         final HttpRequest request = bcontext.createHttpRequest();
77         URL JavaDoc baseURL = new URL JavaDoc(baseUri);
78         URL JavaDoc scriptURL = Urls.createURL(baseURL, href);
79         final String JavaDoc scriptURI = scriptURL == null ? href : scriptURL.toExternalForm();
80         // Perform a synchronous request
81
SecurityManager JavaDoc sm = System.getSecurityManager();
82         if(sm == null) {
83             request.open("GET", scriptURI, false);
84         }
85         else {
86             // Code might have restrictions on loading items from elsewhere.
87
AccessController.doPrivileged(new PrivilegedAction() {
88                 public Object JavaDoc run() {
89                     request.open("GET", scriptURI, false);
90                     return null;
91                 }
92             });
93         }
94         int status = request.getStatus();
95         if(status != 200 && status != 0) {
96             logger.warning("Unable to parse CSS. URI=[" + scriptURI + "]. Response status was " + status + ".");
97             return null;
98         }
99         
100         String JavaDoc text = request.getResponseText();
101         if(text != null && !"".equals(text)) {
102             String JavaDoc processedText = considerDoubleSlashComments ? preProcessCss(text) : text;
103             CSSOMParser parser = new CSSOMParser();
104             InputSource is = getCssInputSourceForStyleSheet(processedText);
105             is.setURI(scriptURI);
106             try {
107                 CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(is);
108                 sheet.setHref(scriptURI);
109                 return sheet;
110             } catch(Throwable JavaDoc err) {
111                 logger.log(Level.WARNING,"Unable to parse CSS. URI=[" + scriptURI + "].", err);
112                 return null;
113             }
114         }
115         else {
116             return null;
117         }
118     }
119     
120     public static boolean matchesMedia(String JavaDoc mediaValues, HtmlRendererContext rcontext) {
121         if(mediaValues == null || mediaValues.length() == 0) {
122             return true;
123         }
124         if(rcontext == null) {
125             return false;
126         }
127         StringTokenizer tok = new StringTokenizer(mediaValues, ",");
128         while(tok.hasMoreTokens()) {
129             String JavaDoc token = tok.nextToken().trim();
130             String JavaDoc mediaName = Strings.trimForAlphaNumDash(token);
131             if(rcontext.isMedia(mediaName)) {
132                 return true;
133             }
134         }
135         return false;
136     }
137
138     public static boolean matchesMedia(MediaList mediaList, HtmlRendererContext rcontext) {
139         if(mediaList == null) {
140             return true;
141         }
142         int length = mediaList.getLength();
143         if(length == 0) {
144             return true;
145         }
146         if(rcontext == null) {
147             return false;
148         }
149         for(int i = 0; i < length; i++) {
150             String JavaDoc mediaName = mediaList.item(i);
151             if(rcontext.isMedia(mediaName)) {
152                 return true;
153             }
154         }
155         return false;
156     }
157
158 }
159
Popular Tags