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.DCModuleImpl;
020: import com.sun.syndication.feed.module.DCSubjectImpl;
021: import com.sun.syndication.feed.module.Module;
022: import com.sun.syndication.feed.module.DCModule;
023: import com.sun.syndication.feed.module.DCSubject;
024: import com.sun.syndication.io.ModuleParser;
025: import com.sun.syndication.io.WireFeedParser;
026: import org.jdom.Attribute;
027: import org.jdom.Element;
028: import org.jdom.Namespace;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: /**
035: * Parser for the Dublin Core module.
036: */
037: public class DCModuleParser implements ModuleParser {
038:
039: private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
040: private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
041:
042: private static final Namespace DC_NS = Namespace
043: .getNamespace(DCModule.URI);
044: private static final Namespace RDF_NS = Namespace
045: .getNamespace(RDF_URI);
046: private static final Namespace TAXO_NS = Namespace
047: .getNamespace(TAXO_URI);
048:
049: public final String getNamespaceUri() {
050: return DCModule.URI;
051: }
052:
053: private final Namespace getDCNamespace() {
054: return DC_NS;
055: }
056:
057: private final Namespace getRDFNamespace() {
058: return RDF_NS;
059: }
060:
061: private final Namespace getTaxonomyNamespace() {
062: return TAXO_NS;
063: }
064:
065: /**
066: * Parse an element tree and return the module found in it.
067: * <p>
068: * @param dcRoot the root element containing the module elements.
069: * @return the module parsed from the element tree, <i>null</i> if none.
070: */
071: public Module parse(Element dcRoot) {
072: boolean foundSomething = false;
073: DCModule dcm = new DCModuleImpl();
074:
075: List eList = dcRoot.getChildren("title", getDCNamespace());
076: if (eList.size() > 0) {
077: foundSomething = true;
078: dcm.setTitles(parseElementList(eList));
079: }
080: eList = dcRoot.getChildren("creator", getDCNamespace());
081: if (eList.size() > 0) {
082: foundSomething = true;
083: dcm.setCreators(parseElementList(eList));
084: }
085: eList = dcRoot.getChildren("subject", getDCNamespace());
086: if (eList.size() > 0) {
087: foundSomething = true;
088: dcm.setSubjects(parseSubjects(eList));
089: }
090: eList = dcRoot.getChildren("description", getDCNamespace());
091: if (eList.size() > 0) {
092: foundSomething = true;
093: dcm.setDescriptions(parseElementList(eList));
094: }
095: eList = dcRoot.getChildren("publisher", getDCNamespace());
096: if (eList.size() > 0) {
097: foundSomething = true;
098: dcm.setPublishers(parseElementList(eList));
099: }
100: eList = dcRoot.getChildren("contributor", getDCNamespace());
101: if (eList.size() > 0) {
102: foundSomething = true;
103: dcm.setContributors(parseElementList(eList));
104: }
105: eList = dcRoot.getChildren("date", getDCNamespace());
106: if (eList.size() > 0) {
107: foundSomething = true;
108: dcm.setDates(parseElementListDate(eList));
109: }
110: eList = dcRoot.getChildren("type", getDCNamespace());
111: if (eList.size() > 0) {
112: foundSomething = true;
113: dcm.setTypes(parseElementList(eList));
114: }
115: eList = dcRoot.getChildren("format", getDCNamespace());
116: if (eList.size() > 0) {
117: foundSomething = true;
118: dcm.setFormats(parseElementList(eList));
119: }
120: eList = dcRoot.getChildren("identifier", getDCNamespace());
121: if (eList.size() > 0) {
122: foundSomething = true;
123: dcm.setIdentifiers(parseElementList(eList));
124: }
125: eList = dcRoot.getChildren("source", getDCNamespace());
126: if (eList.size() > 0) {
127: foundSomething = true;
128: dcm.setSources(parseElementList(eList));
129: }
130: eList = dcRoot.getChildren("language", getDCNamespace());
131: if (eList.size() > 0) {
132: foundSomething = true;
133: dcm.setLanguages(parseElementList(eList));
134: }
135: eList = dcRoot.getChildren("relation", getDCNamespace());
136: if (eList.size() > 0) {
137: foundSomething = true;
138: dcm.setRelations(parseElementList(eList));
139: }
140: eList = dcRoot.getChildren("coverage", getDCNamespace());
141: if (eList.size() > 0) {
142: foundSomething = true;
143: dcm.setCoverages(parseElementList(eList));
144: }
145: eList = dcRoot.getChildren("rights", getDCNamespace());
146: if (eList.size() > 0) {
147: foundSomething = true;
148: dcm.setRightsList(parseElementList(eList));
149: }
150:
151: return (foundSomething) ? dcm : null;
152: }
153:
154: /**
155: * Utility method to parse a taxonomy from an element.
156: * <p>
157: * @param desc the taxonomy description element.
158: * @return the string contained in the resource of the element.
159: */
160: protected final String getTaxonomy(Element desc) {
161: String d = null;
162: Element taxo = desc.getChild("topic", getTaxonomyNamespace());
163: if (taxo != null) {
164: Attribute a = taxo.getAttribute("resource",
165: getRDFNamespace());
166: if (a != null) {
167: d = a.getValue();
168: }
169: }
170: return d;
171: }
172:
173: /**
174: * Utility method to parse a list of subjects out of a list of elements.
175: * <p>
176: * @param eList the element list to parse.
177: * @return a list of subjects parsed from the elements.
178: */
179: protected final List parseSubjects(List eList) {
180: List subjects = new ArrayList();
181: for (Iterator i = eList.iterator(); i.hasNext();) {
182: Element eSubject = (Element) i.next();
183: Element eDesc = eSubject.getChild("Description",
184: getRDFNamespace());
185: if (eDesc != null) {
186: String taxonomy = getTaxonomy(eDesc);
187: List eValues = eDesc.getChildren("value",
188: getRDFNamespace());
189: for (Iterator v = eValues.iterator(); v.hasNext();) {
190: Element eValue = (Element) v.next();
191: DCSubject subject = new DCSubjectImpl();
192: subject.setTaxonomyUri(taxonomy);
193: subject.setValue(eValue.getText());
194: subjects.add(subject);
195: }
196: } else {
197: DCSubject subject = new DCSubjectImpl();
198: subject.setValue(eSubject.getText());
199: subjects.add(subject);
200: }
201: }
202:
203: return subjects;
204: }
205:
206: /**
207: * Utility method to parse a list of strings out of a list of elements.
208: * <p>
209: * @param eList the list of elements to parse.
210: * @return the list of strings
211: */
212: protected final List parseElementList(List eList) {
213: List values = new ArrayList();
214: for (Iterator i = eList.iterator(); i.hasNext();) {
215: Element e = (Element) i.next();
216: values.add(e.getText());
217: }
218:
219: return values;
220: }
221:
222: /**
223: * Utility method to parse a list of dates out of a list of elements.
224: * <p>
225: * @param eList the list of elements to parse.
226: * @return the list of dates.
227: */
228: protected final List parseElementListDate(List eList) {
229: List values = new ArrayList();
230: for (Iterator i = eList.iterator(); i.hasNext();) {
231: Element e = (Element) i.next();
232: values.add(DateParser.parseDate(e.getText()));
233: }
234:
235: return values;
236: }
237: }
|