001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.rendering;
023
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.webengine.WebEngine;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.model.WebContext;
031
032import freemarker.template.AdapterTemplateModel;
033import freemarker.template.SimpleScalar;
034import freemarker.template.TemplateCollectionModel;
035import freemarker.template.TemplateHashModelEx;
036import freemarker.template.TemplateMethodModelEx;
037import freemarker.template.TemplateModel;
038import freemarker.template.TemplateModelException;
039import freemarker.template.TemplateModelIterator;
040
041/**
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class ScriptMethod implements TemplateMethodModelEx {
045
046    public Object exec(List arguments) throws TemplateModelException {
047        int size = arguments.size();
048        if (size < 1) {
049            throw new TemplateModelException("Invalid number of arguments for script(...) method");
050        }
051
052        SimpleScalar val = (SimpleScalar) arguments.get(0);
053        if (val == null) {
054            throw new TemplateModelException("src attribute is required");
055        }
056        String src = val.getAsString();
057
058        Map<String, Object> args = new HashMap<String, Object>();
059        if (arguments.size() > 1) {
060            Object o = arguments.get(1);
061            if (o instanceof SimpleScalar) {
062                String arg = ((SimpleScalar) o).getAsString();
063                args.put("_args", new String[] { arg });
064            } else if (!(o instanceof TemplateHashModelEx)) {
065                throw new TemplateModelException("second argument should be a map");
066            } else {
067                TemplateHashModelEx t = (TemplateHashModelEx) o;
068                TemplateCollectionModel keys = t.keys();
069                TemplateModelIterator it = keys.iterator();
070
071                while (it.hasNext()) {
072                    TemplateModel k = it.next();
073                    String kk = k.toString();
074                    TemplateModel v = t.get(kk);
075                    Object vv = null;
076                    if (v instanceof AdapterTemplateModel) {
077                        vv = ((AdapterTemplateModel) v).getAdaptedObject(null);
078                    } else {
079                        vv = v.toString();
080                    }
081                    args.put(kk, vv);
082                }
083            }
084        }
085
086        WebContext ctx = WebEngine.getActiveContext();
087        if (ctx != null) {
088            try {
089                return ctx.runScript(src, args);
090            } catch (WebException e) {
091                throw new TemplateModelException("Failed to run script: " + src, e);
092            }
093        } else {
094            throw new IllegalStateException("Not In a Web Context");
095        }
096    }
097
098}