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.Module;
20  import com.sun.syndication.io.ModuleParser;
21  import com.sun.syndication.io.WireFeedParser;
22  import org.jdom.Element;
23  import org.jdom.Namespace;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   */
30  public class ModuleParsers extends PluginManager {
31      public ModuleParsers(String propertyKey, WireFeedParser parentParser) {
32          super(propertyKey, parentParser, null);
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                  Module 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      private boolean hasElementsFrom(Element root, Namespace namespace) {
64          boolean hasElements = false;
65  //        boolean hasElements = namespace.equals(root.getNamespace());
66  
67          if (!hasElements) {
68              List children = root.getChildren();
69              for (int i=0;!hasElements && i < children.size();i++) {
70                  Element child = (Element) children.get(i);
71                  hasElements = namespace.equals(child.getNamespace());
72              }
73          }
74          return hasElements;
75      }
76  }