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