1 4 5 9 10 package org.openlaszlo.xml.internal; 11 12 import java.io.File ; 13 import java.io.FileReader ; 14 import java.io.FileWriter ; 15 import java.io.IOException ; 16 import java.io.Reader ; 17 import java.io.Writer ; 18 import java.io.StringBufferInputStream ; 19 20 import java.util.List ; 21 import java.util.Stack ; 22 23 24 import org.jdom.Attribute; 25 import org.jdom.Comment; 26 import org.jdom.Document; 27 import org.jdom.Element; 28 import org.jdom.JDOMException; 29 import org.jdom.input.SAXBuilder; 30 31 39 public class XMLCompiler { 40 41 private static Stack counterstack = new Stack (); 42 private static Stack basestack = new Stack (); 43 44 private static String bas = ""; 45 46 52 public static String compile(String x) { 53 StringBuffer base = new StringBuffer (); 55 StringBufferInputStream sis = new StringBufferInputStream (x); 56 SAXBuilder sb = new SAXBuilder(); 57 try { 58 Document doc = sb.build(sis); 59 Element e = doc.getRootElement(); 60 return compile(e); 61 } catch (Exception ignored) {} 62 63 return null; 64 } 65 66 72 public static String compile(Element e) { 73 return compile(e, Schema.DEFAULT_SCHEMA); 74 } 75 76 83 public static String compile(Element e, Schema schema) { 84 StringBuffer base = new StringBuffer (); 86 return compile(e, schema, base); 87 } 88 89 98 public static String compile(Element e, Schema schema, 99 StringBuffer base) { 100 String varname = ""; 103 return compile(e, schema, base, varname); 104 } 105 106 116 public static String compile(Element e, Schema schema, 117 StringBuffer base, String varname) { 118 StringBuffer out = new StringBuffer (); 119 120 int lastcount = -1; 121 122 123 141 142 if (base.length() <= 0) { 143 counterstack = new Stack (); 145 counterstack.push(new Integer (-1)); 146 lastcount = counterstack.size(); 147 148 basestack = new Stack (); 149 basestack.push(base); 150 151 bas = ""; 152 out.append("function x(n, a, t) {\nvar o = {};\no.n = n;\no.a = a;\no.c = [];\nif (t.length) {o.t = t;}\nreturn o;\n}\n"); 153 } 154 155 157 Integer in = new Integer (-1); 161 in = (Integer )counterstack.pop(); 162 int inum = in.intValue(); 163 inum++; 164 in = new Integer (inum); 165 166 counterstack.push(in); 167 168 if (base.length() > 0) { 169 base.append("["); 170 base.append(in); 172 base.append("]"); 173 } else { 174 base.append("root"); 175 } 176 177 String name = e.getName(); 178 out.append(base + "=x('" + name + "'"); 180 181 List attributes = e.getAttributes(); 183 out.append(",{"); 185 for (int i = 0; i < attributes.size(); i++) { 186 Attribute a = (Attribute)attributes.get(i); 187 188 String val = null; 189 Schema.Type type = schema.getAttributeType(e, a.getName()); 190 if (type == schema.STRING_TYPE) { 191 val = "'" + escapeQuote( a.getValue() ) + "'"; 192 } else { 193 try { 195 float v = a.getFloatValue(); 196 val = escapeQuote( a.getValue() ); 197 } catch (Exception ex) { 198 val = "'" + escapeQuote( a.getValue() ) + "'"; 199 } 200 } 201 202 out.append(a.getName() + ":" + val); 203 204 if (i < attributes.size() - 1) { 205 out.append(","); 206 } 207 } 208 out.append("}"); 209 211 212 String text = e.getTextTrim(); 214 if ((text != null) && (text.length() > 0)) { 215 out.append(",'" + escapeQuote( text ) + "');\n"); 216 } else { 217 out.append(");\n"); 218 } 219 220 List nestedElements = e.getChildren(); 222 223 224 if (nestedElements.size() > 0) { 225 counterstack.push(new Integer (-1)); 226 227 for (int i = 0; i < nestedElements.size(); i++) { 228 Element c = (Element)nestedElements.get(i); 229 if (i > 0) { 230 StringBuffer temp = new StringBuffer (bas); 231 out.append( compile(c, schema, temp) ); 233 } else { 234 basestack.push(bas + ""); 236 238 base.append(".c"); 239 bas = base + ""; 240 out.append( compile(c, schema, base) ); 241 } 242 } 243 244 if (counterstack.size() != lastcount) { 245 counterstack.pop(); 246 bas = (String )basestack.pop(); 247 lastcount = counterstack.size(); 249 } 250 } 251 252 return out.toString(); 253 } 254 255 303 304 311 private static String escapeQuote(String s) { 312 String retvalue = s; 313 if ( s.indexOf ("'") != -1 || s.indexOf ("\"") != -1 || s.indexOf ("\\") != -1) { 314 StringBuffer hold = new StringBuffer (); 315 char c; 316 for ( int i = 0; i < s.length(); i++ ) { 317 if ( (c=s.charAt(i)) == '\'' ) { 318 hold.append ("\\'"); 319 } else if ((c=s.charAt(i)) == '\"') { 320 hold.append ("\\\""); 321 } else if ((c=s.charAt(i)) == '\\') { 322 hold.append ("\\\\"); 323 } else { 324 hold.append(c); 325 } 326 } 327 retvalue = hold.toString(); 328 } 329 return retvalue; 330 } 331 } 332 | Popular Tags |