001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.formats;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.theme.Manager;
030import org.nuxeo.theme.rendering.Filter;
031import org.nuxeo.theme.rendering.FilterType;
032import org.nuxeo.theme.rendering.FilterTypeFamily;
033import org.nuxeo.theme.types.TypeFamily;
034
035@XObject("format-filter")
036public final class FormatFilterType extends FilterType {
037
038    @XNode("@name")
039    public String name;
040
041    @XNode("engine")
042    public String engine = "*";
043
044    @XNode("template-engine")
045    public String templateEngine = "*";
046
047    @XNode("mode")
048    public String mode = "*";
049
050    @XNode("format-type")
051    public String formatName;
052
053    private final Map<String, Filter> filters = new HashMap<String, Filter>();
054
055    public FormatFilterType() {
056    }
057
058    public FormatFilterType(@SuppressWarnings("unused") final String name, final String formatName) {
059        this.formatName = formatName;
060    }
061
062    @Override
063    public Filter getFilter() {
064        if (filters.containsKey(formatName)) {
065            return filters.get(formatName);
066        }
067        FormatType formatType = (FormatType) Manager.getTypeRegistry().lookup(TypeFamily.FORMAT, formatName);
068        FormatFilter filter = new FormatFilter();
069        filter.setFormatType(formatType);
070        filters.put(formatName, filter);
071        return filter;
072    }
073
074    @Override
075    public FilterTypeFamily getFilterTypeFamily() {
076        return FilterTypeFamily.FORMAT;
077    }
078
079    public String getFormatName() {
080        return formatName;
081    }
082
083    public void setFormatName(final String formatName) {
084        this.formatName = formatName;
085    }
086
087    @Override
088    public String getTypeName() {
089        return String.format("%s/%s/%s/%s", engine, templateEngine, mode, name);
090    }
091
092    @Override
093    public String getClassName() {
094        return FormatFilter.class.getName();
095    }
096
097}