1 30 31 package com.steadystate.css.parser; 32 33 import java.io.*; 34 import org.w3c.css.sac.*; 35 import org.w3c.css.sac.helpers.ParserFactory; 36 37 42 public class ParseTest extends HandlerBase { 43 44 private static final String PARSER = "com.steadystate.css.parser.SACParser"; 45 46 private int _propertyCounter = 0; 47 private int _indentSize = 0; 48 49 public ParseTest() { 50 try { 51 CSSOMParser.setProperty("org.w3c.css.sac.parser", PARSER); 52 ParserFactory factory = new ParserFactory(); 53 Parser parser = factory.makeParser(); 54 parser.setDocumentHandler(this); 56 57 Reader r = new FileReader("c:\\working\\css2parser\\stylesheets\\page_test.css"); 65 66 InputSource is = new InputSource(r); 67 parser.parseStyleSheet(is); 70 71 } catch (Exception e) { 72 System.err.println("Exception: " + e.getMessage()); 73 } 74 } 75 76 public static void main(String [] args) { 77 new ParseTest(); 78 } 79 80 public void startDocument(InputSource source) 81 throws CSSException { 82 System.out.println("startDocument"); 83 } 84 85 public void endDocument(InputSource source) throws CSSException { 86 System.out.println("endDocument"); 87 } 88 89 public void comment(String text) throws CSSException { 90 } 91 92 public void ignorableAtRule(String atRule) throws CSSException { 93 System.out.println(atRule); 94 } 95 96 public void namespaceDeclaration(String prefix, String uri) 97 throws CSSException { 98 } 99 100 public void importStyle(String uri, SACMediaList media, 101 String defaultNamespaceURI) 102 throws CSSException { 103 System.out.print("@import url(" + uri + ")"); 104 if (media.getLength() > 0) { 105 System.out.println(" " + media.toString() + ";"); 106 } else { 107 System.out.println(";"); 108 } 109 } 110 111 public void startMedia(SACMediaList media) throws CSSException { 112 System.out.println(indent() + "@media " + media.toString() + " {"); 113 incIndent(); 114 } 115 116 public void endMedia(SACMediaList media) throws CSSException { 117 decIndent(); 118 System.out.println(indent() + "}"); 119 } 120 121 public void startPage(String name, String pseudo_page) throws CSSException { 122 System.out.print(indent() + "@page"); 123 if (name != null) { 124 System.out.print(" " + name); 125 } 126 if (pseudo_page != null) { 127 System.out.println(" " + pseudo_page); 128 } 129 System.out.println(" {"); 130 _propertyCounter = 0; 131 incIndent(); 132 } 133 134 public void endPage(String name, String pseudo_page) throws CSSException { 135 System.out.println(); 136 decIndent(); 137 System.out.println(indent() + "}"); 138 } 139 140 public void startFontFace() throws CSSException { 141 System.out.println(indent() + "@font-face {"); 142 _propertyCounter = 0; 143 incIndent(); 144 } 145 146 public void endFontFace() throws CSSException { 147 System.out.println(); 148 decIndent(); 149 System.out.println(indent() + "}"); 150 } 151 152 public void startSelector(SelectorList selectors) throws CSSException { 153 System.out.println(indent() + selectors.toString() + " {"); 154 _propertyCounter = 0; 155 incIndent(); 156 } 157 158 public void endSelector(SelectorList selectors) throws CSSException { 159 System.out.println(); 160 decIndent(); 161 System.out.println(indent() + "}"); 162 } 163 164 public void property(String name, LexicalUnit value, boolean important) 165 throws CSSException { 166 if (_propertyCounter++ > 0) { 167 System.out.println(";"); 168 } 169 System.out.print(indent() + name + ":"); 170 171 LexicalUnit nextVal = value; 173 while (nextVal != null) { 174 System.out.print(" " + ((LexicalUnitImpl)nextVal).toDebugString()); 176 nextVal = nextVal.getNextLexicalUnit(); 177 } 178 179 if (important) { 181 System.out.print(" !important"); 182 } 183 } 184 185 private String indent() { 186 StringBuffer sb = new StringBuffer (16); 187 for (int i = 0; i < _indentSize; i++) { 188 sb.append(" "); 189 } 190 return sb.toString(); 191 } 192 193 private void incIndent() { 194 _indentSize += 4; 195 } 196 197 private void decIndent() { 198 _indentSize -= 4; 199 } 200 } 201 | Popular Tags |