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.feed.module.DCModule;
21  import com.sun.syndication.feed.module.DCSubject;
22  import com.sun.syndication.io.ModuleGenerator;
23  import org.jdom.Attribute;
24  import org.jdom.Element;
25  import org.jdom.Namespace;
26  
27  import java.util.List;
28  import java.util.Set;
29  import java.util.HashSet;
30  import java.util.Collections;
31  
32  /***
33   * Feed Generator for DC ModuleImpl
34   * <p/>
35   *
36   * @author Elaine Chien
37   *
38   */
39  public class DCModuleGenerator implements ModuleGenerator {
40  
41      private static final String DC_URI  = "http://purl.org/dc/elements/1.1/";
42      private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
43      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
44  
45      private static final Namespace DC_NS  = Namespace.getNamespace("dc", DC_URI);
46      private static final Namespace TAXO_NS = Namespace.getNamespace("taxo", TAXO_URI);
47      private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
48  
49      private static final Set NAMESPACES;
50  
51      static {
52          Set nss = new HashSet();
53          nss.add(DC_NS);
54          nss.add(TAXO_NS);
55          nss.add(RDF_NS);
56          NAMESPACES = Collections.unmodifiableSet(nss);
57      }
58  
59      public String getNamespaceUri() {
60          return DC_URI;
61      }
62  
63      /***
64       * Returns a set with all the URIs (JDOM Namespace elements) this module generator uses.
65       * <p/>
66       * It is used by the the feed generators to add their namespace definition in
67       * the root element of the generated document (forward-missing of Java 5.0 Generics).
68       * <p/>
69       *
70       * @return a set with all the URIs (JDOM Namespace elements) this module generator uses.
71       */
72      public Set getNamespaces() {
73          return NAMESPACES;
74      }
75  
76  
77      public void generate(Module module, Element element) {
78  
79          DCModule dcModule = (DCModule)module;
80  
81          if (dcModule.getTitle() != null) {
82              element.addContent(generateSimpleElement("title", dcModule.getTitle()));
83          }
84          if (dcModule.getCreator() != null) {
85              element.addContent(generateSimpleElement("creator", dcModule.getCreator()));
86          }
87          List subjects = dcModule.getSubjects();
88          for (int i = 0; i < subjects.size(); i++) {
89              element.addContent(generateSubjectElement((DCSubject) subjects.get(i)));
90          }
91          if (dcModule.getDescription() != null) {
92              element.addContent(generateSimpleElement("description", dcModule.getDescription()));
93          }
94          if (dcModule.getPublisher() != null) {
95              element.addContent(generateSimpleElement("publisher", dcModule.getPublisher()));
96          }
97          List contributors = dcModule.getContributors();
98          if (contributors != null) {
99              for (int i = 0; i < contributors.size(); i++) {
100                 String contributor = (String)contributors.get(i);
101                 element.addContent(generateSimpleElement("contributor", contributor));
102             }
103         }
104         if (dcModule.getDate() != null) {
105             element.addContent(
106                 generateSimpleElement("date", DateParser.formatW3CDateTime(dcModule.getDate())));
107         }
108         if (dcModule.getType() != null) {
109             element.addContent(generateSimpleElement("type", dcModule.getType()));
110         }
111         if (dcModule.getFormat() != null) {
112             element.addContent(generateSimpleElement("format", dcModule.getFormat()));
113         }
114         if (dcModule.getIdentifier() != null) {
115             element.addContent(generateSimpleElement("identifier", dcModule.getIdentifier()));
116         }
117         if (dcModule.getSource() != null) {
118             element.addContent(generateSimpleElement("source", dcModule.getSource()));
119         }
120         if (dcModule.getLanguage() != null) {
121             element.addContent(generateSimpleElement("language", dcModule.getLanguage()));
122         }
123         if (dcModule.getRelation() != null) {
124             element.addContent(generateSimpleElement("relation", dcModule.getRelation()));
125         }
126         if (dcModule.getCoverage() != null) {
127             element.addContent(generateSimpleElement("coverage", dcModule.getCoverage()));
128         }
129         if (dcModule.getRights() != null) {
130             element.addContent(generateSimpleElement("rights", dcModule.getRights()));
131         }
132     }
133 
134     protected Element generateSubjectElement(DCSubject subject) {
135 
136         Element subjectElement = new Element("subject", DC_NS);
137 
138         if (subject.getTaxonomyUri() != null) {
139             Element descriptionElement = new Element("Description", RDF_NS);
140             Element topicElement = new Element("topic", TAXO_NS);
141             Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), RDF_NS);
142             topicElement.setAttribute(resourceAttribute);
143             descriptionElement.addContent(topicElement);
144 
145             if (subject.getValue() != null) {
146                 Element valueElement = new Element("value", RDF_NS);
147                 valueElement.addContent(subject.getValue());
148                 descriptionElement.addContent(valueElement);
149             }
150             subjectElement.addContent(descriptionElement);
151         } else {
152             subjectElement.addContent(subject.getValue());
153         }
154         return subjectElement;
155     }
156 
157 
158     protected Element generateSimpleElement(String name, String value)  {
159 
160         Element element = new Element(name, DC_NS);
161         element.addContent(value);
162 
163         return element;
164     }
165 
166 }