1 |
275 |
package yarfraw.mapping.backward.impl; |
2 |
|
import static yarfraw.io.parser.ElementQName.RSS20_COPYRIGHTS; |
3 |
|
import static yarfraw.io.parser.ElementQName.RSS20_DESCRIPTION; |
4 |
|
import static yarfraw.io.parser.ElementQName.RSS20_DOCS; |
5 |
|
import static yarfraw.io.parser.ElementQName.RSS20_GENERATOR; |
6 |
|
import static yarfraw.io.parser.ElementQName.RSS20_LANGUAGE; |
7 |
|
import static yarfraw.io.parser.ElementQName.RSS20_LAST_BUILD_DATE; |
8 |
|
import static yarfraw.io.parser.ElementQName.RSS20_LINK; |
9 |
|
import static yarfraw.io.parser.ElementQName.RSS20_MANAGINGEDITOR; |
10 |
|
import static yarfraw.io.parser.ElementQName.RSS20_PUBDATE; |
11 |
|
import static yarfraw.io.parser.ElementQName.RSS20_TITLE; |
12 |
|
import static yarfraw.io.parser.ElementQName.RSS20_TTL; |
13 |
|
import static yarfraw.io.parser.ElementQName.RSS20_WEBMASTER; |
14 |
|
import static yarfraw.utils.XMLUtils.same; |
15 |
|
|
16 |
|
import javax.xml.bind.JAXBElement; |
17 |
|
|
18 |
|
import org.apache.commons.lang.builder.ToStringBuilder; |
19 |
|
import org.apache.commons.logging.Log; |
20 |
|
import org.apache.commons.logging.LogFactory; |
21 |
|
import org.w3c.dom.Element; |
22 |
|
|
23 |
|
import yarfraw.core.datamodel.CategorySubject; |
24 |
|
import yarfraw.core.datamodel.ChannelFeed; |
25 |
|
import yarfraw.core.datamodel.Cloud; |
26 |
|
import yarfraw.core.datamodel.Day; |
27 |
|
import yarfraw.core.datamodel.Image; |
28 |
|
import yarfraw.core.datamodel.TextInput; |
29 |
|
import yarfraw.core.datamodel.YarfrawException; |
30 |
|
import yarfraw.generated.rss20.elements.TCategory; |
31 |
|
import yarfraw.generated.rss20.elements.TCloud; |
32 |
|
import yarfraw.generated.rss20.elements.TImage; |
33 |
|
import yarfraw.generated.rss20.elements.TRssChannel; |
34 |
|
import yarfraw.generated.rss20.elements.TRssItem; |
35 |
|
import yarfraw.generated.rss20.elements.TSkipDay; |
36 |
|
import yarfraw.generated.rss20.elements.TSkipDaysList; |
37 |
|
import yarfraw.generated.rss20.elements.TSkipHoursList; |
38 |
|
import yarfraw.generated.rss20.elements.TTextInput; |
39 |
|
import yarfraw.mapping.backward.ToChannelRss20; |
40 |
|
|
41 |
54 |
public class ToChannelRss20Impl implements ToChannelRss20{ |
42 |
54 |
private static final Log LOG = LogFactory.getLog(ToChannelRss20Impl.class); |
43 |
54 |
private static final ToChannelRss20 _instance = new ToChannelRss20Impl(); |
44 |
|
|
45 |
54 |
private ToChannelRss20Impl() {} |
46 |
|
public static ToChannelRss20 getInstance(){ |
47 |
275 |
return _instance; |
48 |
|
} |
49 |
|
|
50 |
|
@SuppressWarnings("unchecked") |
51 |
|
public ChannelFeed execute(TRssChannel ch) throws YarfrawException { |
52 |
275 |
if(ch == null){ |
53 |
0 |
return null; |
54 |
|
} |
55 |
275 |
ChannelFeed c = new ChannelFeed(); |
56 |
|
|
57 |
275 |
if(ch.getItem() != null){ |
58 |
7271 |
for(TRssItem item : ch.getItem()){ |
59 |
6721 |
c.addItem(Rss20MappingUtils.toItem(item)); |
60 |
|
} |
61 |
|
} |
62 |
275 |
c.getOtherAttributes().putAll(ch.getOtherAttributes()); |
63 |
|
|
64 |
2767 |
for(Object o : ch.getTitleOrLinkOrDescription()){ |
65 |
2217 |
if(o == null){ |
66 |
0 |
continue; |
67 |
|
} |
68 |
2217 |
if (o instanceof JAXBElement) { |
69 |
1719 |
JAXBElement jaxbElement = (JAXBElement) o; |
70 |
1719 |
Object val = jaxbElement.getValue(); |
71 |
1719 |
if(same(jaxbElement.getName(), RSS20_TITLE)){ |
72 |
275 |
c.setTitle((String)jaxbElement.getValue()); |
73 |
1444 |
}else if (same(jaxbElement.getName(), RSS20_LINK)) { |
74 |
245 |
c.addLink((String)jaxbElement.getValue()); |
75 |
1199 |
}else if (same(jaxbElement.getName(), RSS20_DESCRIPTION)) { |
76 |
245 |
c.setDescriptionOrSubtitle((String)jaxbElement.getValue()); |
77 |
954 |
}else if (same(jaxbElement.getName(), RSS20_COPYRIGHTS)) { |
78 |
86 |
c.setRights((String)jaxbElement.getValue()); |
79 |
868 |
}else if (same(jaxbElement.getName(), RSS20_DOCS)) { |
80 |
65 |
c.setDocs((String)jaxbElement.getValue()); |
81 |
803 |
}else if (same(jaxbElement.getName(), RSS20_GENERATOR)) { |
82 |
96 |
c.setGenerator((String)jaxbElement.getValue()); |
83 |
707 |
}else if (same(jaxbElement.getName(), RSS20_LANGUAGE)) { |
84 |
203 |
c.setLang((String)jaxbElement.getValue()); |
85 |
504 |
}else if (same(jaxbElement.getName(), RSS20_LAST_BUILD_DATE)) { |
86 |
95 |
c.setLastBuildOrUpdatedDate((String)jaxbElement.getValue()); |
87 |
409 |
}else if (same(jaxbElement.getName(), RSS20_MANAGINGEDITOR)) { |
88 |
57 |
c.addManagingEditorOrAuthorOrPublisher((String)jaxbElement.getValue()); |
89 |
352 |
}else if (same(jaxbElement.getName(), RSS20_PUBDATE)) { |
90 |
81 |
c.setPubDate((String)jaxbElement.getValue()); |
91 |
271 |
}else if (same(jaxbElement.getName(), RSS20_TTL)) { |
92 |
56 |
c.setTtl(Integer.valueOf(jaxbElement.getValue().toString())); |
93 |
215 |
}else if (same(jaxbElement.getName(), RSS20_WEBMASTER)) { |
94 |
63 |
c.addWebMasterOrCreator((String)jaxbElement.getValue()); |
95 |
152 |
}else if (val instanceof TCategory) { |
96 |
27 |
TCategory cat = (TCategory) val; |
97 |
54 |
c.addCategorySubject(new CategorySubject(cat.getValue()) |
98 |
27 |
.setDomainOrScheme(cat.getDomain())); |
99 |
125 |
}else if (val instanceof TSkipDaysList) { |
100 |
6 |
TSkipDaysList sdl = (TSkipDaysList)val; |
101 |
24 |
for(TSkipDay day : sdl.getDay()){ |
102 |
12 |
c.addSkipDay(Day.valueOf(day.value())); |
103 |
|
} |
104 |
|
|
105 |
119 |
}else if (val instanceof TSkipHoursList) { |
106 |
6 |
TSkipHoursList shl = (TSkipHoursList)val; |
107 |
54 |
for(Integer h : shl.getHour()){ |
108 |
42 |
c.addSkipHour(h); |
109 |
|
} |
110 |
113 |
}else if (val instanceof TCloud) { |
111 |
6 |
TCloud cloud = (TCloud)val; |
112 |
12 |
c.setCloud(new Cloud(cloud.getDomain(), |
113 |
6 |
cloud.getPort() == null ? null : cloud.getPort().toString(), |
114 |
6 |
cloud.getPath(), cloud.getRegisterProcedure(), |
115 |
6 |
cloud.getProtocol()==null?null:cloud.getProtocol().value())); |
116 |
107 |
}else if (val instanceof TImage) { |
117 |
101 |
TImage image = (TImage)val; |
118 |
202 |
c.setImageOrIcon(new Image(image.getUrl(), image.getTitle(), image.getLink(), |
119 |
101 |
image.getWidth(), image.getHeight(), image.getDescription())); |
120 |
6 |
}else if (val instanceof TTextInput) { |
121 |
6 |
TTextInput in = (TTextInput)val; |
122 |
6 |
c.setTexInput(new TextInput(in.getTitle(), in.getDescription(), in.getName(), in.getLink())); |
123 |
|
}else{ |
124 |
0 |
LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!"); |
125 |
|
} |
126 |
|
} |
127 |
498 |
else if (o instanceof Element) { |
128 |
498 |
Element e = (Element) o; |
129 |
498 |
c.getOtherElements().add(e); |
130 |
|
}else{ |
131 |
0 |
LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!"); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
275 |
return c; |
136 |
|
} |
137 |
|
|
138 |
|
} |