001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.api.descriptor;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeList;
029import org.nuxeo.common.xmap.annotation.XObject;
030import org.nuxeo.template.api.TemplateProcessor;
031
032@XObject("templateProcessor")
033public class TemplateProcessorDescriptor implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    protected static final Log log = LogFactory.getLog(TemplateProcessorDescriptor.class);
038
039    protected TemplateProcessor processor;
040
041    @XNode("@name")
042    protected String name;
043
044    @XNode("@label")
045    protected String label;
046
047    @SuppressWarnings("rawtypes")
048    @XNode("@class")
049    protected Class className;
050
051    @XNode("@default")
052    protected boolean defaultProcessor = true;
053
054    @XNode("@enabled")
055    protected boolean enabled = true;
056
057    @XNodeList(value = "supportedMimeType", type = ArrayList.class, componentType = String.class)
058    protected List<String> supportedMimeTypes = new ArrayList<String>();
059
060    @XNodeList(value = "supportedExtension", type = ArrayList.class, componentType = String.class)
061    protected List<String> supportedExtensions = new ArrayList<String>();
062
063    public boolean init() {
064        if (getProcessor() == null) {
065            return false;
066        }
067        return true;
068    }
069
070    public TemplateProcessor getProcessor() {
071        if (processor == null) {
072            try {
073                processor = (TemplateProcessor) className.newInstance();
074            } catch (ReflectiveOperationException e) {
075                log.error("Unable to instanciate Processor", e);
076            }
077        }
078        return processor;
079    }
080
081    public String getName() {
082        return name;
083    }
084
085    public String getLabel() {
086        return label;
087    }
088
089    @SuppressWarnings("rawtypes")
090    public Class getClassName() {
091        return className;
092    }
093
094    public List<String> getSupportedMimeTypes() {
095        return supportedMimeTypes;
096    }
097
098    public List<String> getSupportedExtensions() {
099        return supportedExtensions;
100    }
101
102    public boolean isDefaultProcessor() {
103        return defaultProcessor;
104    }
105
106    public boolean isEnabled() {
107        return enabled;
108    }
109
110    public TemplateProcessorDescriptor clone() {
111
112        TemplateProcessorDescriptor clone = new TemplateProcessorDescriptor();
113        clone.enabled = enabled;
114        clone.supportedExtensions = supportedExtensions;
115        clone.supportedMimeTypes = supportedMimeTypes;
116        clone.className = className;
117        clone.processor = processor;
118        clone.defaultProcessor = defaultProcessor;
119        clone.label = label;
120        clone.name = name;
121
122        return clone;
123    }
124
125    public void merge(TemplateProcessorDescriptor srcTpd) {
126        defaultProcessor = srcTpd.defaultProcessor;
127        if (srcTpd.className != null) {
128            className = srcTpd.className;
129        }
130        if (srcTpd.label != null) {
131            label = srcTpd.label;
132        }
133        if (srcTpd.supportedExtensions != null && srcTpd.supportedExtensions.size() > 0) {
134            supportedExtensions = srcTpd.supportedExtensions;
135        }
136        if (srcTpd.supportedMimeTypes != null && srcTpd.supportedMimeTypes.size() > 0) {
137            supportedMimeTypes = srcTpd.supportedMimeTypes;
138        }
139    }
140}