aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java
blob: 63dc92b9a2fbda1b15e0f2296cb002ea859e99eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package org.apache.velocity.runtime;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.velocity.Template;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.directive.Macro;
import org.apache.velocity.runtime.directive.VelocimacroProxy;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Manages VMs in namespaces.  Currently, two namespace modes are
 * supported:
 *
 * <ul>
 * <li>flat - all allowable VMs are in the global namespace</li>
 * <li>local - inline VMs are added to it's own template namespace</li>
 * </ul>
 *
 * Thanks to <a href="mailto:JFernandez@viquity.com">Jose Alberto Fernandez</a>
 * for some ideas incorporated here.
 *
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
 * @author <a href="mailto:JFernandez@viquity.com">Jose Alberto Fernandez</a>
 * @version $Id$
 */
public class VelocimacroManager
{
    private boolean registerFromLib = false;

    /** reference to global namespace hash */
    private final Map<String, MacroEntry> globalNamespace;

    /** set of names of library templates/namespaces */
    private final Map<String, Template> libraries = new ConcurrentHashMap<>(17, 0.5f, 20);

    private RuntimeServices rsvc = null;

    /*
     * big switch for namespaces.  If true, then properties control
     * usage. If false, no.
     */
    private boolean namespacesOn = true;
    private boolean inlineLocalMode = false;
    private boolean inlineReplacesGlobal = false;

    /**
     * Adds the global namespace to the hash.
     */
    VelocimacroManager(RuntimeServices rsvc)
    {
        /*
         *  add the global namespace to the namespace hash. We always have that.
         */

        globalNamespace = new ConcurrentHashMap<>(101, 0.5f, 20);
        this.rsvc = rsvc;
    }

    /**
     * Adds a VM definition to the cache.
     *
     * Called by VelocimacroFactory.addVelociMacro (after parsing and discovery in Macro directive)
     *
     * @param vmName Name of the new VelociMacro.
     * @param macroBody String representation of the macro body.
     * @param macroArgs  Array of macro arguments, containing the
     *        #macro() arguments and default values.  the 0th is the name.
     * @param definingTemplate The template from which this macro has been loaded.
     * @param canReplaceGlobalMacro whether this macro can replace a global macro
     * @return Whether everything went okay.
     */
    public boolean addVM(final String vmName, final Node macroBody, List<Macro.MacroArg> macroArgs,
                         final Template definingTemplate, boolean canReplaceGlobalMacro)
    {
        if (macroBody == null)
        {
            // happens only if someone uses this class without the Macro directive
            // and provides a null value as an argument
            throw new VelocityException("Null AST for "+vmName+" in " + definingTemplate.getName());
        }

        MacroEntry me = new MacroEntry(vmName, macroBody, macroArgs, definingTemplate.getName(), rsvc);

        me.setFromLibrary(registerFromLib);

        /*
         *  the client (VMFactory) will signal to us via
         *  registerFromLib that we are in startup mode registering
         *  new VMs from libraries.  Therefore, we want to
         *  addto the library map for subsequent auto reloads
         */

        boolean isLib = true;

        MacroEntry exist = globalNamespace.get(vmName);

        if (registerFromLib)
        {
           libraries.put(definingTemplate.getName(), definingTemplate);
        }
        else
        {
            /*
             *  now, we first want to check to see if this namespace (template)
             *  is actually a library - if so, we need to use the global namespace
             *  we don't have to do this when registering, as namespaces should
             *  be shut off. If not, the default value is true, so we still go
             *  global
             */

            isLib = libraries.containsKey(definingTemplate.getName());
        }

        if ( !isLib && usingNamespaces() )
        {
            definingTemplate.getMacros().put(vmName, me);
        }
        else
        {
            /*
             *  otherwise, add to global template.  First, check if we
             *  already have it to preserve some of the autoload information
             */


            if (exist != null)
            {
                me.setFromLibrary(exist.getFromLibrary());
            }

            /*
             *  now add it
             */

            globalNamespace.put(vmName, me);

        }
        return true;
    }

