| 1 |
|
package yarfraw.io; |
| 2 |
|
|
| 3 |
|
import java.io.File; |
| 4 |
|
import java.io.IOException; |
| 5 |
|
import java.io.InputStream; |
| 6 |
|
import java.net.URI; |
| 7 |
|
|
| 8 |
|
import javax.xml.bind.JAXBContext; |
| 9 |
|
import javax.xml.bind.JAXBElement; |
| 10 |
|
import javax.xml.bind.JAXBException; |
| 11 |
|
import javax.xml.bind.Unmarshaller; |
| 12 |
|
import javax.xml.bind.ValidationEvent; |
| 13 |
|
import javax.xml.bind.ValidationEventHandler; |
| 14 |
|
import javax.xml.bind.helpers.DefaultValidationEventHandler; |
| 15 |
|
|
| 16 |
|
import org.apache.commons.httpclient.HttpException; |
| 17 |
|
import org.apache.commons.httpclient.HttpURL; |
| 18 |
|
import org.apache.commons.httpclient.methods.GetMethod; |
| 19 |
|
import org.apache.commons.httpclient.params.HttpClientParams; |
| 20 |
|
import org.apache.commons.io.IOUtils; |
| 21 |
|
import org.apache.commons.logging.Log; |
| 22 |
|
import org.apache.commons.logging.LogFactory; |
| 23 |
|
|
| 24 |
|
import yarfraw.core.datamodel.ChannelFeed; |
| 25 |
|
import yarfraw.core.datamodel.FeedFormat; |
| 26 |
|
import yarfraw.core.datamodel.YarfrawException; |
| 27 |
|
import yarfraw.generated.atom10.elements.FeedType; |
| 28 |
|
import yarfraw.generated.rss10.elements.RDF; |
| 29 |
|
import yarfraw.generated.rss20.elements.TRss; |
| 30 |
|
import yarfraw.mapping.backward.impl.ToChannelAtom03Impl; |
| 31 |
|
import yarfraw.mapping.backward.impl.ToChannelAtom10Impl; |
| 32 |
|
import yarfraw.mapping.backward.impl.ToChannelRss10Impl; |
| 33 |
|
import yarfraw.mapping.backward.impl.ToChannelRss20Impl; |
| 34 |
|
import yarfraw.utils.JAXBUtils; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
993 |
public class FeedReader extends AbstractBaseFeedParser{ |
| 42 |
66 |
private static final Log LOG = LogFactory.getLog(FeedReader.class); |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public FeedReader(File file, FeedFormat format){ |
| 49 |
15 |
super(file, format); |
| 50 |
15 |
} |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
public FeedReader(String pathName, FeedFormat format){ |
| 58 |
0 |
super(new File(pathName), format); |
| 59 |
0 |
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
public FeedReader(URI uri, FeedFormat format){ |
| 67 |
6 |
super(new File(uri), format); |
| 68 |
6 |
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
public FeedReader(File file){ |
| 77 |
57 |
super(file); |
| 78 |
57 |
} |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
public FeedReader(String pathName){ |
| 87 |
0 |
super(new File(pathName)); |
| 88 |
0 |
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
public FeedReader(URI uri){ |
| 97 |
30 |
super(new File(uri)); |
| 98 |
30 |
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
public FeedReader(HttpURL httpUrl, HttpClientParams params) throws YarfrawException, IOException{ |
| 112 |
3 |
super(httpUrl, params); |
| 113 |
3 |
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
public FeedReader(HttpURL httpUrl) throws YarfrawException, IOException{ |
| 126 |
159 |
super(httpUrl, null); |
| 127 |
156 |
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
public FeedReader(GetMethod getMethod) throws YarfrawException, IOException{ |
| 139 |
3 |
super(getMethod); |
| 140 |
3 |
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
public static ChannelFeed readChannel(FeedFormat format, InputStream inputStream) throws YarfrawException{ |
| 151 |
|
Unmarshaller u; |
| 152 |
|
try { |
| 153 |
24 |
u = getUnMarshaller(format); |
| 154 |
24 |
return toChannel(format, u.unmarshal(inputStream)); |
| 155 |
0 |
} catch (JAXBException e) { |
| 156 |
0 |
throw new YarfrawException("Unable to unmarshal file", e); |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
public ChannelFeed readChannel(ValidationEventHandler validationEventHandler) throws YarfrawException{ |
| 167 |
|
Unmarshaller u; |
| 168 |
326 |
InputStream input = null; |
| 169 |
|
try { |
| 170 |
326 |
input = getStream(); |
| 171 |
323 |
if(input == null){ |
| 172 |
0 |
LOG.warn("Unable to read from null stream, returning null"); |
| 173 |
0 |
return null; |
| 174 |
|
} |
| 175 |
323 |
u = getUnMarshaller(_format); |
| 176 |
323 |
if(validationEventHandler != null){ |
| 177 |
6 |
u.setEventHandler(validationEventHandler); |
| 178 |
|
} |
| 179 |
323 |
return toChannel(_format, u.unmarshal(input)); |
| 180 |
3 |
} catch (JAXBException e) { |
| 181 |
3 |
throw new YarfrawException("Unable to unmarshal file", e); |
| 182 |
|
} |
| 183 |
0 |
catch (HttpException e) { |
| 184 |
0 |
throw new YarfrawException("Unable to read from remote url", e); |
| 185 |
|
} |
| 186 |
0 |
catch (IOException e) { |
| 187 |
0 |
throw new YarfrawException("Unable to read", e); |
| 188 |
|
}finally{ |
| 189 |
326 |
IOUtils.closeQuietly(input); |
| 190 |
|
} |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
@SuppressWarnings("unchecked") |
| 194 |
|
private static ChannelFeed toChannel(FeedFormat format, Object o) throws YarfrawException{ |
| 195 |
344 |
if(format == FeedFormat.RSS20){ |
| 196 |
275 |
return ToChannelRss20Impl.getInstance().execute(((JAXBElement<TRss>)o).getValue().getChannel()); |
| 197 |
69 |
}else if(format == FeedFormat.RSS10){ |
| 198 |
24 |
return ToChannelRss10Impl.getInstance().execute((RDF)o); |
| 199 |
45 |
}else if(format == FeedFormat.ATOM10){ |
| 200 |
42 |
return ToChannelAtom10Impl.getInstance().execute(((JAXBElement<FeedType>)o).getValue()); |
| 201 |
3 |
}else if(format == FeedFormat.ATOM03){ |
| 202 |
3 |
return ToChannelAtom03Impl.getInstance() |
| 203 |
|
.execute(((JAXBElement<yarfraw.generated.atom03.elements.FeedType>)o).getValue()); |
| 204 |
|
}else{ |
| 205 |
0 |
throw new UnsupportedOperationException("Unknown Feed Format"); |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
public ChannelFeed readChannel() throws YarfrawException{ |
| 216 |
312 |
return readChannel(null); |
| 217 |
|
} |
| 218 |
|
|
| 219 |
694 |
private static class WarningHandler implements ValidationEventHandler{ |
| 220 |
|
|
| 221 |
|
public boolean handleEvent(ValidationEvent event) { |
| 222 |
3 |
DefaultValidationEventHandler d = new DefaultValidationEventHandler(); |
| 223 |
3 |
d.handleEvent(event); |
| 224 |
3 |
return event.getSeverity()== ValidationEvent.FATAL_ERROR; |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
private static Unmarshaller getUnMarshaller(FeedFormat format) throws JAXBException{ |
| 230 |
347 |
JAXBContext context = JAXBUtils.getContext(format); |
| 231 |
347 |
Unmarshaller u = context.createUnmarshaller(); |
| 232 |
347 |
u.setEventHandler(new WarningHandler()); |
| 233 |
347 |
return u; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
|
| 237 |
|
} |