View Javadoc

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  class Atom10MappingUtils{
47  
48    private static final Log LOG = LogFactory.getLog(Atom10MappingUtils.class);
49    
50    public static Text toText(TextType in){
51      Text ret = new Text();
52      for(Object o : in.getContent()){
53        if(o == null){
54          continue;
55        }
56        if (o instanceof JAXBElement<?>) {
57          JAXBElement<?> jaxb = (JAXBElement<?>) o;
58          Object val = jaxb.getValue();
59          ret.setText(String.valueOf(val));
60        }else if(o instanceof Element){
61          ret.setXhtmlDiv((Element)o);
62        }else{
63          ret.setText(String.valueOf(o));
64        }
65      }
66      
67      ret.setLang(in.getLang());
68      ret.setBase(in.getBase());
69      ret.getOtherAttributes().putAll(in.getOtherAttributes());
70      
71      return ret;
72    }
73    
74    public static CategorySubject toCategorySubject(CategoryType in) {
75      CategorySubject ret = new CategorySubject();
76      ret.setLabel(in.getLabel());
77      ret.setCategoryOrSubjectOrTerm(in.getTerm());
78      ret.setDomainOrScheme(in.getScheme());
79      
80      ret.setLang(in.getLang());
81      ret.setBase(in.getBase());
82      ret.getOtherAttributes().putAll(in.getOtherAttributes());
83      return ret;
84    }
85    
86    public static Person toPersonType(PersonType in){
87      Person ret = new Person();
88      
89      for(Object o : in.getNameOrUriOrEmail()){
90        if(o == null){
91          continue;
92        }
93        if (o instanceof JAXBElement<?>) {
94          JAXBElement<?> jaxb = (JAXBElement<?>) o;
95          Object val = jaxb.getValue();
96          if(XMLUtils.same(jaxb.getName(), ATOM10_EMAIL)){
97            ret.setEmailOrText((String)val);
98          }else if(XMLUtils.same(jaxb.getName(), ATOM10_NAME)){
99            ret.setName((String)val);
100         }else if(val instanceof UriType){
101           ret.setUri(((UriType)val).getValue());
102         }else{
103           LOG.warn("Unexpected JAXB Element: "+ ToStringBuilder.reflectionToString(val));
104         }
105       }else if(o instanceof Element){
106         ret.getOtherElements().add((Element)o);
107       }else{
108         LOG.warn("Unexpected Element: "+ ToStringBuilder.reflectionToString(o));
109       }
110     }
111     
112     ret.setLang(in.getLang());
113     ret.setBase(in.getBase());
114     ret.getOtherAttributes().putAll(in.getOtherAttributes());
115     
116     return ret;
117   }
118   
119   private static Content toContent(ContentType in){
120     Content ret = new Content();
121     for(Object o : in.getContent()){
122       if(o == null){
123         continue;
124       }
125       if (o instanceof JAXBElement<?>) {
126         JAXBElement<?> jaxb = (JAXBElement<?>) o;
127         Object val = jaxb.getValue();
128         ret.addContentText(String.valueOf(val));
129       }else if(o instanceof Element){
130         ret.getOtherElements().add((Element)o);
131       }else{
132         String s = String.valueOf(o);
133         if(StringUtils.isNotBlank(s)){
134           ret.addContentText(s);
135         }
136       }
137     }
138     
139     ret.setSrc(in.getSrc());
140     ret.setType(in.getType());
141     ret.setLang(in.getLang());
142     ret.setBase(in.getBase());
143     ret.getOtherAttributes().putAll(in.getOtherAttributes());
144     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     ItemEntry ret = new ItemEntry();
171 
172     ret.setLang(in.getLang());
173     ret.setBase(in.getBase());
174     ret.getOtherAttributes().putAll(in.getOtherAttributes());
175     
176     for(Object o : in.getAuthorOrCategoryOrContent()){
177       if(o == null){
178         continue;
179       }
180       if (o instanceof JAXBElement<?>) {
181         JAXBElement<?> jaxb = (JAXBElement<?>) o;
182         Object val = jaxb.getValue();
183         
184         if (XMLUtils.same(jaxb.getName(), ATOM10_AUTHOR)) {
185           ret.addAuthorOrCreator(toPersonType((PersonType)val));
186         }else if(val instanceof CategoryType){
187           ret.addCategorySubject(toCategorySubject((CategoryType)val));
188         }else if(val instanceof ContentType){
189           ret.setContent(toContent((ContentType)val));
190         }else if(XMLUtils.same(jaxb.getName(), ATOM10_CONTRIBUTOR)){
191           ret.addContributor(toPersonType((PersonType)val));
192         }
193         //contributor are ignored
194         else if(val instanceof LinkType){ 
195           ret.addLink(toAtomLink((LinkType)val));
196         }else if (XMLUtils.same(jaxb.getName(), ATOM10_PUBLISHED)) {
197           //partially supported
198           DateTimeType dt = (DateTimeType) val;
199           if(dt.getValue() != null){
200             ret.setPubDate(dt.getValue());
201           }
202         }else if (XMLUtils.same(jaxb.getName(), ATOM10_RIGHTS)) {
203           TextType text = (TextType) val;
204           ret.setRights(toText(text));
205         }//FIXME: source not supported
206         else if (XMLUtils.same(jaxb.getName(), ATOM10_SUMMARY)) {
207           TextType text = (TextType) val;
208           ret.setDescriptionOrSummary(toText(text));
209         }else if (XMLUtils.same(jaxb.getName(), ATOM10_TITLE)) {
210           TextType text = (TextType) val;
211           ret.setTitle(toText(text));
212         }else if (XMLUtils.same(jaxb.getName(), ATOM10_UPDATED)) {
213           //  partially supported
214           DateTimeType dt = (DateTimeType) val;
215           if(dt.getValue() != null){
216             ret.setUpdatedDate(dt.getValue());
217           }
218         }else if(val instanceof IdType){
219           ret.setUid(toId((IdType)val));
220         }else{
221           LOG.warn("Unexpected JAXB Element: "+ ToStringBuilder.reflectionToString(val));
222         }
223       }else if (o instanceof Element) {
224         Element e = (Element) o;
225         ret.getOtherElements().add(e);
226       }else{
227         LOG.warn("Unexpected Element: "+ ToStringBuilder.reflectionToString(o));
228       }
229     }
230     return ret;
231   }
232   
233   public static Link toAtomLink(LinkType link){
234     Link ret = new Link();
235     ret.setBase(link.getBase());
236     ret.setLang(link.getLang());
237     if(link.getOtherAttributes() != null){
238       ret.getOtherAttributes().putAll(link.getOtherAttributes());
239     }
240 
241     ret.setHref(link.getHref());
242     ret.setHreflang(link.getHreflang());
243     ret.setLength(link.getLength() == null ? null : link.getLength().intValue());
244     ret.setRel(link.getRel());
245     ret.setTitle(link.getTitle());
246     ret.setType(link.getType());
247     
248     return ret;
249   }
250   
251   public static Id toId(IdType in){
252     Id ret = new Id();
253     ret.setBase(in.getBase());
254     ret.setLang(in.getLang());
255     if(in.getOtherAttributes() != null){
256       ret.getOtherAttributes().putAll(in.getOtherAttributes());
257     }
258     ret.setIdValue(in.getValue());
259     return ret;
260   }
261   
262   public static Image toImage(IconType in){
263     Image ret= new Image();
264     ret.setUrl(in.getValue());
265     ret.setBase(in.getBase());
266     ret.setLang(in.getLang());
267     if(in.getOtherAttributes() != null){
268       ret.getOtherAttributes().putAll(in.getOtherAttributes());
269     }
270     
271     return ret;
272   }
273 
274 }