001/*
002 * (C) Copyright 2006-2013 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: TagConfigFactory.java 28460 2008-01-03 15:34:05Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.tag.handler;
023
024import javax.faces.view.facelets.ComponentConfig;
025import javax.faces.view.facelets.ConverterConfig;
026import javax.faces.view.facelets.FaceletHandler;
027import javax.faces.view.facelets.Tag;
028import javax.faces.view.facelets.TagAttribute;
029import javax.faces.view.facelets.TagAttributes;
030import javax.faces.view.facelets.TagConfig;
031import javax.faces.view.facelets.ValidatorConfig;
032
033import org.nuxeo.ecm.platform.ui.web.binding.alias.UIAliasHolder;
034
035import com.sun.faces.facelets.tag.TagAttributeImpl;
036import com.sun.faces.facelets.tag.TagAttributesImpl;
037
038/**
039 * Helper for generating configs outside of a library context.
040 *
041 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
042 */
043public final class TagConfigFactory {
044
045    private TagConfigFactory() {
046    }
047
048    private static class TagConfigWrapper implements TagConfig {
049
050        protected final Tag tag;
051
052        protected final String tagId;
053
054        protected final FaceletHandler nextHandler;
055
056        TagConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes, FaceletHandler nextHandler) {
057            tag = new Tag(tagConfig.getTag(), getOrCreateTagAttributes(attributes));
058            if (tagConfigId == null) {
059                tagId = tagConfig.getTagId();
060            } else {
061                tagId = tagConfig.getTagId() + tagConfigId;
062            }
063            this.nextHandler = nextHandler;
064        }
065
066        @Override
067        public FaceletHandler getNextHandler() {
068            return nextHandler;
069        }
070
071        @Override
072        public Tag getTag() {
073            return tag;
074        }
075
076        @Override
077        public String getTagId() {
078            return tagId;
079        }
080    }
081
082    private static class ComponentConfigWrapper extends TagConfigWrapper implements ComponentConfig {
083
084        protected final String componentType;
085
086        protected final String rendererType;
087
088        ComponentConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
089                FaceletHandler nextHandler, String componentType, String rendererType) {
090            super(tagConfig, tagConfigId, getOrCreateTagAttributes(attributes), nextHandler);
091            this.componentType = componentType;
092            this.rendererType = rendererType;
093        }
094
095        @Override
096        public String getComponentType() {
097            return componentType;
098        }
099
100        @Override
101        public String getRendererType() {
102            return rendererType;
103        }
104    }
105
106    private static class ConverterConfigWrapper extends TagConfigWrapper implements ConverterConfig {
107
108        protected final String converterId;
109
110        ConverterConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
111                FaceletHandler nextHandler, String converterId) {
112            super(tagConfig, tagConfigId, getOrCreateTagAttributes(attributes), nextHandler);
113            this.converterId = converterId;
114        }
115
116        @Override
117        public String getConverterId() {
118            return converterId;
119        }
120    }
121
122    private static class ValidatorConfigWrapper extends TagConfigWrapper implements ValidatorConfig {
123
124        protected final String validatorId;
125
126        ValidatorConfigWrapper(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
127                FaceletHandler nextHandler, String validatorId) {
128            super(tagConfig, tagConfigId, getOrCreateTagAttributes(attributes), nextHandler);
129            this.validatorId = validatorId;
130        }
131
132        @Override
133        public String getValidatorId() {
134            return validatorId;
135        }
136    }
137
138    public static TagConfig createTagConfig(TagConfig tagConfig, String tagConfigId, TagAttributes attributes,
139            FaceletHandler nextHandler) {
140        return new TagConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler);
141    }
142
143    public static ComponentConfig createComponentConfig(TagConfig tagConfig, String tagConfigId,
144            TagAttributes attributes, FaceletHandler nextHandler, String componentType, String rendererType) {
145        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, componentType, rendererType);
146    }
147
148    public static ConverterConfig createConverterConfig(TagConfig tagConfig, String tagConfigId,
149            TagAttributes attributes, FaceletHandler nextHandler, String converterId) {
150        return new ConverterConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, converterId);
151    }
152
153    public static ValidatorConfig createValidatorConfig(TagConfig tagConfig, String tagConfigId,
154            TagAttributes attributes, FaceletHandler nextHandler, String validatorId) {
155        return new ValidatorConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler, validatorId);
156    }
157
158    /**
159     * @since 6.0
160     */
161    public static ComponentConfig createAliasTagConfig(TagConfig tagConfig, String tagConfigId,
162            TagAttributes attributes, FaceletHandler nextHandler) {
163        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler,
164                UIAliasHolder.COMPONENT_TYPE, null);
165    }
166
167    /**
168     * @since 6.0
169     */
170    public static ComponentConfig createAliasTagConfig(TagConfig tagConfig, String tagConfigId, String var,
171            String value, String cache, String anchor, FaceletHandler nextHandler) {
172        TagAttribute[] attrs = new TagAttribute[4];
173        attrs[0] = createAttribute(tagConfig, "var", var);
174        attrs[1] = createAttribute(tagConfig, "value", value);
175        attrs[2] = createAttribute(tagConfig, "cache", cache);
176        attrs[3] = createAttribute(tagConfig, "anchor", anchor);
177        TagAttributes attributes = new TagAttributesImpl(attrs);
178        return new ComponentConfigWrapper(tagConfig, tagConfigId, attributes, nextHandler,
179                UIAliasHolder.COMPONENT_TYPE, null);
180    }
181
182    protected static TagAttribute createAttribute(TagConfig tagConfig, String name, String value) {
183        return new TagAttributeImpl(tagConfig.getTag().getLocation(), "", name, name, value);
184    }
185
186    protected static TagAttributes getOrCreateTagAttributes(TagAttributes attributes) {
187        if (attributes == null) {
188            return new TagAttributesImpl(new TagAttribute[0]);
189        }
190        return attributes;
191    }
192
193}