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