Coverage Report - com.ccg.macros.at.Strings
 
Classes in this File Line Coverage Branch Coverage Complexity
Strings
45% 
14% 
2.286
 
 1  
 /*----------------------------------------------------------------
 2  
  * $Id: Strings.java,v 1.1.1.1 2003/08/21 10:36:29 pkb Exp $
 3  
  * 
 4  
  * (c)2001 - Paul Blankenbaker */
 5  
 //----------------------------------------------------------------
 6  
 
 7  
 package com.ccg.macros.at;
 8  
 
 9  
 import com.ccg.util.JavaString;
 10  
 import com.ccg.macros.*;
 11  
 
 12  
 import java.io.IOException;
 13  
 import java.util.Vector;
 14  
 
 15  
 
 16  
 //----------------------------------------------------------------
 17  
 /** General purpose "String" related macros.
 18  
  * 
 19  
  * <p>This class defines many macros related to the processing of
 20  
  * strings. This class is not typically used directly by Java
 21  
  * developers, but indirectly when <b>&#064;macro</b> processing text
 22  
  * files.</p>
 23  
  * 
 24  
  * @version $Revision: 1.1.1.1 $
 25  
  * 
 26  
  * @since 1.0
 27  
  * 
 28  
  * @author $Author: pkb $ */
 29  
 //----------------------------------------------------------------
 30  
 
 31  
 public class Strings extends Base {
 32  
 
 33  
   //----------------------------------------------------------------
 34  
   /** Constructs the object and readies it for {@link #install installation}.
 35  
    * 
 36  
    * @since        1.0
 37  
    * 
 38  
    * @see #install */
 39  
   //----------------------------------------------------------------
 40  
 
 41  
   public Strings() {
 42  4
     super(true);
 43  4
   }
 44  
 
 45  
 
 46  
   //----------------------------------------------------------------
 47  
   /** &#064;javaEscape(TEXT) - create a Java "escape" string from a
 48  
    * source string.
 49  
    * 
 50  
    * <p>This macro evaluates the TEXT argument and then puts in a Java
 51  
    * escape code for each character that a escape code exists for (for
 52  
    * example, a tab character would become "\t").
 53  
    *
 54  
    * <p>Refer to the {@link JavaString} class for additional details
 55  
    * on the recognized escape codes.
 56  
    * 
 57  
    * @param        out
 58  
    * 
 59  
    *         The {@link Output output device} to write the results of
 60  
    *         processing the argument to.
 61  
    * 
 62  
    * @param        args
 63  
    * 
 64  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 65  
    * 
 66  
    * @throws        IOException
 67  
    * 
 68  
    *         If there is a problem writing to the {@link Output output device}
 69  
    * 
 70  
    * @throws        InterpretException
 71  
    * 
 72  
    *         If a problem is encountered interpretting the arguments passed.
 73  
    * 
 74  
    * @since        1.0 */
 75  
   //----------------------------------------------------------------
 76  
 
 77  
   public void javaEscape(Output out, Vector args)
 78  
     throws IOException, InterpretException {
 79  
 
 80  
                                 // encode the string
 81  2
     String text = getStringParameter(args,0);
 82  2
     out.write(JavaString.encode(text));
 83  
 
 84  2
   }
 85  
 
 86  
 
 87  
   //----------------------------------------------------------------
 88  
   /** &#064;javaUnescape(TEXT) - takes a "escaped" Java string and
 89  
    * outputs the "unescaped" values.
 90  
    * 
 91  
    * <p>This macro evaluates the TEXT argument and then replaces each
 92  
    * Java escape code with its proper character (for example, a "\t"
 93  
    * would become a REAL tab character).
 94  
    *
 95  
    * <p>Refer to the {@link JavaString} class for additional details
 96  
    * on the recognized escape codes.
 97  
    * 
 98  
    * @param        out
 99  
    * 
 100  
    *         The {@link Output output device} to write the results of
 101  
    *         processing the argument to.
 102  
    * 
 103  
    * @param        args
 104  
    * 
 105  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 106  
    * 
 107  
    * @throws        IOException
 108  
    * 
 109  
    *         If there is a problem writing to the {@link Output output device}
 110  
    * 
 111  
    * @throws        InterpretException
 112  
    * 
 113  
    *         If a problem is encountered interpretting the arguments passed.
 114  
    * 
 115  
    * @since        1.0 */
 116  
   //----------------------------------------------------------------
 117  
 
 118  
   public void javaUnescape(Output out, Vector args)
 119  
     throws IOException, InterpretException {
 120  
 
 121  
                                 // decode the string
 122  2
     String text = getStringParameter(args,0);
 123  
     try {
 124  2
       out.write(JavaString.decode(text));
 125  0
     } catch (java.text.ParseException pe) {
 126  0
       throw new InterpretException("@javaUnescape(\""+text+"\") - "+
 127  
                                    pe.getMessage());
 128  2
     }
 129  
               
 130  2
   }
 131  
 
 132  
 
 133  
   //----------------------------------------------------------------
 134  
   /** &#064;replaceChar(TEXT,OLDCHAR,NEWCHAR) - replace all occurances
 135  
    * of one character with another.
 136  
    * 
 137  
    * <p>This macro evaluates the TEXT argument passed and replaces ALL
 138  
    * occurrences of the OLDCHAR with the NEWCHAR. For example,
 139  
    * "&#064;replaceChar("hello world"," ","_")" would result in
 140  
    * "hello_world".
 141  
    * 
 142  
    * @param        out
 143  
    * 
 144  
    *         The {@link Output output device} to write the results of
 145  
    *         processing the argument to.
 146  
    * 
 147  
    * @param        args
 148  
    * 
 149  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 150  
    * 
 151  
    * @throws        IOException
 152  
    * 
 153  
    *         If there is a problem writing to the {@link Output output device}
 154  
    * 
 155  
    * @throws        InterpretException
 156  
    * 
 157  
    *         If a problem is encountered interpretting the arguments passed.
 158  
    * 
 159  
    * @since        1.0 */
 160  
   //----------------------------------------------------------------
 161  
 
 162  
   public void replaceChar(Output out, Vector args)
 163  
     throws IOException, InterpretException {
 164  
 
 165  18
     String text = getStringParameter(args,0);
 166  18
     String o = getStringParameter(args,1);
 167  18
     String n = getStringParameter(args,2);
 168  18
     if (o.length() > 0 && n.length() > 0) {
 169  18
       text = text.replace(o.charAt(0),n.charAt(0));
 170  
     }
 171  
 
 172  18
     out.write(text);
 173  18
   }
 174  
 
 175  
 
 176  
   //----------------------------------------------------------------
 177  
   /** &#064;indexOf(TEXT,SEARCH_IN) - searches for index of text
 178  
    * string in larger string.
 179  
    * 
 180  
    * <p>This macro evaluates the TEXT argument passed and evaluates
 181  
    * the SEARCH_IN argument. It then uses the {@link
 182  
    * String#indexOf(String)} method and returns th eposition
 183  
    * indicated. It is really useful to determine if one string contains
 184  
    * another as shown below:
 185  
    *
 186  
    * <pre>
 187  
    * &#064;define("goodValues","megan|erik|scott")
 188  
    * &#064;define("lookFor","erik")
 189  
    * &#064;if("&#064;indexOf("&#064;lookFor()","&#064;goodValues()")","-1","Not Found","Found")
 190  
    * </pre>
 191  
    * 
 192  
    * @param        out
 193  
    * 
 194  
    *         The {@link Output output device} to write the results of
 195  
    *         processing the argument to.
 196  
    * 
 197  
    * @param        args
 198  
    * 
 199  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 200  
    * 
 201  
    * @throws        IOException
 202  
    * 
 203  
    *         If there is a problem writing to the {@link Output output device}
 204  
    * 
 205  
    * @throws        InterpretException
 206  
    * 
 207  
    *         If a problem is encountered interpretting the arguments passed.
 208  
    * 
 209  
    * @since        1.0 */
 210  
   //----------------------------------------------------------------
 211  
 
 212  
   public void indexOf(Output out, Vector args)
 213  
     throws IOException, InterpretException {
 214  
 
 215  8
     String text = getStringParameter(args,0);
 216  8
     String search = getStringParameter(args,1);
 217  
 
 218  8
     int pos = search.indexOf(text);
 219  8
     out.write(Integer.toString(pos));
 220  8
   }
 221  
 
 222  
 
 223  
   //----------------------------------------------------------------
 224  
   /** &#064;toOsFileName(TEXT) - replace '/' or '\' characters in file
 225  
    * names according to the OS preference.
 226  
    * 
 227  
    * <p>This macro is typically used when creating scripts. It takes a
 228  
    * TEXT string and replaces any '/' or '\' characters found with the
 229  
    * proper character for the Operating System's file naming
 230  
    * scheme. For example, "/tmp\fred.txt" would become
 231  
    * "/tmp/fred.txt" on a Unix system and "\tmp\fred.txt" on a
 232  
    * Windows system.
 233  
    *
 234  
    * <p>NOTE: In the future, this macro may strip off any leading
 235  
    * drive letter (like "c:" in the DOS world) if the output is for a
 236  
    * Unix system.
 237  
    * 
 238  
    * @param        out
 239  
    * 
 240  
    *         The {@link Output output device} to write the results of
 241  
    *         processing the argument to.
 242  
    * 
 243  
    * @param        args
 244  
    * 
 245  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 246  
    * 
 247  
    * @throws        IOException
 248  
    * 
 249  
    *         If there is a problem writing to the {@link Output output device}
 250  
    * 
 251  
    * @throws        InterpretException
 252  
    * 
 253  
    *         If a problem is encountered interpretting the arguments passed.
 254  
    * 
 255  
    * @since        1.0 */
 256  
   //----------------------------------------------------------------
 257  
 
 258  
   public void toOsFileName(Output out, Vector args)
 259  
     throws IOException, InterpretException {
 260  
 
 261  0
     out.write(com.ccg.io.FileName.fixSlashes(getStringParameter(args,0)));
 262  
 
 263  0
   }
 264  
 
 265  
 
 266  
   //----------------------------------------------------------------
 267  
   /** &#064;changeCase(TEXT,MODE) - change the case of TEXT to
 268  
    * uppercase, lowercase or mixed case.
 269  
    * 
 270  
    * <p>This macro is used to change the case of the specified
 271  
    * TEXT. The MODE should be "u" if you want UPPER CASE, "l" if you
 272  
    * want lower case, or "m" if you want "Mixed Case".
 273  
    * 
 274  
    * @param        out
 275  
    * 
 276  
    *         The {@link Output output device} to write the results of
 277  
    *         processing the argument to.
 278  
    * 
 279  
    * @param        args
 280  
    * 
 281  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 282  
    * 
 283  
    * @throws        IOException
 284  
    * 
 285  
    *         If there is a problem writing to the {@link Output output device}
 286  
    * 
 287  
    * @throws        InterpretException
 288  
    * 
 289  
    *         If a problem is encountered interpretting the arguments passed.
 290  
    * 
 291  
    * @since        1.0 */
 292  
   //----------------------------------------------------------------
 293  
 
 294  
   public void changeCase(Output out, Vector args)
 295  
     throws IOException, InterpretException {
 296  
 
 297  0
     String text = getStringParameter(args,0);
 298  0
     String modeStr = getStringParameter(args,1);
 299  
 
 300  0
     char mode = 'l';
 301  0
     if (modeStr.length() > 0) mode = modeStr.charAt(0);
 302  
 
 303  
                                 // upper case
 304  0
     if (mode == 'u') out.write(text.toUpperCase());
 305  
                                 // lower case
 306  0
     else if (mode != 'm') out.write(text.toLowerCase());
 307  
     else {                        // mixed case
 308  0
       int tlen = text.length();
 309  0
       StringBuffer sb = new StringBuffer(tlen);
 310  0
       boolean needToUpper = true;
 311  
 
 312  0
       for (int i = 0; i < tlen; i++) {
 313  0
         char c = text.charAt(i);
 314  0
         if (Character.isLetterOrDigit(c)) {
 315  0
           if (needToUpper) {
 316  0
             c = Character.toUpperCase(c);
 317  0
             needToUpper = false;
 318  0
           } else {
 319  0
             c = Character.toLowerCase(c);
 320  
           }
 321  0
         }
 322  
         else {
 323  0
           needToUpper = true;
 324  
         }
 325  0
         sb.append(c);
 326  
       }
 327  0
       out.write(sb.toString());
 328  
     }
 329  
 
 330  0
   }
 331  
 
 332  
 }