Coverage Report - yarfraw.mapping.forward.impl.Atom10MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Atom10MappingUtils
91% 
95% 
0
 
 1  
 package yarfraw.mapping.forward.impl;
 2  
 
 3  
 import java.math.BigInteger;
 4  
 import java.util.List;
 5  
 
 6  
 import org.apache.commons.logging.Log;
 7  
 import org.apache.commons.logging.LogFactory;
 8  
 
 9  
 import yarfraw.core.datamodel.CategorySubject;
 10  
 import yarfraw.core.datamodel.Content;
 11  
 import yarfraw.core.datamodel.FeedFormat;
 12  
 import yarfraw.core.datamodel.Id;
 13  
 import yarfraw.core.datamodel.Image;
 14  
 import yarfraw.core.datamodel.ItemEntry;
 15  
 import yarfraw.core.datamodel.Link;
 16  
 import yarfraw.core.datamodel.Person;
 17  
 import yarfraw.core.datamodel.Text;
 18  
 import yarfraw.core.datamodel.YarfrawException;
 19  
 import yarfraw.generated.atom10.elements.CategoryType;
 20  
 import yarfraw.generated.atom10.elements.ContentType;
 21  
 import yarfraw.generated.atom10.elements.DateTimeType;
 22  
 import yarfraw.generated.atom10.elements.EntryType;
 23  
 import yarfraw.generated.atom10.elements.IconType;
 24  
 import yarfraw.generated.atom10.elements.IdType;
 25  
 import yarfraw.generated.atom10.elements.LinkType;
 26  
 import yarfraw.generated.atom10.elements.ObjectFactory;
 27  
 import yarfraw.generated.atom10.elements.PersonType;
 28  
 import yarfraw.generated.atom10.elements.TextType;
 29  
 import yarfraw.generated.atom10.elements.UriType;
 30  
 import yarfraw.utils.CommonUtils;
 31  
 /**
 32  
  * Util methods for mapping Yarfraw core model to Atom10 Jaxb model
 33  
  * @author jliang
 34  
  *
 35  
  */
 36  
 public class Atom10MappingUtils{
 37  9
   private static final ObjectFactory FACTORY = new ObjectFactory ();
 38  9
   private static final Log LOG = LogFactory.getLog(Atom10MappingUtils.class);
 39  0
   private Atom10MappingUtils(){}
 40  
 
 41  
   public static LinkType toLink(Link link){
 42  159
     LinkType ret = FACTORY.createLinkType();
 43  159
     ret.setBase(link.getBase());
 44  159
     ret.setLang(link.getLang());
 45  159
     if(link.getOtherAttributes() != null){
 46  159
       ret.getOtherAttributes().putAll(link.getOtherAttributes());
 47  
     }
 48  159
     ret.setHref(link.getHref());
 49  159
     ret.setHreflang(link.getHreflang());
 50  159
     ret.setLength(link.getLength() == null? null : new BigInteger(String.valueOf(link.getLength())));
 51  159
     ret.setRel(link.getRel());
 52  159
     ret.setTitle(link.getTitle());
 53  159
     ret.setType(link.getType());
 54  159
     return ret;
 55  
   }
 56  
   
 57  
   public static PersonType toPersonType(Person p){
 58  105
     PersonType ret = new PersonType();
 59  
 
 60  105
     if(p.getName()!= null){
 61  105
       ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeName(p.getName()));
 62  
     }
 63  105
     if(p.getEmailOrText()!= null){
 64  9
       ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeEmail(p.getEmailOrText()));
 65  
     }    
 66  105
     if(p.getUri() != null){
 67  9
       UriType uri = FACTORY.createUriType();
 68  9
       uri.setValue(p.getUri());
 69  9
       ret.getNameOrUriOrEmail().add(FACTORY.createPersonTypeUri(uri));
 70  
     }
 71  
     
 72  105
     if(p.getOtherElements() != null){
 73  105
       ret.getNameOrUriOrEmail().addAll(p.getOtherElements());
 74  
     }
 75  105
     if(p.getOtherAttributes() != null){
 76  105
       ret.getOtherAttributes().putAll(p.getOtherAttributes());
 77  
     }
 78  
 
 79  105
     ret.setBase(p.getBase());
 80  105
     ret.setLang(p.getLang());
 81  105
     return ret;
 82  
   }
 83  
   
 84  
   /*
 85  
    * atomEntry =
 86  
    element atom:entry {
 87  
       atomCommonAttributes,
 88  
       (atomAuthor*
 89  
        & atomCategory*
 90  
        & atomContent?
 91  
        & atomContributor*
 92  
        & atomId
 93  
        & atomLink*
 94  
        & atomPublished?
 95  
        & atomRights?
 96  
        & atomSource?
 97  
        & atomSummary?
 98  
        & atomTitle
 99  
        & atomUpdated
 100  
        & extensionElement*)
 101  
    }
 102  
    */
 103  
   public  static EntryType toEntry(ItemEntry item) throws YarfrawException{
 104  87
     EntryType ret = FACTORY.createEntryType();
 105  87
     List<Object> elementList = ret.getAuthorOrCategoryOrContent();
 106  87
     ObjectFactory factory = FACTORY;
 107  
     
 108  
     
 109  87
     if(item.getAuthorOrCreator() != null){
 110  84
       for(Person author : item.getAuthorOrCreator()){
 111  84
         elementList.add(factory.createEntryTypeAuthor(toPersonType(author)));
 112  
       }
 113  
     }
 114  
 
 115  87
     if(item.getCategorySubjects() != null){
 116  3
       for(CategorySubject c : item.getCategorySubjects()){
 117  6
           if(c != null){
 118  6
             elementList.add(factory.createEntryTypeCategory(toCategoryType(c)));
 119  
           }
 120  
         }
 121  
     }
 122  
 
 123  87
     if(item.getContent() !=  null){
 124  39
       elementList.add(factory.createEntryTypeContent(toContent(item.getContent())));
 125  
     }
 126  
     
 127  87
     if(item.getContributors() != null){
 128  9
       for(Person c : item.getContributors()){
 129  18
         elementList.add(factory.createEntryTypeContributor(toPersonType(c)));
 130  
       }
 131  
     }
 132  
     
 133  87
     if(item.getUid() != null){
 134  87
       elementList.add(factory.createEntryTypeId(toAtomId(item.getUid())));
 135  
     }
 136  
     
 137  
     
 138  87
     if(item.getLinks() != null ){
 139  87
       for(Link link : item.getLinks()){
 140  126
         elementList.add(factory.createEntryTypeLink(toLink(link)));
 141  
       }
 142  
     }
 143  
     
 144  
     //partially supported
 145  87
     if(item.getPubDate() != null){
 146  39
       DateTimeType date = factory.createDateTimeType();
 147  39
       String dateString = item.getPubDate();
 148  39
       if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){
 149  0
         String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10);
 150  0
         if(newDateString != null){
 151  0
           dateString = newDateString;
 152  
         }else{
 153  0
           LOG.warn("The dateString "+dateString+" is in valid according to Atom 1.0 specs, unabel to convert it to a valid format, writing it as is");
 154  
         }
 155  
       }
 156  39
       date.setValue(dateString);
 157  39
       elementList.add(factory.createEntryTypePublished(date));
 158  
     }
 159  
     
 160  87
     if(item.getRights() != null){
 161  3
       elementList.add(factory.createEntryTypeRights(
 162  
               Atom10MappingUtils.toTextType(item.getRights())));
 163  
     }
 164  
     
 165  
     //FIXME: atomSource is not supported
 166  
     
 167  87
     if(item.getDescriptionOrSummary() != null ){
 168  78
       elementList.add(factory.createEntryTypeSummary(
 169  
               toTextType(item.getDescriptionOrSummary())));
 170  
     }
 171  
     
 172  
 //    not supported
 173  87
     if(item.getComments() != null){
 174  0
       LOG.info("Item.Comments field is not supported by Atom 1.0. It will be ignored");      
 175  
     }
 176  
     
 177  87
     if(item.getTitle() != null){
 178  87
       elementList.add(factory.createEntryTypeTitle(
 179  
               Atom10MappingUtils.toTextType(item.getTitle())));
 180  
     }
 181  
     
 182  
     //partially supported
 183  87
     if(item.getUpdatedDate() != null){
 184  87
       DateTimeType date = factory.createDateTimeType();
 185  87
       String dateString = item.getUpdatedDate();
 186  87
       if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){
 187  0
         String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10);
 188  0
         if(newDateString != null){
 189  0
           dateString = newDateString;
 190  
         }else{
 191  0
           LOG.warn("The dateString "+dateString+" is in valid according to Atom 1.0 specs, unabel to convert it to a valid format, writing it as is");
 192  
         }
 193  
       }
 194  87
       date.setValue(dateString);
 195  87
       elementList.add(factory.createEntryTypeUpdated(date));
 196  
     }
 197  
     
 198  
 //  not supported
 199  87
     if(item.getEnclosure() != null){
 200  0
       LOG.info("Item.Enclosure field is not supported by Atom 1.0. It will be ignored. Use Item.AtomLink to add enclosure element to item");
 201  
     }
 202  
     
 203  87
     if(item.getSource() != null){
 204  0
       LOG.info("Item.Source field is not supported by Atom 1.0. It will be ignored");
 205  
     }
 206  
     
 207  87
     if(item.getOtherElements() != null){
 208  87
       ret.getAuthorOrCategoryOrContent().addAll(item.getOtherElements());
 209  
     }
 210  87
     if(item.getOtherAttributes() != null){
 211  87
       ret.getOtherAttributes().putAll(item.getOtherAttributes());
 212  
     }
 213  
 
 214  87
     ret.setBase(item.getBase());
 215  87
     ret.setLang(item.getLang());
 216  
           
 217  87
     if(item.getOtherAttributes() != null){
 218  87
       ret.getOtherAttributes().putAll(item.getOtherAttributes());
 219  
     }
 220  87
     return ret;
 221  
   }
 222  
   
 223  
   public static ContentType toContent(Content in){
 224  39
     ContentType ret = FACTORY.createContentType();
 225  39
     ret.setSrc(in.getSrc());
 226  39
     ret.setType(in.getType());
 227  39
     if(in.getContentText()!= null){
 228  27
       ret.getContent().addAll(in.getContentText());
 229  
     }
 230  
 
 231  39
     if(in.getOtherElements() != null){
 232  39
       ret.getContent().addAll(in.getOtherElements());
 233  
     }
 234  39
     if(in.getOtherAttributes() != null){
 235  39
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 236  
     }
 237  
 
 238  39
     ret.setBase(in.getBase());
 239  39
     ret.setLang(in.getLang());
 240  39
     return ret;
 241  
   }
 242  
   
 243  
   public static IdType toAtomId(Id in){
 244  111
     IdType ret = FACTORY.createIdType();
 245  111
     ret.setValue(in.getIdValue());
 246  
     
 247  111
     if(in.getOtherAttributes() != null){
 248  111
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 249  
     }
 250  
 
 251  111
     ret.setBase(in.getBase());
 252  111
     ret.setLang(in.getLang());
 253  111
     return ret;
 254  
   }
 255  
   
 256  
   public static CategoryType toCategoryType(CategorySubject in){
 257  12
     CategoryType ret = FACTORY.createCategoryType();
 258  12
     ret.setTerm(in.getCategoryOrSubjectOrTerm());
 259  12
     ret.setScheme(in.getDomainOrScheme());
 260  12
     ret.setLabel(in.getLabel());
 261  12
     if(in.getOtherAttributes() != null){
 262  12
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 263  
     }
 264  
     
 265  12
     return ret;
 266  
   }
 267  
   
 268  
   public static TextType toTextType(Text in){
 269  213
     TextType ret = FACTORY.createTextType();
 270  
     
 271  213
     if(in.getType() != null){
 272  213
       ret.setType(in.getType().toString());
 273  
     }
 274  213
     if(in.getText() != null){
 275  213
       ret.getContent().add(in.getText());
 276  
     }
 277  213
     if(in.getXhtmlDiv() != null){
 278  0
       ret.getContent().add(in.getXhtmlDiv());
 279  
     }
 280  213
     if(in.getOtherElements() != null){
 281  213
       ret.getContent().addAll(in.getOtherElements());
 282  
     }
 283  213
     if(in.getOtherAttributes() != null){
 284  213
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 285  
     }
 286  
 
 287  213
     ret.setBase(in.getBase());
 288  213
     ret.setLang(in.getLang());
 289  
     
 290  213
     return ret;
 291  
   }
 292  
 
 293  
   public static IconType toIcon(Image in) {
 294  3
     IconType ret = FACTORY.createIconType();
 295  3
     ret.setValue(in.getUrl());
 296  3
     if(in.getOtherAttributes() != null){
 297  3
       ret.getOtherAttributes().putAll(in.getOtherAttributes());
 298  
     }
 299  
 
 300  3
     ret.setBase(in.getBase());
 301  3
     ret.setLang(in.getLang());
 302  
     
 303  3
     return ret;
 304  
   }
 305  
   
 306  
 }