View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
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   */
17  package com.sun.syndication.io.impl;
18  
19  import com.sun.syndication.feed.module.ModuleI;
20  import com.sun.syndication.io.ModuleGenerator;
21  import org.jdom.Element;
22  import org.jdom.Namespace;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  /***
32   */
33  public class ModulesGenerator {
34      private Map _feedModuleGenerators = new HashMap();
35      private Map _itemModuleGenerators = new HashMap();
36  
37      private void loadGenerators(String defaultFile,String fileProperty,String key,Map map) {
38          PlugableClasses pClasses = new PlugableClasses(defaultFile,fileProperty,
39                                                          key,true,ModulesGenerator.class.getClassLoader());
40          Object[] generators;
41          try {
42              generators = pClasses.createInstances();
43          }
44          catch (Exception ex) {
45              throw new RuntimeException(ex);
46          }
47          for (int i=0;i<generators.length;i++) {
48              ModuleGenerator generator = (ModuleGenerator) generators[i];
49              map.put(generator.getNamespaceUri(),generator);
50          }
51      }
52  
53      public ModulesGenerator() {
54          this(Constants.DEFAULT_IMPLS_FILE,Constants.IMPLS_SYSTEM_PROPERTY);
55      }
56  
57      public ModulesGenerator(String defaultFile,String fileProperty) {
58          loadGenerators(defaultFile,fileProperty,Constants.FEED_MODULE_GENERATORS_KEY,_feedModuleGenerators);
59          loadGenerators(defaultFile,fileProperty,Constants.ITEM_MODULE_GENERATORS_KEY,_itemModuleGenerators);
60      }
61  
62      public List getFeedModuleNamespaces() {
63          return Collections.unmodifiableList(new ArrayList(_feedModuleGenerators.keySet()));
64      }
65  
66      public List getItemModuleNamespaces() {
67          return Collections.unmodifiableList(new ArrayList(_itemModuleGenerators.keySet()));
68      }
69  
70      public void generateFeedModules(List modules, Element element) {
71          generateModules(_feedModuleGenerators, modules, element);
72      }
73  
74      public void generateItemModules(List modules, Element element) {
75          generateModules(_itemModuleGenerators, modules, element);
76      }
77  
78      private void generateModules(Map generators, List modules, Element element) {
79  
80          List moduleElements = new ArrayList(modules.size());
81  
82          for (int i = 0; i < modules.size(); i++) {
83              ModuleI module = (ModuleI) modules.get(i);
84              String namespaceURI = module.getUri();
85              ModuleGenerator generator = (ModuleGenerator)generators.get(namespaceURI);
86              if (generator != null) {
87                  generator.generate(module, element);
88              }
89          }
90      }
91  
92  }