1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import org.openlaszlo.css.CSSParser; 13 import org.openlaszlo.sc.Function; 14 import org.openlaszlo.sc.ScriptCompiler; 15 import org.openlaszlo.utils.ChainedException; 16 17 import java.io.*; 18 import java.util.*; 19 import org.jdom.Element; 20 import org.apache.log4j.*; 21 import java.text.DecimalFormat ; 22 import java.text.FieldPosition ; 23 24 public class CompilerUtils { 25 33 public static String sourceLocationDirective(Element elt, boolean start) { 34 String pathname = Parser.getSourceMessagePathname(elt); 36 Integer lineno = Parser.getSourceLocation(elt, Parser.LINENO, start); 37 Integer colno = Parser.getSourceLocation(elt, Parser.COLNO, start); 38 return sourceLocationDirective(pathname, lineno, colno); 39 } 40 41 47 public static String sourceUniqueName(Element elt, boolean start) { 48 String pathname = Parser.getSourceMessagePathname(elt); 50 Integer lineno = Parser.getSourceLocation(elt, Parser.LINENO, start); 51 Integer colno = Parser.getSourceLocation(elt, Parser.COLNO, start); 52 if (colno.intValue() < 0) 57 colno = new Integer (0); 58 59 if (pathname == null) { 60 pathname = "unknown_file"; 61 } else { 62 pathname = encodeJavaScriptIdentifier(pathname); 63 } 64 return "$" + pathname + '_' + lineno + '_' + colno; 65 } 66 67 73 public static String encodeJavaScriptIdentifier(String s) { 74 StringBuffer buffer = new StringBuffer (); 75 for (int i = 0; i < s.length(); i++) { 76 char c = s.charAt(i); 77 if (!(Character.isJavaIdentifierPart(c) || 80 (i == 0 && Character.isJavaIdentifierStart(c))) || 81 c == '$') { 82 String hex = Integer.toHexString((int) c); 83 if (hex.length() < 2) 84 hex = "0" + hex; 85 buffer.append('$'); 86 buffer.append(hex.toUpperCase()); 87 } else { 88 buffer.append(c); 89 } 90 } 91 return buffer.toString(); 92 } 93 94 97 public static String attributeLocationDirective(Element elt, 98 String attrname) 99 { 100 return sourceLocationDirective(elt, true); 101 } 102 103 104 public static String attributeUniqueName(Element elt, String attrname) { 105 return sourceUniqueName(elt, true); 106 } 107 108 116 public static String sourceLocationDirective(String pathname, 117 Integer lineno, 118 Integer colno) 119 { 120 StringBuffer buffer = new StringBuffer (); 121 if (pathname != null) { 122 buffer.append("\n#file " + pathname + "\n"); 123 } 124 if (lineno != null) { 125 if (buffer.length() == 0) { 129 buffer.append('\n'); 130 } 131 buffer.append("#line " + lineno + "\n"); 132 if (colno != null) { 137 for (int i = colno.intValue(); i > 0; --i) { 138 buffer.append(' '); 139 } 140 } 141 } 142 return buffer.toString(); 143 } 144 145 public static String sourceLocationPrettyString(Element elt) { 146 String pathname = Parser.getSourceMessagePathname(elt); 148 Integer lineno = Parser.getSourceLocation(elt, Parser.LINENO, true); 149 Integer colno = Parser.getSourceLocation(elt, Parser.COLNO, true); 150 return pathname+":"+lineno+":"+colno; 151 } 152 153 156 static boolean isAtToplevel(Element element) { 157 Element parent = element.getParent(); 161 return parent == null 162 || (ToplevelCompiler.isElement(parent) && 163 isAtToplevel(parent)); 164 } 165 } 166 | Popular Tags |