View Javadoc

1   package com.sun.syndication.io.impl;
2   
3   import com.sun.syndication.feed.WireFeed;
4   import com.sun.syndication.feed.module.Extendable;
5   import com.sun.syndication.io.WireFeedParser;
6   import java.util.ArrayList;
7   import java.util.Iterator;
8   import org.jdom.Element;
9   
10  import java.util.List;
11  import org.jdom.Namespace;
12  
13  /**
14   * @author Alejandro Abdelnur
15   */
16  public abstract class BaseWireFeedParser implements WireFeedParser {
17      /**
18       * [TYPE].feed.ModuleParser.classes=  [className] ...
19       *
20       */
21      private static final String FEED_MODULE_PARSERS_POSFIX_KEY = ".feed.ModuleParser.classes";
22  
23      /**
24       * [TYPE].item.ModuleParser.classes= [className] ...
25       *
26       */
27      private static final String ITEM_MODULE_PARSERS_POSFIX_KEY = ".item.ModuleParser.classes";
28  
29  
30      private String _type;
31      private ModuleParsers _feedModuleParsers;
32      private ModuleParsers _itemModuleParsers;
33  
34      protected BaseWireFeedParser(String type) {
35          _type = type;
36          _feedModuleParsers = new ModuleParsers(type+FEED_MODULE_PARSERS_POSFIX_KEY, this);
37          _itemModuleParsers = new ModuleParsers(type+ITEM_MODULE_PARSERS_POSFIX_KEY, this);
38      }
39  
40      /**
41       * Returns the type of feed the parser handles.
42       * <p>
43       * @see WireFeed for details on the format of this string.
44       * <p>
45       * @return the type of feed the parser handles.
46       *
47       */
48      public String getType() {
49          return _type;
50      }
51  
52      protected List parseFeedModules(Element feedElement) {
53          return _feedModuleParsers.parseModules(feedElement);
54      }
55  
56      protected List parseItemModules(Element itemElement) {
57          return _itemModuleParsers.parseModules(itemElement);
58      }
59      
60      protected List extractForeignMarkup(Element e, Extendable ext, Namespace basens) {
61          ArrayList foreignMarkup = new ArrayList();
62          Iterator children = e.getChildren().iterator();
63          while (children.hasNext()) {
64              Element elem = (Element)children.next();
65              if  ( 
66                 // if elemet not in the RSS namespace
67                 !basens.equals(elem.getNamespace())
68                 // and elem was not handled by a module
69                 && null == ext.getModule(elem.getNamespaceURI())) {
70  
71                 // save it as foreign markup, 
72                 // but we can't detach it while we're iterating
73                 foreignMarkup.add(elem.clone()); 
74              }
75          }
76          // Now we can detach the foreign markup elements
77          Iterator fm = foreignMarkup.iterator();
78          while (fm.hasNext()) {
79              Element elem = (Element)fm.next();
80              elem.detach();
81          }
82          return foreignMarkup;
83      }
84  }
85