View Javadoc

1   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  public class ToChannelRss20Impl implements ToChannelRss20{
42    private static final Log LOG = LogFactory.getLog(ToChannelRss20Impl.class);
43    private static final ToChannelRss20 _instance = new ToChannelRss20Impl();
44    
45    private ToChannelRss20Impl() {}
46    public static ToChannelRss20 getInstance(){
47      return _instance;
48    }
49    
50    @SuppressWarnings("unchecked")
51    public ChannelFeed execute(TRssChannel ch) throws YarfrawException {
52      if(ch == null){
53        return null;
54      }
55      ChannelFeed c = new ChannelFeed();
56      
57      if(ch.getItem() != null){
58        for(TRssItem item : ch.getItem()){
59          c.addItem(Rss20MappingUtils.toItem(item));
60        }
61      }
62      c.getOtherAttributes().putAll(ch.getOtherAttributes());
63      
64      for(Object o : ch.getTitleOrLinkOrDescription()){
65        if(o == null){
66          continue;
67        }
68        if (o instanceof JAXBElement) {
69          JAXBElement jaxbElement = (JAXBElement) o;
70          Object val = jaxbElement.getValue();
71          if(same(jaxbElement.getName(), RSS20_TITLE)){
72            c.setTitle((String)jaxbElement.getValue());
73          }else if (same(jaxbElement.getName(), RSS20_LINK)) {  
74            c.addLink((String)jaxbElement.getValue());
75          }else if (same(jaxbElement.getName(), RSS20_DESCRIPTION)) {
76            c.setDescriptionOrSubtitle((String)jaxbElement.getValue());
77          }else if (same(jaxbElement.getName(), RSS20_COPYRIGHTS)) {
78            c.setRights((String)jaxbElement.getValue());
79          }else if (same(jaxbElement.getName(), RSS20_DOCS)) {
80            c.setDocs((String)jaxbElement.getValue());
81          }else if (same(jaxbElement.getName(), RSS20_GENERATOR)) {
82            c.setGenerator((String)jaxbElement.getValue());
83          }else if (same(jaxbElement.getName(), RSS20_LANGUAGE)) {
84            c.setLang((String)jaxbElement.getValue());
85          }else if (same(jaxbElement.getName(), RSS20_LAST_BUILD_DATE)) {
86            c.setLastBuildOrUpdatedDate((String)jaxbElement.getValue());
87          }else if (same(jaxbElement.getName(), RSS20_MANAGINGEDITOR)) {
88            c.addManagingEditorOrAuthorOrPublisher((String)jaxbElement.getValue());
89          }else if (same(jaxbElement.getName(), RSS20_PUBDATE)) {
90            c.setPubDate((String)jaxbElement.getValue());
91          }else if (same(jaxbElement.getName(), RSS20_TTL)) {
92            c.setTtl(Integer.valueOf(jaxbElement.getValue().toString()));
93          }else if (same(jaxbElement.getName(), RSS20_WEBMASTER)) {
94            c.addWebMasterOrCreator((String)jaxbElement.getValue());
95          }else if (val instanceof TCategory) {
96            TCategory cat = (TCategory) val;
97            c.addCategorySubject(new CategorySubject(cat.getValue())
98                                      .setDomainOrScheme(cat.getDomain()));
99          }else if (val instanceof TSkipDaysList) {
100           TSkipDaysList sdl = (TSkipDaysList)val;
101           for(TSkipDay day : sdl.getDay()){
102             c.addSkipDay(Day.valueOf(day.value()));
103           }
104           
105         }else if (val instanceof TSkipHoursList) {
106           TSkipHoursList shl = (TSkipHoursList)val;
107           for(Integer h : shl.getHour()){
108             c.addSkipHour(h);
109           }
110         }else if (val instanceof TCloud) {
111           TCloud cloud = (TCloud)val;
112           c.setCloud(new Cloud(cloud.getDomain(), 
113               cloud.getPort() == null ? null : cloud.getPort().toString(), 
114                   cloud.getPath(), cloud.getRegisterProcedure(), 
115                   cloud.getProtocol()==null?null:cloud.getProtocol().value()));
116         }else if (val instanceof TImage) {
117           TImage image = (TImage)val;
118           c.setImageOrIcon(new Image(image.getUrl(), image.getTitle(), image.getLink(), 
119                 image.getWidth(), image.getHeight(), image.getDescription()));
120         }else if (val instanceof TTextInput) {
121           TTextInput in = (TTextInput)val;
122           c.setTexInput(new TextInput(in.getTitle(), in.getDescription(), in.getName(), in.getLink()));
123         }else{
124           LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!");
125         }
126       }
127       else if (o instanceof Element) {
128         Element e = (Element) o;
129         c.getOtherElements().add(e);
130       }else{
131         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
132       }
133     }                                           
134     
135     return c;
136   }
137   
138 }