From 50adfcc3005d44503dfc3270519c254f1d29579d Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 29 Dec 2011 16:23:56 +0000 Subject: [PATCH] * make xml templating resilient against unexpectedly undefined properties --- .../filebot/cli/ScriptShell.lib.groovy | 5 ++- .../filebot/format/UndefinedObject.java | 41 +++++++++++++++++++ .../filebot/web/TheTVDBClient.java | 2 +- .../filebot/web/TheTVDBClientTest.java | 2 +- website/scripts/artwork.tmdb.groovy | 6 +-- website/scripts/artwork.tvdb.groovy | 6 +-- 6 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 source/net/sourceforge/filebot/format/UndefinedObject.java diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index ecb3153b..64f0a58c 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -73,9 +73,10 @@ String.metaClass.saveAs = { f, csn = "utf-8" -> Charset.forName(csn).encode(dele import groovy.text.XmlTemplateEngine import groovy.text.GStringTemplateEngine import net.sourceforge.filebot.format.PropertyBindings +import net.sourceforge.filebot.format.UndefinedObject -Object.metaClass.applyXmlTemplate = { template -> new XmlTemplateEngine("\t", false).createTemplate(template).make(new PropertyBindings(delegate, "")).toString() } -Object.metaClass.applyTextTemplate = { template -> new GStringTemplateEngine().createTemplate(template).make(new PropertyBindings(delegate, "")).toString() } +Object.metaClass.applyXmlTemplate = { template -> new XmlTemplateEngine("\t", false).createTemplate(template).make(new PropertyBindings(delegate, new UndefinedObject(""))).toString() } +Object.metaClass.applyTextTemplate = { template -> new GStringTemplateEngine().createTemplate(template).make(new PropertyBindings(delegate, new UndefinedObject(""))).toString() } // Shell helper diff --git a/source/net/sourceforge/filebot/format/UndefinedObject.java b/source/net/sourceforge/filebot/format/UndefinedObject.java new file mode 100644 index 00000000..b4360c4e --- /dev/null +++ b/source/net/sourceforge/filebot/format/UndefinedObject.java @@ -0,0 +1,41 @@ + +package net.sourceforge.filebot.format; + + +import groovy.lang.GroovyObjectSupport; + + +public class UndefinedObject extends GroovyObjectSupport { + + private String value; + + + private UndefinedObject(String value) { + this.value = value; + } + + + @Override + public Object getProperty(String property) { + return this; + } + + + @Override + public Object invokeMethod(String name, Object args) { + return this; + } + + + @Override + public void setProperty(String property, Object newValue) { + // ignore + } + + + @Override + public String toString() { + return value; + } + +} diff --git a/source/net/sourceforge/filebot/web/TheTVDBClient.java b/source/net/sourceforge/filebot/web/TheTVDBClient.java index bcad0bb9..ef5cd305 100644 --- a/source/net/sourceforge/filebot/web/TheTVDBClient.java +++ b/source/net/sourceforge/filebot/web/TheTVDBClient.java @@ -476,7 +476,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider { } - public List getGenre() { + public List getGenres() { // e.g. |Comedy| return split(get(SeriesProperty.Genre)); } diff --git a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java index 770b944d..3e7d1dd9 100644 --- a/test/net/sourceforge/filebot/web/TheTVDBClientTest.java +++ b/test/net/sourceforge/filebot/web/TheTVDBClientTest.java @@ -155,7 +155,7 @@ public class TheTVDBClientTest { assertEquals("Adam Baldwin", it.getActors().get(2)); assertEquals("TV-PG", it.getContentRating()); assertEquals("2007-09-24", it.getFirstAired().toString()); - assertEquals("Action and Adventure", it.getGenre().get(0)); + assertEquals("Action and Adventure", it.getGenres().get(0)); assertEquals(934814, it.getImdbId(), 0); assertEquals("English", it.getLanguage().getDisplayLanguage(Locale.ENGLISH)); assertEquals(310, it.getOverview().length()); diff --git a/website/scripts/artwork.tmdb.groovy b/website/scripts/artwork.tmdb.groovy index 21ee4d03..76f1855e 100644 --- a/website/scripts/artwork.tmdb.groovy +++ b/website/scripts/artwork.tmdb.groovy @@ -1,7 +1,7 @@ // filebot -script "http://filebot.sf.net/scripts/artwork.tmdb.groovy" -trust-script /path/to/media/ // EXPERIMENTAL // HERE THERE BE DRAGONS -if (net.sourceforge.filebot.Settings.applicationRevisionNumber < 808) throw new Exception("Application revision too old") +if (net.sourceforge.filebot.Settings.applicationRevisionNumber < 812) throw new Exception("Application revision too old") /* @@ -24,13 +24,13 @@ def fetchArtwork(outputFile, movieInfo, artworkType, artworkSize) { def fetchNfo(outputFile, movieInfo) { movieInfo.applyXmlTemplate(''' $name - ${released?.year} + $released.year $rating $votes $overview $runtime $certification - ${genres.size() > 0 ? genres.get(0) : ''} + ${!genres.empty ? genres[0] : ''} tt${imdbId.pad(7)} ''').saveAs(outputFile) diff --git a/website/scripts/artwork.tvdb.groovy b/website/scripts/artwork.tvdb.groovy index 66ba01eb..6a5befb2 100644 --- a/website/scripts/artwork.tvdb.groovy +++ b/website/scripts/artwork.tvdb.groovy @@ -1,7 +1,7 @@ // filebot -script "http://filebot.sf.net/scripts/artwork.tvdb.groovy" -trust-script /path/to/media/ // EXPERIMENTAL // HERE THERE BE DRAGONS -if (net.sourceforge.filebot.Settings.applicationRevisionNumber < 808) throw new Exception("Application revision too old") +if (net.sourceforge.filebot.Settings.applicationRevisionNumber < 812) throw new Exception("Application revision too old") /* @@ -26,13 +26,13 @@ def fetchNfo(outputFile, series) { println info info.applyXmlTemplate(''' $name - ${firstAired?.year} + $firstAired.year $rating $ratingCount $overview $runtime $contentRating - ${genre.size() > 0 ? genre.get(0) : ''} + ${!genres.empty ? genres[0] : ''} $id $bannerUrl $firstAired