1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io.impl;
18
19 import com.sun.syndication.util.PlugableClasses;
20 import com.sun.syndication.io.FeedParser;
21 import org.jdom.Document;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 /***
28 * Parses an XML document (JDOM Document) into a Feed.
29 * <p>
30 * It accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) and
31 * Atom 0.3 feeds.
32 * <p>
33 * The FeedParser is a liberal parser.
34 * <p>
35 * FeedParser instances are thread safe.
36 * <p>
37 * Parsers for a specific type must extend this class and register in the parser list.
38 * (Right now registration is hardcoded in the FeedParser constructor).
39 * <p>
40 * @author Alejandro Abdelnur
41 *
42 */
43 public class FeedParsers {
44 private List PARSERS;
45 private List TYPES;
46
47 private List loadParsers(String defaultFile,String fileProperty) {
48 List list = new ArrayList();
49 PlugableClasses pClasses = new PlugableClasses(defaultFile,fileProperty,Constants.FEED_PARSERS_KEY,
50 true,FeedParsers.class.getClassLoader());
51 Object[] parsers;
52 try {
53 parsers = pClasses.createInstances();
54 }
55 catch (Exception ex) {
56 throw new RuntimeException(ex);
57 }
58 for (int i=0;i<parsers.length;i++) {
59 FeedParser parser = (FeedParser) parsers[i];
60 list.add(parser);
61 }
62 return list;
63 }
64
65 /***
66 * Creates a parser instance.
67 * <p>
68 *
69 */
70 public FeedParsers() {
71 this(Constants.DEFAULT_IMPLS_FILE,Constants.IMPLS_SYSTEM_PROPERTY);
72 }
73
74 public FeedParsers(String defaultFile,String fileProperty) {
75 PARSERS = loadParsers(defaultFile,fileProperty);
76 List types = new ArrayList();
77 for (int i=0;i<PARSERS.size();i++) {
78 types.add(((FeedParser)PARSERS.get(i)).getType());
79 }
80 TYPES = Collections.unmodifiableList(types);
81 }
82
83 public List getSupportedFeedTypes() {
84 return TYPES;
85 }
86
87 /***
88 * Finds the real parser type for the given document feed.
89 * <p>
90 * @param document document feed to find the parser for.
91 * @return the parser for the given document or <b>null</b> if there is no parser for that document.
92 *
93 */
94 public FeedParser getParserFor(Document document) {
95 FeedParser parser = null;
96 for (int i=0;parser==null && i<PARSERS.size();i++) {
97 parser = (FeedParser) PARSERS.get(i);
98 if (!parser.isMyType(document)) {
99 parser = null;
100 }
101 }
102 return parser;
103 }
104
105 }