001/*
002 * (C) Copyright 2015 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.facelets.library;
020
021import javax.faces.FacesException;
022import javax.faces.view.facelets.TagAttributes;
023import javax.faces.view.facelets.TagConfig;
024import javax.faces.view.facelets.TagHandler;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.platform.forms.layout.api.service.LayoutStore;
029import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
030import org.nuxeo.ecm.platform.forms.layout.facelets.WidgetTagHandler;
031import org.nuxeo.ecm.platform.forms.layout.service.WebLayoutManager;
032import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
033import org.nuxeo.runtime.api.Framework;
034
035import com.sun.faces.facelets.tag.AbstractTagLibrary;
036
037/**
038 * Tag library implementation to register tags for registered widget types on the fly.
039 *
040 * @since 8.1
041 */
042public class RuntimeWidgetTagLibrary extends AbstractTagLibrary {
043
044    public final static String Namespace = "http://nuxeo.org/nxforms/runtime/widget";
045
046    public RuntimeWidgetTagLibrary() {
047        this(Namespace);
048    }
049
050    public RuntimeWidgetTagLibrary(String namespace) {
051        super(namespace);
052    }
053
054    @Override
055    public boolean containsTagHandler(String ns, String localName) {
056        if (getNamespace().equals(ns)) {
057            LayoutStore service = Framework.getService(LayoutStore.class);
058            return service.getWidgetDefinition(getWidgetCategory(localName), getWidgetName(localName)) != null;
059        }
060        return false;
061    }
062
063    @Override
064    public TagHandler createTagHandler(String ns, String localName, TagConfig tag) throws FacesException {
065        FaceletHandlerHelper helper = new FaceletHandlerHelper(tag);
066        TagAttributes attributes = tag.getTag().getAttributes();
067        attributes = FaceletHandlerHelper.addTagAttribute(attributes,
068                helper.createAttribute("name", getWidgetName(localName)));
069        attributes = FaceletHandlerHelper.addTagAttribute(attributes,
070                helper.createAttribute("category", getWidgetCategory(localName)));
071        TagConfig config = TagConfigFactory.createTagConfig(tag, tag.getTagId(), attributes, tag.getNextHandler());
072        WidgetTagHandler h = new WidgetTagHandler(config);
073        return h;
074    }
075
076    protected String getWidgetName(String tagLocalName) {
077        // XXX maybe handle category by parsing local name
078        return tagLocalName;
079    }
080
081    protected String getWidgetCategory(String tagLocalName) {
082        // XXX maybe handle category by parsing local name
083        return WebLayoutManager.JSF_CATEGORY;
084    }
085
086}