| 1 |
|
package yarfraw.io; |
| 2 |
|
|
| 3 |
|
import java.io.File; |
| 4 |
|
import java.io.FileNotFoundException; |
| 5 |
|
import java.io.FileOutputStream; |
| 6 |
|
import java.io.OutputStream; |
| 7 |
|
import java.net.URI; |
| 8 |
|
|
| 9 |
|
import javax.xml.bind.JAXBContext; |
| 10 |
|
import javax.xml.bind.JAXBException; |
| 11 |
|
import javax.xml.bind.Marshaller; |
| 12 |
|
import javax.xml.bind.ValidationEventHandler; |
| 13 |
|
|
| 14 |
|
import org.apache.commons.io.IOUtils; |
| 15 |
|
|
| 16 |
|
import yarfraw.core.datamodel.ChannelFeed; |
| 17 |
|
import yarfraw.core.datamodel.FeedFormat; |
| 18 |
|
import yarfraw.core.datamodel.YarfrawException; |
| 19 |
|
import yarfraw.generated.rss20.elements.ObjectFactory; |
| 20 |
|
import yarfraw.generated.rss20.elements.TRss; |
| 21 |
|
import yarfraw.mapping.forward.impl.ToAtom10ChannelImpl; |
| 22 |
|
import yarfraw.mapping.forward.impl.ToRss10ChannelImpl; |
| 23 |
|
import yarfraw.mapping.forward.impl.ToRss20ChannelImpl; |
| 24 |
|
import yarfraw.utils.JAXBUtils; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
153 |
public class FeedWriter extends AbstractBaseIO{ |
| 33 |
|
|
| 34 |
57 |
private static final ObjectFactory RSS20_FACTORY = new ObjectFactory(); |
| 35 |
|
|
| 36 |
|
public FeedWriter(File file, FeedFormat format){ |
| 37 |
3 |
super(file, format); |
| 38 |
3 |
} |
| 39 |
|
|
| 40 |
|
public FeedWriter(String pathName, FeedFormat format){ |
| 41 |
0 |
super(new File(pathName), format); |
| 42 |
0 |
} |
| 43 |
|
|
| 44 |
|
public FeedWriter(URI uri, FeedFormat format){ |
| 45 |
0 |
super(new File(uri), format); |
| 46 |
0 |
} |
| 47 |
|
|
| 48 |
|
public FeedWriter(File file){ |
| 49 |
105 |
super(file); |
| 50 |
105 |
} |
| 51 |
|
|
| 52 |
|
public FeedWriter(String pathName){ |
| 53 |
21 |
this(new File(pathName)); |
| 54 |
21 |
} |
| 55 |
|
|
| 56 |
|
public FeedWriter(URI uri){ |
| 57 |
0 |
this(new File(uri)); |
| 58 |
0 |
} |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public void writeChannel(ChannelFeed channel) throws YarfrawException{ |
| 66 |
105 |
writeChannel(channel, null); |
| 67 |
105 |
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
public static void writeChannel(FeedFormat format, ChannelFeed channel, OutputStream outputStream) throws YarfrawException{ |
| 78 |
3 |
if(format == null || channel == null ||outputStream==null){ |
| 79 |
0 |
throw new YarfrawException("format, channel, or outputStream is null"); |
| 80 |
|
} |
| 81 |
|
try { |
| 82 |
3 |
Marshaller m = getMarshaller(format); |
| 83 |
3 |
m.marshal(getJaxbElementFromFormat(format, channel), outputStream); |
| 84 |
0 |
} catch (JAXBException e) { |
| 85 |
0 |
throw new YarfrawException("Unable to write channel", e); |
| 86 |
3 |
} |
| 87 |
3 |
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
public void writeChannel(ChannelFeed channel, ValidationEventHandler validationEventHandler) throws YarfrawException{ |
| 97 |
108 |
FileOutputStream out = null; |
| 98 |
|
try { |
| 99 |
108 |
Marshaller m = getMarshaller(_format); |
| 100 |
108 |
if(validationEventHandler != null){ |
| 101 |
3 |
m.setEventHandler(validationEventHandler); |
| 102 |
|
} |
| 103 |
108 |
out = new FileOutputStream(_file); |
| 104 |
108 |
m.marshal(getJaxbElementFromFormat(_format, channel), out); |
| 105 |
0 |
} catch (JAXBException e) { |
| 106 |
0 |
throw new YarfrawException("Unable to write channel", e); |
| 107 |
|
} |
| 108 |
0 |
catch (FileNotFoundException e) { |
| 109 |
0 |
throw new YarfrawException("Unable to write channel", e); |
| 110 |
|
}finally{ |
| 111 |
108 |
IOUtils.closeQuietly(out); |
| 112 |
108 |
} |
| 113 |
108 |
} |
| 114 |
|
|
| 115 |
|
private static Object getJaxbElementFromFormat(FeedFormat format, ChannelFeed channel) throws YarfrawException{ |
| 116 |
111 |
if(format == FeedFormat.RSS20){ |
| 117 |
81 |
TRss rss = RSS20_FACTORY.createTRss(); |
| 118 |
81 |
rss.setVersion(2.0d); |
| 119 |
81 |
rss.setChannel(ToRss20ChannelImpl.getInstance().execute(channel).getValue()); |
| 120 |
81 |
return RSS20_FACTORY.createRss(rss); |
| 121 |
30 |
}else if(format == FeedFormat.RSS10){ |
| 122 |
12 |
return ToRss10ChannelImpl.getInstance().execute(channel); |
| 123 |
18 |
}else if(format == FeedFormat.ATOM10){ |
| 124 |
18 |
return ToAtom10ChannelImpl.getInstance().execute(channel); |
| 125 |
0 |
}else if(format == FeedFormat.ATOM03){ |
| 126 |
0 |
throw new UnsupportedOperationException("Yarfraw does not support writting to Atom 0.3 format, use Atom 1.0 instead."); |
| 127 |
|
}else{ |
| 128 |
0 |
throw new UnsupportedOperationException("Unknown Feed Format"); |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
private static Marshaller getMarshaller(FeedFormat format) throws JAXBException{ |
| 133 |
111 |
JAXBContext context = JAXBUtils.getContext(format); |
| 134 |
111 |
Marshaller m = context.createMarshaller(); |
| 135 |
111 |
m.setProperty(JAXBUtils.PREFIX_MAPPER_PROPERTY_NAME, JAXBUtils.getNamespacePrefixMapper(format)); |
| 136 |
111 |
return m; |
| 137 |
|
} |
| 138 |
|
} |