1 package yarfraw.mapping.forward.impl;
2
3 import java.math.BigInteger;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import yarfraw.core.datamodel.CategorySubject;
10 import yarfraw.core.datamodel.Content;
11 import yarfraw.core.datamodel.FeedFormat;
12 import yarfraw.core.datamodel.Id;
13 import yarfraw.core.datamodel.Image;
14 import yarfraw.core.datamodel.ItemEntry;
15 import yarfraw.core.datamodel.Link;
16 import yarfraw.core.datamodel.Person;
17 import yarfraw.core.datamodel.Text;
18 import yarfraw.core.datamodel.YarfrawException;
19 import yarfraw.generated.atom10.elements.CategoryType;
20 import yarfraw.generated.atom10.elements.ContentType;
21 import yarfraw.generated.atom10.elements.DateTimeType;
22 import yarfraw.generated.atom10.elements.EntryType;
23 import yarfraw.generated.atom10.elements.IconType;
24 import yarfraw.generated.atom10.elements.IdType;
25 import yarfraw.generated.atom10.elements.LinkType;
26 import yarfraw.generated.atom10.elements.ObjectFactory;
27 import yarfraw.generated.atom10.elements.PersonType;
28 import yarfraw.generated.atom10.elements.TextType;
29 import yarfraw.generated.atom10.elements.UriType;
30 import yarfraw.utils.CommonUtils;
31 /***
32 * Util methods for mapping Yarfraw core model to Atom10 Jaxb model
33 * @author jliang
34 *
35 */
36 public class Atom10MappingUtils{
37 private static final ObjectFactory FACTORY = new ObjectFactory ();
38 private static final Log LOG = LogFactory.getLog(Atom10MappingUtils.class);
39 private Atom10MappingUtils(){}
40
41 public static LinkType toLink(Link link){
42 LinkType ret = FACTORY.createLinkType();
43 ret.setBase(link.getBase());
44 ret.setLang(link.getLang());
45 if(link.getOtherAttributes() != null){
46 ret.getOtherAttributes().putAll(link.getOtherAttributes());
47 }
48 ret.setHref(link.getHref());
49 ret.setHreflang(link.getHreflang());
50 ret.setLength(link.getLength() == null? null : new BigInteger(String.valueOf(link.getLength())));
51 ret.setRel(link.getRel());
52 ret.setTitle(link.getTitle());
53 ret.setType(link.getType());
54 return ret;
55 }
56
57 public static PersonType toPersonType(Person p){
58 PersonType ret = new PersonType();
59
60 if(p.getName()!= null){
61 ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeName(p.getName()));
62 }
63 if(p.getEmailOrText()!= null){
64 ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeEmail(p.getEmailOrText()));
65 }
66 if(p.getUri() != null){
67 UriType uri = FACTORY.createUriType();
68 uri.setValue(p.getUri());
69 ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeUri(uri));
70 }
71
72 if(p.getOtherElements() != null){
73 ret.getNameOrUriOrEmail().addAll(p.getOtherElements());
74 }
75 if(p.getOtherAttributes() != null){
76 ret.getOtherAttributes().putAll(p.getOtherAttributes());
77 }
78
79 ret.setBase(p.getBase());
80 ret.setLang(p.getLang());
81 return ret;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public static EntryType toEntry(ItemEntry item) throws YarfrawException{
104 EntryType ret = FACTORY.createEntryType();
105 List<Object> elementList = ret.getAuthorOrCategoryOrContent();
106 ObjectFactory factory = FACTORY;
107
108
109 if(item.getAuthorOrCreator() != null){
110 for(Person author : item.getAuthorOrCreator()){
111 elementList.add(factory.createEntryTypeAuthor(toPersonType(author)));
112 }
113 }
114
115 if(item.getCategorySubjects() != null){
116 for(CategorySubject c : item.getCategorySubjects()){
117 if(c != null){
118 elementList.add(factory.createEntryTypeCategory(toCategoryType(c)));
119 }
120 }
121 }
122
123 if(item.getContent() != null){
124 elementList.add(factory.createEntryTypeContent(toContent(item.getContent())));
125 }
126
127 if(item.getContributors() != null){
128 for(Person c : item.getContributors()){
129 elementList.add(factory.createEntryTypeContributor(toPersonType(c)));
130 }
131 }
132
133 if(item.getUid() != null){
134 elementList.add(factory.createEntryTypeId(toAtomId(item.getUid())));
135 }
136
137
138 if(item.getLinks() != null ){
139 for(Link link : item.getLinks()){
140 elementList.add(factory.createEntryTypeLink(toLink(link)));
141 }
142 }
143
144
145 if(item.getPubDate() != null){
146 DateTimeType date = factory.createDateTimeType();
147 String dateString = item.getPubDate();
148 if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){
149 String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10);
150 if(newDateString != null){
151 dateString = newDateString;
152 }else{
153 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 date.setValue(dateString);
157 elementList.add(factory.createEntryTypePublished(date));
158 }
159
160 if(item.getRights() != null){
161 elementList.add(factory.createEntryTypeRights(
162 Atom10MappingUtils.toTextType(item.getRights())));
163 }
164
165
166
167 if(item.getDescriptionOrSummary() != null ){
168 elementList.add(factory.createEntryTypeSummary(
169 toTextType(item.getDescriptionOrSummary())));
170 }
171
172
173 if(item.getComments() != null){
174 LOG.info("Item.Comments field is not supported by Atom 1.0. It will be ignored");
175 }
176
177 if(item.getTitle() != null){
178 elementList.add(factory.createEntryTypeTitle(
179 Atom10MappingUtils.toTextType(item.getTitle())));
180 }
181
182
183 if(item.getUpdatedDate() != null){
184 DateTimeType date = factory.createDateTimeType();
185 String dateString = item.getUpdatedDate();
186 if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){
187 String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10);
188 if(newDateString != null){
189 dateString = newDateString;
190 }else{
191 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");
192 }
193 }
194 date.setValue(dateString);
195 elementList.add(factory.createEntryTypeUpdated(date));
196 }
197
198
199 if(item.getEnclosure() != null){
200 LOG.info("Item.Enclosure field is not supported by Atom 1.0. It will be ignored. Use Item.AtomLink to add enclosure element to item");
201 }
202
203 if(item.getSource() != null){
204 LOG.info("Item.Source field is not supported by Atom 1.0. It will be ignored");
205 }
206
207 if(item.getOtherElements() != null){
208 ret.getAuthorOrCategoryOrContent().addAll(item.getOtherElements());
209 }
210 if(item.getOtherAttributes() != null){
211 ret.getOtherAttributes().putAll(item.getOtherAttributes());
212 }
213
214 ret.setBase(item.getBase());
215 ret.setLang(item.getLang());
216
217 if(item.getOtherAttributes() != null){
218 ret.getOtherAttributes().putAll(item.getOtherAttributes());
219 }
220 return ret;
221 }
222
223 public static ContentType toContent(Content in){
224 ContentType ret = FACTORY.createContentType();
225 ret.setSrc(in.getSrc());
226 ret.setType(in.getType());
227 if(in.getContentText()!= null){
228 ret.getContent().addAll(in.getContentText());
229 }
230
231 if(in.getOtherElements() != null){
232 ret.getContent().addAll(in.getOtherElements());
233 }
234 if(in.getOtherAttributes() != null){
235 ret.getOtherAttributes().putAll(in.getOtherAttributes());
236 }
237
238 ret.setBase(in.getBase());
239 ret.setLang(in.getLang());
240 return ret;
241 }
242
243 public static IdType toAtomId(Id in){
244 IdType ret = FACTORY.createIdType();
245 ret.setValue(in.getIdValue());
246
247 if(in.getOtherAttributes() != null){
248 ret.getOtherAttributes().putAll(in.getOtherAttributes());
249 }
250
251 ret.setBase(in.getBase());
252 ret.setLang(in.getLang());
253 return ret;
254 }
255
256 public static CategoryType toCategoryType(CategorySubject in){
257 CategoryType ret = FACTORY.createCategoryType();
258 ret.setTerm(in.getCategoryOrSubjectOrTerm());
259 ret.setScheme(in.getDomainOrScheme());
260 ret.setLabel(in.getLabel());
261 if(in.getOtherAttributes() != null){
262 ret.getOtherAttributes().putAll(in.getOtherAttributes());
263 }
264
265 return ret;
266 }
267
268 public static TextType toTextType(Text in){
269 TextType ret = FACTORY.createTextType();
270
271 if(in.getType() != null){
272 ret.setType(in.getType().toString());
273 }
274 if(in.getText() != null){
275 ret.getContent().add(in.getText());
276 }
277 if(in.getXhtmlDiv() != null){
278 ret.getContent().add(in.getXhtmlDiv());
279 }
280 if(in.getOtherElements() != null){
281 ret.getContent().addAll(in.getOtherElements());
282 }
283 if(in.getOtherAttributes() != null){
284 ret.getOtherAttributes().putAll(in.getOtherAttributes());
285 }
286
287 ret.setBase(in.getBase());
288 ret.setLang(in.getLang());
289
290 return ret;
291 }
292
293 public static IconType toIcon(Image in) {
294 IconType ret = FACTORY.createIconType();
295 ret.setValue(in.getUrl());
296 if(in.getOtherAttributes() != null){
297 ret.getOtherAttributes().putAll(in.getOtherAttributes());
298 }
299
300 ret.setBase(in.getBase());
301 ret.setLang(in.getLang());
302
303 return ret;
304 }
305
306 }