View Javadoc

1   package yarfraw.mapping.backward.impl;
2   import static yarfraw.io.parser.ElementQName.RSS20_AUTHOR;
3   import static yarfraw.io.parser.ElementQName.RSS20_COMMENTS;
4   import static yarfraw.io.parser.ElementQName.RSS20_DESCRIPTION;
5   import static yarfraw.io.parser.ElementQName.RSS20_LINK;
6   import static yarfraw.io.parser.ElementQName.RSS20_PUBDATE;
7   import static yarfraw.io.parser.ElementQName.RSS20_TITLE;
8   import static yarfraw.utils.XMLUtils.same;
9   
10  import java.util.Map;
11  
12  import javax.xml.bind.JAXBElement;
13  import javax.xml.namespace.QName;
14  
15  import org.apache.commons.lang.builder.ToStringBuilder;
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.w3c.dom.Element;
19  
20  import yarfraw.core.datamodel.CategorySubject;
21  import yarfraw.core.datamodel.Enclosure;
22  import yarfraw.core.datamodel.Id;
23  import yarfraw.core.datamodel.ItemEntry;
24  import yarfraw.core.datamodel.Source;
25  import yarfraw.core.datamodel.YarfrawException;
26  import yarfraw.generated.rss20.elements.TCategory;
27  import yarfraw.generated.rss20.elements.TEnclosure;
28  import yarfraw.generated.rss20.elements.TGuid;
29  import yarfraw.generated.rss20.elements.TRssItem;
30  import yarfraw.generated.rss20.elements.TSource;
31  class Rss20MappingUtils{
32  
33    private static final Log LOG = LogFactory.getLog(Rss20MappingUtils.class);
34   
35  
36    private Rss20MappingUtils(){}
37    
38    @SuppressWarnings("unchecked")
39    public static ItemEntry toItem(TRssItem ti) throws YarfrawException {
40      if(ti == null){
41        return null;
42      }
43      ItemEntry item = new ItemEntry();
44      for(Object o : ti.getTitleOrDescriptionOrLink()){
45        if(o == null){
46          continue;
47        }
48        if(ti.getOtherAttributes() != null){
49          for(Map.Entry<QName, String> e : ti.getOtherAttributes().entrySet()){
50            item.addOtherAttributes(e.getKey(), e.getValue());
51          }
52        }
53        if (o instanceof JAXBElement) {
54          JAXBElement jaxbElement = (JAXBElement) o;
55          Object val = jaxbElement.getValue();
56          if(same(jaxbElement.getName(), RSS20_AUTHOR)){
57            item.addAuthorOrCreator((String)jaxbElement.getValue());
58          }else if (same(jaxbElement.getName(), RSS20_COMMENTS)) {
59            item.setComments((String)jaxbElement.getValue());
60          }else if (same(jaxbElement.getName(), RSS20_DESCRIPTION)) {
61            item.setDescriptionOrSummary((String)jaxbElement.getValue());
62          }else if (same(jaxbElement.getName(), RSS20_LINK)) {
63            item.addLink((String)jaxbElement.getValue());
64          }else if (same(jaxbElement.getName(), RSS20_PUBDATE)) {
65            item.setPubDate((String)jaxbElement.getValue());
66          }else if (same(jaxbElement.getName(), RSS20_TITLE)) {
67            item.setTitle((String)jaxbElement.getValue());
68          }else if (val instanceof TCategory) {
69            TCategory cat = (TCategory) val;
70            item.addCategorySubject(new CategorySubject().setCategoryOrSubjectOrTerm(cat.getValue())
71                    .setDomainOrScheme(cat.getDomain()));
72          }else if (val instanceof TEnclosure) {
73            TEnclosure en = (TEnclosure)val;
74            item.setEnclosure(new Enclosure(en.getUrl(), 
75                      en.getLength() == null ? null : en.getLength().toString(), 
76                      en.getType(), en.getValue()));
77          }else if (val instanceof TGuid) {
78            TGuid guid = (TGuid)val;
79            item.setUid(new Id(guid.getValue()).setPermaLink(guid.isIsPermaLink()));
80          }else if (val instanceof TSource) {
81            TSource source = (TSource)val;
82            item.setSource(new Source(source.getUrl(), source.getValue()));
83          }else{
84            LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!");
85          }
86        }else if (o instanceof Element) {
87          Element e = (Element) o;
88          item.getOtherElements().add(e);
89        }else{
90          LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
91        }
92      }
93  
94      return item;
95    }
96  }