allow media urls with space (fixes #20)
This commit is contained in:
parent
8335c6d43a
commit
6c676c8c60
3 changed files with 82 additions and 17 deletions
|
@ -22,6 +22,7 @@
|
|||
package org.rometools.feed.module.mediarss.io;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -102,7 +103,7 @@ public class MediaModuleParser implements ModuleParser {
|
|||
|
||||
if (content.getAttributeValue("url") != null) {
|
||||
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));
|
||||
} catch (final Exception ex) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
|
@ -113,17 +114,23 @@ public class MediaModuleParser implements ModuleParser {
|
|||
if (mc != null) {
|
||||
values.add(mc);
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
|
@ -143,17 +150,23 @@ public class MediaModuleParser implements ModuleParser {
|
|||
}
|
||||
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
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) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
|
@ -161,20 +174,25 @@ public class MediaModuleParser implements ModuleParser {
|
|||
mc.setLanguage(content.getAttributeValue("lang"));
|
||||
mc.setMetadata(parseMetadata(content));
|
||||
try {
|
||||
mc.setSamplingrate(content.getAttributeValue("samplingrate") == null ? null : NumberParser.parseFloat(content
|
||||
.getAttributeValue("samplingrate")));
|
||||
if (content.getAttributeValue("samplingrate") != null) {
|
||||
mc.setSamplingrate(Float.valueOf(content.getAttributeValue("samplingrate")));
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
LOG.warn("Exception parsing content tag.", ex);
|
||||
}
|
||||
|
||||
mc.setType(content.getAttributeValue("type"));
|
||||
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) {
|
||||
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 {
|
||||
LOG.warn("Could not find MediaContent.");
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ package org.rometools.feed.module.mediarss;
|
|||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
@ -18,6 +17,7 @@ import junit.framework.Test;
|
|||
import junit.framework.TestSuite;
|
||||
|
||||
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.SyndFeed;
|
||||
|
@ -31,16 +31,23 @@ import com.sun.syndication.io.SyndFeedOutput;
|
|||
*/
|
||||
public class MediaModuleTest extends AbstractTestCase {
|
||||
|
||||
/**
|
||||
* @param testName id of test
|
||||
*/
|
||||
public MediaModuleTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return actual test suite
|
||||
*/
|
||||
public static Test suite() {
|
||||
final TestSuite suite = new TestSuite(MediaModuleTest.class);
|
||||
|
||||
return suite;
|
||||
return new TestSuite(MediaModuleTest.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception if file not found or not accessible
|
||||
*/
|
||||
public void testGoogleVideo() throws Exception {
|
||||
final SyndFeed feed = getSyndFeed(new File(getTestFile("data/YouTube-MostPopular.rss")));
|
||||
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 {
|
||||
final File test = new File(super.getTestFile("xml"));
|
||||
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();
|
||||
return input.build(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>
|
Loading…
Reference in a new issue