如果你不介意快速而肮脏的解决方案,你可以试试这个(它取自插件的集成测试并修改):
import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
import feedsplugin.FeedBuilder
import com.sun.syndication.io.SyndFeedOutput
class TestController {
private renderToString(feedType, feedVersion, Closure closure) {
def builder = new FeedBuilder()
builder.feed(closure)
def type = feedType
def version = feedVersion
SyndFeedOutput output = new SyndFeedOutput()
def sw = new StringWriter()
output.output(builder.makeFeed(type, version),sw)
sw.toString()
}
private removeDcDate(String rssFeed) {
def dom = new XmlSlurper().parseText(rssFeed)
dom.channel[0].item.eachWithIndex { item, i ->
dom.channel[0].item[i].date = {}
}
def newResp = XmlUtil.serialize(new StreamingMarkupBuilder().bind {
mkp.declareNamespace (rdf:"http://www.w3.org/1999/02/22-rdf-syntax-ns#")
mkp.declareNamespace (dc:"http://purl.org/dc/elements/1.1/")
mkp.declareNamespace (content:"http://purl.org/rss/1.0/modules/content/")
mkp.declareNamespace (itunes:"http://www.itunes.com/dtds/podcast-1.0.dtd")
mkp.declareNamespace (taxo:"http://purl.org/rss/1.0/modules/taxonomy/")
mkp.yield dom
})
newResp
}
def test = {
def articles = ['A', 'B', 'C']
def outStr= renderToString("rss", "2.0") {
title = 'Test feed'
link = 'http://somewhere.com/'
description = "This is a test feed"
articles.each() { article ->
entry("Title for \$article") {
content(type:'text/html') {
return "Content for \$article"
}
link = 'http://somewhere.com/x'
publishedDate = new Date()
}
}
}
def cleanedFeed = removeDcDate(outStr)
render text: cleanedFeed, contentType: "application/rss+xml", encoding: "UTF-8"
}
}
基本上它所做的是直接调用 feedbuilder 来获取 feed 的字符串表示,然后对其进行解析并删除“dc:date”标签。结果照常呈现。
我已经在带有 grails 1.3.7 的插件测试套件中测试了这个解决方法