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.samples.module;
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 com.sun.syndication.io.impl.DateParser;
23  import org.jdom.Element;
24  import org.jdom.Namespace;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /***
30   * Parser for the Sample ModuleImpl.
31   * <p>
32   * @author Alejandro Abdelnur
33   */
34  public class SampleModuleParser implements ModuleParser {
35      private static final Namespace SAMPLE_NS  = Namespace.getNamespace("sample", SampleModule.URI);
36  
37      public String getNamespaceUri() {
38          return SampleModule.URI;
39      }
40  
41      public Module parse(Element dcRoot) {
42          boolean foundSomething = false;
43          SampleModule fm = new SampleModuleImpl();
44  
45          Element e = dcRoot.getChild("bar", SAMPLE_NS);
46          if (e != null) {
47              foundSomething = true;
48              fm.setBar(e.getText());
49          }
50  
51          List eList = dcRoot.getChildren("foo", SAMPLE_NS);
52          if (eList.size() > 0) {
53              foundSomething = true;
54              fm.setFoos(parseFoos(eList));
55          }
56          e = dcRoot.getChild("date", SAMPLE_NS);
57          if (e != null) {
58              foundSomething = true;
59              fm.setDate(DateParser.parseDate(e.getText()));
60          }
61          return (foundSomething) ? fm : null;
62      }
63  
64      private List parseFoos(List eList) {
65          List foos = new ArrayList();
66          for (int i = 0; i < eList.size(); i++) {
67              Element e = (Element) eList.get(i);
68              foos.add(e.getText());
69          }
70          return foos;
71      }
72  
73  }
74