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 ; 10 import org.jahia.clipbuilder.html.util.*; 11 import org.jahia.clipbuilder.html.web.Url.Impl.*; 12 import org.w3c.dom.*; 13 14 19 public abstract class CssUtilities { 20 23 public static String DIV_CLASS = "webClippingClassesDiv"; 24 27 public static String HTML_CLASS = "webClippingClasses"; 28 29 32 public static String BODY_CLASS = "webClippingClassesBody"; 33 36 public static String DIV_CLASS_DECLARATION = "." + DIV_CLASS; 37 40 public static String HTML_CLASS_DECLARATION = "." + HTML_CLASS; 41 42 45 public static String BODY_CLASS_DECLARATION = "." + BODY_CLASS; 46 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CssUtilities.class); 47 48 49 58 public static String getRefactoredCssContent(String baseUrl, String cssContent, String media) throws WebClippingException { 59 try { 60 java.io.InputStream in = new java.io.StringBufferInputStream (cssContent); 62 Reader r = new InputStreamReader(in); 63 64 CSSOMParser parser = new CSSOMParser(); 66 InputSource is = new InputSource(r); 67 68 CSSStyleSheet stylesheet = parser.parseStyleSheet(is); 69 70 String result = getRefactoredCssStyleSheet(baseUrl, stylesheet).toString(); 72 73 if (media != null && !media.equalsIgnoreCase("")) { 75 return "\n@media " + media + "{\n" + result + "}"; 76 } 77 return result; 78 } 79 catch (Exception ex) { 80 logger.error("Css Error "); 81 ex.printStackTrace(); 82 return ""; 84 } 85 } 86 87 88 96 public static String getRefactoredStyleRule(String baseUrl, String cssContent) throws WebClippingException { 97 try { 98 int[] urlIndex = getIndexUrlFromRuleText(cssContent); 100 if (urlIndex[0] > 0 && urlIndex[1] > 0) { 101 String url = cssContent.substring(urlIndex[0], urlIndex[1]); 104 if (url != null) { 105 String absoluteUrl = URLUtilities.getUrlAbsoluteValue(baseUrl, url); 106 cssContent = cssContent.replaceAll(url, absoluteUrl); 107 } 108 } 109 110 String result = cssContent; 112 113 return result; 114 } 115 catch (Exception ex) { 116 logger.error("Css Error "); 117 ex.printStackTrace(); 118 return ""; 120 } 121 } 122 123 124 125 132 private static CSSStyleSheet getRefactoredCssStyleSheet(String 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 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 relativeToAbsoluteUrlRessources(baseUrl, styleRule); 146 147 } 150 } 151 catch (Exception ex) { 152 logger.warn("[ Error has ocuured ]"); 153 ex.printStackTrace(); 154 } 155 } 156 return stylesheet; 157 } 158 159 160 166 private static int[] getIndexUrlFromRuleText(String rule) { 167 int[] res = new int[2]; 168 int indexUrl = rule.indexOf("url"); 169 if (indexUrl > 0) { 170 indexUrl = indexUrl + 4; 172 } 173 else { 174 res[0] = -1; 175 res[1] = -1; 176 return res; 177 } 178 String 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 196 private static void importRule(String baseUrl, CSSImportRuleImpl importRule, int position) throws Exception , WebClippingException { 197 String href = importRule.getHref(); 199 String url = URLUtilities.getUrlAbsoluteValue(baseUrl, href); 200 String cssContent = org.jahia.clipbuilder.html.web.http.SimpleHttpSecureClient.getContentAsString(url); 201 logger.debug("[ Its an imported rule:" + url + " ]"); 202 203 java.io.InputStream in = new java.io.StringBufferInputStream (cssContent); 205 Reader r = new InputStreamReader(in); 206 207 CSSOMParser parser = new CSSOMParser(); 209 InputSource is = new InputSource(r); 210 CSSStyleSheet stylesheet = parser.parseStyleSheet(is); 211 stylesheet = getRefactoredCssStyleSheet(baseUrl, stylesheet); 212 213 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 231 private static void relativeToAbsoluteUrlRessources(String baseUrl, CSSRule rule) throws Exception { 232 String ruleText = rule.getCssText(); 233 int[] urlIndex = getIndexUrlFromRuleText(ruleText); 234 if (urlIndex[0] > 0 && urlIndex[1] > 0) { 235 String url = ruleText.substring(urlIndex[0], urlIndex[1]); 238 if (url != null) { 239 String absoluteUrl = URLUtilities.getUrlAbsoluteValue(baseUrl, url); 240 ruleText = ruleText.replaceAll(url, absoluteUrl); 241 rule.setCssText(ruleText); 242 } 243 } 244 } 245 246 247 254 private static void addClassSelector(CSSStyleRule rule) throws DOMException { 255 String selector = rule.getSelectorText(); 256 StringTokenizer s = new StringTokenizer (selector, ","); 257 String newSelector = null; 258 259 while (s.hasMoreTokens()) { 261 String selectorToken = s.nextToken(); 262 if (selectorToken.indexOf('.') > 0) { 263 } 265 else if (selectorToken.indexOf('#') > 0) { 266 267 } 268 else { 269 selectorToken = HTML_CLASS_DECLARATION + " " + selectorToken; 270 } 271 selectorToken = selectorToken.replaceAll("body", BODY_CLASS_DECLARATION); 273 274 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 |