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

Classes in this File | Line Coverage | Branch Coverage | Complexity
BuildContext |
37% | 20/53
32% | 11/34
| 3.7777777777777777;3.778

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.util.HashMap;
27 |  |
     import java.util.Map;
28 |  |
29 |  |
     import org.webmacro.Broker;
30 |  |
     import org.webmacro.Context;
31 |  |
     import org.webmacro.Macro;
32 |  |
     import org.webmacro.NotFoundException;
33 |  |
     import org.webmacro.ResourceException;
34 |  |
     import org.webmacro.Template;
35 |  |
36 |  |
37 |  |
     /**
38 |  |
      * Contains data structures which are manipulated during the
39 |  |
      * builder phase of parsing. It extends Map so that
40 |  |
      * user provided directives can store information in it during
41 |  |
      * the builder phase. Although WebMacro's built in directives
42 |  |
      * make no use of this hashtable (they use the other structures
43 |  |
      * added in the derived class), other user provided directives
44 |  |
      * might. Therefore you should adopt a sensible naming scheme for
45 |  |
      * your keys, to avoid conflicting with keys inserted by someone else.
46 |  |
      */
47 |  |
     public class BuildContext extends Context
48 |  |
     {
49 |  |
50 | 224 |
         private final Map _types = new HashMap();
51 | 224 |
         private final Map _macros = new HashMap();
52 |  |
53 |  |
         public BuildContext (Broker b)
54 |  |
         {
55 | 224 |
             super(b);
56 | 224 |
         }
57 |  |
58 |  |
         public final Parser getParser (String pname)
59 |  |
                 throws NotFoundException
60 |  |
         {
61 |  |
             try
62 |  |
             {
63 | 0 |
                 return (Parser) getBroker().get("parser", pname);
64 |  |
             }
65 | 0 |
             catch (NotFoundException e)
66 |  |
             {
67 | 0 |
                 throw e;
68 |  |
             }
69 | 0 |
             catch (ResourceException e)
70 |  |
             {
71 | 0 |
                 throw new NotFoundException(e.toString(), e);
72 |  |
             }
73 |  |
         }
74 |  |
75 |  |
         /**
76 |  |
          * Find out whether the named variable is a tool, local variable,
77 |  |
          * or property variable.
78 |  |
          */
79 |  |
         public Object getVariableType (String name)
80 |  |
         {
81 | 5834 |
             Object ret = _types.get(name);
82 | 5834 |
             return (ret == null) ? Variable.PROPERTY_TYPE : ret;
83 |  |
         }
84 |  |
85 |  |
         /**
86 |  |
          * Declare whether the named variable is to be treated as a tool,
87 |  |
          * local variable, or property variable type.
88 |  |
          */
89 |  |
         public void setVariableType (String name, Object type)
90 |  |
         {
91 | 0 |
             if (name == null)
92 |  |
             {
93 | 0 |
                 return;
94 |  |
             }
95 | 0 |
             if (type == null)
96 |  |
             {
97 | 0 |
                 _types.remove(name);
98 |  |
             }
99 |  |
             else
100 |  |
             {
101 | 0 |
                 _types.put(name, type);
102 |  |
             }
103 | 0 |
         }
104 |  |
105 |  |
         /**
106 |  |
          * Add a MacroDefinition to the build context.
107 |  |
          */
108 |  |
         public void putMacro (String name, MacroDefinition macro)
109 |  |
         {
110 | 0 |
             _macros.put(name, macro);
111 | 0 |
         }
112 |  |
113 |  |
         /**
114 |  |
          * Search for a MacroDefinition in the build context.
115 |  |
          */
116 |  |
         public MacroDefinition getMacro (String name)
117 |  |
         {
118 | 0 |
             return (MacroDefinition) _macros.get(name);
119 |  |
         }
120 |  |
121 |  |
         /**
122 |  |
          * Add #macros and #params from the specified Template to
123 |  |
          * this bulid context.
124 |  |
          */
125 |  |
         public void mergeConstants (Template t)
126 |  |
         {
127 | 0 |
             Map macros = t.getMacros();
128 | 0 |
             Map params = t.getParameters();
129 | 0 |
             if (macros != null)
130 | 0 |
                 _macros.putAll(macros);
131 | 0 |
             if (params != null)
132 | 0 |
                 super.putAll(params);
133 | 0 |
         }
134 |  |
135 |  |
         /**
136 |  |
          * Return the map of MacroDefinitions.
137 |  |
          */
138 |  |
         public Map getMacros ()
139 |  |
         {
140 | 224 |
             return _macros;
141 |  |
         }
142 |  |
143 |  |
         /**
144 |  |
          * Create a variable (or resolve a constant at build time).
145 |  |
          * Used by various build() routines.
146 |  |
          */
147 |  |
         Object resolveVariableReference (Object names[])
148 |  |
                 throws BuildException
149 |  |
         {
150 | 5834 |
             Object v = null;
151 |  |
152 | 5834 |
             if (names.length < 1)
153 | 0 |
                 throw new BuildException("Variable with name of length zero!");
154 |  |
155 | 5834 |
             Object c[] = new Object[names.length];
156 | 16818 |
             for (int i = 0; i < c.length; i++)
157 |  |
             {
158 | 10984 |
                 c[i] = (names[i] instanceof Builder) ?
159 |  |
                         ((Builder) names[i]).build(this) : names[i];
160 |  |
             }
161 | 5834 |
             String firstName = c[0].toString();
162 | 5834 |
             Object type = null;
163 | 5834 |
             if (c[0] instanceof FunctionCall)
164 |  |
             {
165 | 0 |
                 type = FunctionVariable.TYPE;
166 |  |
             }
167 |  |
             else
168 |  |
             {
169 | 5834 |
                 type = getVariableType(firstName);
170 |  |
             }
171 | 5834 |
             if (type == Variable.PROPERTY_TYPE)
172 |  |
             {
173 | 5834 |
                 if (containsKey(firstName))
174 |  |
                 {
175 | 0 |
                     Object expansion = get(firstName);
176 | 0 |
                     if (expansion instanceof Macro)
177 |  |
                     {
178 | 0 |
                         v = (c.length == 1) ? expansion
179 |  |
                                 : new MacroPropertyVariable((Macro) expansion, c);
180 |  |
                     }
181 |  |
                     else
182 |  |
                     {
183 | 0 |
                         v = (c.length == 1) ? expansion
184 |  |
                                 : new ConstantPropertyVariable(expansion, c);
185 |  |
                     }
186 | 0 |
                 }
187 |  |
                 else
188 |  |
                 {
189 | 5834 |
                     v = (c.length == 1)
190 |  |
                             ? (Object) new SimplePropertyVariable(c)
191 |  |
                             : (Object) new PropertyVariable(c);
192 |  |
                 }
193 |  |
             }
194 | 0 |
             else if (type == Variable.LOCAL_TYPE)
195 |  |
             {
196 | 0 |
                 v = new GlobalVariable(c);
197 |  |
             }
198 | 0 |
             else if (type == FunctionVariable.TYPE)
199 |  |
             {
200 | 0 |
                 v = new FunctionVariable(c);
201 |  |
             }
202 |  |
             else
203 |  |
             {
204 | 0 |
                 throw new BuildException("Unrecognized Variable Type: " + type);
205 |  |
             }
206 | 5834 |
             return v;
207 |  |
         }
208 |  |
209 |  |
     }
210 |  |
211 |  |