validation - Grails供稿-插件,<pubDate> && <dc:date>,验证错误

标签 validation grails groovy rss feed

我刚刚尝试了 grails feeds-plugin http://grails.org/Feeds+Plugin

它非常容易且运行良好,但我不了解一件事。

一旦添加publishedDate,就像这样:

     reviews.each() {
         review -> entry('fooTitle'){
             publishedDate = review.dtCreated
             review.rating + ' ' + review.comment
        }
     }

它生成两个标签:
      <pubDate>Tue, 04 Sep 2012 12:10:02 GMT</pubDate>
      <dc:date>2012-09-04T12:10:02Z</dc:date>

数据库中的条目是:
"dtCreated": ISODate("2013-01-15T00:52:47.0Z"),

但我只希望生成<pubDate>,因为供稿验证器会引发以下错误:
An item should not include both pubDate and dc:date 

我该如何解决?我很想使用这个插件,但是我需要一个有效的RSS。

最佳答案

如果您不介意快速而又肮脏的解决方案,则可以尝试以下方法(它来自插件的集成测试并经过修改):

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以获取提要的字符串表示形式,然后对其进行解析,并删除“dc:date”标记。结果照常渲染。

我已经在Grails 1.3.7的插件测试套件中测试了此解决方法

关于validation - Grails供稿-插件,<pubDate> && <dc:date>,验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15003747/

相关文章:

WinForms TabControl 验证 : Switch to a tab where validation failed

validation - 验证动态填充的选择字段

grails - 将域ID设置为int或Long

java - Groovy 方法调用语法

html - 在 &lt;input type ="number"> 中禁用文本输入

swift - 如何从 Outlet Collection 获取文本字段值以进行密码验证?

grails - 为什么 Grails 命令对象默认提交对域对象的更改?

hibernate - 使用Oracle 11g hibernate (严重)-权限不足

hibernate - Grails "a different object with the same identifier value was already associated with the session"错误

groovy - 找不到合适的类加载器可供抓取