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.ModuleParser;
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 ModulesParser {
35      private Map _feedModuleParsers = new HashMap();
36      private Map _itemModuleParsers = new HashMap();
37  
38      private void loadParsers(String defaultFile,String fileProperty,String key,Map map) {
39          PlugableClasses pClasses = new PlugableClasses(defaultFile,fileProperty,
40                                                          key,true,ModulesParser.class.getClassLoader());
41          Object[] parsers;
42          try {
43              parsers = pClasses.createInstances();
44          }
45          catch (Exception ex) {
46              throw new RuntimeException(ex);
47          }
48          for (int i=0;i<parsers.length;i++) {
49              ModuleParser parser = (ModuleParser) parsers[i];
50              map.put(parser.getNamespaceUri(),parser);
51          }
52      }
53  
54      public ModulesParser() {
55          this(Constants.DEFAULT_IMPLS_FILE,Constants.IMPLS_SYSTEM_PROPERTY);
56      }
57  
58      public ModulesParser(String defaultFile,String fileProperty) {
59          loadParsers(defaultFile,fileProperty,Constants.FEED_MODULE_PARSERS_KEY,_feedModuleParsers);
60          loadParsers(defaultFile,fileProperty,Constants.ITEM_MODULE_PARSERS_KEY,_itemModuleParsers);
61      }
62  
63      public List getFeedModuleNamespaces() {
64          return Collections.unmodifiableList(new ArrayList(_feedModuleParsers.keySet()));
65      }
66  
67      public List getItemModuleNamespaces() {
68          return Collections.unmodifiableList(new ArrayList(_itemModuleParsers.keySet()));
69      }
70  
71      public List parseFeedModules(Element root) {
72          return parseModules(_feedModuleParsers,root);
73      }
74  
75      public List parseItemModules(Element root) {
76          return parseModules(_itemModuleParsers,root);
77      }
78  
79  
80      private boolean hasElementsFrom(Element root,Namespace namespace) {
81          boolean itHas = false;
82          List children = root.getChildren();
83          for (int i=0;!itHas && i<children.size();i++) {
84              Element child = (Element) children.get(i);
85              itHas = namespace.equals(child.getNamespace());
86          }
87         return itHas;
88      }
89  
90      private List parseModules(Map parsers,Element root) {
91          List modules = null;
92          Iterator i = parsers.keySet().iterator();
93          while (i.hasNext()) {
94              String namespaceURI = (String) i.next();
95              Namespace namespace = Namespace.getNamespace(namespaceURI);
96              if (hasElementsFrom(root,namespace)) {
97                  ModuleParser parser = (ModuleParser) parsers.get(namespaceURI);
98                  if (parser!=null) {
99                      ModuleI module = parser.parse(root);
100                     if (module!=null) {
101                         if (modules==null) {
102                             modules = new ArrayList();
103                         }
104                         modules.add(module);
105                     }
106                 }
107             }
108         }
109         return modules;
110     }
111 
112 }