View Javadoc

1   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  class Rss10MappingUtils{
46  
47    private static final Log LOG = LogFactory.getLog(Rss10MappingUtils.class);
48    
49    private Rss10MappingUtils(){}
50    
51    public static Integer calculateTtl(UpdatePeriodEnum updatePeriod, BigInteger updateFrequency){
52      return CommonUtils.calculateTtl(updatePeriod, updateFrequency);
53    }
54    
55    @SuppressWarnings("unchecked")
56    public static ChannelFeed toChannel(TRss10Channel ch, RDF rdf){
57      ChannelFeed ret = new ChannelFeed();
58      if(ch.getOtherAttributes() != null){
59        ret.getOtherAttributes().putAll(ch.getOtherAttributes());
60      }
61      UpdatePeriodEnum updatePeriod = null;
62      BigInteger updateFrequency = null;
63      Map<String, Integer> ordering = new HashMap<String, Integer>();
64      List<ItemEntry> items = toItems(rdf.getChannelOrImageOrItem());
65      ret.setAbout(ch.getAbout());
66      ret.setResource(ch.getResource());
67      ret.getOtherAttributes().putAll(ch.getOtherAttributes());
68      for(Object o : ch.getTitleOrLinkOrDescription()){
69        if(o == null)
70          continue;
71        
72        if (o instanceof JAXBElement) {
73          JAXBElement jaxb = (JAXBElement) o;
74          Object val = jaxb.getValue();
75          if(same(jaxb.getName(), RSS10_TITLE)){
76            ret.setTitle((String)jaxb.getValue());
77          }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
78            ret.setDescriptionOrSubtitle((String)jaxb.getValue());
79          }else if(same(jaxb.getName(), RSS10_LINK)){
80            ret.addLink((String)jaxb.getValue());
81          }else if(same(jaxb.getName(), RSS10_UPDATEFREQUENCY)){
82            updateFrequency = (BigInteger)jaxb.getValue();
83          }else if(same(jaxb.getName(), RSS10_DC_SUBJECT)){
84            ret.addCategorySubject(getDcTypeText(jaxb));
85          }else if(same(jaxb.getName(), RSS10_DC_PUBLISHER)){
86            ret.addManagingEditorOrAuthorOrPublisher(getDcTypeText(jaxb));
87          }else if(same(jaxb.getName(), RSS10_DC_CREATOR)){
88            ret.addWebMasterOrCreator(getDcTypeText(jaxb));
89          }else if(same(jaxb.getName(), RSS10_DC_RIGHTS)){
90            ret.setRights(getDcTypeText(jaxb));
91          }else if(same(jaxb.getName(), RSS10_DC_DATE)){
92            ret.setPubDate(getDcTypeText(jaxb));
93          }else if(same(jaxb.getName(), RSS10_DC_LANGUAGE)){
94            ret.setLang(getDcTypeText(jaxb));
95          }else if(same(jaxb.getName(), RSS10_DC_CONTRIBUTOR)){
96            ret.addContributor(getDcTypeText(jaxb));
97          }else if(val instanceof UpdatePeriodEnum){
98            updatePeriod = (UpdatePeriodEnum)val;
99          }else if(val instanceof TRss10Image){
100           //this only put resources
101           Image image = new Image();
102           image.setResource(((TRss10Image)val).getResource());
103           ret.setImageOrIcon(image);
104         }else if(val instanceof TRss10TextInput){
105           //this only put resources
106           TextInput in = new TextInput();
107           in.setResource(((TRss10TextInput)val).getResource());
108           ret.setTexInput(in);
109         }else if(val instanceof Items){
110           Seq seq = ((Items)val).getSeq();
111           int i = 0;
112           for(Li li : seq.getLi()){
113             ordering.put(li.getResource(), i++);
114           }
115         }else{
116           LOG.warn("Unexpected JAXBElement: "+ToStringBuilder.reflectionToString(jaxb)+" this should not happen!");
117         }
118       }else if (o instanceof Element) {
119         Element e = (Element) o;
120         ret.getOtherElements().add(e);
121       }else{
122         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
123       }
124     }  
125     ret.setTtl(calculateTtl(updatePeriod, updateFrequency));
126     if(ordering.entrySet().size() != 0){
127       Collections.sort(items, new ItemComparacotr(ordering)); 
128     }
129     ret.setItems(items);
130     
131     return ret;
132   }
133   
134   private static class ItemComparacotr implements Comparator<ItemEntry>{
135     Map<String, Integer> _ordering = null;
136     public ItemComparacotr(Map<String, Integer> ordering){
137       _ordering = ordering;
138     }
139     public int compare(ItemEntry o1, ItemEntry o2) {
140       Integer ord1 = _ordering.get(o1.getAbout());
141       Integer ord2 = _ordering.get(o2.getAbout());
142       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     List<ItemEntry> items = new ArrayList<ItemEntry>();
149     for(Object o : objs){
150       if (o instanceof JAXBElement) {
151         Object value = ((JAXBElement)o).getValue();
152         if(value instanceof TRss10Item){
153           TRss10Item it = (TRss10Item)value;
154           ItemEntry item = new ItemEntry();
155           for(Object io : it.getTitleOrDescriptionOrLink()){
156             if (io instanceof JAXBElement) {
157               JAXBElement jaxb = (JAXBElement) io;
158               if(same(jaxb.getName(), RSS10_TITLE)){
159                 item.setTitle((String)jaxb.getValue());
160               }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
161                 item.setDescriptionOrSummary((String)jaxb.getValue());
162               }else if(same(jaxb.getName(), RSS10_LINK)){
163                 item.addLink((String)jaxb.getValue());
164               }else if(same(jaxb.getName(), RSS10_DC_CONTRIBUTOR)){
165                 item.addContributor(getDcTypeText(jaxb));
166               }else if(same(jaxb.getName(), RSS10_DC_CREATOR)){
167                 item.addAuthorOrCreator(getDcTypeText(jaxb));
168               }else if(same(jaxb.getName(), RSS10_DC_RIGHTS)){
169                 item.setRights(getDcTypeText(jaxb));
170               }else if(same(jaxb.getName(), RSS10_DC_DATE)){
171                 item.setPubDate(getDcTypeText(jaxb));
172               }else if(same(jaxb.getName(), RSS10_DC_SUBJECT)){
173                 item.addCategorySubject(getDcTypeText(jaxb));
174               }else{
175                 LOG.warn("Unexpected jaxbElement under <item>: "+ ToStringBuilder.reflectionToString(jaxb));
176               }
177             }else if (io instanceof Element) {
178               Element e = (Element) io;
179               item.getOtherElements().add(e);
180             }else{
181               LOG.warn("Unexpected object under <item>: "+ToStringBuilder.reflectionToString(io));
182             }
183           }
184           
185           if(it.getResource() == null){
186             it.setResource(Utils.getHrefLink(item.getLinks()));
187           }
188           
189           items.add(item);
190         }
191       }
192     }
193     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     for(Object o : input.getTitleOrDescriptionOrName()){
204       if(o == null)
205         continue;
206       
207       if (o instanceof JAXBElement) {
208         JAXBElement<?> jaxb = (JAXBElement<?>) o;
209         if(same(jaxb.getName(), RSS10_TITLE)){
210           ret.setTitle((String)jaxb.getValue());
211         }else if(same(jaxb.getName(), RSS10_DESCRIPTION)){
212           ret.setDescription((String)jaxb.getValue());
213         }else if(same(jaxb.getName(), RSS10_LINK)){
214           ret.setLink((String)jaxb.getValue());
215         }else if(same(jaxb.getName(), RSS10_NAME)){
216           ret.setName((String)jaxb.getValue());
217         }else{
218           LOG.warn("Unexpected JAXBElement: "+ToStringBuilder.reflectionToString(jaxb)+" this should not happen!");
219         }
220       }else if (o instanceof Element) {
221         Element e = (Element) o;
222         ret.getOtherElements().add(e);
223       }else{
224         LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
225       }
226     }
227     
228     ret.setAbout(input.getAbout());
229     
230     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     ret.setLink(img.getLink());
241     ret.setTitle(img.getTitle());
242     ret.setUrl(img.getUrl());
243     ret.setAbout(img.getAbout());
244     
245     return ret;
246   }
247 }