| 1 |
81 |
package yarfraw.mapping.forward.impl; |
| 2 |
|
|
| 3 |
|
import java.math.BigInteger; |
| 4 |
|
import java.util.List; |
| 5 |
|
|
| 6 |
|
import javax.xml.bind.JAXBElement; |
| 7 |
|
|
| 8 |
|
import org.apache.commons.logging.Log; |
| 9 |
|
import org.apache.commons.logging.LogFactory; |
| 10 |
|
|
| 11 |
|
import yarfraw.core.datamodel.CategorySubject; |
| 12 |
|
import yarfraw.core.datamodel.ChannelFeed; |
| 13 |
|
import yarfraw.core.datamodel.Day; |
| 14 |
|
import yarfraw.core.datamodel.FeedFormat; |
| 15 |
|
import yarfraw.core.datamodel.ItemEntry; |
| 16 |
|
import yarfraw.core.datamodel.YarfrawException; |
| 17 |
|
import yarfraw.generated.rss20.elements.ObjectFactory; |
| 18 |
|
import yarfraw.generated.rss20.elements.TRssChannel; |
| 19 |
|
import yarfraw.generated.rss20.elements.TSkipDay; |
| 20 |
|
import yarfraw.generated.rss20.elements.TSkipDaysList; |
| 21 |
|
import yarfraw.generated.rss20.elements.TSkipHoursList; |
| 22 |
|
import yarfraw.mapping.forward.ToRss20Channel; |
| 23 |
|
import yarfraw.utils.CommonUtils; |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
45 |
public class ToRss20ChannelImpl implements ToRss20Channel{ |
| 31 |
45 |
private static final Log LOG = LogFactory.getLog(ToRss20ChannelImpl.class); |
| 32 |
45 |
private static ToRss20Channel _instance = new ToRss20ChannelImpl(); |
| 33 |
|
|
| 34 |
|
public static ToRss20Channel getInstance(){ |
| 35 |
81 |
return _instance; |
| 36 |
|
} |
| 37 |
|
|
| 38 |
45 |
private ToRss20ChannelImpl(){} |
| 39 |
|
public JAXBElement<TRssChannel> execute(ChannelFeed channel) throws YarfrawException { |
| 40 |
81 |
return new ObjectFactory().createChannel(toChannel(channel)); |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
private TRssChannel toChannel(ChannelFeed ch){ |
| 44 |
81 |
ObjectFactory factory = new ObjectFactory(); |
| 45 |
81 |
TRssChannel ret = factory.createTRssChannel(); |
| 46 |
81 |
List<Object> elementList = ret.getTitleOrLinkOrDescription(); |
| 47 |
81 |
if(ch.getOtherElements() != null){ |
| 48 |
81 |
ret.getAny().addAll(ch.getOtherElements()); |
| 49 |
|
} |
| 50 |
81 |
if(ch.getOtherAttributes() != null){ |
| 51 |
81 |
ret.getOtherAttributes().putAll(ch.getOtherAttributes()); |
| 52 |
|
} |
| 53 |
81 |
if(ch.getCategorySubjects() != null){ |
| 54 |
12 |
for(CategorySubject c : ch.getCategorySubjects()){ |
| 55 |
6 |
if(c != null){ |
| 56 |
6 |
elementList.add(Rss20MappingUtils.toRss20Category(c)); |
| 57 |
|
} |
| 58 |
|
} |
| 59 |
|
} |
| 60 |
|
|
| 61 |
81 |
if(ch.getCloud() != null){ |
| 62 |
3 |
elementList.add(Rss20MappingUtils.toRss20Cloud(ch.getCloud())); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
81 |
if(ch.getRightsText() != null){ |
| 66 |
3 |
elementList.add(factory.createTRssChannelCopyright(ch.getRightsText())); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
81 |
if(ch.getDescriptionOrSubtitleText() != null){ |
| 70 |
48 |
elementList.add(factory.createTRssChannelDescription(ch.getDescriptionOrSubtitleText())); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
81 |
if(ch.getDocs() != null){ |
| 74 |
24 |
elementList.add(factory.createTRssChannelDocs(ch.getDocs())); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
81 |
if(ch.getGenerator() != null){ |
| 78 |
24 |
String generator = ch.getGenerator().getValue(); |
| 79 |
24 |
elementList.add(factory.createTRssChannelGenerator(generator)); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
81 |
if(ch.getImageOrIcon() != null){ |
| 83 |
3 |
elementList.add(Rss20MappingUtils.toRss20Image(ch.getImageOrIcon())); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
81 |
if(ch.getItems() != null){ |
| 87 |
753 |
for(ItemEntry t : ch.getItems()){ |
| 88 |
597 |
if(t != null){ |
| 89 |
597 |
ret.getItem().add(Rss20MappingUtils.toRss20Item(t).getValue()); |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
| 94 |
81 |
if(ch.getLang() != null){ |
| 95 |
48 |
elementList.add(factory.createTRssChannelLanguage(ch.getLang())); |
| 96 |
|
} |
| 97 |
81 |
String link = Utils.getHrefLink(ch.getLinks()); |
| 98 |
81 |
if(link != null){ |
| 99 |
45 |
elementList.add(factory.createTRssChannelLink(link)); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
81 |
if(ch.getLastBuildOrUpdatedDate() != null){ |
| 103 |
24 |
String dateString = ch.getLastBuildOrUpdatedDate(); |
| 104 |
24 |
if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.RSS20)){ |
| 105 |
0 |
String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.RSS20); |
| 106 |
0 |
if(newDateString != null){ |
| 107 |
0 |
dateString = newDateString; |
| 108 |
|
}else{ |
| 109 |
0 |
LOG.warn("The dateString "+dateString+" is in valid according to RSS 2.0 specs, unabel to convert it to a valid format, writing it as is"); |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
24 |
elementList.add(factory.createTRssChannelLastBuildDate(dateString)); |
| 113 |
|
} |
| 114 |
|
|
| 115 |
81 |
String editor = Utils.getEmailOrText(ch.getManagingEditorOrAuthorOrPublisher()); |
| 116 |
81 |
if(editor != null){ |
| 117 |
24 |
elementList.add(factory.createTRssChannelManagingEditor(editor)); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
81 |
if(ch.getPubDate() != null){ |
| 121 |
24 |
String dateString = ch.getPubDate(); |
| 122 |
24 |
if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.RSS20)){ |
| 123 |
0 |
String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.RSS20); |
| 124 |
0 |
if(newDateString != null){ |
| 125 |
0 |
dateString = newDateString; |
| 126 |
|
}else{ |
| 127 |
0 |
LOG.warn("The dateString "+dateString+" is in valid according to RSS 1.0 specs, unabel to convert it to a valid format, writing it as is"); |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
24 |
elementList.add(factory.createTRssChannelPubDate(dateString)); |
| 131 |
|
} |
| 132 |
|
|
| 133 |
81 |
if(ch.getSkipDays() != null){ |
| 134 |
3 |
TSkipDaysList tdl = new TSkipDaysList(); |
| 135 |
12 |
for(Day day : ch.getSkipDays()){ |
| 136 |
6 |
tdl.getDay().add(TSkipDay.fromValue(day.toString())); |
| 137 |
|
} |
| 138 |
3 |
elementList.add(new ObjectFactory().createSkipDays( tdl)); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
81 |
if(ch.getSkipHours() != null){ |
| 142 |
3 |
TSkipHoursList thl = new TSkipHoursList(); |
| 143 |
3 |
thl.getHour().addAll(ch.getSkipHours()); |
| 144 |
3 |
elementList.add(new ObjectFactory().createSkipHours( thl)); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
81 |
if(ch.getTexInput() != null){ |
| 148 |
3 |
elementList.add(Rss20MappingUtils.toRss20TextInput(ch.getTexInput())); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
81 |
if(ch.getTitleText() != null){ |
| 152 |
78 |
elementList.add(factory.createTRssChannelTitle(ch.getTitleText())); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
81 |
if(ch.getTtl() != null){ |
| 156 |
3 |
elementList.add(factory.createTRssChannelTtl(new BigInteger(String.valueOf(ch.getTtl())))); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
81 |
String webmaster = Utils.getEmailOrText(ch.getWebMasterOrCreator()); |
| 160 |
81 |
if(webmaster != null){ |
| 161 |
24 |
elementList.add(factory.createTRssChannelWebMaster(webmaster)); |
| 162 |
|
} |
| 163 |
|
|
| 164 |
81 |
return ret; |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
} |