Skip to content

Commit 3f233c5

Browse files
committed
Added atom:author element parsing
1 parent 505ebc8 commit 3f233c5

File tree

8 files changed

+218
-2
lines changed

8 files changed

+218
-2
lines changed

src/main/kotlin/com/github/magneticflux/rss/Converters.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ internal val fallbackPersister = createRssPersister()
1414
*/
1515
internal val InputNode.fullName: String
1616
get() {
17-
val prefix = this.prefix
17+
var prefix = this.prefix
1818
val name = this.name
19+
20+
if (prefix.isEmpty())
21+
prefix = when (this.reference) {
22+
"http://www.w3.org/1999/xhtml" -> "xhtml"
23+
"http://www.w3.org/2005/Atom" -> "atom"
24+
else -> prefix
25+
}
26+
1927
return if (prefix.isEmpty()) name else "$prefix:$name"
2028
}
2129

src/main/kotlin/com/github/magneticflux/rss/Persisters.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.github.magneticflux.rss
22

3+
import com.github.magneticflux.rss.namespaces.atom.converters.AtomAuthorConverter
4+
import com.github.magneticflux.rss.namespaces.atom.converters.AtomFeedConverter
5+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomAuthor
6+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomFeed
37
import com.github.magneticflux.rss.namespaces.itunes.converters.ITunesImageConverter
48
import com.github.magneticflux.rss.namespaces.itunes.converters.ITunesSubCategoryConverter
59
import com.github.magneticflux.rss.namespaces.itunes.converters.ITunesTopLevelCategoryConverter
@@ -62,9 +66,13 @@ fun createRssStrategy(): Strategy {
6266
this.bind(Enclosure::class.java, EnclosureConverter)
6367
this.bind(Guid::class.java, GuidConverter)
6468
this.bind(Source::class.java, SourceConverter)
69+
6570
this.bind(ITunesTopLevelCategory::class.java, ITunesTopLevelCategoryConverter)
6671
this.bind(ITunesSubCategory::class.java, ITunesSubCategoryConverter)
6772
this.bind(ITunesImage::class.java, ITunesImageConverter)
73+
74+
this.bind(AtomFeed::class.java, AtomFeedConverter)
75+
this.bind(AtomAuthor::class.java, AtomAuthorConverter)
6876
})
6977
}
7078

