1 21 22 package org.lobobrowser.html.style; 23 24 import java.io.BufferedReader ; 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 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 preProcessCss(String text) { 50 try { 52 BufferedReader reader = new BufferedReader (new StringReader (text)); 53 String line; 54 StringBuffer sb = new StringBuffer (); 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 ioe) { 63 throw new IllegalStateException (ioe.getMessage()); 65 } 66 } 67 68 public static InputSource getCssInputSourceForStyleSheet(String text) { 69 java.io.Reader reader = new StringReader (text); 70 InputSource is = new InputSource(reader); 71 return is; 72 } 73 74 public static CSSStyleSheet parse(String href, HTMLDocumentImpl doc, String baseUri, boolean considerDoubleSlashComments) throws MalformedURLException { 75 UserAgentContext bcontext = doc.getUserAgentContext(); 76 final HttpRequest request = bcontext.createHttpRequest(); 77 URL baseURL = new URL (baseUri); 78 URL scriptURL = Urls.createURL(baseURL, href); 79 final String scriptURI = scriptURL == null ? href : scriptURL.toExternalForm(); 80 SecurityManager sm = System.getSecurityManager(); 82 if(sm == null) { 83 request.open("GET", scriptURI, false); 84 } 85 else { 86 AccessController.doPrivileged(new PrivilegedAction() { 88 public Object 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 text = request.getResponseText(); 101 if(text != null && !"".equals(text)) { 102 String 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 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 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 token = tok.nextToken().trim(); 130 String 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 mediaName = mediaList.item(i); 151 if(rcontext.isMedia(mediaName)) { 152 return true; 153 } 154 } 155 return false; 156 } 157 158 } 159 | Popular Tags |