1 |
|
package yarfraw.mapping.forward.impl; |
2 |
|
|
3 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toAtomId; |
4 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toCategoryType; |
5 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toEntry; |
6 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toIcon; |
7 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toLink; |
8 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toPersonType; |
9 |
|
import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toTextType; |
10 |
|
|
11 |
|
import java.util.List; |
12 |
|
|
13 |
|
import javax.xml.bind.JAXBElement; |
14 |
|
|
15 |
|
import org.apache.commons.logging.Log; |
16 |
|
import org.apache.commons.logging.LogFactory; |
17 |
|
|
18 |
|
import yarfraw.core.datamodel.CategorySubject; |
19 |
|
import yarfraw.core.datamodel.ChannelFeed; |
20 |
|
import yarfraw.core.datamodel.FeedFormat; |
21 |
|
import yarfraw.core.datamodel.Generator; |
22 |
|
import yarfraw.core.datamodel.ItemEntry; |
23 |
|
import yarfraw.core.datamodel.Link; |
24 |
|
import yarfraw.core.datamodel.Person; |
25 |
|
import yarfraw.core.datamodel.YarfrawException; |
26 |
|
import yarfraw.generated.atom10.elements.DateTimeType; |
27 |
|
import yarfraw.generated.atom10.elements.FeedType; |
28 |
|
import yarfraw.generated.atom10.elements.GeneratorType; |
29 |
|
import yarfraw.generated.atom10.elements.ObjectFactory; |
30 |
|
import yarfraw.mapping.forward.ToAtom10Channel; |
31 |
|
import yarfraw.utils.CommonUtils; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
18 |
public class ToAtom10ChannelImpl implements ToAtom10Channel{ |
38 |
9 |
private static final Log LOG = LogFactory.getLog(ToAtom10ChannelImpl.class); |
39 |
9 |
private static ToAtom10Channel _instance = new ToAtom10ChannelImpl(); |
40 |
9 |
private static final ObjectFactory FACTORY = new ObjectFactory(); |
41 |
|
|
42 |
|
public static ToAtom10Channel getInstance(){ |
43 |
18 |
return _instance; |
44 |
|
} |
45 |
9 |
private ToAtom10ChannelImpl(){} |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
public JAXBElement<FeedType> execute(ChannelFeed ch) throws YarfrawException { |
67 |
18 |
ObjectFactory factory = FACTORY; |
68 |
18 |
FeedType ret = factory.createFeedType(); |
69 |
18 |
List<Object> elementList = ret.getAuthorOrCategoryOrContributor(); |
70 |
|
|
71 |
18 |
if(ch.getUid() != null){ |
72 |
12 |
elementList.add(factory.createEntryTypeId(toAtomId(ch.getUid()))); |
73 |
|
} |
74 |
|
|
75 |
18 |
if(ch.getManagingEditorOrAuthorOrPublisher() != null){ |
76 |
3 |
for(Person author : ch.getManagingEditorOrAuthorOrPublisher()){ |
77 |
3 |
elementList.add(factory.createFeedTypeAuthor(toPersonType(author))); |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
18 |
if(ch.getCategorySubjects() != null){ |
83 |
3 |
for(CategorySubject c : ch.getCategorySubjects()){ |
84 |
6 |
if(c != null){ |
85 |
6 |
elementList.add(factory.createFeedTypeCategory(toCategoryType(c))); |
86 |
|
} |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
18 |
if(ch.getContributors() != null){ |
91 |
0 |
for(Person c : ch.getContributors()){ |
92 |
0 |
elementList.add(factory.createFeedTypeContributor(toPersonType(c))); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
18 |
if(ch.getGenerator() != null){ |
97 |
12 |
GeneratorType g = factory.createGeneratorType(); |
98 |
12 |
Generator in = ch.getGenerator(); |
99 |
12 |
g.setUri(in.getUri()); |
100 |
12 |
g.setValue(in.getValue()); |
101 |
12 |
g.setVersion(in.getVersion()); |
102 |
|
|
103 |
12 |
if(in.getOtherAttributes() != null){ |
104 |
12 |
g.getOtherAttributes().putAll(in.getOtherAttributes()); |
105 |
|
} |
106 |
|
|
107 |
12 |
g.setBase(in.getBase()); |
108 |
12 |
g.setLang(in.getLang()); |
109 |
12 |
elementList.add(factory.createFeedTypeGenerator(g)); |
110 |
|
} |
111 |
|
|
112 |
18 |
if(ch.getImageOrIcon() != null){ |
113 |
3 |
elementList.add(factory.createFeedTypeIcon(toIcon(ch.getImageOrIcon()))); |
114 |
|
} |
115 |
|
|
116 |
18 |
if(ch.getUid() != null){ |
117 |
12 |
elementList.add(factory.createFeedTypeId(toAtomId(ch.getUid()))); |
118 |
|
} |
119 |
|
|
120 |
18 |
if(ch.getLinks() != null ){ |
121 |
18 |
for(Link link : ch.getLinks()){ |
122 |
33 |
elementList.add(factory.createFeedTypeLink(toLink(link))); |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
18 |
if(ch.getLogo() != null){ |
127 |
0 |
elementList.add(factory.createFeedTypeIcon(toIcon(ch.getLogo()))); |
128 |
|
} |
129 |
|
|
130 |
18 |
if(ch.getRights() != null){ |
131 |
15 |
elementList.add(factory.createFeedTypeRights( |
132 |
|
toTextType(ch.getRights()))); |
133 |
|
} |
134 |
|
|
135 |
18 |
if(ch.getDescriptionOrSubtitle() != null){ |
136 |
12 |
elementList.add(factory.createFeedTypeSubtitle( |
137 |
|
toTextType(ch.getDescriptionOrSubtitle()))); |
138 |
|
} |
139 |
|
|
140 |
18 |
if(ch.getTitle() != null){ |
141 |
18 |
elementList.add(factory.createFeedTypeTitle( |
142 |
|
toTextType(ch.getTitle()))); |
143 |
|
} |
144 |
|
|
145 |
18 |
if(ch.getLastBuildOrUpdatedDate() != null){ |
146 |
12 |
DateTimeType date = factory.createDateTimeType(); |
147 |
12 |
String dateString = ch.getLastBuildOrUpdatedDate(); |
148 |
12 |
if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){ |
149 |
3 |
String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10); |
150 |
3 |
if(newDateString != null){ |
151 |
0 |
dateString = newDateString; |
152 |
|
}else{ |
153 |
3 |
LOG.warn("The dateString "+dateString+" is in valid according to ATOM 1.0 specs, unabel to convert it to a valid format, writing it as is"); |
154 |
|
} |
155 |
|
} |
156 |
12 |
date.setValue(dateString); |
157 |
12 |
elementList.add(factory.createFeedTypeUpdated(date)); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
18 |
if(ch.getPubDate() != null){ |
162 |
6 |
LOG.info("PubDate under <feed> level is not supported, it will be ignored"); |
163 |
|
} |
164 |
|
|
165 |
18 |
if(ch.getItems() != null){ |
166 |
18 |
for(ItemEntry item : ch.getItems()){ |
167 |
87 |
elementList.add(factory.createFeedTypeEntry(toEntry(item))); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
18 |
ret.setBase(ch.getBase()); |
172 |
18 |
ret.setLang(ch.getLang()); |
173 |
|
|
174 |
18 |
if(ch.getOtherElements() != null){ |
175 |
18 |
elementList.addAll(ch.getOtherElements()); |
176 |
|
} |
177 |
18 |
if(ch.getOtherAttributes() != null){ |
178 |
18 |
ret.getOtherAttributes().putAll(ch.getOtherAttributes()); |
179 |
|
} |
180 |
18 |
return factory.createFeed(ret); |
181 |
|
} |
182 |
|
|
183 |
|
} |