    /**
     * Gets a VelocimacroProxy object by the name / source template duple.
     *
     * @param vmName Name of the VelocityMacro to look up.
     * @param renderingTemplate Template we are currently rendering.
     * @param template Source Template.
     * @return A proxy representing the Macro.
     */
    public VelocimacroProxy get(final String vmName, final Template renderingTemplate, final Template template)
    {
        if( inlineReplacesGlobal && renderingTemplate != null )
        {
            /*
             * if VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL is true (local macros can
             * override global macros) and we know which template we are rendering at the
             * moment, check if local namespace contains a macro we are looking for
             * if so, return it instead of the global one
             */

            MacroEntry me = (MacroEntry)renderingTemplate.getMacros().get(vmName);
            if( me != null )
            {
                return me.getProxy();
            }
        }

        if( usingNamespaces() && template != null )
        {
            MacroEntry me = (MacroEntry)template.getMacros().get(vmName);
            if( template.getMacros().size() > 0 && me != null )
            {
                return me.getProxy();
            }
        }

        MacroEntry me = globalNamespace.get(vmName);

        if (me != null)
        {
            return me.getProxy();
        }

        return null;
    }

    /**
     *  public switch to let external user of manager to control namespace
     *  usage indep of properties.  That way, for example, at startup the
     *  library files are loaded into global namespace
     *
     * @param namespaceOn True if namespaces should be used.
     */
    public void setNamespaceUsage(final boolean namespaceOn)
    {
        this.namespacesOn = namespaceOn;
    }

    /**
     * Should macros registered from Libraries be marked special?
     * @param registerFromLib True if macros from Libs should be marked.
     */
    public void setRegisterFromLib(final boolean registerFromLib)
    {
        this.registerFromLib = registerFromLib;
    }

    /**
     * Should macros from the same template be inlined?
     *
     * @param inlineLocalMode True if macros should be inlined on the same template.
     */
    public void setTemplateLocalInlineVM(final boolean inlineLocalMode)
    {
        this.inlineLocalMode = inlineLocalMode;
    }

    /**
     *  determines if currently using namespaces.
     *
     *  @return true if using namespaces, false if not
     */
    private boolean usingNamespaces()
    {
        /*
         *  if the big switch turns of namespaces, then ignore the rules
         */

        if (!namespacesOn)
        {
            return false;
        }

        /*
         *  currently, we only support the local template namespace idea
         */

        return inlineLocalMode;

    }

    /**
     * Return the library name for a given macro.
     * @param vmName Name of the Macro to look up.
     * @param template Template
     * @return The name of the library which registered this macro in a namespace.
     */
    public String getLibraryName(final String vmName, Template template)
    {
        if (usingNamespaces())
        {
            /*
             *  if we have this macro defined in this namespace, then
             *  it is masking the global, library-based one, so
             *  just return null
             */
            MacroEntry me = (MacroEntry)template.getMacros().get(vmName);
            if( me != null )
                return null;
        }

        MacroEntry me = globalNamespace.get(vmName);

        if (me != null)
        {
            return me.getSourceTemplate();
        }

        return null;
    }

    /**
     * @param is
     * @since 1.6
     */
    public void setInlineReplacesGlobal(boolean is)
    {
        inlineReplacesGlobal = is;
    }


    /**
     *  wrapper class for holding VM information
     */
    private static class MacroEntry
    {
        private final String sourceTemplate;
        private boolean fromLibrary = false;
        private VelocimacroProxy vp;

        private MacroEntry(final String vmName, final Node macro,
                   List<Macro.MacroArg> macroArgs, final String sourceTemplate,
                   RuntimeServices rsvc)
        {
            this.sourceTemplate = sourceTemplate;

            vp = new VelocimacroProxy();
            vp.init(rsvc);
            vp.setName(vmName);
            vp.setMacroArgs(macroArgs);
            vp.setNodeTree((SimpleNode)macro);
            vp.setLocation(macro.getLine(), macro.getColumn(), macro.getTemplate());
        }

        /**
         * Has the macro been registered from a library.
         * @param fromLibrary True if the macro was registered from a Library.
         */
        public void setFromLibrary(final boolean fromLibrary)
        {
            this.fromLibrary = fromLibrary;
        }

        /**
         * Returns true if the macro was registered from a library.
         * @return True if the macro was registered from a library.
         */
        public boolean getFromLibrary()
        {
            return fromLibrary;
        }

        public String getSourceTemplate()
        {
            return sourceTemplate;
        }

        VelocimacroProxy getProxy()
        {
            return vp;
        }
    }
}