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