##### Coverage Report - org.webmacro.engine.QuotedStringBuilder

Classes in this File | Line Coverage | Branch Coverage | Complexity
QuotedString |
45% | 10/22
50% | 4/8
| 3.4;3.4
QuotedStringBuilder |
100% | 19/19
100% | 10/10
| 3.4;3.4

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
23 |  |
24 |  |
     package org.webmacro.engine;
25 |  |
26 |  |
     import java.io.IOException;
27 |  |
     import java.util.Enumeration;
28 |  |
     import java.util.Vector;
29 |  |
30 |  |
     import org.webmacro.Context;
31 |  |
     import org.webmacro.FastWriter;
32 |  |
     import org.webmacro.Macro;
33 |  |
     import org.webmacro.PropertyException;
34 |  |
     import org.webmacro.TemplateVisitor;
35 |  |
     import org.webmacro.Visitable;
36 |  |
37 |  |
38 |  |
     /**
39 |  |
      * @author justin
40 |  |
      * @since 19 Oct 1999
41 |  |
      *
42 |  |
      */
43 | 424 |
     public final class QuotedStringBuilder extends Vector implements Builder
44 |  |
     {
45 |  |
46 |  |
       private static final long serialVersionUID = -7489766042268586054L;
47 |  |
48 |  |
       final public Object build (BuildContext bc) throws BuildException
49 |  |
         {
50 | 424 |
             StringBuffer str = new StringBuffer(100);
51 | 424 |
             QuotedString qs = new QuotedString();
52 |  |
53 | 424 |
             Enumeration elems = elements();
54 |  |
55 | 846 |
             while (elems.hasMoreElements())
56 |  |
             {
57 | 422 |
                 Object txt = elems.nextElement();
58 |  |
59 | 422 |
                 if (txt instanceof Builder)
60 |  |
                 {
61 | 110 |
                     txt = ((Builder) txt).build(bc);
62 |  |
                 }
63 |  |
64 | 422 |
                 if (txt instanceof String)
65 |  |
                 {
66 | 312 |
                     str.append(txt);
67 |  |
                 }
68 |  |
                 else
69 |  |
                 {
70 | 110 |
                     qs.addElement(str.toString());
71 | 110 |
                     qs.addElement(txt);
72 | 110 |
                     str.setLength(0);
73 |  |
                 }
74 | 422 |
             }
75 | 424 |
             if (str.length() > 0)
76 |  |
             {
77 | 276 |
                 qs.addElement(str.toString());
78 |  |
             }
79 |  |
80 | 424 |
             if (qs.size() == 1)
81 |  |
             {
82 | 276 |
                 return qs.elementAt(0);
83 |  |
             }
84 |  |
             else
85 |  |
             {
86 | 148 |
                 return qs;
87 |  |
             }
88 |  |
         }
89 |  |
     }
90 |  |
91 |  |
92 |  |
     /**
93 |  |
      * A quoted string is a vector of strings and macros. When parsing,
94 |  |
      * it begins with a quotation mark and extends until a matching
95 |  |
      * close quotation mark (single or double quotes accepted, the
96 |  |
      * close quote must match the open quote).
97 |  |
      * <p>
98 |  |
      * When parsing, you can use the escape character to protect values
99 |  |
      * that might otherwise be interepreted. The escape character is \
100 |  |
      * <p>
101 |  |
      * It usually contains a series of strings and variables. If you put
102 |  |
      * a Macro into it, the Macro.evaluate()/Macro.write() method will
103 |  |
      * be used to include its contents. If you include a non-Macro,
104 |  |
      * its Object.toString() method will be used instead.
105 |  |
      * <p>
106 |  |
      * Unix users should note that there is no difference between double
107 |  |
      * and single quotes with quoted string. It is more like the quoting
108 |  |
      * used for HTML attributes. Internal variables are alway evaluated.
109 |  |
      * <p>
110 |  |
      * Examples:<pre>
111 |  |
      *
112 |  |
      *    #include 'this is a quoted string with a $variable in it'
113 |  |
      *    #include "use double quotes and you can put don't in it"
114 |  |
      *    #include "use the escape char to write \$10 in a QuotedString"
115 |  |
      *
116 |  |
      * </pre>Here the text inside the quotes is the QuotedString.
117 |  |
      */
118 |  |
     final class QuotedString extends Vector implements Macro, Visitable
119 |  |
     {
120 |  |
       private static final long serialVersionUID = 7578610597935217L;
121 |  |
122 |  |
         /**
123 |  |
          * Create a new quoted string
124 |  |
          */
125 |  |
         QuotedString ()
126 | 424 |
         {
127 | 424 |
         }
128 |  |
129 |  |
         /**
130 |  |
          * Return the value of the quoted string, after substituting all
131 |  |
          * contained variables and removing the quotation marks.
132 |  |
          * @exception PropertyException is required data is missing
133 |  |
          */
134 |  |
         public Object evaluate (Context data)
135 |  |
                 throws PropertyException
136 |  |
         {
137 |  |
             Object o;
138 | 7270 |
             StringBuffer str = new StringBuffer(96);
139 | 18122 |
             for (int i = 0; i < elementCount; i++)
140 |  |
             {
141 | 10852 |
                 o = elementData[i];
142 | 10852 |
                 if (!(o instanceof Macro))
143 |  |
                 {
144 | 5426 |
                     str.append(o.toString());
145 |  |
                 }
146 |  |
                 else
147 |  |
                 {    // should only contain Variables and Strings
148 |  |
                     try
149 |  |
                     {
150 | 5426 |
                         str.append(((Macro) o).evaluate(data));
151 |  |
                     }
152 | 0 |
                     catch (ClassCastException e)
153 |  |
                     {
154 | 0 |
                         throw new PropertyException(
155 |  |
                                 "QuotedString: Expected macro or string, got: " + o);
156 | 5426 |
                     }
157 |  |
                 }
158 |  |
             }
159 | 7270 |
             return str.toString(); // never null, we created it above
160 |  |
         }
161 |  |
162 |  |
163 |  |
         /**
164 |  |
          * Write the quoted string out. Performs the same operation as
165 |  |
          * evaluate(context) but writes it to the stream. Although this is
166 |  |
          * required by the Macro superclass, we don't expect it to be used much
167 |  |
          * since a quoted string does not really appear in a Block (it appears
168 |  |
          * as the argument to a function or directive.)
169 |  |
          * @exception PropertyException is required data is missing
170 |  |
          * @exception IOException if could not write to output stream
171 |  |
          */
172 |  |
         final public void write (FastWriter out, Context data)
173 |  |
                 throws PropertyException, IOException
174 |  |
         {
175 | 0 |
             out.write(evaluate(data).toString()); // evaluate never returns null
176 | 0 |
         }
177 |  |
178 |  |
         public void accept (TemplateVisitor v)
179 |  |
         {
180 | 0 |
             v.beginBlock();
181 | 0 |
             for (int i = 0; i < elementCount; i++)
182 |  |
             {
183 | 0 |
                 Object o = elementData[i];
184 | 0 |
                 if (!(o instanceof Macro))
185 | 0 |
                     v.visitString((String) o);
186 |  |
                 else
187 | 0 |
                     v.visitMacro((Macro) o);
188 |  |
             }
189 | 0 |
             v.endBlock();
190 | 0 |
         }
191 |  |
192 |  |
     }