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.ModuleGenerator;
21  import com.sun.syndication.io.impl.DateParser;
22  import org.jdom.Element;
23  import org.jdom.Namespace;
24  
25  import java.util.List;
26  import java.util.Set;
27  import java.util.HashSet;
28  import java.util.Collections;
29  
30  /***
31   * Generator for the Sample ModuleImpl.
32   * <p>
33   * @author Alejandro Abdelnur
34   */
35  public class SampleModuleGenerator  implements ModuleGenerator {
36      private static final Namespace SAMPLE_NS  = Namespace.getNamespace("sample", SampleModule.URI);
37  
38      public String getNamespaceUri() {
39          return SampleModule.URI;
40      }
41  
42      private static final Set NAMESPACES;
43  
44      static {
45          Set nss = new HashSet();
46          nss.add(SAMPLE_NS);
47          NAMESPACES = Collections.unmodifiableSet(nss);
48      }
49  
50      /***
51       * Returns a set with all the URIs (JDOM Namespace elements) this module generator uses.
52       * <p/>
53       * It is used by the the feed generators to add their namespace definition in
54       * the root element of the generated document (forward-missing of Java 5.0 Generics).
55       * <p/>
56       *
57       * @return a set with all the URIs (JDOM Namespace elements) this module generator uses.
58       */
59      public Set getNamespaces() {
60          return NAMESPACES;
61      }
62  
63  
64      public void generate(Module module, Element element) {
65  
66          SampleModule fm = (SampleModule)module;
67  
68          if (fm.getBar() != null) {
69              element.addContent(generateSimpleElement("bar", fm.getBar()));
70          }
71  
72          List foos = fm.getFoos();
73          for (int i = 0; i < foos.size(); i++) {
74              element.addContent(generateSimpleElement("foo",foos.get(i).toString()));
75          }
76          if (fm.getDate() != null) {
77              element.addContent(
78                  generateSimpleElement("date", DateParser.formatW3CDateTime(fm.getDate())));
79          }
80      }
81  
82      protected Element generateSimpleElement(String name, String value)  {
83  
84          Element element = new Element(name, SAMPLE_NS);
85          element.addContent(value);
86  
87          return element;
88      }
89  
90  }