allow media urls with space (fixes #20)

This commit is contained in:
Martin Kurz 2014-04-18 18:31:09 +02:00
parent 8335c6d43a
commit 6c676c8c60
3 changed files with 82 additions and 17 deletions

View file

@ -22,6 +22,7 @@
package org.rometools.feed.module.mediarss.io; package org.rometools.feed.module.mediarss.io;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -102,7 +103,7 @@ public class MediaModuleParser implements ModuleParser {
if (content.getAttributeValue("url") != null) { if (content.getAttributeValue("url") != null) {
try { try {
mc = new MediaContent(new UrlReference(new URI(content.getAttributeValue("url")))); mc = new MediaContent(new UrlReference(new URI(content.getAttributeValue("url").replace(' ', '+'))));
mc.setPlayer(parsePlayer(content)); mc.setPlayer(parsePlayer(content));
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
@ -113,17 +114,23 @@ public class MediaModuleParser implements ModuleParser {
if (mc != null) { if (mc != null) {
values.add(mc); values.add(mc);
try { try {
mc.setAudioChannels(content.getAttributeValue("channels") == null ? null : new Integer(content.getAttributeValue("channels"))); if (content.getAttributeValue("channels") != null) {
mc.setAudioChannels(Integer.valueOf(content.getAttributeValue("channels")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
try { try {
mc.setBitrate(content.getAttributeValue("bitrate") == null ? null : new Float(content.getAttributeValue("bitrate"))); if (content.getAttributeValue("bitrate") != null) {
mc.setBitrate(Float.valueOf(content.getAttributeValue("bitrate")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
try { try {
mc.setDuration(content.getAttributeValue("duration") == null ? null : new Long(content.getAttributeValue("duration"))); if (content.getAttributeValue("duration") != null) {
mc.setDuration(Long.valueOf(content.getAttributeValue("duration")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
@ -143,17 +150,23 @@ public class MediaModuleParser implements ModuleParser {
} }
try { try {
mc.setFileSize(content.getAttributeValue("fileSize") == null ? null : NumberParser.parseLong(content.getAttributeValue("fileSize"))); if (content.getAttributeValue("fileSize") != null) {
mc.setFileSize(Long.valueOf(content.getAttributeValue("fileSize")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
try { try {
mc.setFramerate(content.getAttributeValue("framerate") == null ? null : NumberParser.parseFloat(content.getAttributeValue("framerate"))); if (content.getAttributeValue("framerate") != null) {
mc.setFramerate(Float.valueOf(content.getAttributeValue("framerate")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
try { try {
mc.setHeight(content.getAttributeValue("height") == null ? null : NumberParser.parseInt(content.getAttributeValue("height"))); if (content.getAttributeValue("height") != null) {
mc.setHeight(Integer.valueOf(content.getAttributeValue("height")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
@ -161,20 +174,25 @@ public class MediaModuleParser implements ModuleParser {
mc.setLanguage(content.getAttributeValue("lang")); mc.setLanguage(content.getAttributeValue("lang"));
mc.setMetadata(parseMetadata(content)); mc.setMetadata(parseMetadata(content));
try { try {
mc.setSamplingrate(content.getAttributeValue("samplingrate") == null ? null : NumberParser.parseFloat(content if (content.getAttributeValue("samplingrate") != null) {
.getAttributeValue("samplingrate"))); mc.setSamplingrate(Float.valueOf(content.getAttributeValue("samplingrate")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
mc.setType(content.getAttributeValue("type")); mc.setType(content.getAttributeValue("type"));
try { try {
mc.setWidth(content.getAttributeValue("width") == null ? null : NumberParser.parseInt(content.getAttributeValue("width"))); if (content.getAttributeValue("width") != null) {
mc.setWidth(Integer.valueOf(content.getAttributeValue("width")));
}
} catch (final Exception ex) { } catch (final Exception ex) {
LOG.warn("Exception parsing content tag.", ex); LOG.warn("Exception parsing content tag.", ex);
} }
mc.setDefaultContent(content.getAttributeValue("isDefault") == null ? false : Boolean.getBoolean(content.getAttributeValue("isDefault"))); if (content.getAttributeValue("isDefault") != null) {
mc.setDefaultContent(Boolean.valueOf(content.getAttributeValue("isDefault")));
}
} else { } else {
LOG.warn("Could not find MediaContent."); LOG.warn("Could not find MediaContent.");
} }

View file

@ -9,7 +9,6 @@ package org.rometools.feed.module.mediarss;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -18,6 +17,7 @@ import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.rometools.feed.module.AbstractTestCase; import org.rometools.feed.module.AbstractTestCase;
import org.rometools.feed.module.mediarss.types.MediaContent;
import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeed;
@ -31,16 +31,23 @@ import com.sun.syndication.io.SyndFeedOutput;
*/ */
public class MediaModuleTest extends AbstractTestCase { public class MediaModuleTest extends AbstractTestCase {
/**
* @param testName id of test
*/
public MediaModuleTest(final String testName) { public MediaModuleTest(final String testName) {
super(testName); super(testName);
} }
/**
* @return actual test suite
*/
public static Test suite() { public static Test suite() {
final TestSuite suite = new TestSuite(MediaModuleTest.class); return new TestSuite(MediaModuleTest.class);
return suite;
} }
/**
* @throws Exception if file not found or not accessible
*/
public void testGoogleVideo() throws Exception { public void testGoogleVideo() throws Exception {
final SyndFeed feed = getSyndFeed(new File(getTestFile("data/YouTube-MostPopular.rss"))); final SyndFeed feed = getSyndFeed(new File(getTestFile("data/YouTube-MostPopular.rss")));
for (final Object element : feed.getEntries()) { for (final Object element : feed.getEntries()) {
@ -50,6 +57,9 @@ public class MediaModuleTest extends AbstractTestCase {
} }
} }
/**
* @throws Exception if file not found or not accessible
*/
public void testParse() throws Exception { public void testParse() throws Exception {
final File test = new File(super.getTestFile("xml")); final File test = new File(super.getTestFile("xml"));
final File[] files = test.listFiles(); final File[] files = test.listFiles();
@ -74,7 +84,31 @@ public class MediaModuleTest extends AbstractTestCase {
} }
} }
private SyndFeed getSyndFeed(final File file) throws FileNotFoundException, IllegalArgumentException, IOException, FeedException { /**
* test url with whitespace in media element (https://github.com/rometools/rome-modules/issues/20).
*
* @throws Exception if file not found or not accessible
*/
public void testParseMediaContentContainingURLWithSpaces() throws Exception {
final SyndFeed feed = getSyndFeed(new File(getTestFile("org/rometools/feed/module/mediarss/issue-20.xml")));
final SyndEntry entry = (SyndEntry) feed.getEntries().get(0);
final MediaEntryModule m = (MediaEntryModule) entry.getModule(MediaEntryModule.URI);
assertNotNull("missing media entry module", m);
final MediaContent[] mcs = m.getMediaContents();
assertNotNull("missing media:content", mcs);
assertEquals("wrong count of media:content", 1, mcs.length);
final MediaContent mc = mcs[0];
assertEquals("http://www.foo.com/path/containing+spaces/trailer.mov", mc.getReference().toString());
}
/**
* @param file to parse
* @return SyndFeed implementation
* @throws IllegalArgumentException
* @throws IOException if file not found or not accessible
* @throws FeedException if parsing failed
*/
private SyndFeed getSyndFeed(final File file) throws IOException, FeedException {
final SyndFeedInput input = new SyndFeedInput(); final SyndFeedInput input = new SyndFeedInput();
return input.build(file); return input.build(file);
} }

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>My Movie Review Site</title>
<link>http://www.foo.com</link>
<description>I review movies.</description>
<item>
<title>Movie Title: Is this a good movie?</title>
<link>http://www.foo.com/item1.htm</link>
<media:content url="http://www.foo.com/path/containing spaces/trailer.mov" fileSize="12216320" type="video/quicktime" expression="sample" />
</item>
</channel>
</rss>