@@ -80,4 +88,4 @@ fun createRssMatcher(): Matcher {
8088
this.bind(Locale::class.java, LocaleLanguageTransform)
8189
this.bind(URL::class.java, URLTransform)
8290
}
83-
}
91+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.magneticflux.rss.namespaces.atom.converters
2+
3+
import com.github.magneticflux.rss.children
4+
import com.github.magneticflux.rss.createChild
5+
import com.github.magneticflux.rss.fullName
6+
import com.github.magneticflux.rss.namespaces.atom.elements.ATOM_REFERENCE
7+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomAuthor
8+
import org.simpleframework.xml.convert.Converter
9+
import org.simpleframework.xml.stream.InputNode
10+
import org.simpleframework.xml.stream.OutputNode
11+
12+
/**
13+
* @author Mitchell Skaggs
14+
* @since 1.2.0
15+
*/
16+
object AtomAuthorConverter : Converter<AtomAuthor> {
17+
override fun read(node: InputNode): AtomAuthor {
18+
lateinit var name: String
19+
var uri: String? = null
20+
var email: String? = null
21+
22+
node.children.forEach {
23+
when (it.fullName) {
24+
"atom:name" -> name = it.value
25+
"atom:uri" -> uri = it.value
26+
"atom:email" -> email = it.value
27+
}
28+
}
29+
30+
return AtomAuthor(name, uri, email)
31+
}
32+
33+
override fun write(node: OutputNode, value: AtomAuthor) {
34+
val writable = value.toWritable()
35+
36+
node.createChild(ATOM_REFERENCE, "name", writable.name)
37+
writable.uri?.let { node.createChild(ATOM_REFERENCE, "uri", it) }
38+
writable.email?.let { node.createChild(ATOM_REFERENCE, "email", it) }
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.magneticflux.rss.namespaces.atom.converters
2+
3+
import com.github.magneticflux.rss.children
4+
import com.github.magneticflux.rss.fallbackPersister
5+
import com.github.magneticflux.rss.fullName
6+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomAuthor
7+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomFeed
8+
import org.simpleframework.xml.convert.Converter
9+
import org.simpleframework.xml.stream.InputNode
10+
import org.simpleframework.xml.stream.OutputNode
11+
12+
/**
13+
* @author Mitchell Skaggs
14+
* @since 1.2.0
15+
*/
16+
object AtomFeedConverter : Converter<AtomFeed> {
17+
override fun read(node: InputNode): AtomFeed {
18+
var author: AtomAuthor? = null
19+
20+
node.children.forEach {
21+
when (it.fullName) {
22+
"atom:author" -> author = fallbackPersister.read(AtomAuthor::class.java, it)
23+
}
24+
}
25+
26+
return AtomFeed(author)
27+
}
28+
29+
override fun write(node: OutputNode, value: AtomFeed) {
30+
val writable = value.toWritable()
31+
32+
writable.author?.let { fallbackPersister.write(it, node) }
33+
}
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.github.magneticflux.rss.namespaces.atom.elements
2+
3+
import com.github.magneticflux.rss.namespaces.atom.converters.AtomAuthorConverter
4+
import com.github.magneticflux.rss.namespaces.standard.elements.HasReadWrite
5+
import org.simpleframework.xml.Namespace
6+
import org.simpleframework.xml.Root
7+
8+
/**
9+
* Properties common to all representations of a `<person>` element.
10+
*
11+
* @since 1.2.0
12+
*/
13+
interface ICommonAtomPerson : HasReadWrite<IAtomPerson, IWritableAtomPerson> {
14+
val name: String
15+
val uri: String?
16+
val email: String?
17+
}
18+
19+
/**
20+
* The final RSS view of a `<person>` element. Defaults are used if applicable.
21+
*
22+
* @since 1.2.0
23+
*/
24+
interface IAtomPerson : ICommonAtomPerson {
25+
override fun toReadOnly(): IAtomPerson = this
26+
}
27+
28+
/**
29+
* The literal contents of a `<person>` element. Elements with defaults may be omitted or invalid.
30+
*
31+
* @since 1.2.0
32+
*/
33+
interface IWritableAtomPerson : ICommonAtomPerson {
34+
override fun toWritable(): IWritableAtomPerson = this
35+
}
36+
37+
/**
38+
* This class represents an Author object in a [AtomFeed].
39+
*
40+
* @author Mitchell Skaggs
41+
* @since 1.2.0
42+
* @see AtomAuthorConverter
43+
*/
44+
@Root(name = "author")
45+
@Namespace(reference = ATOM_REFERENCE)
46+
data class AtomAuthor(
47+
override val name: String,
48+
override val uri: String?,
49+
override val email: String?
50+
) : IAtomPerson, IWritableAtomPerson
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.magneticflux.rss.namespaces.atom.elements
2+
3+
import com.github.magneticflux.rss.namespaces.atom.converters.AtomFeedConverter
4+
import com.github.magneticflux.rss.namespaces.standard.elements.HasReadWrite
5+
import org.simpleframework.xml.Namespace
6+
import org.simpleframework.xml.Root
7+
8+
/**
9+
* Properties common to all representations of a `<feed>` element.
10+
*
11+
* @since 1.2.0
12+
*/
13+
interface ICommonAtomFeed : HasReadWrite<IAtomFeed, IWritableAtomFeed> {
14+
val author: AtomAuthor?
15+
}
16+
17+
/**
18+
* The final RSS view of a `<feed>` element. Defaults are used if applicable.
19+
*
20+
* @since 1.2.0
21+
*/
22+
interface IAtomFeed : ICommonAtomFeed {
23+
override fun toReadOnly(): IAtomFeed = this
24+
}
25+
26+
/**
27+
* The literal contents of a `<feed>` element. Elements with defaults may be omitted or invalid.
28+
*
29+
* @since 1.2.0
30+
*/
31+
interface IWritableAtomFeed : ICommonAtomFeed {
32+
override fun toWritable(): IWritableAtomFeed = this
33+
}
34+
35+
/**
36+
* This class represents an atom:feed.
37+
*
38+
* @author Mitchell Skaggs
39+
* @since 1.2.0
40+
* @see AtomFeedConverter
41+
*/
42+
@Root(name = "feed")
43+
@Namespace(reference = ATOM_REFERENCE)
44+
data class AtomFeed(
45+
override val author: AtomAuthor?
46+
) : IAtomFeed, IWritableAtomFeed

src/test/kotlin/com/github/magneticflux/rss/RssTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.magneticflux.rss
22

33
import com.github.magneticflux.rss.SampleUtils.getSample
4+
import com.github.magneticflux.rss.namespaces.atom.elements.AtomFeed
45
import com.github.magneticflux.rss.namespaces.itunes.elements.ITunesSubCategory
56
import com.github.magneticflux.rss.namespaces.itunes.elements.ITunesTopLevelCategory
67
import com.github.magneticflux.rss.namespaces.standard.elements.Category
@@ -392,5 +393,29 @@ class RssTest : Spek({
392393
}
393394
}
394395
}
396+
397+
given("sample_atom", { "$it RSS feed" }) {
398+
val rssFeed = persister.read(AtomFeed::class.java, getSample("$it.xml"))
399+
400+
on("feed reread") {
401+
val rssText = persister.write(rssFeed)
402+
403+
println(rssText)
404+
405+
val rereadRssFeed = persister.read(AtomFeed::class.java, rssText)
406+
407+
it("should equal original RSS feed read") {
408+
assertThat(rereadRssFeed, equalTo(rssFeed))
409+
}
410+
411+
it("should have the same hashCode as original RSS feed read") {
412+
assertThat(rereadRssFeed.hashCode(), equalTo(rssFeed.hashCode()))
413+
}
414+
415+
it("should have the same String representation as original RSS feed read") {
416+
assertThat(rereadRssFeed.toString(), equalTo(rssFeed.toString()))
417+
}
418+
}
419+
}
395420
}
396421
})

src/test/resources/com/github/magneticflux/rss/sample_atom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<feed xmlns="http://www.w3.org/2005/Atom">
3+
<author>
4+
<name>Mark Pilgrim</name>
5+
<uri>http://example.org/</uri>
6+
<email>f8dy@example.com</email>
7+
</author>
38
<title type="text">dive into mark</title>
49
<subtitle type="html">
510
A &lt;em&gt;lot&lt;/em&gt; of effort

0 commit comments

Comments
 (0)