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 public class ToChannelAtom10Impl implements ToChannelAtom10{
45
46 private static final Log LOG = LogFactory.getLog(ToChannelAtom10Impl.class);
47 private static final ToChannelAtom10 _instance = new ToChannelAtom10Impl();
48
49 private ToChannelAtom10Impl() {}
50 public static ToChannelAtom10 getInstance(){
51 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 if(feed == null){
75 return null;
76 }
77 ChannelFeed c = new ChannelFeed();
78
79 c.setLang(feed.getLang());
80 c.setBase(feed.getBase());
81 c.getOtherAttributes().putAll(feed.getOtherAttributes());
82 for(Object o : feed.getAuthorOrCategoryOrContributor()){
83 if(o == null){
84 continue;
85 }
86 if (o instanceof JAXBElement<?>) {
87 JAXBElement<?> jaxbElement = (JAXBElement<?>) o;
88 Object val = jaxbElement.getValue();
89 if (same(jaxbElement.getName(), ATOM10_TITLE)) {
90 TextType text = (TextType) val;
91 c.setTitle(toText(text));
92 }else if (same(jaxbElement.getName(), ATOM10_SUBTITLE)) {
93 TextType text = (TextType) val;
94 c.setDescriptionOrSubtitle(toText(text));
95 }else if (same(jaxbElement.getName(), ATOM10_AUTHOR)) {
96 c.addManagingEditorOrAuthorOrPublisher(toPersonType((PersonType)val));
97 }else if(same(jaxbElement.getName(), ATOM10_CONTRIBUTOR)){
98 c.addContributor(toPersonType((PersonType)val));
99 }else if(val instanceof CategoryType){
100 c.addCategorySubject(toCategorySubject((CategoryType)val));
101 }else if (val instanceof GeneratorType) {
102 GeneratorType gen = (GeneratorType)val;
103 c.setGenerator(toGenerator(gen));
104 }else if(val instanceof IconType){
105 c.setImageOrIcon(toImage((IconType)val));
106 }else if(val instanceof IdType){
107 c.setUid(toId((IdType)val));
108 }else if(val instanceof LinkType){
109 c.addLink(toAtomLink((LinkType)val));
110 }else if(val instanceof LogoType){
111 LogoType logo = (LogoType)val;
112 c.setLogo(toLogo(logo));
113 }
114 else if (same(jaxbElement.getName(), ATOM10_RIGHTS)) {
115 TextType text = (TextType) val;
116 c.setRights(toText(text));
117 }else if (same(jaxbElement.getName(), ATOM10_UPDATED)) {
118
119 DateTimeType dt = (DateTimeType) val;
120 if(dt.getValue() != null){
121 c.setLastBuildOrUpdatedDate(dt.getValue());
122 }
123 }else if(val instanceof EntryType){
124 c.addItem(toItem((EntryType)val));
125 }else{
126 LOG.warn("Unexpected jaxbElement: "+ToStringBuilder.reflectionToString(jaxbElement)+" this should not happen!");
127 }
128 }
129 else if (o instanceof Element) {
130 Element e = (Element) o;
131 c.getOtherElements().add(e);
132 }else{
133 LOG.warn("Unexpected object: "+ToStringBuilder.reflectionToString(o)+" this should not happen!");
134 }
135 }
136
137 return c;
138 }
139
140 private static Generator toGenerator(GeneratorType in){
141 Generator ret = new Generator(in.getValue());
142 ret.setUri(in.getUri());
143 ret.setVersion(in.getVersion());
144 ret.setLang(in.getLang());
145 ret.setBase(in.getBase());
146 ret.getOtherAttributes().putAll(in.getOtherAttributes());
147 return ret;
148
149 }
150 public static Image toLogo(LogoType in){
151 Image ret= new Image();
152 ret.setUrl(in.getValue());
153 ret.setBase(in.getBase());
154 ret.setLang(in.getLang());
155 if(in.getOtherAttributes() != null){
156 ret.getOtherAttributes().putAll(in.getOtherAttributes());
157 }
158
159 return ret;
160 }
161
162 }