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.ArrayList;
28  import java.util.Date;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Set;
32  import java.util.HashSet;
33  import java.util.Collections;
34  
35  
36  /***
37   * Feed Generator for DublinCore Module.
38   * <p/>
39   *
40   * @author Elaine Chien
41   *
42   */
43  public class DCModuleGenerator implements ModuleGenerator {
44  
45      private static final String DC_URI  = "http://purl.org/dc/elements/1.1/";
46      private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
47      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
48  
49      private static final Namespace DC_NS  = Namespace.getNamespace("dc", DC_URI);
50      private static final Namespace TAXO_NS = Namespace.getNamespace("taxo", TAXO_URI);
51      private static final Namespace RDF_NS = Namespace.getNamespace("rdf", RDF_URI);
52  
53      private static final Set NAMESPACES;
54  
55      static {
56          Set nss = new HashSet();
57          nss.add(DC_NS);
58          nss.add(TAXO_NS);
59          nss.add(RDF_NS);
60          NAMESPACES = Collections.unmodifiableSet(nss);
61      }
62  
63      public final String getNamespaceUri() {
64          return DC_URI;
65      }
66      
67      private final Namespace getDCNamespace() {
68          return DC_NS;
69      }
70  
71      private final Namespace getRDFNamespace() {
72          return RDF_NS;
73      }
74  
75      private final Namespace getTaxonomyNamespace() {
76          return TAXO_NS;
77      }
78  
79      /***
80       * Returns a set with all the URIs (JDOM Namespace elements) this module
81       * generator uses.
82       * <p/>
83       * It is used by the the feed generators to add their namespace definition
84       * in the root element of the generated document (forward-missing of
85       * Java 5.0 Generics).
86       * <p/>
87       *
88       * @return a set with all the URIs this module generator uses.
89       */
90      public final Set getNamespaces() {
91          return NAMESPACES;
92      }
93  
94      /***
95       * Populate an element tree with elements for a module.
96       * <p>
97       * @param module the module to populate from.
98       * @param element the root element to attach child elements to.
99       */
100     public final void generate(Module module, Element element) {
101         DCModule dcModule = (DCModule) module;
102 
103         if (dcModule.getTitle() != null) {
104             element.addContent(generateSimpleElementList("title", dcModule.getTitles()));
105         }
106         if (dcModule.getCreator() != null) {
107             element.addContent(generateSimpleElementList("creator", dcModule.getCreators()));
108         }
109         List subjects = dcModule.getSubjects();
110         for (int i = 0; i < subjects.size(); i++) {
111             element.addContent(generateSubjectElement((DCSubject) subjects.get(i)));
112         }
113         if (dcModule.getDescription() != null) {
114             element.addContent(generateSimpleElementList("description", dcModule.getDescriptions()));
115         }
116         if (dcModule.getPublisher() != null) {
117             element.addContent(generateSimpleElementList("publisher", dcModule.getPublishers()));
118         }
119         if (dcModule.getContributors() != null) {
120             element.addContent(generateSimpleElementList("contributor", dcModule.getContributors()));
121         }
122         if (dcModule.getDate() != null) {
123             for (Iterator i = dcModule.getDates().iterator(); i.hasNext();) {
124                 element.addContent(generateSimpleElement("date",
125                         DateParser.formatW3CDateTime((Date) i.next())));
126             }
127         }
128         if (dcModule.getType() != null) {
129             element.addContent(generateSimpleElementList("type", dcModule.getTypes()));
130         }
131         if (dcModule.getFormat() != null) {
132             element.addContent(generateSimpleElementList("format", dcModule.getFormats()));
133         }
134         if (dcModule.getIdentifier() != null) {
135             element.addContent(generateSimpleElementList("identifier", dcModule.getIdentifiers()));
136         }
137         if (dcModule.getSource() != null) {
138             element.addContent(generateSimpleElementList("source", dcModule.getSources()));
139         }
140         if (dcModule.getLanguage() != null) {
141             element.addContent(generateSimpleElementList("language", dcModule.getLanguages()));
142         }
143         if (dcModule.getRelation() != null) {
144             element.addContent(generateSimpleElementList("relation", dcModule.getRelations()));
145         }
146         if (dcModule.getCoverage() != null) {
147             element.addContent(generateSimpleElementList("coverage", dcModule.getCoverages()));
148         }
149         if (dcModule.getRights() != null) {
150             element.addContent(generateSimpleElementList("rights", dcModule.getRightsList()));
151         }
152     }
153 
154     /***
155      * Utility method to generate an element for a subject.
156      * <p>
157      * @param subject the subject to generate an element for.
158      * @return the element for the subject.
159      */
160     protected final Element generateSubjectElement(DCSubject subject) {
161         Element subjectElement = new Element("subject", getDCNamespace());
162 
163         if (subject.getTaxonomyUri() != null) {
164             Element descriptionElement = new Element("Description", getRDFNamespace());
165             Element topicElement = new Element("topic", getTaxonomyNamespace());
166             Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), getRDFNamespace());
167             topicElement.setAttribute(resourceAttribute);
168             descriptionElement.addContent(topicElement);
169 
170             if (subject.getValue() != null) {
171                 Element valueElement = new Element("value", getRDFNamespace());
172                 valueElement.addContent(subject.getValue());
173                 descriptionElement.addContent(valueElement);
174             }
175             subjectElement.addContent(descriptionElement);
176         } else {
177             subjectElement.addContent(subject.getValue());
178         }
179         return subjectElement;
180     }
181 
182 
183     /***
184      * Utility method to generate a single element containing a string.
185      * <p>
186      * @param name the name of the elment to generate.
187      * @param value the value of the text in the element.
188      * @return the element generated.
189      */
190     protected final Element generateSimpleElement(String name, String value)  {
191         Element element = new Element(name, getDCNamespace());
192         element.addContent(value);
193 
194         return element;
195     }
196 
197     /***
198      * Utility method to generate a list of simple elements.
199      * <p>
200      * @param name the name of the element list to generate.
201      * @param value the list of values for the elements.
202      * @return a list of Elements created.
203      */
204     protected final List generateSimpleElementList(String name, List value) {
205         List elements = new ArrayList();
206         for (Iterator i = value.iterator(); i.hasNext();) {
207             elements.add(generateSimpleElement(name, (String) i.next()));
208         }
209 
210         return elements;
211     }
212 }