001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.io.impl;
018:
019: import com.sun.syndication.feed.module.Module;
020: import com.sun.syndication.feed.module.DCModule;
021: import com.sun.syndication.feed.module.DCSubject;
022: import com.sun.syndication.io.ModuleGenerator;
023: import org.jdom.Attribute;
024: import org.jdom.Element;
025: import org.jdom.Namespace;
026:
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Set;
032: import java.util.HashSet;
033: import java.util.Collections;
034:
035: /**
036: * Feed Generator for DublinCore Module.
037: * <p/>
038: *
039: * @author Elaine Chien
040: *
041: */
042: public class DCModuleGenerator implements ModuleGenerator {
043:
044: private static final String DC_URI = "http://purl.org/dc/elements/1.1/";
045: private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
046: private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
047:
048: private static final Namespace DC_NS = Namespace.getNamespace("dc",
049: DC_URI);
050: private static final Namespace TAXO_NS = Namespace.getNamespace(
051: "taxo", TAXO_URI);
052: private static final Namespace RDF_NS = Namespace.getNamespace(
053: "rdf", RDF_URI);
054:
055: private static final Set NAMESPACES;
056:
057: static {
058: Set nss = new HashSet();
059: nss.add(DC_NS);
060: nss.add(TAXO_NS);
061: nss.add(RDF_NS);
062: NAMESPACES = Collections.unmodifiableSet(nss);
063: }
064:
065: public final String getNamespaceUri() {
066: return DC_URI;
067: }
068:
069: private final Namespace getDCNamespace() {
070: return DC_NS;
071: }
072:
073: private final Namespace getRDFNamespace() {
074: return RDF_NS;
075: }
076:
077: private final Namespace getTaxonomyNamespace() {
078: return TAXO_NS;
079: }
080:
081: /**
082: * Returns a set with all the URIs (JDOM Namespace elements) this module
083: * generator uses.
084: * <p/>
085: * It is used by the the feed generators to add their namespace definition
086: * in the root element of the generated document (forward-missing of
087: * Java 5.0 Generics).
088: * <p/>
089: *
090: * @return a set with all the URIs this module generator uses.
091: */
092: public final Set getNamespaces() {
093: return NAMESPACES;
094: }
095:
096: /**
097: * Populate an element tree with elements for a module.
098: * <p>
099: * @param module the module to populate from.
100: * @param element the root element to attach child elements to.
101: */
102: public final void generate(Module module, Element element) {
103: DCModule dcModule = (DCModule) module;
104:
105: if (dcModule.getTitle() != null) {
106: element.addContent(generateSimpleElementList("title",
107: dcModule.getTitles()));
108: }
109: if (dcModule.getCreator() != null) {
110: element.addContent(generateSimpleElementList("creator",
111: dcModule.getCreators()));
112: }
113: List subjects = dcModule.getSubjects();
114: for (int i = 0; i < subjects.size(); i++) {
115: element
116: .addContent(generateSubjectElement((DCSubject) subjects
117: .get(i)));
118: }
119: if (dcModule.getDescription() != null) {
120: element.addContent(generateSimpleElementList("description",
121: dcModule.getDescriptions()));
122: }
123: if (dcModule.getPublisher() != null) {
124: element.addContent(generateSimpleElementList("publisher",
125: dcModule.getPublishers()));
126: }
127: if (dcModule.getContributors() != null) {
128: element.addContent(generateSimpleElementList("contributor",
129: dcModule.getContributors()));
130: }
131: if (dcModule.getDate() != null) {
132: for (Iterator i = dcModule.getDates().iterator(); i
133: .hasNext();) {
134: element.addContent(generateSimpleElement("date",
135: DateParser.formatW3CDateTime((Date) i.next())));
136: }
137: }
138: if (dcModule.getType() != null) {
139: element.addContent(generateSimpleElementList("type",
140: dcModule.getTypes()));
141: }
142: if (dcModule.getFormat() != null) {
143: element.addContent(generateSimpleElementList("format",
144: dcModule.getFormats()));
145: }
146: if (dcModule.getIdentifier() != null) {
147: element.addContent(generateSimpleElementList("identifier",
148: dcModule.getIdentifiers()));
149: }
150: if (dcModule.getSource() != null) {
151: element.addContent(generateSimpleElementList("source",
152: dcModule.getSources()));
153: }
154: if (dcModule.getLanguage() != null) {
155: element.addContent(generateSimpleElementList("language",
156: dcModule.getLanguages()));
157: }
158: if (dcModule.getRelation() != null) {
159: element.addContent(generateSimpleElementList("relation",
160: dcModule.getRelations()));
161: }
162: if (dcModule.getCoverage() != null) {
163: element.addContent(generateSimpleElementList("coverage",
164: dcModule.getCoverages()));
165: }
166: if (dcModule.getRights() != null) {
167: element.addContent(generateSimpleElementList("rights",
168: dcModule.getRightsList()));
169: }
170: }
171:
172: /**
173: * Utility method to generate an element for a subject.
174: * <p>
175: * @param subject the subject to generate an element for.
176: * @return the element for the subject.
177: */
178: protected final Element generateSubjectElement(DCSubject subject) {
179: Element subjectElement = new Element("subject",
180: getDCNamespace());
181:
182: if (subject.getTaxonomyUri() != null) {
183: Element descriptionElement = new Element("Description",
184: getRDFNamespace());
185: Element topicElement = new Element("topic",
186: getTaxonomyNamespace());
187: Attribute resourceAttribute = new Attribute("resource",
188: subject.getTaxonomyUri(), getRDFNamespace());
189: topicElement.setAttribute(resourceAttribute);
190: descriptionElement.addContent(topicElement);
191:
192: if (subject.getValue() != null) {
193: Element valueElement = new Element("value",
194: getRDFNamespace());
195: valueElement.addContent(subject.getValue());
196: descriptionElement.addContent(valueElement);
197: }
198: subjectElement.addContent(descriptionElement);
199: } else {
200: subjectElement.addContent(subject.getValue());
201: }
202: return subjectElement;
203: }
204:
205: /**
206: * Utility method to generate a single element containing a string.
207: * <p>
208: * @param name the name of the elment to generate.
209: * @param value the value of the text in the element.
210: * @return the element generated.
211: */
212: protected final Element generateSimpleElement(String name,
213: String value) {
214: Element element = new Element(name, getDCNamespace());
215: element.addContent(value);
216:
217: return element;
218: }
219:
220: /**
221: * Utility method to generate a list of simple elements.
222: * <p>
223: * @param name the name of the element list to generate.
224: * @param value the list of values for the elements.
225: * @return a list of Elements created.
226: */
227: protected final List generateSimpleElementList(String name,
228: List value) {
229: List elements = new ArrayList();
230: for (Iterator i = value.iterator(); i.hasNext();) {
231: elements
232: .add(generateSimpleElement(name, (String) i.next()));
233: }
234:
235: return elements;
236: }
237: }
|