Coverage Report - yarfraw.mapping.backward.impl.Atom10MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Atom10MappingUtils
86% 
95% 
0
 
 1  
 package yarfraw.mapping.backward.impl;
 2  
 
 3  
 import static yarfraw.io.parser.ElementQName.ATOM10_AUTHOR;
 4  
 import static yarfraw.io.parser.ElementQName.ATOM10_CONTRIBUTOR;
 5  
 import static yarfraw.io.parser.ElementQName.ATOM10_EMAIL;
 6  
 import static yarfraw.io.parser.ElementQName.ATOM10_NAME;
 7  
 import static yarfraw.io.parser.ElementQName.ATOM10_PUBLISHED;
 8  
 import static yarfraw.io.parser.ElementQName.ATOM10_RIGHTS;
 9  
 import static yarfraw.io.parser.ElementQName.ATOM10_SUMMARY;
 10  
 import static yarfraw.io.parser.ElementQName.ATOM10_TITLE;
 11  
 import static yarfraw.io.parser.ElementQName.ATOM10_UPDATED;
 12  
 
 13  
 import javax.xml.bind.JAXBElement;
 14  
 
 15  
 import org.apache.commons.lang.StringUtils;
 16  
 import org.apache.commons.lang.builder.ToStringBuilder;
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.w3c.dom.Element;
 20  
 
 21  
 import yarfraw.core.datamodel.CategorySubject;
 22  
 import yarfraw.core.datamodel.Content;
 23  
 import yarfraw.core.datamodel.Id;
 24  
 import yarfraw.core.datamodel.Image;
 25  
 import yarfraw.core.datamodel.ItemEntry;
 26  
 import yarfraw.core.datamodel.Link;
 27  
 import yarfraw.core.datamodel.Person;
 28  
 import yarfraw.core.datamodel.Text;
 29  
 import yarfraw.generated.atom10.elements.CategoryType;
 30  
 import yarfraw.generated.atom10.elements.ContentType;
 31  
 import yarfraw.generated.atom10.elements.DateTimeType;
 32  
 import yarfraw.generated.atom10.elements.EntryType;
 33  
 import yarfraw.generated.atom10.elements.IconType;
 34  
 import yarfraw.generated.atom10.elements.IdType;
 35  
 import yarfraw.generated.atom10.elements.LinkType;
 36  
 import yarfraw.generated.atom10.elements.PersonType;
 37  
 import yarfraw.generated.atom10.elements.TextType;
 38  
 import yarfraw.generated.atom10.elements.UriType;
 39  
 import yarfraw.utils.XMLUtils;
 40  
 
 41  
 /**
 42  
  * TODO: document me
 43  
  * @author jliang
 44  
  *
 45  
  */
 46  0
 class Atom10MappingUtils{
 47  
 
 48  15
   private static final Log LOG = LogFactory.getLog(Atom10MappingUtils.class);
 49  
   
 50  
   public static Text toText(TextType in){
 51  675
     Text ret = new Text();
 52  675
     for(Object o : in.getContent()){
 53  675
       if(o == null){
 54  0
         continue;
 55  
       }
 56  675
       if (o instanceof JAXBElement<?>) {
 57  0
         JAXBElement<?> jaxb = (JAXBElement<?>) o;
 58  0
         Object val = jaxb.getValue();
 59  0
         ret.setText(String.valueOf(val));
 60  0
       }else if(o instanceof Element){
 61  0
         ret.setXhtmlDiv((Element)o);
 62  
       }else{
 63  675
         ret.setText(String.valueOf(o));
 64  
       }
 65  
     }
 66  
     
 67  675
     ret.setLang(in.getLang());
 68  675
     ret.setBase(in.getBase());
 69  675
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 70  
     
 71  675
     return ret;
 72  
   }
 73  
   
 74  
   public static CategorySubject toCategorySubject(CategoryType in) {
 75  78
     CategorySubject ret = new CategorySubject();
 76  78
     ret.setLabel(in.getLabel());
 77  78
     ret.setCategoryOrSubjectOrTerm(in.getTerm());
 78  78
     ret.setDomainOrScheme(in.getScheme());
 79  
     
 80  78
     ret.setLang(in.getLang());
 81  78
     ret.setBase(in.getBase());
 82  78
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 83  78
     return ret;
 84  
   }
 85  
   
 86  
   public static Person toPersonType(PersonType in){
 87  387
     Person ret = new Person();
 88  
     
 89  387
     for(Object o : in.getNameOrUriOrEmail()){
 90  687
       if(o == null){
 91  0
         continue;
 92  
       }
 93  687
       if (o instanceof JAXBElement<?>) {
 94  687
         JAXBElement<?> jaxb = (JAXBElement<?>) o;
 95  687
         Object val = jaxb.getValue();
 96  687
         if(XMLUtils.same(jaxb.getName(), ATOM10_EMAIL)){
 97  150
           ret.setEmailOrText((String)val);
 98  537
         }else if(XMLUtils.same(jaxb.getName(), ATOM10_NAME)){
 99  387
           ret.setName((String)val);
 100  150
         }else if(val instanceof UriType){
 101  150
           ret.setUri(((UriType)val).getValue());
 102  
         }else{
 103  0
           LOG.warn("Unexpected JAXB Element: "+ ToStringBuilder.reflectionToString(val));
 104  
         }
 105  687
       }else if(o instanceof Element){
 106  0
         ret.getOtherElements().add((Element)o);
 107  
       }else{
 108  0
         LOG.warn("Unexpected Element: "+ ToStringBuilder.reflectionToString(o));
 109  
       }
 110  
     }
 111  
     
 112  387
     ret.setLang(in.getLang());
 113  387
     ret.setBase(in.getBase());
 114  387
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 115  
     
 116  387
     return ret;
 117  
   }
 118  
   
 119  
   private static Content toContent(ContentType in){
 120  264
     Content ret = new Content();
 121  264
     for(Object o : in.getContent()){
 122  288
       if(o == null){
 123  0
         continue;
 124  
       }
 125  288
       if (o instanceof JAXBElement<?>) {
 126  0
         JAXBElement<?> jaxb = (JAXBElement<?>) o;
 127  0
         Object val = jaxb.getValue();
 128  0
         ret.addContentText(String.valueOf(val));
 129  0
       }else if(o instanceof Element){
 130  24
         ret.getOtherElements().add((Element)o);
 131  
       }else{
 132  264
         String s = String.valueOf(o);
 133  264
         if(StringUtils.isNotBlank(s)){
 134  240
           ret.addContentText(s);
 135  
         }
 136  288
       }
 137  
     }
 138  
     
 139  264
     ret.setSrc(in.getSrc());
 140  264
     ret.setType(in.getType());
 141  264
     ret.setLang(in.getLang());
 142  264
     ret.setBase(in.getBase());
 143  264
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 144  264
     return ret;
 145  
   }
 146  
   
 147  
   /**
 148  
    * atomEntry =
 149  
    element atom:entry {
 150  
       atomCommonAttributes,
 151  
       (atomAuthor*
 152  
        & atomCategory*
 153  
        & atomContent?
 154  
        & atomContributor*
 155  
        & atomId
 156  
        & atomLink*
 157  
        & atomPublished?
 158  
        & atomRights?
 159  
        & atomSource?
 160  
        & atomSummary?
 161  
        & atomTitle
 162  
        & atomUpdated
 163  
        & extensionElement*)
 164  
    }
 165  
    * @param in
 166  
    * @return
 167  
    */
 168  
   public static ItemEntry toItem(EntryType in){
 169  
     
 170  366
     ItemEntry ret = new ItemEntry();
 171  
 
 172  366
     ret.setLang(in.getLang());
 173  366
     ret.setBase(in.getBase());
 174  366
     ret.getOtherAttributes().putAll(in.getOtherAttributes());
 175  
     
 176  366
     for(Object o : in.getAuthorOrCategoryOrContent()){
 177  3300
       if(o == null){
 178  0
         continue;
 179  
       }
 180  3300
       if (o instanceof JAXBElement<?>) {
 181  3246
         JAXBElement<?> jaxb = (JAXBElement<?>) o;
 182  3246
         Object val = jaxb.getValue();
 183  
         
 184  3246
         if (XMLUtils.same(jaxb.getName(), ATOM10_AUTHOR)) {
 185  354
           ret.addAuthorOrCreator(toPersonType((PersonType)val));
 186  2892
         }else if(val instanceof CategoryType){
 187  66
           ret.addCategorySubject(toCategorySubject((CategoryType)val));
 188  2826
         }else if(val instanceof ContentType){
 189  264
           ret.setContent(toContent((ContentType)val));
 190  2562
         }else if(XMLUtils.same(jaxb.getName(), ATOM10_CONTRIBUTOR)){
 191  18
           ret.addContributor(toPersonType((PersonType)val));
 192  
         }
 193  
         //contributor are ignored
 194  2544
         else if(val instanceof LinkType){ 
 195  960
           ret.addLink(toAtomLink((LinkType)val));
 196  1584
         }else if (XMLUtils.same(jaxb.getName(), ATOM10_PUBLISHED)) {
 197  
           //partially supported
 198  264
           DateTimeType dt = (DateTimeType) val;
 199  264
           if(dt.getValue() != null){
 200  264
             ret.setPubDate(dt.getValue());
 201  
           }
 202  264
         }else if (XMLUtils.same(jaxb.getName(), ATOM10_RIGHTS)) {
 203  6
           TextType text = (TextType) val;
 204  6
           ret.setRights(toText(text));
 205  6
         }//FIXME: source not supported
 206  1314
         else if (XMLUtils.same(jaxb.getName(), ATOM10_SUMMARY)) {
 207  222
           TextType text = (TextType) val;
 208  222
           ret.setDescriptionOrSummary(toText(text));
 209  222
         }else if (XMLUtils.same(jaxb.getName(), ATOM10_TITLE)) {
 210  366
           TextType text = (TextType) val;
 211  366
           ret.setTitle(toText(text));
 212  366
         }else if (XMLUtils.same(jaxb.getName(), ATOM10_UPDATED)) {
 213  
           //  partially supported
 214  360
           DateTimeType dt = (DateTimeType) val;
 215  360
           if(dt.getValue() != null){
 216  360
             ret.setUpdatedDate(dt.getValue());
 217  
           }
 218  360
         }else if(val instanceof IdType){
 219  366
           ret.setUid(toId((IdType)val));
 220  
         }else{
 221  0
           LOG.warn("Unexpected JAXB Element: "+ ToStringBuilder.reflectionToString(val));
 222  
         }
 223  3246
       }else if (o instanceof Element) {
 224  54
         Element e = (Element) o;
 225  54
         ret.getOtherElements().add(e);
 226  54
       }else{
 227  0
         LOG.warn("Unexpected Element: "+ ToStringBuilder.reflectionToString(o));
 228  
       }
 229  
     }
 230  366
     return ret;
 231  
   }
 232  
   
 233  
   public static Link toAtomLink(LinkType link){
 234  1050
     Link ret = new Link();
 235  1050
     ret.setBase(link.getBase());
 236  1050
     ret.setLang(link.getLang());
 237  1050
     if(link.getOtherAttributes() != null){
 238  1050
       ret.getOtherAttributes().putAll(link.getOtherAttributes());
 239  
     }
 240  
 
 241  1050
     ret.setHref(link.getHref());
 242  1050
     ret.setHreflang(link.getHreflang());
 243  1050
     ret.setLength(link.getLength() == null ? null : link.getLength().intValue());
 244  1050
     ret.setRel(link.getRel());
 245  1050
     ret.setTitle(link.getTitle());
 246  1050
     ret.setType(link.getType());
 247  
     
 248  1050
     return ret;
 249  
   }
 250  
   
 251  
   public static Id toId(IdType in){
 252  420
     Id ret = new Id();
 253  420
     ret.setBase(in.getBase());
 254  420
     ret.setLang(in.getLang());
 255  420
     if(in.getOtherAttributes() != null){
 256  420
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 257  
     }
 258  420
     ret.setIdValue(in.getValue());
 259  420
     return ret;
 260  
   }
 261  
   
 262  
   public static Image toImage(IconType in){
 263  6
     Image ret= new Image();
 264  6
     ret.setUrl(in.getValue());
 265  6
     ret.setBase(in.getBase());
 266  6
     ret.setLang(in.getLang());
 267  6
     if(in.getOtherAttributes() != null){
 268  6
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 269  
     }
 270  
     
 271  6
     return ret;
 272  
   }
 273  
 
 274  
 }