-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atom.scala
245 lines (202 loc) · 8.82 KB
/
Atom.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package org.caoilte.atomizer.model
import javax.xml.bind.annotation._
import javax.xml.bind.annotation.adapters._
import org.caoilte.atomizer.model.Atom.{AtomDateTimeAdapter, AtomDateTimeOptionAdapter}
import org.caoilte.atomizer.model.Email.EmailOptionAdapter
import org.caoilte.atomizer.model.Generator.GeneratorOptionAdapter
import org.caoilte.atomizer.model.Link.{LinkOptionAdapter, RelationshipAdapter, Relationship}
import org.caoilte.atomizer.model.Source.SourceOptionAdapter
import org.caoilte.atomizer.model.Text.TextOptionAdapter
import org.caoilte.jaxb._
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import spray.http.{MediaType, Uri}
import scala.collection.mutable
import scala.runtime.ScalaRunTime
object Atom {
val dateFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ")
class AtomDateTimeAdapter extends DateTimeAdapter(dateFormat)
class AtomDateTimeOptionAdapter extends DateTimeOptionAdapter(dateFormat)
}
case class Atom(feed: Feed)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Feed(id:String,
title:Text,
@xmlTypeAdapter(classOf[AtomDateTimeAdapter]) updated: DateTime,
entry:Array[Entry],
author:Array[Person] = Array[Person](),
@xmlTypeAdapter(classOf[LinkOptionAdapter]) link:Option[Link] = None,
category:Array[Category] = Array[Category](),
contributor:Array[Person] = Array[Person](),
@xmlTypeAdapter(classOf[GeneratorOptionAdapter]) generator: Option[Generator] = None,
@xmlTypeAdapter(classOf[UriOptionAdapter]) icon:Option[Uri] = None,
@xmlTypeAdapter(classOf[UriOptionAdapter]) logo:Option[Uri] = None,
@xmlTypeAdapter(classOf[TextOptionAdapter]) rights:Option[Text] = None,
@xmlTypeAdapter(classOf[StringOptionAdapter]) subtitle: Option[String] = None) {
val xmlns = "http://www.w3.org/2005/Atom"
private def this() =
this(
"", Text(""), new DateTime(), Array[Entry](), Array[Person](), None, Array[Category](), Array[Person](),
None, None, None, None, None)
def productArity = 13
def productElement(n: Int): Any = n match {
case 0 => id
case 1 => title
case 2 => updated
case 3 => mutable.WrappedArray.make(entry)
case 4 => mutable.WrappedArray.make(author)
case 5 => link
case 6 => mutable.WrappedArray.make(category)
case 7 => mutable.WrappedArray.make(contributor)
case 8 => generator
case 9 => icon
case 10 => logo
case 11 => rights
case 12 => subtitle
case _ => throw new IndexOutOfBoundsException(n.toString)
}
override def equals(that: Any) = ScalaRunTime._equals(this, that)
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Person(name:String,
@xmlTypeAdapter(classOf[UriOptionAdapter]) uri:Option[Uri] = None,
@xmlTypeAdapter(classOf[EmailOptionAdapter]) email:Option[Email] = None) {
private def this() = this("", None, None)
}
object Email {
class EmailOptionAdapter extends OptionAdapter[Email](null, Email(""))
}
@XmlAccessorType(XmlAccessType.FIELD)
case class Email(@xmlValue email:String) {
override def toString = email
private def this() = this("")
}
object Link {
sealed trait Relationship
case object alternative extends Relationship
case object enclosure extends Relationship
case object related extends Relationship
case object self extends Relationship
case object via extends Relationship
case object first extends Relationship
case object last extends Relationship
case object previous extends Relationship
case object next extends Relationship
class RelationshipAdapter extends XmlAdapter[String, Relationship] {
def marshal(v: Relationship): String = if (v.equals(alternative)) null else v.toString
def unmarshal(v: String):Relationship = v match {
case "alternative" => alternative
case "enclosure" => enclosure
case "related" => related
case "self" => self
case "via" => via
case "first" => first
case "last" => last
case "previous" => previous
case "next" => next
case null => alternative
case other => throw new IllegalArgumentException(s"Invalid relationship '$other'")
}
}
class LinkOptionAdapter extends OptionAdapter[Link](null)
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Link(@xmlAttribute @xmlTypeAdapter(classOf[UriAdapter]) href:Uri,
@xmlAttribute @xmlTypeAdapter(classOf[RelationshipAdapter]) rel:Relationship = Link.alternative,
@xmlAttribute @xmlTypeAdapter(classOf[MediaTypeOptionAdapter]) `type`:Option[MediaType] = None,
@xmlAttribute @xmlTypeAdapter(classOf[StringOptionAdapter]) hreflang:Option[String] = None,
@xmlAttribute @xmlTypeAdapter(classOf[LongOptionAdapter]) length:Option[Long] = None) {
private def this() = this(Uri(""))
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Category(@xmlAttribute term: String,
@xmlAttribute @xmlTypeAdapter(classOf[UriOptionAdapter]) scheme:Option[Uri] = None,
@xmlAttribute @xmlTypeAdapter(classOf[StringOptionAdapter]) label:Option[String] = None) {
private def this() = this("")
}
object Generator {
class GeneratorOptionAdapter extends OptionAdapter[Generator](null, Generator(""))
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Generator(@xmlValue text:String,
@xmlAttribute @xmlTypeAdapter(classOf[UriOptionAdapter]) uri:Option[Uri] = None,
@xmlAttribute @xmlTypeAdapter(classOf[StringOptionAdapter]) version:Option[String] = None) {
private def this() = this("", None, None)
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Entry(id:String,
title:Text,
@xmlTypeAdapter(classOf[AtomDateTimeAdapter]) updated: DateTime,
author:Array[Person]= Array[Person](),
@xmlTypeAdapter(classOf[Text.TextOptionAdapter]) content:Option[Text] = None,
@xmlTypeAdapter(classOf[LinkOptionAdapter]) link:Option[Link],
@xmlTypeAdapter(classOf[Text.TextOptionAdapter]) summary:Option[Text],
category: Array[Category] = Array[Category](),
contributor: Array[Person] = Array[Person](),
@xmlTypeAdapter(classOf[AtomDateTimeOptionAdapter]) published:Option[DateTime] = None,
@xmlTypeAdapter(classOf[SourceOptionAdapter]) source:Option[Source] = None,
@xmlTypeAdapter(classOf[Text.TextOptionAdapter]) rights:Option[Text] = None) {
private def this() =
this("", Text(""), new DateTime(), Array[Person](), None, None, None, Array[Category](), Array[Person](),
None, None, None)
def productArity = 12
def productElement(n: Int): Any = n match {
case 0 => id
case 1 => title
case 2 => updated
case 3 => mutable.WrappedArray.make(author)
case 4 => content
case 5 => link
case 6 => summary
case 7 => mutable.WrappedArray.make(category)
case 8 => mutable.WrappedArray.make(contributor)
case 9 => published
case 10 => source
case 11 => rights
case _ => throw new IndexOutOfBoundsException(n.toString)
}
override def equals(that: Any) = ScalaRunTime._equals(this, that)
}
object Source {
class SourceOptionAdapter extends OptionAdapter[Source](null)
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Source(@xmlTypeAdapter(classOf[StringOptionAdapter]) id:Option[String],
@xmlTypeAdapter(classOf[TextOptionAdapter]) title:Option[Text],
@xmlTypeAdapter(classOf[AtomDateTimeOptionAdapter]) updated: Option[DateTime],
@xmlTypeAdapter(classOf[TextOptionAdapter]) rights:Option[Text] = None) {
private def this() = this(None, None, None, None)
}
object Text {
sealed trait Type
case object text extends Type
case object html extends Type
case object xhtml extends Type
class TypeAdapter extends XmlAdapter[String, Type] {
def marshal(v: Type): String = if (v.equals(text)) null else v.toString
def unmarshal(v: String):Type = v match {
case "text" => text
case "html" => html
case "xhtml" => xhtml
case null => text
case other => throw new IllegalArgumentException(s"Invalid type '$other'")
}
}
class TypeOptionAdapter extends CustomOptionAdapter[String, Type](new TypeAdapter)
class TextOptionAdapter extends OptionAdapter[Text](null, Text(""))
}
trait Content
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
case class Text(@xmlValue content:String,
@xmlAttribute @xmlTypeAdapter(classOf[Text.TypeAdapter]) `type`:Text.Type = Text.text) extends Content {
private def this() = this("", Text.text)
}
case class ContentLink(src:Uri, `type`:MediaType)