Coverage Report - yarfraw.mapping.backward.impl.ToChannelAtom10Impl
 
Classes in this File Line Coverage Branch Coverage Complexity
ToChannelAtom10Impl
93% 
100% 
0
 
 1  
 package yarfraw.mapping.backward.impl;
 2  
 import static yarfraw.io.parser.ElementQName.ATOM10_AUTHOR;
 3  
 import static yarfraw.io.parser.ElementQName.ATOM10_CONTRIBUTOR;
 4  
 import static yarfraw.io.parser.ElementQName.ATOM10_RIGHTS;
 5  
 import static yarfraw.io.parser.ElementQName.ATOM10_SUBTITLE;
 6  
 import static yarfraw.io.parser.ElementQName.ATOM10_TITLE;
 7  
 import static yarfraw.io.parser.ElementQName.ATOM10_UPDATED;
 8  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toAtomLink;
 9  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toCategorySubject;
 10  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toId;
 11  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toImage;
 12  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toItem;
 13  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toPersonType;
 14  
 import static yarfraw.mapping.backward.impl.Atom10MappingUtils.toText;
 15  
 import static yarfraw.utils.XMLUtils.same;
 16  
 
 17  
 import javax.xml.bind.JAXBElement;
 18  
 
 19  
 import org.apache.commons.lang.builder.ToStringBuilder;
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 import org.w3c.dom.Element;
 23  
 
 24  
 import yarfraw.core.datamodel.ChannelFeed;
 25  
 import yarfraw.core.datamodel.Generator;
 26  
 import yarfraw.core.datamodel.Image;
 27  
 import yarfraw.generated.atom10.elements.CategoryType;
 28  
 import yarfraw.generated.atom10.elements.DateTimeType;
 29  
 import yarfraw.generated.atom10.elements.EntryType;
 30  
 import yarfraw.generated.atom10.elements.FeedType;
 31  
 import yarfraw.generated.atom10.elements.GeneratorType;
 32  
 import yarfraw.generated.atom10.elements.IconType;
 33  
 import yarfraw.generated.atom10.elements.IdType;
 34  
 import yarfraw.generated.atom10.elements.LinkType;
 35  
 import yarfraw.generated.atom10.elements.LogoType;
 36  
 import yarfraw.generated.atom10.elements.PersonType;
 37  
 import yarfraw.generated.atom10.elements.TextType;
 38  
 import yarfraw.mapping.backward.ToChannelAtom10;
 39  
 /**
 40  
  * TODO: document me
 41  
  * @author jliang
 42  
  *
 43  
  */
 44  42
 public class ToChannelAtom10Impl implements ToChannelAtom10{
 45  
 
 46  15
   private static final Log LOG = LogFactory.getLog(ToChannelAtom10Impl.class);
 47  15
   private static final ToChannelAtom10 _instance = new ToChannelAtom10Impl();
 48  
     
 49  15
   private ToChannelAtom10Impl() {}
 50  
   public static ToChannelAtom10 getInstance(){
 51  42
     return _instance;
 52  
   }
 53  
   /**
 54  
    * atomFeed =
 55  
    element atom:feed {
 56  
       atomCommonAttributes,
 57  
       (atomAuthor*
 58  
        & atomCategory*
 59  
        & atomContributor*
 60  
        & atomGenerator?
 61  
        & atomIcon?
 62  
        & atomId
 63  
        & atomLink*
 64  
        & atomLogo?
 65  
        & atomRights?
 66  
        & atomSubtitle?
 67  
        & atomTitle
 68  
        & atomUpdated
 69  
        & extensionElement*),
 70  
       atomEntry*
 71  
    }
 72  
    */
 73  
   public ChannelFeed execute(FeedType feed){
 74  42
     if(feed == null){
 75  0
       return null;
 76  
     }
 77  42
     ChannelFeed c = new ChannelFeed();
 78  
 
 79  42
     c.setLang(feed.getLang());
 80  42
     c.setBase(feed.getBase());
 81  42
     c.getOtherAttributes().putAll(feed.getOtherAttributes());
 82  42
     for(Object o : feed.getAuthorOrCategoryOrContributor()){
 83  765
       if(o == null){
 84  0
         continue;
 85  
       }
 86  765
       if (o instanceof JAXBElement<?>) {
 87  696
         JAXBElement<?> jaxbElement = (JAXBElement<?>) o;
 88  696
         Object val = jaxbElement.getValue();
 89  696
         if (same(jaxbElement.getName(), ATOM10_TITLE)) {
 90  42
           TextType text = (TextType) val;
 91  42
           c.setTitle(toText(text));
 92  42
         }else if (same(jaxbElement.getName(), ATOM10_SUBTITLE)) {
 93  18
           TextType text = (TextType) val;
 94  18
           c.setDescriptionOrSubtitle(toText(text));
 95  18
         }else if (same(jaxbElement.getName(), ATOM10_AUTHOR)) {
 96  15
           c.addManagingEditorOrAuthorOrPublisher(toPersonType((PersonType)val));
 97  621
         }else if(same(jaxbElement.getName(), ATOM10_CONTRIBUTOR)){
 98  0
           c.addContributor(toPersonType((PersonType)val));
 99  621
         }else if(val instanceof CategoryType){
 100  12
           c.addCategorySubject(toCategorySubject((CategoryType)val));
 101  609
         }else if (val instanceof GeneratorType) {
 102  27
           GeneratorType gen = (GeneratorType)val;
 103  27
           c.setGenerator(toGenerator(gen));
 104  27
         }else if(val instanceof IconType){
 105  6
           c.setImageOrIcon(toImage((IconType)val));
 106  576
         }else if(val instanceof IdType){
 107  54
           c.setUid(toId((IdType)val));
 108  522
         }else if(val instanceof LinkType){ 
 109  90
           c.addLink(toAtomLink((LinkType)val));
 110  432
         }else if(val instanceof LogoType){ 
 111  3
           LogoType logo = (LogoType)val;
 112  3
           c.setLogo(toLogo(logo));
 113  3
         }
 114  429
         else if (same(jaxbElement.getName(), ATOM10_RIGHTS)) {
 115  21
           TextType text = (TextType) val;
 116  21
           c.setRights(toText(text));
 117  21
         }else if (same(jaxbElement.getName(), ATOM10_UPDATED)) {
 118  
           //partially supported
 119  42
           DateTimeType dt = (DateTimeType) val;
 120  42
           if(dt.getValue() != null){
 121  42
             c.setLastBuildOrUpdatedDate(dt.getValue());
 122  
           }
 123  42
         }else if(val instanceof EntryType){ 
 124  366
           c.addItem(toItem((EntryType)val));
 125  
         }else{
 126  0
           LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!");
 127  
         }
 128  696
       }
 129  69
       else if (o instanceof Element) {
 130  69
         Element e = (Element) o;
 131  69
         c.getOtherElements().add(e);
 132  69
       }else{
 133  0
         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
 134  
       }
 135  
     }                                           
 136  
     
 137  42
     return c;
 138  
   }
 139  
   
 140  
   private static Generator toGenerator(GeneratorType in){
 141  27
     Generator ret = new Generator(in.getValue());
 142  27
     ret.setUri(in.getUri());
 143  27
     ret.setVersion(in.getVersion());
 144  27
     ret.setLang(in.getLang());
 145  27
     ret.setBase(in.getBase());
 146  27
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 147  27
     return ret;
 148  
     
 149  
   }
 150  
   public static Image toLogo(LogoType in){
 151  3
     Image ret= new Image();
 152  3
     ret.setUrl(in.getValue());
 153  3
     ret.setBase(in.getBase());
 154  3
     ret.setLang(in.getLang());
 155  3
     if(in.getOtherAttributes() != null){
 156  3
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 157  
     }
 158  
     
 159  3
     return ret;
 160  
   }
 161  
 
 162  
 }