View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content.transformer;
20  
21  import java.util.HashMap;
22  
23  import org.mycore.common.config.MCRConfiguration2;
24  
25  /**
26   * Creates and returns MCRContentTransformer instances by their ID.
27   * 
28   * @author Frank L\u00FCtzenkirchen
29   */
30  public class MCRContentTransformerFactory {
31  
32      /** Map of transformer instances by ID */
33      private static HashMap<String, MCRContentTransformer> transformers = new HashMap<>();
34  
35      /** 
36       * Returns the transformer with the given ID. If the transformer is not instantiated yet,
37       * it is created and initialized.
38       */
39      public static MCRContentTransformer getTransformer(String id) {
40          if (transformers.containsKey(id)) {
41              return transformers.get(id);
42          } else {
43              return buildTransformer(id);
44          }
45      }
46  
47      /**
48       * Creates and initializes the transformer with the given ID.
49       */
50      private static synchronized MCRContentTransformer buildTransformer(String id) {
51          String property = "MCR.ContentTransformer." + id + ".Class";
52          if (MCRConfiguration2.getString(property).isEmpty()
53              && MCRConfiguration2.getString("MCR.ContentTransformer." + id + ".Stylesheet").isEmpty()) {
54              //check for reasonable default:
55              return null;
56          }
57          MCRContentTransformer transformer = MCRConfiguration2.<MCRContentTransformer>getInstanceOf(property)
58              .orElseGet(MCRXSLTransformer::new);
59          transformer.init(id);
60          transformers.put(id, transformer);
61          return transformer;
62      }
63  }