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.ModuleI;
20  import com.sun.syndication.feed.module.DCModuleI;
21  import com.sun.syndication.feed.module.DCSubjectI;
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  
29  /***
30   * Feed Generator for DC Module
31   * <p/>
32   *
33   * @author Elaine Chien
34   *
35   */
36  public class DCModuleGenerator implements ModuleGenerator {
37  
38      private static final String DC_URI  = "http://purl.org/dc/elements/1.1/";
39      private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
40      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
41  
42      private static final Namespace DC_NS  = Namespace.getNamespace("dc", DC_URI);
43      private static final Namespace TAXO_NS = Namespace.getNamespace("taxo", TAXO_URI);
44      private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
45  
46      public String getNamespaceUri() {
47          return DC_URI;
48      }
49  
50      public void generate(ModuleI module, Element element) {
51  
52          DCModuleI dcModule = (DCModuleI)module;
53  
54          if (dcModule.getTitle() != null) {
55              element.addContent(generateSimpleElement("title", dcModule.getTitle()));
56          }
57          if (dcModule.getCreator() != null) {
58              element.addContent(generateSimpleElement("creator", dcModule.getCreator()));
59          }
60          List subjects = dcModule.getSubjects();
61          for (int i = 0; i < subjects.size(); i++) {
62              element.addContent(generateSubjectElement((DCSubjectI) subjects.get(i)));
63          }
64          if (dcModule.getDescription() != null) {
65              element.addContent(generateSimpleElement("description", dcModule.getDescription()));
66          }
67          if (dcModule.getPublisher() != null) {
68              element.addContent(generateSimpleElement("publisher", dcModule.getPublisher()));
69          }
70          List contributors = dcModule.getContributors();
71          if (contributors != null) {
72              for (int i = 0; i < contributors.size(); i++) {
73                  String contributor = (String)contributors.get(i);
74                  element.addContent(generateSimpleElement("contributor", contributor));
75              }
76          }
77          if (dcModule.getDate() != null) {
78              element.addContent(
79                  generateSimpleElement("date", DateParser.parseW3CDateTime(dcModule.getDate())));
80          }
81          if (dcModule.getType() != null) {
82              element.addContent(generateSimpleElement("type", dcModule.getType()));
83          }
84          if (dcModule.getFormat() != null) {
85              element.addContent(generateSimpleElement("format", dcModule.getFormat()));
86          }
87          if (dcModule.getIdentifier() != null) {
88              element.addContent(generateSimpleElement("identifier", dcModule.getIdentifier()));
89          }
90          if (dcModule.getSource() != null) {
91              element.addContent(generateSimpleElement("source", dcModule.getSource()));
92          }
93          if (dcModule.getLanguage() != null) {
94              element.addContent(generateSimpleElement("language", dcModule.getLanguage()));
95          }
96          if (dcModule.getRelation() != null) {
97              element.addContent(generateSimpleElement("relation", dcModule.getRelation()));
98          }
99          if (dcModule.getCoverage() != null) {
100             element.addContent(generateSimpleElement("coverage", dcModule.getCoverage()));
101         }
102         if (dcModule.getRights() != null) {
103             element.addContent(generateSimpleElement("rights", dcModule.getRights()));
104         }
105     }
106 
107     protected Element generateSubjectElement(DCSubjectI subject) {
108 
109         Element subjectElement = new Element("subject", DC_NS);
110 
111         if (subject.getTaxonomyUri() != null) {
112             Element descriptionElement = new Element("Description", RDF_NS);
113             Element topicElement = new Element("topic", TAXO_NS);
114             Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), RDF_NS);
115             topicElement.setAttribute(resourceAttribute);
116             descriptionElement.addContent(topicElement);
117 
118             if (subject.getValue() != null) {
119                 Element valueElement = new Element("value", RDF_NS);
120                 valueElement.addContent(subject.getValue());
121                 descriptionElement.addContent(valueElement);
122             }
123             subjectElement.addContent(descriptionElement);
124         } else {
125             subjectElement.addContent(subject.getValue());
126         }
127         return subjectElement;
128     }
129 
130 
131     protected Element generateSimpleElement(String name, String value)  {
132 
133         Element element = new Element(name, DC_NS);
134         element.addContent(value);
135 
136         return element;
137     }
138 
139 }