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.io.FeedGenerator;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /***
29   * Generates an XML document (JDOM Document) out of a Feed.
30   * <p>
31   * It can generate all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
32   * Atom 0.3 feed.
33   * <p>
34   * FeedGenerator instances are thread safe.
35   * <p>
36   * Generators for a specific type must extend this class and register in the generator list.
37   * (Right now registration is hardcoded in the FeedGenerator constructor).
38   * <p>
39   * @author Alejandro Abdelnur
40   *
41   */
42  public class FeedGenerators {
43      private Map _generators;
44  
45      private List _types;
46  
47      private Map loadGenerators(String defaultFile,String fileProperty) {
48          Map map = new HashMap();
49          PlugableClasses pClasses = new PlugableClasses(defaultFile,fileProperty,Constants.FEED_GENERATORS_KEY,
50                                                         true,FeedGenerators.class.getClassLoader());
51          Object[] generators;
52          try {
53              generators = pClasses.createInstances();
54          }
55          catch (Exception ex) {
56              throw new RuntimeException(ex);
57          }
58          for (int i=0;i<generators.length;i++) {
59  
60              FeedGenerator generator  = (FeedGenerator) generators[i];
61              map.put(generator.getType(),generator);
62          }
63          return map;
64      }
65  
66      public FeedGenerators() {
67          this(Constants.DEFAULT_IMPLS_FILE,Constants.IMPLS_SYSTEM_PROPERTY);
68      }
69  
70      public FeedGenerators(String defaultFile,String fileProperty) {
71          _generators = loadGenerators(defaultFile,fileProperty);
72          _types = Collections.unmodifiableList(new ArrayList(_generators.keySet()));
73      }
74  
75      public List getSupportedFeedTypes() {
76          return _types;
77      }
78  
79      public FeedGenerator getGenerator(String feedType) {
80          return (FeedGenerator) _generators.get(feedType);
81      }
82  
83  }