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.DCModuleImpl;
20  import com.sun.syndication.feed.module.DCSubjectImpl;
21  import com.sun.syndication.feed.module.Module;
22  import com.sun.syndication.feed.module.DCModule;
23  import com.sun.syndication.feed.module.DCSubject;
24  import com.sun.syndication.io.ModuleParser;
25  import org.jdom.Attribute;
26  import org.jdom.Element;
27  import org.jdom.Namespace;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  public class DCModuleParser implements ModuleParser {
33  
34      public String getNamespaceUri() {
35          return DCModule.URI;
36      }
37  
38      private Namespace getDCNamespace() {
39          return Namespace.getNamespace(DCModule.URI);
40      }
41  
42      public Module parse(Element dcRoot) {
43          boolean foundSomething = false;
44          DCModule dcm = new DCModuleImpl();
45  
46          Element e = dcRoot.getChild("title",getDCNamespace());
47          if (e!=null) {
48              foundSomething = true;
49              dcm.setTitle(e.getText());
50          }
51          e = dcRoot.getChild("creator",getDCNamespace());
52          if (e!=null) {
53              foundSomething = true;
54              dcm.setCreator(e.getText());
55          }
56          List eList = dcRoot.getChildren("subject",getDCNamespace());
57          if (eList.size()>0) {
58              foundSomething = true;
59              dcm.setSubjects(parseSubjects(eList));
60          }
61          e = dcRoot.getChild("description",getDCNamespace());
62          if (e!=null) {
63              foundSomething = true;
64              dcm.setDescription(e.getText());
65          }
66          e = dcRoot.getChild("publisher",getDCNamespace());
67          if (e!=null) {
68              foundSomething = true;
69              dcm.setPublisher(e.getText());
70          }
71          eList = dcRoot.getChildren("contributor",getDCNamespace());
72          if (eList.size()>0) {
73              foundSomething = true;
74              dcm.setContributors(parseContributors(eList));
75          }
76          e = dcRoot.getChild("date",getDCNamespace());
77          if (e!=null) {
78              foundSomething = true;
79              dcm.setDate(DateParser.parseW3CDateTime(e.getText()));
80          }
81          e = dcRoot.getChild("type",getDCNamespace());
82          if (e!=null) {
83              foundSomething = true;
84              dcm.setType(e.getText());
85          }
86          e = dcRoot.getChild("format",getDCNamespace());
87          if (e!=null) {
88              foundSomething = true;
89              dcm.setFormat(e.getText());
90          }
91          e = dcRoot.getChild("identifier",getDCNamespace());
92          if (e!=null) {
93              foundSomething = true;
94              dcm.setIdentifier(e.getText());
95          }
96          e = dcRoot.getChild("source",getDCNamespace());
97          if (e!=null) {
98              foundSomething = true;
99              dcm.setSource(e.getText());
100         }
101         e = dcRoot.getChild("language",getDCNamespace());
102         if (e!=null) {
103             foundSomething = true;
104             dcm.setLanguage(e.getText());
105         }
106         e = dcRoot.getChild("relation",getDCNamespace());
107         if (e!=null) {
108             foundSomething = true;
109             dcm.setRelation(e.getText());
110         }
111         e = dcRoot.getChild("coverage",getDCNamespace());
112         if (e!=null) {
113             foundSomething = true;
114             dcm.setCoverage(e.getText());
115         }
116         e = dcRoot.getChild("rights",getDCNamespace());
117         if (e!=null) {
118             foundSomething = true;
119             dcm.setRights(e.getText());
120         }
121 
122         return (foundSomething) ? dcm : null;
123     }
124 
125     private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
126     private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
127 
128     private String getTaxonomy(Element desc) {
129         String d = null;
130         Element taxo = desc.getChild("topic",Namespace.getNamespace(TAXO_URI));
131         if (taxo!=null) {
132             Attribute a = taxo.getAttribute("resource",Namespace.getNamespace((RDF_URI)));
133             if (a!=null) {
134                 d = a.getValue();
135             }
136         }
137         return d;
138     }
139 
140 
141 
142     private List parseSubjects(List eList) {
143         List subjects = new ArrayList();
144         for (int i=0;i<eList.size();i++) {
145             Element eSubject = (Element) eList.get(i);
146             Element eDesc = eSubject.getChild("Description",Namespace.getNamespace(RDF_URI));
147             if (eDesc!=null) {
148                 String taxonomy = getTaxonomy(eDesc);
149                 List eValues = eDesc.getChildren("value",Namespace.getNamespace(RDF_URI));
150                 for (int j=0;j<eValues.size();j++) {
151                     Element eValue = (Element) eValues.get(j);
152                     DCSubject subject = new DCSubjectImpl();
153                     subject.setTaxonomyUri(taxonomy);;
154                     subject.setValue(eValue.getText());
155                     subjects.add(subject);
156                 }
157             }
158             else {
159                 DCSubject subject = new DCSubjectImpl();
160                 subject.setValue(eSubject.getText());
161                 subjects.add(subject);
162             }
163         }
164         return subjects;
165     }
166 
167     private List parseContributors(List eList) {
168         List contributors = new ArrayList();
169         for (int i=0;i<eList.size();i++) {
170             Element e = (Element) eList.get(i);
171             contributors.add(e.getText());
172         }
173         return contributors;
174 
175     }
176 
177 }