001/*
002 * (C) Copyright 2006-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id: TagConfigFactory.java 28460 2008-01-03 15:34:05Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.tag.handler;
021
022import javax.faces.view.facelets.ComponentConfig;
023import javax.faces.view.facelets.ConverterConfig;
024import javax.faces.view.facelets.FaceletHandler;
025import javax.faces.view.facelets.Tag;
026import javax.faces.view.facelets.TagAttribute;
027import javax.faces.view.facelets.TagAttributes;
028import javax.faces.view.facelets.TagConfig;
029import javax.faces.view.facelets.ValidatorConfig;
030
031import org.nuxeo.ecm.platform.ui.web.binding.alias.UIAliasHolder;
032
033import com.sun.faces.facelets.tag.TagAttributeImpl;
034import com.sun.faces.facelets.tag.TagAttributesImpl;
035
036/**
037 * Helper for generating configs outside of a library context.
038 *
039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
040 */
041public final class TagConfigFactory {
042
043    private TagConfigFactory() {
044    }
045
046    private static class TagConfigWrapper implements TagConfig {
047
048        protected final Tag tag;
049
050        protected final String tagId;
051
052        protected final FaceletHandler nextHandler;
053
054        TagConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes, FaceletHandler nextHandler) {
055            tag = new Tag(tagConfig.getTag(), attributes);
056            if (tagConfigId == null) {
057                tagId = tagConfig.getTagId();
058            } else {
059                tagId = tagConfig.getTagId() + tagConfigId;
060            }
061            this.nextHandler = nextHandler;
062        }
063
064        @Override
065        public FaceletHandler getNextHandler() {
066            return nextHandler;
067        }
068
069        @Override
070        public Tag getTag() {
071            return tag;
072        }
073
074        @Override
075        public String getTagId() {
076            return tagId;
077        }
078    }
079
080    private static class ComponentConfigWrapper extends TagConfigWrapper implements ComponentConfig {
081
082        protected final String componentType;
083
084        protected final String rendererType;
085
086        ComponentConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
087                FaceletHandler nextHandler, String componentType, String rendererType) {
088            super(tagConfig, tagConfigId, attributes, nextHandler);
089            this.componentType = componentType;
090            this.rendererType = rendererType;
091        }
092
093        @Override
094        public String getComponentType() {
095            return componentType;
096        }
097
098        @Override
099        public String getRendererType() {
100            return rendererType;
101        }
102    }
103
104    private static class ConverterConfigWrapper extends TagConfigWrapper implements ConverterConfig {
105
106        protected final String converterId;
107
108        ConverterConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
109                FaceletHandler nextHandler, String converterId) {
110            super(tagConfig, tagConfigId, attributes, nextHandler);
111            this.converterId = converterId;
112        }
113
114        @Override
115        public String getConverterId() {
116            return converterId;
117        }
118    }
119
120    private static class ValidatorConfigWrapper extends TagConfigWrapper implements ValidatorConfig {
121
122        protected final String validatorId;
123
124        ValidatorConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
125                FaceletHandler nextHandler, String validatorId) {
126            super(tagConfig, tagConfigId, attributes, nextHandler);
127            this.validatorId = validatorId;
128        }
129
130        @Override
131        public String getValidatorId() {
132            return validatorId;
133        }
134    }
135
136    public static TagConfig createTagConfig(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
137            FaceletHandler nextHandler) {
138        return new TagConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler);
139    }
140
141    public static ComponentConfig createComponentConfig(TagConfig tagConfig, String tagConfigId,
142            TagAttributes attributes, FaceletHandler nextHandler, String componentType, String rendererType) {
143        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, componentType, rendererType);
144    }
145
146    public static ConverterConfig createConverterConfig(TagConfig tagConfig, String tagConfigId,
147            TagAttributes attributes, FaceletHandler nextHandler, String converterId) {
148        return new ConverterConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, converterId);
149    }
150
151    public static ValidatorConfig createValidatorConfig(TagConfig tagConfig, String tagConfigId,
152            TagAttributes attributes, FaceletHandler nextHandler, String validatorId) {
153        return new ValidatorConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, validatorId);
154    }
155
156    /**
157     * @since 6.0
158     */
159    public static ComponentConfig createAliasTagConfig(TagConfig tagConfig, String tagConfigId,
160            TagAttributes attributes, FaceletHandler nextHandler) {
161        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler,
162                UIAliasHolder.COMPONENT_TYPE, null);
163    }
164
165    /**
166     * @since 6.0
167     */
168    public static ComponentConfig createAliasTagConfig(TagConfig tagConfig, String tagConfigId, String var,
169            String value, String cache, String anchor, FaceletHandler nextHandler) {
170        TagAttribute[] attrs = new TagAttribute[4];
171        attrs[0] = createAttribute(tagConfig, "var", var);
172        attrs[1] = createAttribute(tagConfig, "value", value);
173        attrs[2] = createAttribute(tagConfig, "cache", cache);
174        attrs[3] = createAttribute(tagConfig, "anchor", anchor);
175        TagAttributes attributes = new TagAttributesImpl(attrs);
176        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler,
177                UIAliasHolder.COMPONENT_TYPE, null);
178    }
179
180    protected static TagAttribute createAttribute(TagConfig tagConfig, String name, String value) {
181        return new TagAttributeImpl(tagConfig.getTag().getLocation(), "", name, name, value);
182    }
183
184}