1 30 31 package test; 32 33 import java.io.*; 34 import com.steadystate.css.*; 35 import org.w3c.dom.*; 36 import org.w3c.dom.css.*; 37 38 45 public class TestDOM { 46 47 public static void main(String [] args) throws ParseException { 48 try { 49 Reader r = new FileReader("c:\\working\\css2parser\\stylesheets\\basic.css"); 50 CSS2Parser parser = new CSS2Parser(r); 51 60 CSSStyleDeclaration n = parser.styleDeclaration(); 61 62 System.out.println("No. of properties: " + n.getLength()); 64 65 for (int i = 0; i < n.getLength(); i++) { 66 String name = n.item(i); 67 System.out.println(name + " : " + n.getPropertyValue(name)); 68 } 69 70 System.out.println("\ngetCssText"); 72 System.out.println(n.getCssText()); 73 74 System.out.println("\nsetCssText"); 77 n.setCssText("{ alpha: 2; beta: 20px; gamma: 40em; delta: 1mm; epsilon: 24pt }"); 78 79 System.out.println(n.getCssText()); 80 81 System.out.println(); 83 System.out.println("Removing 'gamma'"); 84 System.out.println(n.removeProperty("gamma")); 85 System.out.println("Removing 'alpha'"); 86 System.out.println(n.removeProperty("alpha")); 87 System.out.println("Removing 'epsilon'"); 88 System.out.println(n.removeProperty("epsilon")); 89 90 System.out.println(n.getCssText()); 92 93 System.out.println(); 96 System.out.println("setting 'beta' to 40px"); 97 n.setProperty("beta", "40px", null); 98 System.out.println("setting 'omega' to 1 with 'important' priority"); 99 n.setProperty("omega", "1", "important"); 100 101 System.out.println(n.getCssText()); 103 104 System.out.println(); 106 System.out.println("Retrieving 'beta' as a CSSPrimitiveValue"); 107 CSSPrimitiveValue value = (CSSPrimitiveValue) n.getPropertyCSSValue("beta"); 108 System.out.println("getCssText: 'beta' = " + value.getCssText()); 109 System.out.println("getFloatValue: 'beta' = " + value.getFloatValue(CSSPrimitiveValue.CSS_PX)); 110 System.out.println("Setting 'beta' to 100px"); 111 value.setFloatValue(CSSPrimitiveValue.CSS_PX, 100); 112 113 System.out.println("Adding a new value, which should end-up as a CSSValueList.\nSetting 'list-test' to 100 200 300"); 114 n.setProperty("list-test", "100 200 300", null); 115 value = (CSSPrimitiveValue) n.getPropertyCSSValue("list-test"); 116 System.out.println("getValueType: 'list-test' = " + value.getCssValueType()); 117 118 CSSValueList vl = (CSSValueList) n.getPropertyCSSValue("list-test"); 119 for (int i = 0; i < vl.getLength(); i++) { 120 System.out.println( 121 "getFloatValue: 'list-test[" + 122 String.valueOf( i ) + 123 "]' = " + 124 ((CSSPrimitiveValue)vl.item(i)).getFloatValue(CSSPrimitiveValue.CSS_NUMBER)); 125 } 126 127 System.out.println("Let's see the change within the entire declaration"); 129 System.out.println(n.getCssText()); 130 131 System.out.println("Setting 'list-test' to something completely different, the string 'bogus'."); 133 vl.setCssText("bogus"); 134 135 System.out.println("What happens..."); 136 System.out.println("getValueType: 'list-test' = " + value.getCssValueType()); 137 System.out.println(n.getCssText()); 138 } catch (Exception e) { 139 System.out.println("Error."); 140 System.out.println(e.getMessage()); 141 e.printStackTrace(); 142 } 143 } 144 } 145 | Popular Tags |