1 5 package com.teamkonzept.publishing.markups; 6 7 import java.sql.*; 8 import java.util.*; 9 10 import com.teamkonzept.db.*; 11 import com.teamkonzept.lib.*; 12 import com.teamkonzept.publishing.markups.db.queries.*; 13 import org.apache.log4j.Category; 14 15 public class TKMarkupParamClass 16 { 17 private static final Category cat = Category.getInstance(TKMarkupParamClass.class); 18 19 public String name; 20 public String className; 21 public int id; 22 23 public static int typeId = 1; 24 25 protected static final String identifierFirstCodes = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 26 protected static final String digitCodes = "0123456789"; 27 protected static final String identifierCodes = identifierFirstCodes+digitCodes; 28 29 private TKMarkupStatics statics = null; 30 31 TKMarkupParamClass (TKMarkupStatics statics) { 32 33 this.name = null; 34 this.className = null; 35 } 36 37 TKMarkupParamClass () { 38 39 this (TKMarkupStatics.setup()); 40 } 41 42 public String toString () { 43 44 return name+"/"+className; 45 } 46 47 public String checkValue (String value) { 48 49 return value; 50 } 51 52 public String typeDefault () { 53 54 return null; 55 56 } 57 58 public int parsePattern (String text, int pos, StringBuffer str, String pattern) { 59 60 int i = 0; 61 62 while ((pos+i < text.length()) && (i < pattern.length())) { 63 64 char chr = text.charAt (pos+i); 65 66 if (chr != pattern.charAt(i++)) return pos; 67 else if (str != null) str.append (chr); 68 } 69 70 if (i == pattern.length()) return pos+i; 71 else return pos; 72 } 73 74 public int parseStringValue (String text, int pos, StringBuffer value) 75 throws TKMarkupParserException { 76 77 int oldPos = pos; 78 pos = parsePattern (text,pos,null,"\""); 79 if (pos <= oldPos) return oldPos; 80 81 while (pos < text.length()) { 82 83 char chr = text.charAt (pos); 84 85 if (chr == '"') break; 86 87 else if ((chr == '\\') && (pos+1 < text.length())) { 88 89 if (value == null) pos++; 90 else value.append (text.charAt (++pos)); 91 92 } else if (value != null) value.append (chr); 93 94 pos++; 95 } 96 97 int nextOldPos = pos; 98 pos = parsePattern (text,pos,null,"\""); 99 100 if (pos <= nextOldPos) return oldPos; 101 else return pos; 102 } 103 104 public int parseUnquotedValue (String text, int pos, StringBuffer value) 105 throws TKMarkupParserException { 106 107 if (text == null) return pos; 108 if (value != null) value.setLength (0); 109 110 boolean isId = true; 111 boolean isInt = true; 112 boolean isFloat = true; 113 114 while (pos < text.length()) { 115 116 char chr = text.charAt (pos); 117 118 if ((pos == 0) && (identifierFirstCodes.indexOf(chr) == -1)) isId = false; 119 else if ((pos != 0) && (identifierCodes.indexOf(chr) == -1)) isId = false; 120 121 if (chr == '.') 122 if (!isInt) isFloat = false; 123 else isInt = false; 124 125 else if (digitCodes.indexOf(chr) == -1) isInt = isFloat = false; 126 127 if (!isId && !isInt && !isFloat) break; 128 if (value != null) value.append (chr); 129 130 pos++; 131 } 132 133 return pos; 134 } 135 136 public boolean parseQuotedValue (StringBuffer value) 137 throws TKMarkupParserException { 138 139 if (value == null) return false; 140 141 int pos = parseUnquotedValue (value.toString().trim(),0,value); 142 return pos > 0; 143 } 144 145 public int parseValue (String text, int pos, StringBuffer value) 146 throws TKMarkupParserException { 147 148 if (text == null) return pos; 149 150 int oldPos = pos; 151 pos = parseStringValue (text,pos,value); 152 153 if (pos > oldPos) { 154 155 if (parseQuotedValue (value)) return pos; 156 else return oldPos; 157 } 158 159 return parseUnquotedValue (text,pos,value); 160 } 161 162 public String wrapValue (String value) { 163 164 if (value == null) return null; 165 else if (quotingRequired (value)) return par2quotes(value); 166 else return value; 167 } 168 169 public boolean quotingRequired (String text) { 170 171 boolean isId = true; 172 boolean isInt = true; 173 boolean isFloat = true; 174 175 if (text == null) return false; 176 177 int pos = 0; 178 while (pos < text.length()) { 179 180 char chr = text.charAt (pos++); 181 182 if ((pos == 0) && (identifierFirstCodes.indexOf(chr) == -1)) isId = false; 183 else if ((pos != 0) && (identifierCodes.indexOf(chr) == -1)) isId = false; 184 185 if (chr == '.') 186 if (!isInt) isFloat = false; 187 else isInt = false; 188 189 else if (digitCodes.indexOf(chr) == -1) isInt = isFloat = false; 190 191 if (!isId && !isInt && !isFloat) return true; 192 } 193 194 return false; 195 } 196 197 public static String par2quotes (String text) { 198 199 if (text == null) return null; 200 201 StringBuffer buf = new StringBuffer (); 202 buf.append ('"'); 203 204 int pos = 0; 205 while (pos < text.length()) { 206 207 char chr = text.charAt (pos++); 208 209 if ((chr == '\\') || (chr == '"')) buf.append ('\\'); 210 211 buf.append (chr); 212 } 213 214 buf.append ('"'); 215 return buf.toString(); 216 } 217 218 public boolean isFlag () { 219 220 return false; 221 } 222 223 public TKVector getOptions (String data) { 224 225 if (data == null) return null; 226 227 TKVector options = null; 228 StringBuffer value = null; 229 230 int pos = 0; 231 while (pos < data.length()) { 232 233 char chr = data.charAt (pos++); 234 235 if (chr == ';') { 236 237 if (value == null) continue; 238 239 if (options == null) options = new TKVector(); 240 options.addElement(value.toString()); 241 value = null; 242 243 continue; 244 } 245 246 if ((chr == '\\') && (pos < data.length())) chr = data.charAt (pos++); 247 248 (value == null ? (value = new StringBuffer ()) : value) .append (chr); 249 } 250 251 if (value != null) { 252 if (options == null) options = new TKVector(); 253 options.addElement(value.toString()); 254 } 255 256 return options; 257 } 258 259 public String setOptions (TKVector optionList) { 260 261 if (optionList == null) return null; 262 263 StringBuffer dataBuffer = new StringBuffer (); 264 265 Enumeration e = optionList.elements(); 266 while (e.hasMoreElements()) { 267 268 String option = (String ) e.nextElement(); 269 if (option == null) continue; 270 271 if (dataBuffer.length() != 0) dataBuffer.append (';'); 272 273 for (int i = 0; i < option.length(); i++) { 274 275 char chr = option.charAt(i); 276 277 if (chr == ';') dataBuffer.append ("\\;"); 278 else if (chr == '\\') dataBuffer.append ("\\\\"); 279 else dataBuffer.append (chr); 280 } 281 } 282 283 return dataBuffer.toString(); 284 } 285 286 public static void getClasses() throws SQLException { 287 288 TKMarkupStatics statics = TKMarkupStatics.setup(); 289 290 statics.paramClassNames = new TKHashtable (); 291 statics.paramClassIds = new TKHashtable (); 292 statics.defaultTypeClass = null; 293 294 TKQuery q = TKDBManager.newQuery(TKMarkupParamGetClasses.class); 295 q.execute(); 296 ResultSet rs = q.fetchResultSet(); 297 298 while (rs.next()) { 299 300 int classId = rs.getInt ("CLASS_ID"); 301 String name = rs.getString ("NAME"); 302 String className = rs.getString ("CLASS_NAME"); 303 int type = rs.getInt ("TYPE"); 304 305 if (type != typeId) continue; 306 307 TKMarkupParamClass paramClass = register (statics,classId,name,className); 308 if (paramClass != null) statics.paramClassIds.put (new Integer (classId),paramClass); 309 } 310 } 311 312 public static TKMarkupParamClass register (TKMarkupStatics statics, int id, String name, String className) { 313 314 try { 315 Class objectClass = Class.forName (className); 316 317 TKMarkupParamClass paramClass = (TKMarkupParamClass) objectClass.newInstance(); 318 319 paramClass.id = id; 320 paramClass.name = name; 321 paramClass.className = className; 322 323 statics.paramClassNames.put (name,paramClass); 324 return paramClass; 325 326 } catch (Exception ex) { 327 cat.error ("TKWMAttrSetup.setup() failed: ", ex); 328 return null; 329 } 330 } 331 332 public static synchronized void setup () { 333 334 try { getClasses(); } catch (Exception ex) { 335 336 cat.error ("TKWMAttrSetup.setup() failed: ", ex); 337 } catch (TKSQLError er) { 338 cat.error("TKWMAttrSetup.setup() failed: ", er); 339 } 340 } 341 342 public static synchronized TKMarkupParamClass lookup (TKMarkupStatics statics, String name) { 343 344 return (TKMarkupParamClass) statics.paramClassNames.get (name); 345 } 346 347 public static synchronized TKMarkupParamClass lookup (TKMarkupStatics statics, int id) { 348 349 return (TKMarkupParamClass) statics.paramClassIds.get (new Integer (id)); 350 } 351 352 public static synchronized Enumeration allNames (TKMarkupStatics statics) { 353 354 return statics.paramClassNames.keys(); 355 } 356 357 public static TKMarkupParamClass lookup (String name) { 358 359 return lookup (TKMarkupStatics.setup(),name); 360 } 361 362 public static TKMarkupParamClass lookup (int id) { 363 364 return lookup (TKMarkupStatics.setup(),id); 365 } 366 367 public static Enumeration allNames () { 368 369 return allNames (TKMarkupStatics.setup()); 370 } 371 } 372 373 | Popular Tags |