Coverage Report - yarfraw.mapping.forward.impl.ToRss10ChannelItemImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ToRss10ChannelItemImpl
88% 
100% 
0
 
 1  
 package yarfraw.mapping.forward.impl;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import javax.xml.bind.JAXBElement;
 6  
 
 7  
 import org.apache.commons.logging.Log;
 8  
 import org.apache.commons.logging.LogFactory;
 9  
 
 10  
 import yarfraw.core.datamodel.CategorySubject;
 11  
 import yarfraw.core.datamodel.FeedFormat;
 12  
 import yarfraw.core.datamodel.ItemEntry;
 13  
 import yarfraw.core.datamodel.YarfrawException;
 14  
 import yarfraw.generated.rss10.elements.DcType;
 15  
 import yarfraw.generated.rss10.elements.ObjectFactory;
 16  
 import yarfraw.generated.rss10.elements.TRss10Item;
 17  
 import yarfraw.mapping.forward.ToRss10ChannelItem;
 18  
 import yarfraw.utils.CommonUtils;
 19  
 /**
 20  
  * Util methods for mapping Yarfraw core model to Rss10 Jaxb model
 21  
  * @author jliang
 22  
  *
 23  
  */
 24  54
 public class ToRss10ChannelItemImpl  implements ToRss10ChannelItem{
 25  12
   private static ToRss10ChannelItem _instance = new ToRss10ChannelItemImpl();
 26  12
   private static final ObjectFactory FACTORY = new ObjectFactory();
 27  12
   private static final Log LOG = LogFactory.getLog(ToRss10ChannelItemImpl.class);
 28  
   public static ToRss10ChannelItem getInstance(){
 29  54
     return _instance;
 30  
   }
 31  
   
 32  12
   private ToRss10ChannelItemImpl(){}
 33  
   
 34  
   public JAXBElement<TRss10Item> execute(ItemEntry item)
 35  
           throws YarfrawException {
 36  54
     return toRss10Item(item);
 37  
   }
 38  
   
 39  
   /*
 40  
    * (title, link, description?)
 41  
    */
 42  
   public static JAXBElement<TRss10Item> toRss10Item(ItemEntry item){
 43  54
     TRss10Item ret = FACTORY.createTRss10Item();
 44  54
     List<Object> elementList = ret.getTitleOrDescriptionOrLink();
 45  
     
 46  54
     String author = Utils.getEmailOrText(item.getAuthorOrCreator());
 47  54
     if(author != null){
 48  39
       DcType dc = new DcType();
 49  39
       dc.setValue(author);
 50  39
       elementList.add(FACTORY.createCreator(dc));
 51  
     }
 52  
     
 53  54
     if(item.getRightsText() != null){
 54  9
       DcType dc = new DcType();
 55  9
       dc.setValue(item.getRightsText());
 56  9
       elementList.add(FACTORY.createRights(dc));
 57  
     }
 58  
     
 59  54
     if(item.getCategorySubjects() != null){
 60  39
       for(CategorySubject c : item.getCategorySubjects()){
 61  39
         if(c != null){
 62  39
           DcType dc = new DcType();
 63  39
           dc.setValue(c.getCategoryOrSubjectOrTerm());
 64  39
           elementList.add(FACTORY.createSubject(dc));
 65  39
         }
 66  
       }
 67  
     }
 68  
     
 69  54
     String contributor = Utils.getEmailOrText(item.getContributors());
 70  54
     if(contributor != null){
 71  0
       DcType dc = new DcType();
 72  0
       dc.setValue(contributor);
 73  0
       elementList.add(FACTORY.createContributor(dc));
 74  
     }
 75  
     
 76  
     //not supported
 77  54
     if(item.getComments() != null){
 78  0
       LOG.info("Item.Comments is not supported in Rss 1.0 feed. It will be ignored.");      
 79  
     }
 80  
     
 81  54
     if(item.getDescriptionOrSummaryText() != null){
 82  54
       elementList.add(FACTORY.createTRss10ItemDescription(item.getDescriptionOrSummaryText()));
 83  
     }
 84  
     
 85  
   //not supported
 86  54
     if(item.getEnclosure() != null){
 87  0
       LOG.warn("Item.Enclosure is not supported in Rss 1.0 feed. It will be ignored. Use Rss 2.0 or Atom 1.0 to add enclosure");
 88  
     }
 89  54
     if(item.getUid() != null){
 90  42
       LOG.warn("Item.uid is not supported in Rss 1.0 feed. It will be ignored. Use Rss 2.0 or Atom 1.0 to add unique Id");
 91  
     }
 92  54
     String link = Utils.getHrefLink(item.getLinks());
 93  54
     if(link != null){
 94  51
       elementList.add(FACTORY.createTRss10ItemLink(link));
 95  51
       ret.setAbout(link);
 96  
     }
 97  
     
 98  54
     if(item.getPubDate() != null){
 99  42
       String dateString = item.getPubDate();
 100  42
       if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.RSS10)){
 101  42
         String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.RSS10);
 102  42
         if(newDateString != null){
 103  42
           dateString = newDateString;
 104  
         }else{
 105  0
           LOG.warn("The dateString "+dateString+" is in valid according to RSS 1.0 specs, unabel to convert it to a valid format, writing it as is");
 106  
         }
 107  
       }
 108  42
       DcType dc = new DcType();
 109  42
       dc.setValue(dateString);
 110  42
       elementList.add(FACTORY.createDate(dc));
 111  
     }
 112  
     
 113  
     //not supported
 114  54
     if(item.getSource() != null){
 115  0
       LOG.info("Item.Source is not supported in Rss 1.0 feed. It will be ignored. ");
 116  
     }
 117  
     
 118  54
     if(item.getTitleText() != null){
 119  51
       elementList.add(FACTORY.createTRss10ItemTitle(item.getTitleText()));
 120  
     }
 121  
 
 122  54
     if(item.getOtherElements() != null){
 123  54
       elementList.addAll(item.getOtherElements());
 124  
     }
 125  54
     if(item.getOtherAttributes() != null){
 126  54
       ret.getOtherAttributes().putAll(item.getOtherAttributes());
 127  
     }
 128  
     
 129  54
     if(item.getAbout() != null){
 130  0
       ret.setAbout(item.getAbout());
 131  
     }
 132  54
     ret.setResource(item.getResource());
 133  
     
 134  54
     return FACTORY.createItem(ret);
 135  
   }
 136  
 }