* make xml templating resilient against unexpectedly undefined properties

This commit is contained in:
Reinhard Pointner 2011-12-29 16:23:56 +00:00
parent d86091c3aa
commit 50adfcc300
6 changed files with 52 additions and 10 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -476,7 +476,7 @@ public class TheTVDBClient extends AbstractEpisodeListProvider {
}
public List<String> getGenre() {
public List<String> getGenres() {
// e.g. |Comedy|
return split(get(SeriesProperty.Genre));
}

View File

@ -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());

View File

@ -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('''<movie>
<title>$name</title>
<year>${released?.year}</year>
<year>$released.year</year>
<rating>$rating</rating>
<votes>$votes</votes>
<plot>$overview</plot>
<runtime>$runtime</runtime>
<mpaa>$certification</mpaa>
<genre>${genres.size() > 0 ? genres.get(0) : ''}</genre>
<genre>${!genres.empty ? genres[0] : ''}</genre>
<id>tt${imdbId.pad(7)}</id>
</movie>
''').saveAs(outputFile)

View File

@ -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('''<tvshow xmlns:gsp='http://groovy.codehaus.org/2005/gsp'>
<title>$name</title>
<year>${firstAired?.year}</year>
<year>$firstAired.year</year>
<rating>$rating</rating>
<votes>$ratingCount</votes>
<plot>$overview</plot>
<runtime>$runtime</runtime>
<mpaa>$contentRating</mpaa>
<genre>${genre.size() > 0 ? genre.get(0) : ''}</genre>
<genre>${!genres.empty ? genres[0] : ''}</genre>
<id>$id</id>
<thumb>$bannerUrl</thumb>
<premiered>$firstAired</premiered>