1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.update;
22  
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.efaps.update.schema.program.BPMUpdate;
29  import org.efaps.update.schema.program.CSSUpdate;
30  import org.efaps.update.schema.program.JasperReportUpdate;
31  import org.efaps.update.schema.program.JavaScriptUpdate;
32  import org.efaps.update.schema.program.JavaUpdate;
33  import org.efaps.update.schema.program.WikiUpdate;
34  import org.efaps.update.schema.program.XSLUpdate;
35  
36  /**
37   * Main File type definition for the import and update of files.
38   *
39   * @author The eFaps Team
40   * @version $Id$
41   */
42  public enum FileType {
43  
44      /** Java Source file. */
45      BPM("bpmn", "bpmn2"),
46      /** Java Source file. */
47      JAVA("source-java", "java"),
48      /** Java Script file. */
49      JS("source-js", "js"),
50      /** JasperReport Source file. */
51      JRXML("jasperreport", "jrxml"),
52      /** CSS Source file. */
53      CSS("source-css", "css"),
54      /** Wiki Source file. */
55      WIKI("source-wiki", "wiki"),
56      /** XML Source file. */
57      XML("install-xml", "xml"),
58      /** XSL Source file. */
59      XSL("source-xsl", "xsl", "xslt");
60  
61      /**
62       * Class used for mapping.
63       */
64      private static final class Mapper
65      {
66          /**
67           * Mapping between an extensions and the related file type.
68           */
69          private static final Map<String, FileType> EXT2FILETYPE = new HashMap<String, FileType>();
70  
71          /**
72           * Mapping between a type and the related file type.
73           */
74          private static final Map<String, FileType> TYPE2FILETYPE = new HashMap<String, FileType>();
75  
76          /**
77           * hidden Constructor.
78           */
79          private Mapper()
80          {
81          }
82      }
83  
84      /**
85       * Internal type of the file (used e.g. from the eFaps Maven installer).
86       */
87      private final String type;
88  
89  
90      /**
91       * All extensions of this file type.
92       */
93      private final Set<String> extensions = new HashSet<String>();
94  
95      /**
96       * Set of all update classes.
97       */
98      private final Set<Class<? extends AbstractUpdate>> clazzes = new HashSet<Class<? extends AbstractUpdate>>();
99  
100 
101     /**
102      * File type enum constructor.
103      *
104      * @param _type file type
105      * @param _extensions extensions of the file type
106      */
107     private FileType(final String _type,
108                      final String... _extensions)
109     {
110         this.type = _type;
111         for (final String extension : _extensions) {
112             this.extensions.add(extension);
113             FileType.Mapper.EXT2FILETYPE.put(extension, this);
114         }
115         FileType.Mapper.TYPE2FILETYPE.put(_type, this);
116         if ("source-java".equals(_type)) {
117             this.clazzes.add(JavaUpdate.class);
118         } else if ("source-js".equals(_type)) {
119             this.clazzes.add(JavaScriptUpdate.class);
120         } else if ("source-css".equals(_type)) {
121             this.clazzes.add(CSSUpdate.class);
122         } else if ("source-xsl".equals(_type)) {
123             this.clazzes.add(XSLUpdate.class);
124         } else if ("jasperreport".equals(_type)) {
125             this.clazzes.add(JasperReportUpdate.class);
126         } else if ("source-wiki".equals(_type)) {
127             this.clazzes.add(WikiUpdate.class);
128         } else if ("bpmn".equals(_type)) {
129             this.clazzes.add(BPMUpdate.class);
130         }
131 
132     }
133 
134     /**
135      * Getter method for the instance variable {@link #type}.
136      *
137      * @return value of instance variable {@link #type}
138      */
139     public String getType()
140     {
141         return this.type;
142     }
143 
144     /**
145      * Getter method for the instance variable {@link #clazzes}.
146      *
147      * @return value of instance variable {@link #clazzes}
148      */
149     public Set<Class<? extends AbstractUpdate>> getClazzes()
150     {
151         return this.clazzes;
152     }
153 
154     /**
155      * Depending on the extension the file type is returned.
156      *
157      * @param _extension extension for which the file type is searched
158      * @return file type instance for given extension
159      */
160     public static FileType getFileTypeByExensione(final String _extension)
161     {
162         return FileType.Mapper.EXT2FILETYPE.get(_extension);
163     }
164 
165     /**
166      * Depending on the type the file type is returned.
167      *
168      * @param _type type for which the file type is searched
169      * @return file type instance for given type
170      */
171     public static FileType getFileTypeByType(final String _type)
172     {
173         return FileType.Mapper.TYPE2FILETYPE.get(_type);
174     }
175 }