Coverage Report - yarfraw.mapping.forward.impl.Rss20MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Rss20MappingUtils
94% 
94% 
0
 
 1  
 package yarfraw.mapping.forward.impl;
 2  
 
 3  
 import java.math.BigInteger;
 4  
 import java.util.List;
 5  
 
 6  
 import javax.xml.bind.JAXBElement;
 7  
 
 8  
 import org.apache.commons.logging.Log;
 9  
 import org.apache.commons.logging.LogFactory;
 10  
 
 11  
 import yarfraw.core.datamodel.CategorySubject;
 12  
 import yarfraw.core.datamodel.Cloud;
 13  
 import yarfraw.core.datamodel.Enclosure;
 14  
 import yarfraw.core.datamodel.FeedFormat;
 15  
 import yarfraw.core.datamodel.Id;
 16  
 import yarfraw.core.datamodel.Image;
 17  
 import yarfraw.core.datamodel.ItemEntry;
 18  
 import yarfraw.core.datamodel.Source;
 19  
 import yarfraw.core.datamodel.TextInput;
 20  
 import yarfraw.generated.rss20.elements.ObjectFactory;
 21  
 import yarfraw.generated.rss20.elements.TCategory;
 22  
 import yarfraw.generated.rss20.elements.TCloud;
 23  
 import yarfraw.generated.rss20.elements.TCloudProtocol;
 24  
 import yarfraw.generated.rss20.elements.TEnclosure;
 25  
 import yarfraw.generated.rss20.elements.TGuid;
 26  
 import yarfraw.generated.rss20.elements.TImage;
 27  
 import yarfraw.generated.rss20.elements.TRssItem;
 28  
 import yarfraw.generated.rss20.elements.TSource;
 29  
 import yarfraw.generated.rss20.elements.TTextInput;
 30  
 import yarfraw.utils.CommonUtils;
 31  
 
 32  
 /**
 33  
  * Util methods for mapping Yarfraw core model to Rss20 Jaxb model
 34  
  * @author jliang
 35  
  *
 36  
  */
 37  45
 class Rss20MappingUtils {
 38  45
   private static final ObjectFactory FACTORY = new ObjectFactory();
 39  45
   private static final Log LOG = LogFactory.getLog(Rss20MappingUtils.class);
 40  0
   private Rss20MappingUtils(){}
 41  
   public static JAXBElement<TRssItem> toRss20Item(ItemEntry item){
 42  597
     return FACTORY.createItem(toTItem(item));
 43  
   }
 44  
 
 45  
   public static JAXBElement<TTextInput> toRss20TextInput(TextInput input){
 46  3
     TTextInput ret = new TTextInput();
 47  3
     ret.setDescription(input.getDescription());
 48  3
     ret.setLink(input.getLink());
 49  3
     ret.setName(input.getName());
 50  3
     ret.setTitle(input.getTitle());
 51  3
     return FACTORY.createTRssChannelTextInput(ret);
 52  
   }
 53  
   
 54  
   public static JAXBElement<TImage> toRss20Image(Image image){
 55  3
     TImage ret = new TImage();
 56  3
     ret.setDescription(image.getDescription());
 57  3
     ret.setHeight(image.getHeight());
 58  3
     ret.setWidth(image.getWidth());
 59  3
     ret.setLink(image.getLink());
 60  3
     ret.setTitle(image.getTitle());
 61  3
     ret.setUrl(image.getUrl());
 62  3
     return FACTORY.createTRssChannelImage(ret);
 63  
   }
 64  
   
 65  
   public static JAXBElement<TCategory> toRss20Category(CategorySubject c){
 66  24
     TCategory ret = new TCategory();
 67  24
     ret.setDomain(c.getDomainOrScheme());
 68  24
     ret.setValue(c.getCategoryOrSubjectOrTerm());
 69  24
     return FACTORY.createTRssChannelCategory(ret);
 70  
   }
 71  
   
 72  
   public static JAXBElement<TCloud> toRss20Cloud(Cloud cl){
 73  3
     TCloud ret = new TCloud();
 74  3
     ret.setDomain(cl.getDomain());
 75  3
     ret.setPath(cl.getPath());
 76  3
     ret.setPort(new BigInteger(String.valueOf(cl.getPort())));
 77  3
     ret.setProtocol(TCloudProtocol.fromValue(cl.getProtocol()));
 78  3
     ret.setRegisterProcedure(cl.getRegisterProcedure());
 79  3
     return FACTORY.createTRssChannelCloud(ret);
 80  
   }
 81  
   
 82  
   private static JAXBElement<TEnclosure> toRss20Enclosure(Enclosure en){
 83  3
     TEnclosure ret = new TEnclosure();
 84  3
     ret.setLength(new BigInteger(String.valueOf(en.getLength())));
 85  3
     ret.setType(en.getMimeType());
 86  3
     ret.setUrl(en.getUrl());
 87  3
     ret.setValue(en.getValue());
 88  3
     return FACTORY.createTRssItemEnclosure(ret);
 89  
   }
 90  
   
 91  
   private static JAXBElement<TGuid> toRss20Guid(Id guid){
 92  564
     TGuid ret = new TGuid();
 93  564
     ret.setIsPermaLink(guid.isPermaLink());
 94  564
     ret.setValue(guid.getIdValue());
 95  564
     return FACTORY.createTRssItemGuid(ret);
 96  
   }
 97  
   
 98  
   private static TRssItem toTItem(ItemEntry item){
 99  597
     TRssItem ret = new ObjectFactory().createTRssItem();
 100  597
     List<Object> elementList = ret.getTitleOrDescriptionOrLink();
 101  597
     ObjectFactory factory = FACTORY;
 102  
     
 103  597
     String author = Utils.getEmailOrText(item.getAuthorOrCreator());
 104  597
     if(author != null){
 105  6
       elementList.add(factory.createTRssItemAuthor(author));
 106  
     }
 107  
     
 108  597
     if(item.getCategorySubjects() != null){
 109  30
       for(CategorySubject c : item.getCategorySubjects()){
 110  18
         if(c != null){
 111  18
           elementList.add(toRss20Category(c));
 112  
         }
 113  
       }
 114  
     }
 115  
     
 116  597
     if(item.getComments() != null){
 117  6
       elementList.add(factory.createTRssItemComments(item.getComments()));      
 118  
     }
 119  
     
 120  597
     if(item.getDescriptionOrSummaryText() != null){
 121  567
       elementList.add(factory.createTRssItemDescription(item.getDescriptionOrSummaryText()));
 122  
     }
 123  597
     if(item.getEnclosure() != null){
 124  3
       elementList.add(toRss20Enclosure(item.getEnclosure()));
 125  
     }
 126  597
     if(item.getUid() != null){
 127  564
       elementList.add(toRss20Guid(item.getUid()));
 128  
     }
 129  597
     String link = Utils.getHrefLink(item.getLinks());
 130  597
     if(link != null){
 131  528
       elementList.add(factory.createTRssItemLink(link));
 132  
     }
 133  
     
 134  597
     if(item.getPubDate() != null){
 135  555
       String dateString = item.getPubDate();
 136  555
       if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.RSS20)){
 137  0
         String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.RSS20);
 138  0
         if(newDateString != null){
 139  0
           dateString = newDateString;
 140  
         }else{
 141  0
           LOG.warn("The dateString "+dateString+" is in valid according to RSS 2.0 specs, unabel to convert it to a valid format, writing it as is");
 142  
         }
 143  
       }
 144  555
       elementList.add(factory.createTRssItemPubDate(dateString));
 145  
     }
 146  597
     if(item.getSource() != null){
 147  3
       elementList.add(toRss20Source(item.getSource()));
 148  
     }
 149  597
     if(item.getTitleText() != null){
 150  558
       elementList.add(factory.createTRssItemTitle(item.getTitleText()));
 151  
     }
 152  
     
 153  597
     if(item.getOtherElements() != null){
 154  597
       ret.getTitleOrDescriptionOrLink().addAll(item.getOtherElements());
 155  
     }
 156  597
     if(item.getOtherAttributes() != null){
 157  597
       ret.getOtherAttributes().putAll(item.getOtherAttributes());
 158  
     }
 159  597
     return ret;
 160  
   }
 161  
 
 162  
   private static JAXBElement<TSource> toRss20Source(Source s){
 163  3
     TSource ret = new TSource();
 164  3
     if(s.getUrl() != null){
 165  3
       ret.setUrl(s.getUrl());
 166  
     }
 167  3
     ret.setValue(s.getSource());
 168  3
     return FACTORY.createTRssItemSource(ret);
 169  
   }
 170  
   
 171  
 }