Coverage Report - yarfraw.mapping.backward.impl.Rss20MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Rss20MappingUtils
89% 
100% 
0
 
 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  54
   private static final Log LOG = LogFactory.getLog(Rss20MappingUtils.class);
 34  
  
 35  
 
 36  0
   private Rss20MappingUtils(){}
 37  
   
 38  
   @SuppressWarnings("unchecked")
 39  
   public static ItemEntry toItem(TRssItem ti) throws YarfrawException {
 40  6721
     if(ti == null){
 41  0
       return null;
 42  
     }
 43  6721
     ItemEntry item = new ItemEntry();
 44  6721
     for(Object o : ti.getTitleOrDescriptionOrLink()){
 45  55133
       if(o == null){
 46  0
         continue;
 47  
       }
 48  55133
       if(ti.getOtherAttributes() != null){
 49  55135
         for(Map.Entry<QName, String> e : ti.getOtherAttributes().entrySet()){
 50  18
           item.addOtherAttributes(e.getKey(), e.getValue());
 51  
         }
 52  
       }
 53  55135
       if (o instanceof JAXBElement) {
 54  38382
         JAXBElement jaxbElement = (JAXBElement) o;
 55  38382
         Object val = jaxbElement.getValue();
 56  38382
         if(same(jaxbElement.getName(), RSS20_AUTHOR)){
 57  1506
           item.addAuthorOrCreator((String)jaxbElement.getValue());
 58  36876
         }else if (same(jaxbElement.getName(), RSS20_COMMENTS)) {
 59  162
           item.setComments((String)jaxbElement.getValue());
 60  36714
         }else if (same(jaxbElement.getName(), RSS20_DESCRIPTION)) {
 61  6409
           item.setDescriptionOrSummary((String)jaxbElement.getValue());
 62  30305
         }else if (same(jaxbElement.getName(), RSS20_LINK)) {
 63  6637
           item.addLink((String)jaxbElement.getValue());
 64  23668
         }else if (same(jaxbElement.getName(), RSS20_PUBDATE)) {
 65  6520
           item.setPubDate((String)jaxbElement.getValue());
 66  17148
         }else if (same(jaxbElement.getName(), RSS20_TITLE)) {
 67  6667
           item.setTitle((String)jaxbElement.getValue());
 68  10481
         }else if (val instanceof TCategory) {
 69  3913
           TCategory cat = (TCategory) val;
 70  3913
           item.addCategorySubject(new CategorySubject().setCategoryOrSubjectOrTerm(cat.getValue())
 71  
                   .setDomainOrScheme(cat.getDomain()));
 72  3913
         }else if (val instanceof TEnclosure) {
 73  441
           TEnclosure en = (TEnclosure)val;
 74  441
           item.setEnclosure(new Enclosure(en.getUrl(), 
 75  
                     en.getLength() == null ? null : en.getLength().toString(), 
 76  
                     en.getType(), en.getValue()));
 77  441
         }else if (val instanceof TGuid) {
 78  6124
           TGuid guid = (TGuid)val;
 79  6124
           item.setUid(new Id(guid.getValue()).setPermaLink(guid.isIsPermaLink()));
 80  6124
         }else if (val instanceof TSource) {
 81  3
           TSource source = (TSource)val;
 82  3
           item.setSource(new Source(source.getUrl(), source.getValue()));
 83  3
         }else{
 84  0
           LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!");
 85  
         }
 86  38382
       }else if (o instanceof Element) {
 87  16753
         Element e = (Element) o;
 88  16753
         item.getOtherElements().add(e);
 89  16753
       }else{
 90  0
         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
 91  
       }
 92  
     }
 93  
 
 94  6721
     return item;
 95  
   }
 96  
 }