Coverage Report - yarfraw.mapping.backward.impl.Rss10MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Rss10MappingUtils
93% 
100% 
0
 
 1  201
 package yarfraw.mapping.backward.impl;
 2  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_CONTRIBUTOR;
 3  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_CREATOR;
 4  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_DATE;
 5  
 import static yarfraw.io.parser.ElementQName.RSS10_DESCRIPTION;
 6  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_LANGUAGE;
 7  
 import static yarfraw.io.parser.ElementQName.RSS10_LINK;
 8  
 import static yarfraw.io.parser.ElementQName.RSS10_NAME;
 9  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_PUBLISHER;
 10  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_RIGHTS;
 11  
 import static yarfraw.io.parser.ElementQName.RSS10_DC_SUBJECT;
 12  
 import static yarfraw.io.parser.ElementQName.RSS10_TITLE;
 13  
 import static yarfraw.io.parser.ElementQName.RSS10_UPDATEFREQUENCY;
 14  
 import static yarfraw.utils.XMLUtils.same;
 15  
 import static yarfraw.mapping.backward.impl.Utils.getDcTypeText;
 16  
 import java.math.BigInteger;
 17  
 import java.util.ArrayList;
 18  
 import java.util.Collections;
 19  
 import java.util.Comparator;
 20  
 import java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.xml.bind.JAXBElement;
 25  
 
 26  
 import org.apache.commons.lang.builder.ToStringBuilder;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 import org.w3c.dom.Element;
 30  
 
 31  
 import yarfraw.core.datamodel.ChannelFeed;
 32  
 import yarfraw.core.datamodel.Image;
 33  
 import yarfraw.core.datamodel.ItemEntry;
 34  
 import yarfraw.core.datamodel.TextInput;
 35  
 import yarfraw.generated.rss10.elements.Items;
 36  
 import yarfraw.generated.rss10.elements.Li;
 37  
 import yarfraw.generated.rss10.elements.RDF;
 38  
 import yarfraw.generated.rss10.elements.Seq;
 39  
 import yarfraw.generated.rss10.elements.TRss10Channel;
 40  
 import yarfraw.generated.rss10.elements.TRss10Image;
 41  
 import yarfraw.generated.rss10.elements.TRss10Item;
 42  
 import yarfraw.generated.rss10.elements.TRss10TextInput;
 43  
 import yarfraw.generated.rss10.elements.UpdatePeriodEnum;
 44  
 import yarfraw.utils.CommonUtils;
 45  15
 class Rss10MappingUtils{
 46  
 
 47  15
   private static final Log LOG = LogFactory.getLog(Rss10MappingUtils.class);
 48  
   
 49  0
   private Rss10MappingUtils(){}
 50  
   
 51  
   public static Integer calculateTtl(UpdatePeriodEnum updatePeriod, BigInteger updateFrequency){
 52  27
     return CommonUtils.calculateTtl(updatePeriod, updateFrequency);
 53  
   }
 54  
   
 55  
   @SuppressWarnings("unchecked")
 56  
   public static ChannelFeed toChannel(TRss10Channel ch, RDF rdf){
 57  27
     ChannelFeed ret = new ChannelFeed();
 58  27
     if(ch.getOtherAttributes() != null){
 59  27
       ret.getOtherAttributes().putAll(ch.getOtherAttributes());
 60  
     }
 61  27
     UpdatePeriodEnum updatePeriod = null;
 62  27
     BigInteger updateFrequency = null;
 63  27
     Map<String, Integer> ordering = new HashMap<String, Integer>();
 64  27
     List<ItemEntry> items = toItems(rdf.getChannelOrImageOrItem());
 65  27
     ret.setAbout(ch.getAbout());
 66  27
     ret.setResource(ch.getResource());
 67  27
     ret.getOtherAttributes().putAll(ch.getOtherAttributes());
 68  324
     for(Object o : ch.getTitleOrLinkOrDescription()){
 69  270
       if(o == null)
 70  0
         continue;
 71  
       
 72  270
       if (o instanceof JAXBElement) {
 73  240
         JAXBElement jaxb = (JAXBElement) o;
 74  240
         Object val = jaxb.getValue();
 75  240
         if(same(jaxb.getName(), RSS10_TITLE)){
 76  27
           ret.setTitle((String)jaxb.getValue());
 77  213
         }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
 78  30
           ret.setDescriptionOrSubtitle((String)jaxb.getValue());
 79  183
         }else if(same(jaxb.getName(), RSS10_LINK)){
 80  27
           ret.addLink((String)jaxb.getValue());
 81  156
         }else if(same(jaxb.getName(), RSS10_UPDATEFREQUENCY)){
 82  12
           updateFrequency = (BigInteger)jaxb.getValue();
 83  144
         }else if(same(jaxb.getName(), RSS10_DC_SUBJECT)){
 84  0
           ret.addCategorySubject(getDcTypeText(jaxb));
 85  144
         }else if(same(jaxb.getName(), RSS10_DC_PUBLISHER)){
 86  12
           ret.addManagingEditorOrAuthorOrPublisher(getDcTypeText(jaxb));
 87  132
         }else if(same(jaxb.getName(), RSS10_DC_CREATOR)){
 88  21
           ret.addWebMasterOrCreator(getDcTypeText(jaxb));
 89  111
         }else if(same(jaxb.getName(), RSS10_DC_RIGHTS)){
 90  12
           ret.setRights(getDcTypeText(jaxb));
 91  99
         }else if(same(jaxb.getName(), RSS10_DC_DATE)){
 92  21
           ret.setPubDate(getDcTypeText(jaxb));
 93  78
         }else if(same(jaxb.getName(), RSS10_DC_LANGUAGE)){
 94  12
           ret.setLang(getDcTypeText(jaxb));
 95  66
         }else if(same(jaxb.getName(), RSS10_DC_CONTRIBUTOR)){
 96  6
           ret.addContributor(getDcTypeText(jaxb));
 97  60
         }else if(val instanceof UpdatePeriodEnum){
 98  12
           updatePeriod = (UpdatePeriodEnum)val;
 99  48
         }else if(val instanceof TRss10Image){
 100  
           //this only put resources
 101  12
           Image image = new Image();
 102  12
           image.setResource(((TRss10Image)val).getResource());
 103  12
           ret.setImageOrIcon(image);
 104  36
         }else if(val instanceof TRss10TextInput){
 105  
           //this only put resources
 106  9
           TextInput in = new TextInput();
 107  9
           in.setResource(((TRss10TextInput)val).getResource());
 108  9
           ret.setTexInput(in);
 109  27
         }else if(val instanceof Items){
 110  27
           Seq seq = ((Items)val).getSeq();
 111  27
           int i = 0;
 112  282
           for(Li li : seq.getLi()){
 113  228
             ordering.put(li.getResource(), i++);
 114  
           }
 115  
         }else{
 116  0
           LOG.warn("Unexpected JAXBElement: "+ToStringBuilder.reflectionToString(jaxb)+" this should not happen!");
 117  
         }
 118  30
       }else if (o instanceof Element) {
 119  30
         Element e = (Element) o;
 120  30
         ret.getOtherElements().add(e);
 121  
       }else{
 122  0
         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
 123  
       }
 124  
     }  
 125  27
     ret.setTtl(calculateTtl(updatePeriod, updateFrequency));
 126  27
     if(ordering.entrySet().size() != 0){
 127  27
       Collections.sort(items, new ItemComparacotr(ordering)); 
 128  
     }
 129  27
     ret.setItems(items);
 130  
     
 131  27
     return ret;
 132  
   }
 133  
   
 134  
   private static class ItemComparacotr implements Comparator<ItemEntry>{
 135  27
     Map<String, Integer> _ordering = null;
 136  27
     public ItemComparacotr(Map<String, Integer> ordering){
 137  27
       _ordering = ordering;
 138  27
     }
 139  
     public int compare(ItemEntry o1, ItemEntry o2) {
 140  201
       Integer ord1 = _ordering.get(o1.getAbout());
 141  201
       Integer ord2 = _ordering.get(o2.getAbout());
 142  201
       return (ord1 != null && ord2 != null) ? ord1.compareTo(ord2) : 0;
 143  
     }
 144  
   }
 145  
   
 146  
   @SuppressWarnings("unchecked")
 147  
   private static List<ItemEntry> toItems(List<Object> objs){
 148  27
     List<ItemEntry> items = new ArrayList<ItemEntry>();
 149  330
     for(Object o : objs){
 150  276
       if (o instanceof JAXBElement) {
 151  276
         Object value = ((JAXBElement)o).getValue();
 152  276
         if(value instanceof TRss10Item){
 153  228
           TRss10Item it = (TRss10Item)value;
 154  228
           ItemEntry item = new ItemEntry();
 155  1992
           for(Object io : it.getTitleOrDescriptionOrLink()){
 156  1536
             if (io instanceof JAXBElement) {
 157  1314
               JAXBElement jaxb = (JAXBElement) io;
 158  1314
               if(same(jaxb.getName(), RSS10_TITLE)){
 159  228
                 item.setTitle((String)jaxb.getValue());
 160  1086
               }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
 161  222
                 item.setDescriptionOrSummary((String)jaxb.getValue());
 162  864
               }else if(same(jaxb.getName(), RSS10_LINK)){
 163  228
                 item.addLink((String)jaxb.getValue());
 164  636
               }else if(same(jaxb.getName(), RSS10_DC_CONTRIBUTOR)){
 165  6
                 item.addContributor(getDcTypeText(jaxb));
 166  630
               }else if(same(jaxb.getName(), RSS10_DC_CREATOR)){
 167  222
                 item.addAuthorOrCreator(getDcTypeText(jaxb));
 168  408
               }else if(same(jaxb.getName(), RSS10_DC_RIGHTS)){
 169  9
                 item.setRights(getDcTypeText(jaxb));
 170  399
               }else if(same(jaxb.getName(), RSS10_DC_DATE)){
 171  213
                 item.setPubDate(getDcTypeText(jaxb));
 172  186
               }else if(same(jaxb.getName(), RSS10_DC_SUBJECT)){
 173  186
                 item.addCategorySubject(getDcTypeText(jaxb));
 174  
               }else{
 175  0
                 LOG.warn("Unexpected jaxbElement under <item>: "+ ToStringBuilder.reflectionToString(jaxb));
 176  
               }
 177  222
             }else if (io instanceof Element) {
 178  222
               Element e = (Element) io;
 179  222
               item.getOtherElements().add(e);
 180  
             }else{
 181  0
               LOG.warn("Unexpected object under <item>: "+ToStringBuilder.reflectionToString(io));
 182  
             }
 183  
           }
 184  
           
 185  228
           if(it.getResource() == null){
 186  228
             it.setResource(Utils.getHrefLink(item.getLinks()));
 187  
           }
 188  
           
 189  228
           items.add(item);
 190  
         }
 191  
       }
 192  
     }
 193  27
     return items;
 194  
   }
 195  
   
 196  
   /**
 197  
    * Copies everything except 'resource' over to <code>ret</code>.
 198  
    * @param ret
 199  
    * @param input
 200  
    * @return
 201  
    */
 202  
   public static TextInput populateTextinput(TextInput ret, TRss10TextInput input){
 203  66
     for(Object o : input.getTitleOrDescriptionOrName()){
 204  48
       if(o == null)
 205  0
         continue;
 206  
       
 207  48
       if (o instanceof JAXBElement) {
 208  36
         JAXBElement<?> jaxb = (JAXBElement<?>) o;
 209  36
         if(same(jaxb.getName(), RSS10_TITLE)){
 210  9
           ret.setTitle((String)jaxb.getValue());
 211  27
         }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
 212  9
           ret.setDescription((String)jaxb.getValue());
 213  18
         }else if(same(jaxb.getName(), RSS10_LINK)){
 214  9
           ret.setLink((String)jaxb.getValue());
 215  9
         }else if(same(jaxb.getName(), RSS10_NAME)){
 216  9
           ret.setName((String)jaxb.getValue());
 217  
         }else{
 218  0
           LOG.warn("Unexpected JAXBElement: "+ToStringBuilder.reflectionToString(jaxb)+" this should not happen!");
 219  
         }
 220  12
       }else if (o instanceof Element) {
 221  12
         Element e = (Element) o;
 222  12
         ret.getOtherElements().add(e);
 223  
       }else{
 224  0
         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
 225  
       }
 226  
     }
 227  
     
 228  9
     ret.setAbout(input.getAbout());
 229  
     
 230  9
     return ret;
 231  
   }
 232  
   
 233  
   /**
 234  
    * Copies everything except 'resource' over to <code>ret</code>.
 235  
    * @param ret
 236  
    * @param img
 237  
    * @return
 238  
    */
 239  
   public static Image populateImage(Image ret, TRss10Image img){
 240  12
     ret.setLink(img.getLink());
 241  12
     ret.setTitle(img.getTitle());
 242  12
     ret.setUrl(img.getUrl());
 243  12
     ret.setAbout(img.getAbout());
 244  
     
 245  12
     return ret;
 246  
   }
 247  
 }