001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package freemarker.core;
017
018import freemarker.template.ObjectWrapper;
019import freemarker.template.TemplateException;
020import freemarker.template.TemplateModel;
021import freemarker.template.TemplateModelException;
022import java.io.IOException;
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Map;
027import java.util.Map.Entry;
028
029/**
030 * This class was reverse engineer from bytecode for freemarker 2.3.20-patch2 which was a customized
031 * version implemented by the KRAD team. Unfortunately, the source code for this was in svn.kuali.org
032 * which no longer exists. So this class contains the decompiled bytecode for the InlineTemplateUtils
033 * class from that version, udpated to work with the most recent version of Freemarker.
034 */
035public final class InlineTemplateUtils {
036    private InlineTemplateUtils() {
037    }
038
039    public static void invokeMacro(Environment env, Macro macro, Map args, String body) {
040        HashMap wrappedArgs = new HashMap();
041        Iterator argEntryIterator = args.entrySet().iterator();
042
043        while(argEntryIterator.hasNext()) {
044            Entry e = (Entry)argEntryIterator.next();
045
046            try {
047                wrappedArgs.put(e.getKey(), new InlineTemplateUtils.WrappedObjectExpression(e.getValue()));
048            } catch (TemplateModelException var10) {
049                throw new RuntimeException("Error wrapping argument as a FreeMarker model element", var10);
050            }
051        }
052
053        try {
054            env.invoke(macro, wrappedArgs, (List)null, (List)null, body == null?null: new TemplateElement[] { new TextBlock(body) });
055        } catch (TemplateException var8) {
056            throw new RuntimeException("Error invoking macro " + macro.getCanonicalForm(), var8);
057        } catch (IOException var9) {
058            throw new RuntimeException("Error invoking macro " + macro.getCanonicalForm(), var9);
059        }
060    }
061
062    private static class WrappedObjectExpression extends Expression {
063        private final Object wrappedObject;
064        private final TemplateModel model;
065
066        private WrappedObjectExpression(Object wrappedObject) throws TemplateModelException {
067            this.wrappedObject = wrappedObject;
068            if(wrappedObject instanceof TemplateModel) {
069                this.model = (TemplateModel)wrappedObject;
070            } else {
071                this.model = ObjectWrapper.DEFAULT_WRAPPER.wrap(wrappedObject);
072            }
073
074        }
075
076        TemplateModel _eval(Environment env) throws TemplateException {
077            return this.model;
078        }
079
080        boolean isLiteral() {
081            return false;
082        }
083
084        protected Expression deepCloneWithIdentifierReplaced_inner(String replacedIdentifier, Expression replacement, ReplacemenetState replacementState) {
085            try {
086                return new InlineTemplateUtils.WrappedObjectExpression(this.wrappedObject);
087            } catch (TemplateModelException var5) {
088                throw new RuntimeException("Error cloning wrapped object expression", var5);
089            }
090        }
091
092        public String getCanonicalForm() {
093            return "-inline-wrapped-object-expression-" + (this.wrappedObject == null?"null":this.wrappedObject.getClass().getName());
094        }
095
096        String getNodeTypeSymbol() {
097            return "-inline-wrapped-object-expression";
098        }
099
100        int getParameterCount() {
101            return 0;
102        }
103
104        Object getParameterValue(int idx) {
105            return null;
106        }
107
108        ParameterRole getParameterRole(int idx) {
109            return null;
110        }
111    }
112}