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.List;
26  
27  /***
28   */
29  public class ModuleParsers extends PluginManager {
30  
31      public ModuleParsers(String propertyKey) {
32          super(propertyKey);
33      }
34  
35      public String getKey(Object obj) {
36          return ((ModuleParser)obj).getNamespaceUri();
37      }
38  
39      public List getModuleNamespaces() {
40          return getKeys();
41      }
42  
43      public List parseModules(Element root) {
44          List parsers = getPlugins();
45          List modules = null;
46          for (int i=0;i<parsers.size();i++) {
47              ModuleParser parser = (ModuleParser) parsers.get(i);
48              String namespaceUri = parser.getNamespaceUri();
49              Namespace namespace = Namespace.getNamespace(namespaceUri);
50              if (hasElementsFrom(root,namespace)) {
51                  ModuleI module = parser.parse(root);
52                  if (module!=null) {
53                      if (modules==null) {
54                          modules = new ArrayList();
55                      }
56                      modules.add(module);
57                  }
58              }
59          }
60          return modules;
61      }
62  
63  
64      private boolean hasElementsFrom(Element root,Namespace namespace) {
65          boolean itHas = false;
66          List children = root.getChildren();
67          for (int i=0;!itHas && i<children.size();i++) {
68              Element child = (Element) children.get(i);
69              itHas = namespace.equals(child.getNamespace());
70          }
71         return itHas;
72      }
73  
74  
75  }