Coverage Report - com.ccg.macros.at.Html
 
Classes in this File Line Coverage Branch Coverage Complexity
Html
84% 
100% 
5.667
 
 1  
 /*----------------------------------------------------------------
 2  
  * $Id: Html.java,v 1.1.1.1 2003/08/21 10:36:50 pkb Exp $
 3  
  * 
 4  
  * (c)2001 - Paul Blankenbaker */
 5  
 //----------------------------------------------------------------
 6  
 
 7  
 package com.ccg.macros.at;
 8  
 
 9  
 import com.ccg.macros.*;
 10  
 
 11  
 import java.io.IOException;
 12  
 import java.util.Vector;
 13  
 
 14  
 
 15  
 //----------------------------------------------------------------
 16  
 /** Macros related to the generation of HTML documents.
 17  
  * 
 18  
  * <p>Set of <b>&#064;macros</b> which provide handy functions when
 19  
  * generating HTML output. Developers don't typically make use of
 20  
  * these methods, but end users who write HTML documents will.</p>
 21  
  * 
 22  
  * @version $Revision: 1.1.1.1 $
 23  
  * 
 24  
  * @since 1.0
 25  
  * 
 26  
  * @author $Author: pkb $ */
 27  
 //----------------------------------------------------------------
 28  
 
 29  
 public class Html extends Base {
 30  
 
 31  
   //----------------------------------------------------------------
 32  
   /** Constructs the object and readies it for {@link #install installation}.
 33  
    * 
 34  
    * @since        1.0
 35  
    * 
 36  
    * @see #install */
 37  
   //----------------------------------------------------------------
 38  
 
 39  
   public Html() {
 40  14
     super(true);
 41  14
   }
 42  
 
 43  
 
 44  
   //----------------------------------------------------------------
 45  
   /** &#064;htmlEscape(TEXT) - escape special HTML characters '<', '>' and '&'.
 46  
    * 
 47  
    * <p>This should probably be relocated to a special HTML version of
 48  
    * the {@link At At} class. However, I needed something that would
 49  
    * translate '<' into "&lt;", '>' into "&gt;" and '&' into "&amp;"
 50  
    * so that HTML examples would appear as the expected when viewed by
 51  
    * a browser.
 52  
    *
 53  
    * <p>This macro simply replaces HTML special characters, with the
 54  
    * HTML escape codes.
 55  
    * 
 56  
    * @param        out
 57  
    * 
 58  
    *         The {@link Output output device} to write the results of
 59  
    *         processing the argument to.
 60  
    * 
 61  
    * @param        args
 62  
    * 
 63  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 64  
    * 
 65  
    * @throws        IOException
 66  
    * 
 67  
    *         If there is a problem writing to the {@link Output output device}
 68  
    * 
 69  
    * @throws        InterpretException
 70  
    * 
 71  
    *         If a problem is encountered interpretting the arguments passed.
 72  
    * 
 73  
    * @since        1.0 */
 74  
   //----------------------------------------------------------------
 75  
 
 76  
   public void htmlEscape(Output out, Vector args)
 77  
     throws IOException, InterpretException {
 78  
 
 79  
                                 // exit if nothing to do
 80  16
     if ((args == null) || (args.size() != 1)) return;
 81  
 
 82  
                                 // see if there is anything to do
 83  16
     Object f = args.elementAt(0);
 84  16
     if (f == null) return;
 85  
 
 86  16
     String text = getAtMacros().interpretCheck(f.toString());
 87  
 
 88  16
     if ((text == null) || (text.length() == 0)) return;
 89  
 
 90  16
     int tlen = text.length();
 91  
 
 92  
                                 // now, replace special HTML
 93  
                                 // characters with their escape codes
 94  16
     StringBuffer sb = new StringBuffer(tlen*2);
 95  
 
 96  368
     for (int i = 0; i < tlen; i++) {
 97  352
       char c = text.charAt(i);
 98  352
       if (c == '<') sb.append("&lt;");
 99  340
       else if (c == '&') sb.append("&amp;");
 100  326
       else if (c == '>') sb.append("&gt;");
 101  314
       else sb.append(c);
 102  
     }
 103  
 
 104  16
     out.write(sb);
 105  
 
 106  16
   }
 107  
 
 108  
 
 109  
   //----------------------------------------------------------------
 110  
   /** &#064;encodeFormParam(TEXT) - escape paramters passed as
 111  
    * form values in a URL string.
 112  
    *
 113  
    * <p>This macro simply replaces some characters that might need
 114  
    * special escaping to be included in a URL. For example:
 115  
    *
 116  
    * <pre>
 117  
    * &#064;link("http://mysite.com/post.py?name=&#064;encodeFormParam("Paul Blankenbaker")")
 118  
    * </pre>
 119  
    * 
 120  
    * @param        out
 121  
    * 
 122  
    *         The {@link Output output device} to write the results of
 123  
    *         processing the argument to.
 124  
    * 
 125  
    * @param        args
 126  
    * 
 127  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 128  
    * 
 129  
    * @throws        IOException
 130  
    * 
 131  
    *         If there is a problem writing to the {@link Output output device}
 132  
    * 
 133  
    * @throws        InterpretException
 134  
    * 
 135  
    *         If a problem is encountered interpretting the arguments passed.
 136  
    * 
 137  
    * @since        1.0 */
 138  
   //----------------------------------------------------------------
 139  
 
 140  
   public void encodeFormParam(Output out, Vector args)
 141  
     throws IOException, InterpretException {
 142  
 
 143  
                                 // exit if nothing to do
 144  18
     if ((args == null) || (args.size() != 1)) return;
 145  
 
 146  
                                 // see if there is anything to do
 147  18
     Object f = args.elementAt(0);
 148  18
     if (f == null) return;
 149  
 
 150  18
     String text = getAtMacros().interpretCheck(f.toString());
 151  
 
 152  18
     if ((text == null) || (text.length() == 0)) return;
 153  
 
 154  18
     int tlen = text.length();
 155  
 
 156  
                                 // now, replace special HTML
 157  
                                 // characters with their escape codes
 158  18
     StringBuffer sb = new StringBuffer(tlen*2);
 159  18
     com.ccg.util.Form.encodeSubmitParam(sb,text);
 160  
 
 161  18
     out.write(sb);
 162  
 
 163  18
   }
 164  
 
 165  
 
 166  
   //----------------------------------------------------------------
 167  
   /** &#064;urlJoin(ROOT[,SUB0[,SUB1 ...]) - do a "smart" join of
 168  
    * strings when forming URL(s).
 169  
    * 
 170  
    * <p>It is common to form URLs by "joining" together one or more
 171  
    * strings. For example, consider the following:
 172  
    *
 173  
    * <pre>
 174  
    * &#064define("rootUrl","http://joe.com/")
 175  
    * &#064define("subDir","local/")
 176  
    * &#064define("fileName","joe.html")
 177  
    * &#064rootUrl()&#064subDir()&#064fileName
 178  
    * <pre>
 179  
    *
 180  
    * <p>The above would work, and result in the string
 181  
    * "http://joe.com/local/joe.html".
 182  
    *
 183  
    * <p>However, the above form requires the user to worry about
 184  
    * slashes ("/"), at the start or end. If the "rootUrl" had been
 185  
    * defined as "http://joe.com", it would have come out as
 186  
    * "http://joe.comlocal/joe.html" which is probably not what you
 187  
    * want.
 188  
    *
 189  
    * <p>This takes the "worry" out of this situation, it sticks a
 190  
    * slash ("/") between strings (or removes an extra one). So, using
 191  
    * this makes life much easier. As an example ALL of the following
 192  
    * will expand to the same URL ("http://joe.comlocal/joe.html").
 193  
    *
 194  
    * <pre>
 195  
    * &#064urlJoin("http://joe.com/local","joe.html")
 196  
    * &#064urlJoin("http://joe.com","local","joe.html")
 197  
    * &#064urlJoin("http://joe.com/","/local/","/joe.html")
 198  
    * &#064urlJoin("http://joe.com","/local","joe.html")
 199  
    * <pre>
 200  
    *
 201  
    * <p>You can pass as many (or as few parameters) as you wish, we
 202  
    * just keep joining them all together for you.
 203  
    * 
 204  
    * @param        out
 205  
    * 
 206  
    *         The {@link Output output device} to write the results of
 207  
    *         processing the argument to.
 208  
    * 
 209  
    * @param        args
 210  
    * 
 211  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 212  
    * 
 213  
    * @throws        IOException
 214  
    * 
 215  
    *         If there is a problem writing to the {@link Output output device}
 216  
    * 
 217  
    * @throws        InterpretException
 218  
    * 
 219  
    *         If a problem is encountered interpretting the arguments passed.
 220  
    * 
 221  
    * @since        1.0 */
 222  
   //----------------------------------------------------------------
 223  
 
 224  
   public void urlJoin(Output out, Vector args)
 225  
     throws IOException, InterpretException {
 226  
 
 227  16
     if (args == null) return;
 228  
 
 229  16
     StringBuffer sb = new StringBuffer();
 230  16
     int n = args.size();
 231  
 
 232  52
     for (int i = 0; i < n; i++) {
 233  36
       String text = getStringParameter(args,i);
 234  36
       if (text.length() > 0) {        // if something to append
 235  
                                 // if first non-blank, just append
 236  36
         int sblen = sb.length();
 237  36
         if (sblen == 0) sb.append(text);
 238  
         else {
 239  20
           boolean tHasSlash = (text.charAt(0) == '/');
 240  20
           boolean uEndsWithSlash = (sb.charAt(sblen-1) == '/');
 241  
 
 242  
                                 // if we have one slash
 243  20
           if (tHasSlash != uEndsWithSlash) {
 244  2
             sb.append(text);
 245  2
           }
 246  18
           else if (!tHasSlash) { // need to insert a slash
 247  14
             sb.append('/');
 248  14
             sb.append(text);
 249  14
           }
 250  
           else {
 251  4
             sb.setLength(sblen-1); // trim off one slash
 252  4
             sb.append(text);
 253  
           }
 254  
         }
 255  
       }
 256  
     }
 257  
 
 258  
                                 // write out resulting URL
 259  16
     out.write(sb);
 260  16
   }
 261  
 
 262  
 
 263  
   //----------------------------------------------------------------
 264  
   /** &#064;tableRow(COL0,COL1,...) generate single row in HTML table.
 265  
    * 
 266  
    * <p>This macro looks at all of the parameters, evaluates them and
 267  
    * then puts them insides the proper HTML tags for the purpose of
 268  
    * buiding a table. Here is an example usage:
 269  
    *
 270  
    * <pre>
 271  
    * &#064;tableBegin(2,"My Table")
 272  
    * &#064;tableRow("Paul","Blankenbaker")
 273  
    * &#064;tableRow("Megan","Brown")
 274  
    * &#064;tableRow("Erik","Blankenbaker")
 275  
    * &#064;tableRow("Scott",Blankenbaker")
 276  
    * &#064;tableEnd()
 277  
    * </pre>
 278  
    * 
 279  
    * @param        out
 280  
    * 
 281  
    *         The {@link Output output device} to write the results of
 282  
    *         processing the argument to.
 283  
    * 
 284  
    * @param        args
 285  
    * 
 286  
    *         A {@link Vector vector} of arguments which were passed to the macro.
 287  
    * 
 288  
    * @throws        IOException
 289  
    * 
 290  
    *         If there is a problem writing to the {@link Output output device}
 291  
    * 
 292  
    * @throws        InterpretException
 293  
    * 
 294  
    *         If a problem is encountered interpretting the arguments passed.
 295  
    * 
 296  
    * @since        1.0 */
 297  
   //----------------------------------------------------------------
 298  
 
 299  
   public void tableRow(Output out, Vector args)
 300  
     throws IOException, InterpretException {
 301  
 
 302  38
     StringBuffer row = new StringBuffer(1024);
 303  38
     row.append("<tr>");
 304  
 
 305  
                                 // exit if nothing to do
 306  38
     if ((args != null) || (args.size() != 0)) {
 307  38
       int n = args.size();
 308  116
       for (int i = 0; i < n; i++) {
 309  78
         String text = null;
 310  
                                 // see if there is anything to do
 311  78
         Object f = args.elementAt(i);
 312  78
         if (f != null) text = getAtMacros().interpretCheck(f.toString());
 313  
 
 314  78
         row.append("<td>");
 315  78
         if (text == null || text.length() == 0) row.append("&nbsp;");
 316  72
         else row.append(text);
 317  78
         row.append("</td>");
 318  
       }
 319  
     }
 320  
 
 321  38
     row.append("</tr>");
 322  38
     out.write(row);
 323  38
   }
 324  
 
 325  
 
 326  
 
 327  
   //----------------------------------------------------------------
 328  
   /** Main entry point into the application.
 329  
    * 
 330  
    * @param        args
 331  
    * 
 332  
    *         Array of command line arguments.
 333  
    */
 334  
   //----------------------------------------------------------------
 335  
 
 336  
   static public void main(String args[]) {
 337  
     try {
 338  0
       AtMacros am = new AtMacros();
 339  0
       Html html = new Html();
 340  0
       html.install(am);
 341  0
       Core core = new Core();
 342  0
       core.install(am);
 343  0
       OutputWriter ow = new OutputWriter(System.out);
 344  0
       am.interpret(new InputReader(System.in),ow);
 345  0
       ow.close();
 346  0
     } catch (Exception e) {
 347  0
       e.printStackTrace();
 348  0
     }
 349  0
   }
 350  
 
 351  
 }