1 30 31 package com.steadystate.css.parser; 32 33 import java.io.*; 34 import java.util.*; 35 import org.w3c.dom.css.*; 36 import org.w3c.css.sac.*; 37 import org.w3c.css.sac.helpers.ParserFactory; 38 import com.steadystate.css.dom.*; 39 40 45 public class CSSOMParser { 46 47 private static final String PARSER = "com.steadystate.css.parser.SACParser"; 48 49 private Parser _parser = null; 50 private CSSStyleSheetImpl _parentStyleSheet = null; 51 54 55 public CSSOMParser() { 56 try { 57 if(use_internal) { 59 _parser = new SACParser(); 60 } else { 61 setProperty("org.w3c.css.sac.parser", PARSER); 62 ParserFactory factory = new ParserFactory(); 63 _parser = factory.makeParser(); 64 } 65 } catch (Exception e) { 66 use_internal = true; 67 System.err.println(e.getMessage()); 68 e.printStackTrace(); 69 System.err.println("using the default parser instead"); 70 _parser = new SACParser(); 71 } 72 } 73 74 private static boolean use_internal = false; 75 76 81 public CSSOMParser(Parser parser) 82 { 83 this._parser = parser; 84 } 85 86 public CSSStyleSheet parseStyleSheet(InputSource source) throws IOException { 87 CSSOMHandler handler = new CSSOMHandler(); 88 _parser.setDocumentHandler(handler); 89 _parser.parseStyleSheet(source); 90 return (CSSStyleSheet) handler.getRoot(); 91 } 92 93 public CSSStyleDeclaration parseStyleDeclaration(InputSource source) 94 throws IOException { 95 CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null); 96 parseStyleDeclaration(sd, source); 97 return sd; 98 } 99 100 public void parseStyleDeclaration(CSSStyleDeclaration sd, InputSource source) 101 throws IOException { 102 Stack nodeStack = new Stack(); 103 nodeStack.push(sd); 104 CSSOMHandler handler = new CSSOMHandler(nodeStack); 105 _parser.setDocumentHandler(handler); 106 _parser.parseStyleDeclaration(source); 107 } 108 109 public CSSValue parsePropertyValue(InputSource source) throws IOException { 110 CSSOMHandler handler = new CSSOMHandler(); 111 _parser.setDocumentHandler(handler); 112 return new CSSValueImpl(_parser.parsePropertyValue(source)); 113 } 114 115 public CSSRule parseRule(InputSource source) throws IOException { 116 CSSOMHandler handler = new CSSOMHandler(); 117 _parser.setDocumentHandler(handler); 118 _parser.parseRule(source); 119 return (CSSRule) handler.getRoot(); 120 } 121 122 public SelectorList parseSelectors(InputSource source) throws IOException { 123 HandlerBase handler = new HandlerBase(); 124 _parser.setDocumentHandler(handler); 125 return _parser.parseSelectors(source); 126 } 127 128 public void setParentStyleSheet(CSSStyleSheetImpl parentStyleSheet) { 129 _parentStyleSheet = parentStyleSheet; 130 } 131 132 138 139 class CSSOMHandler implements DocumentHandler { 140 141 private Stack _nodeStack; 142 private Object _root = null; 143 144 public CSSOMHandler(Stack nodeStack) { 145 _nodeStack = nodeStack; 146 } 147 148 public CSSOMHandler() { 149 _nodeStack = new Stack(); 150 } 151 152 public Object getRoot() { 153 return _root; 154 } 155 156 public void startDocument(InputSource source) throws CSSException { 157 if (_nodeStack.empty()) { 158 CSSStyleSheetImpl ss = new CSSStyleSheetImpl(); 159 _parentStyleSheet = ss; 160 ss.setHref(source.getURI()); 161 ss.setMedia(source.getMedia()); 162 ss.setTitle(source.getTitle()); 163 CSSRuleListImpl rules = new CSSRuleListImpl(); 165 ss.setRuleList(rules); 166 _nodeStack.push(ss); 167 _nodeStack.push(rules); 168 } else { 169 } 171 } 172 173 public void endDocument(InputSource source) throws CSSException { 174 175 _nodeStack.pop(); 177 _root = _nodeStack.pop(); 178 } 179 180 public void comment(String text) throws CSSException { 181 } 182 183 public void ignorableAtRule(String atRule) throws CSSException { 184 185 CSSUnknownRuleImpl ir = new CSSUnknownRuleImpl( 187 _parentStyleSheet, 188 null, 189 atRule); 190 if (!_nodeStack.empty()) { 191 ((CSSRuleListImpl)_nodeStack.peek()).add(ir); 192 } else { 193 _root = ir; 195 } 196 } 197 198 public void namespaceDeclaration(String prefix, String uri) 199 throws CSSException { 200 } 201 202 public void importStyle( 203 String uri, 204 SACMediaList media, 205 String defaultNamespaceURI) throws CSSException { 206 207 CSSImportRuleImpl ir = new CSSImportRuleImpl( 209 _parentStyleSheet, 210 null, 211 uri, 212 new MediaListImpl(media)); 213 if (!_nodeStack.empty()) { 214 ((CSSRuleListImpl)_nodeStack.peek()).add(ir); 215 } else { 216 _root = ir; 218 } 219 } 220 221 public void startMedia(SACMediaList media) throws CSSException { 222 223 CSSMediaRuleImpl mr = new CSSMediaRuleImpl( 225 _parentStyleSheet, 226 null, 227 new MediaListImpl(media)); 228 if (!_nodeStack.empty()) { 229 ((CSSRuleListImpl)_nodeStack.peek()).add(mr); 230 } 231 232 CSSRuleListImpl rules = new CSSRuleListImpl(); 234 mr.setRuleList(rules); 235 _nodeStack.push(mr); 236 _nodeStack.push(rules); 237 } 238 239 public void endMedia(SACMediaList media) throws CSSException { 240 241 _nodeStack.pop(); 243 _root = _nodeStack.pop(); 244 } 245 246 public void startPage(String name, String pseudo_page) throws CSSException { 247 248 CSSPageRuleImpl pr = new CSSPageRuleImpl(_parentStyleSheet, null, name, pseudo_page); 250 if (!_nodeStack.empty()) { 251 ((CSSRuleListImpl)_nodeStack.peek()).add(pr); 252 } 253 254 CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(pr); 256 pr.setStyle(decl); 257 _nodeStack.push(pr); 258 _nodeStack.push(decl); 259 } 260 261 public void endPage(String name, String pseudo_page) throws CSSException { 262 263 _nodeStack.pop(); 265 _root = _nodeStack.pop(); 266 } 267 268 public void startFontFace() throws CSSException { 269 270 CSSFontFaceRuleImpl ffr = new CSSFontFaceRuleImpl(_parentStyleSheet, null); 272 if (!_nodeStack.empty()) { 273 ((CSSRuleListImpl)_nodeStack.peek()).add(ffr); 274 } 275 276 CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(ffr); 278 ffr.setStyle(decl); 279 _nodeStack.push(ffr); 280 _nodeStack.push(decl); 281 } 282 283 public void endFontFace() throws CSSException { 284 285 _nodeStack.pop(); 287 _root = _nodeStack.pop(); 288 } 289 290 public void startSelector(SelectorList selectors) throws CSSException { 291 292 CSSStyleRuleImpl sr = new CSSStyleRuleImpl(_parentStyleSheet, null, selectors); 294 if (!_nodeStack.empty()) { 295 ((CSSRuleListImpl)_nodeStack.peek()).add(sr); 296 } 297 298 CSSStyleDeclarationImpl decl = new CSSStyleDeclarationImpl(sr); 300 sr.setStyle(decl); 301 _nodeStack.push(sr); 302 _nodeStack.push(decl); 303 } 304 305 public void endSelector(SelectorList selectors) throws CSSException { 306 307 _nodeStack.pop(); 309 _root = _nodeStack.pop(); 310 } 311 312 public void property(String name, LexicalUnit value, boolean important) 313 throws CSSException { 314 CSSStyleDeclarationImpl decl = 315 (CSSStyleDeclarationImpl) _nodeStack.peek(); 316 decl.addProperty( 317 new Property(name, new CSSValueImpl(value), important)); 318 } 319 } 320 321 public static void setProperty(String key, String val) { 322 Properties props = System.getProperties(); 323 props.put(key, val); 324 System.setProperties(props); 325 } 326 } 327 | Popular Tags |