| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Base |
|
| 4.571428571428571;4.571 |
| 1 | /*---------------------------------------------------------------- |
|
| 2 | * $Id: Base.java,v 1.1.1.1 2002/08/27 15:48:37 pkb Exp $ |
|
| 3 | * |
|
| 4 | * (c)2001 - Paul Blankenbaker |
|
| 5 | * |
|
| 6 | * Revision Log |
|
| 7 | * |
|
| 8 | * $Log: Base.java,v $ |
|
| 9 | * Revision 1.1.1.1 2002/08/27 15:48:37 pkb |
|
| 10 | * initial import of very old @macro() code |
|
| 11 | * |
|
| 12 | * Revision 1.4 2002/08/27 15:48:37 pkb |
|
| 13 | * javadoc updates: It took about 53 million periods, but I went through |
|
| 14 | * the com.ccg packages and got the javadoc (under JVM 1.4) to build |
|
| 15 | * without all of the new warning messages. |
|
| 16 | * |
|
| 17 | * Revision 1.3 2001/11/02 15:58:37 pkb |
|
| 18 | * |
|
| 19 | * Added macros for formatting times and extracting information from the |
|
| 20 | * CVS Id values in source files. |
|
| 21 | * |
|
| 22 | * Revision 1.2 2001/10/19 19:35:01 pkb |
|
| 23 | * |
|
| 24 | * Starting playing with the "@macros" again. Cleaned up a few more |
|
| 25 | * issues with netsted parameters and added a lot more "supporting |
|
| 26 | * macros". If you've been playing with them, you should prefer |
|
| 27 | * "com.ccg.macros.at.All" over "com.ccg.macros.At" as your interpretter. |
|
| 28 | * |
|
| 29 | * Still needs documentation and javadoc (sigh - where does the time go). |
|
| 30 | * |
|
| 31 | * Revision 1.1 2001/09/10 16:14:03 pkb |
|
| 32 | * |
|
| 33 | * Added updates to @macro processing (starting the separation of |
|
| 34 | * standard at macros into their own sub-package). |
|
| 35 | * |
|
| 36 | * |
|
| 37 | */ |
|
| 38 | //---------------------------------------------------------------- |
|
| 39 | ||
| 40 | package com.ccg.macros.at; |
|
| 41 | ||
| 42 | import com.ccg.macros.*; |
|
| 43 | import com.ccg.util.Debug; |
|
| 44 | ||
| 45 | import java.util.Vector; |
|
| 46 | ||
| 47 | import java.io.InputStream; |
|
| 48 | import java.io.IOException; |
|
| 49 | ||
| 50 | ||
| 51 | //---------------------------------------------------------------- |
|
| 52 | /** Base class for specialized @macro handlers. |
|
| 53 | * |
|
| 54 | * <p>This class is used to build specialized macro handlers. It |
|
| 55 | * provides common methods which derived classes will find useful |
|
| 56 | * (allowing them to concentrate on their specific macro |
|
| 57 | * definitions).</p> |
|
| 58 | * |
|
| 59 | * @version $Revision: 1.1.1.1 $ |
|
| 60 | * |
|
| 61 | * @since 1.0 |
|
| 62 | * |
|
| 63 | * @author $Author: pkb $ */ |
|
| 64 | //---------------------------------------------------------------- |
|
| 65 | ||
| 66 | public class Base { |
|
| 67 | ||
| 68 | //---------------------------------------------------------------- |
|
| 69 | /** Construct the object and optionally interpret a initialization file. |
|
| 70 | * |
|
| 71 | * @param hasFile |
|
| 72 | * |
|
| 73 | * Set to true if there is a associated initialization file. The |
|
| 74 | * initialization file must be found in the same manner as the |
|
| 75 | * class file - except that it will end with ".At" instead of |
|
| 76 | * ".class". For example, the "/com/ccg/macros/at/Html" class has |
|
| 77 | * a "/com/ccg/macros/at/Html.At" file associated with it. |
|
| 78 | * |
|
| 79 | * @since 1.0 */ |
|
| 80 | //---------------------------------------------------------------- |
|
| 81 | ||
| 82 | 48 | protected Base(boolean hasFile) { |
| 83 | 48 | _NeedPFile = hasFile; |
| 84 | 48 | } |
| 85 | ||
| 86 | ||
| 87 | //---------------------------------------------------------------- |
|
| 88 | /** Register's all of the method macros AND loads the predefined |
|
| 89 | * property macros for the class. |
|
| 90 | * |
|
| 91 | * <p>First, this method installs all of the "macro handler" methods |
|
| 92 | * (see {@link AtMacros#addMacros AtMacros.addMacros(Object)} for |
|
| 93 | * information on macro handler methods) of the class. |
|
| 94 | * |
|
| 95 | * <p>If the class also has a associated macro resource file, the |
|
| 96 | * resource file will then be interpretted by the macro processor |
|
| 97 | * (to load simple text style macros). During this "loading" phase, |
|
| 98 | * no output will be generated (unless the associated property file |
|
| 99 | * tries really hard to generate some). |
|
| 100 | * |
|
| 101 | * @param am |
|
| 102 | * |
|
| 103 | * The {@link AtMacros AtMacros} object to register all of the |
|
| 104 | * macros with. Must not be null. |
|
| 105 | * |
|
| 106 | * @since 1.0 */ |
|
| 107 | //---------------------------------------------------------------- |
|
| 108 | ||
| 109 | public void install(AtMacros at) { |
|
| 110 | 38 | _AtMacros = at; // save reference to associated processor |
| 111 | ||
| 112 | // first register any macro handler's |
|
| 113 | // which the class handles directly |
|
| 114 | 38 | at.addMacros(this); |
| 115 | ||
| 116 | 38 | String fname = null; |
| 117 | 38 | InputStream is = null; |
| 118 | ||
| 119 | 38 | if (_NeedPFile) try { |
| 120 | // get full name of class, like: |
|
| 121 | // "com.ccg.macros.at.File.At" |
|
| 122 | 34 | Class cl = getClass(); |
| 123 | 34 | String clname = cl.getName(); |
| 124 | ||
| 125 | 34 | if (clname.indexOf('.') > 0) { |
| 126 | 34 | java.util.StringTokenizer tokens = |
| 127 | new java.util.StringTokenizer(clname,"."); |
|
| 128 | 34 | StringBuffer buf = new StringBuffer(clname.length()+6); |
| 129 | ||
| 130 | 204 | while (tokens.hasMoreTokens()) { |
| 131 | 170 | buf.append('/'); |
| 132 | 170 | buf.append(tokens.nextToken()); |
| 133 | 170 | } |
| 134 | 34 | buf.append(".At"); |
| 135 | ||
| 136 | 34 | fname = buf.toString(); |
| 137 | 34 | } |
| 138 | 0 | else fname = clname+".At"; |
| 139 | ||
| 140 | 34 | if (at.getMacro("include") == null) { |
| 141 | // open up the resource file |
|
| 142 | 6 | is = cl.getResourceAsStream(fname); |
| 143 | ||
| 144 | // and then interpret |
|
| 145 | 6 | at.interpret(new InputReader(is), OutputNull.getInstance()); |
| 146 | 6 | is.close(); |
| 147 | 6 | } |
| 148 | else { // use include method |
|
| 149 | 28 | at.interpret(new Input("@include(\""+fname+"\",f,d)"), |
| 150 | OutputNull.getInstance()); |
|
| 151 | } |
|
| 152 | ||
| 153 | 0 | } catch (Exception e) { |
| 154 | 0 | if (Debug.ENABLED && Debug.isEnabledFor(1)) { |
| 155 | 0 | Debug.out("problem loading file:"+fname,e); |
| 156 | } |
|
| 157 | } finally { |
|
| 158 | 34 | if (is != null) try { is.close(); } catch (Throwable t) { } |
| 159 | 0 | } |
| 160 | ||
| 161 | 38 | } |
| 162 | ||
| 163 | ||
| 164 | //---------------------------------------------------------------- |
|
| 165 | /** Safely retrieve and interpret a parameter. |
|
| 166 | * |
|
| 167 | * <p>Many times a macro isn't picky about the string value of a |
|
| 168 | * parameter passed to the macro - it just wants to make sure it |
|
| 169 | * isn't passed a null. This method will fetch the desired parameter |
|
| 170 | * which was passed to your macro, interpret it and return the |
|
| 171 | * string representation. |
|
| 172 | * |
|
| 173 | * <p>It never returns null, but it may return a zero length string. |
|
| 174 | * |
|
| 175 | * <p>It will throw an exception if interpretation of the parameter |
|
| 176 | * was required but failed. |
|
| 177 | * |
|
| 178 | * @param args |
|
| 179 | * |
|
| 180 | * A {@link Vector vector} of arguments which were passed to the macro. |
|
| 181 | * |
|
| 182 | * @param i |
|
| 183 | * |
|
| 184 | * Index of the argument you want (0 corresponds to first entry). |
|
| 185 | * |
|
| 186 | * @throws InterpretException |
|
| 187 | * |
|
| 188 | * If a internal (or macro error) is encountered while processing. |
|
| 189 | * |
|
| 190 | * @throws IOException |
|
| 191 | * |
|
| 192 | * If interpretation of parameter involved output that triggered |
|
| 193 | * a I/O error. |
|
| 194 | * |
|
| 195 | * @return |
|
| 196 | * |
|
| 197 | * String representation of the parameter to the macro (after |
|
| 198 | * interpretation). Returns "" if parameter omitted or index out |
|
| 199 | * of range (never returns null). |
|
| 200 | * |
|
| 201 | * @since 1.0 */ |
|
| 202 | //---------------------------------------------------------------- |
|
| 203 | ||
| 204 | public final String getStringParameter(Vector args, int i) |
|
| 205 | throws IOException, InterpretException { |
|
| 206 | ||
| 207 | 1166 | if (i >= args.size()) return ""; |
| 208 | ||
| 209 | 1146 | Object o = args.elementAt(i); |
| 210 | 1146 | if (o == null) return ""; |
| 211 | // need to interpret |
|
| 212 | 1146 | String text = getAtMacros().interpretCheck(o.toString()); |
| 213 | ||
| 214 | 1146 | if (text == null) text = ""; |
| 215 | 1146 | return text; |
| 216 | ||
| 217 | } |
|
| 218 | ||
| 219 | ||
| 220 | //---------------------------------------------------------------- |
|
| 221 | /** Safely retrieve and interpret a parameter and get the integer value. |
|
| 222 | * |
|
| 223 | * <p>Many times a macro isn't picky about the string value of a |
|
| 224 | * parameter passed to the macro - it just wants to make sure it |
|
| 225 | * isn't passed a null. This method will fetch the desired parameter |
|
| 226 | * which was passed to your macro, interpret it and return the |
|
| 227 | * string representation. |
|
| 228 | * |
|
| 229 | * <p>It will throw an exception if interpretation of the parameter |
|
| 230 | * was required but failed. |
|
| 231 | * |
|
| 232 | * @param args |
|
| 233 | * |
|
| 234 | * A {@link Vector vector} of arguments which were passed to the |
|
| 235 | * macro. |
|
| 236 | * |
|
| 237 | * @param i |
|
| 238 | * |
|
| 239 | * Index of the argument you want (0 corresponds to first entry). |
|
| 240 | * |
|
| 241 | * @param def |
|
| 242 | * |
|
| 243 | * Default value to return if argument omitted or doesn't |
|
| 244 | * evaluate properly. |
|
| 245 | * |
|
| 246 | * @throws InterpretException |
|
| 247 | * |
|
| 248 | * If a internal (or macro error) is encountered while processing. |
|
| 249 | * |
|
| 250 | * @throws IOException |
|
| 251 | * |
|
| 252 | * If interpretation of parameter involved output that triggered |
|
| 253 | * a I/O error. |
|
| 254 | * |
|
| 255 | * @return |
|
| 256 | * |
|
| 257 | * Integer value of the specified parameter (or your default |
|
| 258 | * value if we could not convert it). |
|
| 259 | * |
|
| 260 | * @since 1.0 */ |
|
| 261 | //---------------------------------------------------------------- |
|
| 262 | ||
| 263 | public final int getIntParameter(Vector args, int i, int def) |
|
| 264 | throws IOException, InterpretException { |
|
| 265 | ||
| 266 | 76 | String istr = getStringParameter(args,i); |
| 267 | 76 | if (istr.length() == 0) return def; |
| 268 | ||
| 269 | try { |
|
| 270 | 76 | def = Integer.parseInt(istr); |
| 271 | 0 | } catch (Throwable t) { |
| 272 | 0 | throw new InterpretException("required integer value, passed \""+ |
| 273 | istr+"\""); |
|
| 274 | 76 | } |
| 275 | ||
| 276 | 76 | return def; |
| 277 | ||
| 278 | } |
|
| 279 | ||
| 280 | ||
| 281 | //---------------------------------------------------------------- |
|
| 282 | /** Safely retrieve and interpret a parameter and get the integer value. |
|
| 283 | * |
|
| 284 | * <p>Many times a macro isn't picky about the string value of a |
|
| 285 | * parameter passed to the macro - it just wants to make sure it |
|
| 286 | * isn't passed a null. This method will fetch the desired parameter |
|
| 287 | * which was passed to your macro, interpret it and return the |
|
| 288 | * string representation. |
|
| 289 | * |
|
| 290 | * <p>It will throw an exception if interpretation of the parameter |
|
| 291 | * was required but failed. |
|
| 292 | * |
|
| 293 | * @param args |
|
| 294 | * |
|
| 295 | * A {@link Vector vector} of arguments which were passed to the |
|
| 296 | * macro. |
|
| 297 | * |
|
| 298 | * @param i |
|
| 299 | * |
|
| 300 | * Index of the argument you want (0 corresponds to first entry). |
|
| 301 | * |
|
| 302 | * @param def |
|
| 303 | * |
|
| 304 | * Default value to return if argument omitted or doesn't |
|
| 305 | * evaluate properly. |
|
| 306 | * |
|
| 307 | * @throws InterpretException |
|
| 308 | * |
|
| 309 | * If a internal (or macro error) is encountered while processing. |
|
| 310 | * |
|
| 311 | * @throws IOException |
|
| 312 | * |
|
| 313 | * If interpretation of parameter involved output that triggered |
|
| 314 | * a I/O error. |
|
| 315 | * |
|
| 316 | * @return |
|
| 317 | * |
|
| 318 | * Integer value of the specified parameter (or your default |
|
| 319 | * value if we could not convert it). |
|
| 320 | * |
|
| 321 | * @since 1.0 */ |
|
| 322 | //---------------------------------------------------------------- |
|
| 323 | ||
| 324 | public final long getLongParameter(Vector args, int i, long def) |
|
| 325 | throws IOException, InterpretException { |
|
| 326 | ||
| 327 | 20 | String istr = getStringParameter(args,i); |
| 328 | 20 | if (istr.length() == 0) return def; |
| 329 | ||
| 330 | try { |
|
| 331 | 20 | def = Long.parseLong(istr); |
| 332 | 0 | } catch (Throwable t) { |
| 333 | 0 | throw new InterpretException("required long integer value, passed \""+ |
| 334 | istr+"\""); |
|
| 335 | 20 | } |
| 336 | ||
| 337 | 20 | return def; |
| 338 | ||
| 339 | } |
|
| 340 | ||
| 341 | ||
| 342 | //---------------------------------------------------------------- |
|
| 343 | /** Safely retrieve and interpret a parameter and get the double value. |
|
| 344 | * |
|
| 345 | * <p>Many times a macro isn't picky about the string value of a |
|
| 346 | * parameter passed to the macro - it just wants to make sure it |
|
| 347 | * isn't passed a null. This method will fetch the desired parameter |
|
| 348 | * which was passed to your macro, interpret it and return the |
|
| 349 | * string representation. |
|
| 350 | * |
|
| 351 | * <p>It will throw an exception if interpretation of the parameter |
|
| 352 | * was required but failed. |
|
| 353 | * |
|
| 354 | * @param args |
|
| 355 | * |
|
| 356 | * A {@link Vector vector} of arguments which were passed to the |
|
| 357 | * macro. |
|
| 358 | * |
|
| 359 | * @param i |
|
| 360 | * |
|
| 361 | * Index of the argument you want (0 corresponds to first entry). |
|
| 362 | * |
|
| 363 | * @param def |
|
| 364 | * |
|
| 365 | * Default value to return if argument omitted or doesn't |
|
| 366 | * evaluate properly. |
|
| 367 | * |
|
| 368 | * @throws InterpretException |
|
| 369 | * |
|
| 370 | * If a internal (or macro error) is encountered while processing. |
|
| 371 | * |
|
| 372 | * @throws IOException |
|
| 373 | * |
|
| 374 | * If interpretation of parameter involved output that triggered |
|
| 375 | * a I/O error. |
|
| 376 | * |
|
| 377 | * @return |
|
| 378 | * |
|
| 379 | * Double value of the specified parameter (or your default |
|
| 380 | * value if we could not convert it). |
|
| 381 | * |
|
| 382 | * @since 1.0 */ |
|
| 383 | //---------------------------------------------------------------- |
|
| 384 | ||
| 385 | public final double getDoubleParameter(Vector args, int i, double def) |
|
| 386 | throws IOException, InterpretException { |
|
| 387 | ||
| 388 | 0 | String istr = getStringParameter(args,i); |
| 389 | 0 | if (istr.length() == 0) return def; |
| 390 | ||
| 391 | try { |
|
| 392 | 0 | def = Double.parseDouble(istr); |
| 393 | 0 | } catch (Throwable t) { |
| 394 | 0 | throw new InterpretException("required double value, passed \""+ |
| 395 | istr+"\""); |
|
| 396 | 0 | } |
| 397 | ||
| 398 | 0 | return def; |
| 399 | ||
| 400 | } |
|
| 401 | ||
| 402 | ||
| 403 | //---------------------------------------------------------------- |
|
| 404 | /** Holds the associated {@link AtMacros AtMacros} processor. |
|
| 405 | * |
|
| 406 | * @serial |
|
| 407 | * |
|
| 408 | * @since 1.0 */ |
|
| 409 | //---------------------------------------------------------------- |
|
| 410 | ||
| 411 | private AtMacros _AtMacros; |
|
| 412 | ||
| 413 | ||
| 414 | //---------------------------------------------------------------- |
|
| 415 | /** Get the associated {@link AtMacros AtMacros} processor. |
|
| 416 | * |
|
| 417 | * <p>This method allows derived classes to get access to the {@link |
|
| 418 | * AtMacros macro processor} which they are working for. It |
|
| 419 | * corresponds to the last registered {@link AtMacros AtMacros} |
|
| 420 | * which was {@link #install installed}. |
|
| 421 | * |
|
| 422 | * @return |
|
| 423 | * |
|
| 424 | * Current AtMacros value assigned during last {@link #install |
|
| 425 | * install}. */ |
|
| 426 | //---------------------------------------------------------------- |
|
| 427 | ||
| 428 | protected AtMacros getAtMacros() { |
|
| 429 | 2438 | return _AtMacros; |
| 430 | } |
|
| 431 | ||
| 432 | ||
| 433 | //---------------------------------------------------------------- |
|
| 434 | // private data |
|
| 435 | //---------------------------------------------------------------- |
|
| 436 | ||
| 437 | private transient boolean _NeedPFile; |
|
| 438 | ||
| 439 | } |