-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.kt
216 lines (197 loc) · 6.97 KB
/
base.kt
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
@file:Suppress("unused", "HasPlatformType", "SpellCheckingInspection")
package staks
import org.codehaus.stax2.XMLInputFactory2
import java.io.File
import java.io.InputStream
import java.net.URL
import javax.xml.stream.XMLEventReader
import javax.xml.stream.events.XMLEvent
import javax.xml.transform.Source
import javax.xml.stream.XMLStreamReader as XMLSR
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(inputStream){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @return [T]
*/
fun <T> staks(input: InputStream, func: (StaxBuilder<T>.() -> () -> T)) = StaxBuilder(reader(input), func).process()
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(inputStream, encoding){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @return [T]
*/
fun <T> staks(input: InputStream, encoding: String, func: (StaxBuilder<T>.() -> () -> T)) =
StaxBuilder(reader(input, encoding), func).process()
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(file){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @return [T]
*/
fun <T> staks(file: File, func: (StaxBuilder<T>.() -> () -> T)) = StaxBuilder(reader(file), func).process()
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(source){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @param source [Source]
* @return [T]
*/
fun <T> staks(source: Source, func: (StaxBuilder<T>.() -> () -> T)) = StaxBuilder(reader(source), func).process()
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(xmlStreamReader){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @return [T]
*/
fun <T> staks(reader: XMLSR, func: (StaxBuilder<T>.() -> () -> T)) = StaxBuilder(reader(reader), func).process()
/**
* Main entry point for building objects.
* Usage example:
* ```kotlin
* val text = staks(url){
* val txt = tagText("a");
* { txt() }
* }
* ```
* Here `text` will contain content of first met `a` tag in XML document. Parsing is lazy, so it will stop after
* first found `a` tag.
* @return [T]
*/
fun <T> staks(src: URL, func: (StaxBuilder<T>.() -> () -> T)) = StaxBuilder(reader(src), func).process()
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(file: File) = factory().createXMLEventReader(file)
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(inputStream: InputStream) = factory().createXMLEventReader(inputStream)
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(inputStream: InputStream, encoding: String) = factory().createXMLEventReader(inputStream, encoding)
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(reader: XMLSR) = factory().createXMLEventReader(reader)
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(source: Source) = factory().createXMLEventReader(source)
/**
* Provider user with [XMLEventReader], suitable for usage with [StaxBuilder].
*/
fun reader(source: URL) = factory().createXMLEventReader(source)
private fun factory(): XMLInputFactory2 =
(XMLInputFactory2.newInstance() as XMLInputFactory2).also { it.configureForConvenience() }
/**
* Root [CompoundHandler] which creates reader and passes [XMLEvent] to underlying registered handlers and evetually
* returns built entity via calling `process method`. For usage examples @see [staks].
*/
class StaxBuilder<T>(override val reader: XMLEventReader, func: StaxBuilder<T>.() -> () -> T) : CompoundHandler<T>() {
private val builder = func()
internal fun process(): T {
while (reader.hasNext()) {
val ev = reader.nextEvent()
for (it in children.filter { it.matches(ev) }) {
it.process(ev)
if (it.isSingular)
children.remove(it)
}
if (children.isEmpty()) break
}
return builder()
}
private fun getDoNotCall() = IllegalStateException("Root builder should not process anything!")
override val value: () -> T get() = throw getDoNotCall()
override val isSingular: Boolean get() = throw getDoNotCall()
override fun matches(event: XMLEvent): Boolean = throw getDoNotCall()
override fun process(ev: XMLEvent): Unit = throw getDoNotCall()
override fun reset(): Unit = throw getDoNotCall()
}
/**
* Extracts data from tag assuming it contains only text or the first text met inside tag.
*/
fun CompoundHandler<*>.tagText(tagName: String): TagTextHandler =
registerChild(TagTextHandler(tagName, reader))
/**
* Extracts data from tag's attribute.
*/
fun CompoundHandler<*>.attribute(tagName: String, attributeName: String): AttrHandler =
registerChild(AttrHandler(tagName, attributeName))
/**
* Extracts data from repeating tag
*
* Usage example:
* ```kotlin
* val names = list("tag-name") {
* var data = tagText("inner-tag-name");
* { data() }
* }
* ```
* After parsing `names()` will contain list of contants of `inner-tag-name` tags.
* @param tagName name of outer tag
* @param func description of inner tags
*/
fun <T> CompoundHandler<*>.list(tagName: String, func: CompoundHandler<*>.() -> () -> T) =
registerChild(ListHandler(tagName, func, reader))
/**
* Creates [UnnamedListHandler] from supplied handler.
* @param handler — underlying Handler with `isSingular` true. Behavior for `isSinguar` false is not defined.
*/
fun <T> CompoundHandler<*>.list(handler: Handler<T>): Handler<List<T>> = decorate(UnnamedListHandler(handler))
/**
* Intended to build object from single complex object
*
* Usage example:
* ```kotlin
* val pair = single("complex") {
* val first = tagText("first")
* val second = tagText("second");
* { first() to second() }
* }
* ```
*
* After parsing `pair()` will contain [Pair] of contents of tags `first` and `second`, located inside `complex` tag.
*
* @param tagName outer tag name
* @param func description of inner tags
*/
fun <T : Any> CompoundHandler<*>.single(tagName: String, func: CompoundHandler<*>.() -> () -> T): SingleHandler<T> =
registerChild(SingleHandler(tagName, reader, func))