001/* 
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.platform.rendering.fm.extensions;
016
017import java.util.List;
018
019import org.nuxeo.ecm.core.api.IdRef;
020import org.nuxeo.ecm.core.api.PathRef;
021
022import freemarker.template.SimpleScalar;
023import freemarker.template.TemplateMethodModelEx;
024import freemarker.template.TemplateModelException;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class DocRefMethod implements TemplateMethodModelEx {
030
031    public Object exec(List arguments) throws TemplateModelException {
032        if (arguments.size() != 1) {
033            throw new TemplateModelException("Invalid number of arguments for docRef(id) method");
034        }
035        String value = null;
036        SimpleScalar scalar = (SimpleScalar) arguments.get(0);
037        if (scalar != null) {
038            value = scalar.getAsString();
039        } else {
040            throw new TemplateModelException("the argument is not defined");
041        }
042
043        if (value.startsWith("/")) {
044            return new PathRef(value);
045        } else {
046            return new IdRef(value);
047        }
048    }
049
050}