1 10 11 package com.triactive.jdo.util; 12 13 import javax.jdo.JDOUserException; 14 15 16 public class MacroString 17 { 18 private final Class thisClass; 19 private final Imports imports; 20 private final String macroString; 21 22 23 public MacroString(Class thisClass, String importsString, String macroString) 24 { 25 this.thisClass = thisClass; 26 this.macroString = macroString; 27 28 imports = new Imports(); 29 imports.importPackage(thisClass); 30 31 if (importsString != null) 32 imports.parseImports(importsString); 33 } 34 35 36 public String substituteMacros(MacroHandler mh) 37 { 38 StringBuffer outBuf = new StringBuffer (); 39 int left, right; 40 41 42 for (int curr = 0; curr < macroString.length(); curr = right + 1) 43 { 44 if ((left = macroString.indexOf('{', curr)) < 0) 45 { 46 outBuf.append(macroString.substring(curr)); 47 break; 48 } 49 50 outBuf.append(macroString.substring(curr, left)); 51 52 if ((right = macroString.indexOf('}', left + 1)) < 0) 53 throw new JDOUserException("Unmatched braces for identifier macro: " + macroString); 54 55 IdentifierMacro im = parseIdentifierMacro(macroString.substring(left + 1, right)); 56 mh.onIdentifierMacro(im); 57 58 outBuf.append(im.value); 59 } 60 61 String tmpString = outBuf.toString(); 62 outBuf = new StringBuffer (); 63 64 65 for (int curr = 0; curr < tmpString.length(); curr = right + 1) 66 { 67 if ((left = tmpString.indexOf('?', curr)) < 0) 68 { 69 outBuf.append(tmpString.substring(curr)); 70 break; 71 } 72 73 outBuf.append(tmpString.substring(curr, left)); 74 75 if ((right = tmpString.indexOf('?', left + 1)) < 0) 76 throw new JDOUserException("Unmatched question marks for parameter macro: " + tmpString); 77 78 ParameterMacro pm = new ParameterMacro(tmpString.substring(left + 1, right)); 79 mh.onParameterMacro(pm); 80 81 outBuf.append('?'); 82 } 83 84 return outBuf.toString(); 85 } 86 87 88 private Class resolveClassDeclaration(String className) 89 { 90 try 91 { 92 return className.equals("this") ? thisClass : imports.resolveClassDeclaration(className); 93 } 94 catch (ClassNotFoundException e) 95 { 96 return null; 97 } 98 } 99 100 101 private IdentifierMacro parseIdentifierMacro(String unparsed) 102 { 103 String className = null; 104 String fieldName = null; 105 String subfieldName = null; 106 107 Class c = resolveClassDeclaration(unparsed); 108 109 if (c == null) 110 { 111 112 int lastDot = unparsed.lastIndexOf('.'); 113 114 if (lastDot < 0) 115 throw new JDOUserException("Cannot parse identifier macro: " + unparsed); 116 117 fieldName = unparsed.substring(lastDot + 1); 118 className = unparsed.substring(0, lastDot); 119 c = resolveClassDeclaration(className); 120 121 if (c == null) 122 { 123 124 int lastDot2 = unparsed.lastIndexOf('.', lastDot - 1); 125 126 if (lastDot2 < 0) 127 throw new JDOUserException("Cannot parse identifier macro: " + unparsed); 128 129 subfieldName = fieldName; 130 fieldName = unparsed.substring(lastDot2 + 1, lastDot); 131 className = unparsed.substring(0, lastDot2); 132 c = resolveClassDeclaration(className); 133 134 if (c == null) 135 throw new JDOUserException("Cannot parse identifier macro: " + unparsed); 136 } 137 } 138 139 return new IdentifierMacro(unparsed, c, fieldName, subfieldName); 140 } 141 142 143 public static class IdentifierMacro 144 { 145 public final String unparsed; 146 public final Class clazz; 147 public final String fieldName; 148 public final String subfieldName; 149 public String value; 150 151 IdentifierMacro(String unparsed, Class clazz, String fieldName, String subfieldName) 152 { 153 this.unparsed = unparsed; 154 this.clazz = clazz; 155 this.fieldName = fieldName; 156 this.subfieldName = subfieldName; 157 this.value = null; 158 } 159 160 public String toString() 161 { 162 return "{" + unparsed + "}"; 163 } 164 } 165 166 167 public static class ParameterMacro 168 { 169 public final String parameterName; 170 171 ParameterMacro(String parameterName) 172 { 173 this.parameterName = parameterName; 174 } 175 176 public String toString() 177 { 178 return "?" + parameterName + "?"; 179 } 180 } 181 182 183 public static interface MacroHandler 184 { 185 void onIdentifierMacro(IdentifierMacro im); 186 void onParameterMacro(ParameterMacro pm); 187 } 188 } 189 | Popular Tags |