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