Skip to content

Commit 4ccca89

Browse files
authored
closes #64 (#65)
* closes #64
1 parent 14a2da1 commit 4ccca89

File tree

10 files changed

+377
-2
lines changed

10 files changed

+377
-2
lines changed

.github/badges/branches.svg

Lines changed: 1 addition & 1 deletion
Loading

.github/badges/jacoco.svg

Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import org.xpathqs.core.selector.base.BaseSelector
26+
import java.lang.reflect.Field
27+
28+
29+
class AssociationFinder(
30+
protected val fields: Collection<Field>,
31+
protected val selectors: Collection<BaseSelector>,
32+
protected val associations: Collection<IModelAssociation>
33+
) {
34+
protected lateinit var unusedFields: HashSet<Field>
35+
36+
val mappings: Collection<ModelAssociation>
37+
get() {
38+
unusedFields = HashSet()
39+
40+
return scanFields(associations.first())
41+
}
42+
43+
private fun scanFields(association: IModelAssociation): ArrayList<ModelAssociation> {
44+
val res = ArrayList<ModelAssociation>()
45+
46+
fields.forEach {
47+
val sel = getAssociation(association, it)
48+
if (sel != null) {
49+
it.isAccessible = true
50+
unusedFields.remove(it)
51+
res.add(
52+
ModelAssociation(
53+
sel,
54+
it
55+
)
56+
)
57+
} else {
58+
unusedFields.add(it)
59+
}
60+
}
61+
62+
return res
63+
}
64+
65+
private fun getAssociation(association: IModelAssociation, field: Field) =
66+
selectors.find {
67+
association.isSelectorMatchToField(field, it)
68+
}
69+
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import org.xpathqs.core.selector.base.BaseSelector
26+
import java.lang.reflect.Field
27+
28+
interface IModelAssociation {
29+
fun isSelectorMatchToField(field: Field, sel: BaseSelector): Boolean
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
interface ISelectorValueExtractor {
26+
fun apply(assoc: ModelAssociation): Any
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import org.xpathqs.core.reflection.SelectorReflectionFields
26+
import org.xpathqs.core.selector.Block
27+
28+
class Model(
29+
private val cls: Class<*>,
30+
private val block: Block,
31+
private val assocList: Collection<IModelAssociation> = listOf(
32+
NameAssociation()
33+
)
34+
) {
35+
val associations: Collection<ModelAssociation> by lazy {
36+
val fields = cls.declaredFields
37+
val selectors = SelectorReflectionFields(block).innerSelectors
38+
39+
val ass =
40+
AssociationFinder(
41+
fields.toList(),
42+
selectors,
43+
assocList
44+
)
45+
46+
ass.mappings
47+
}
48+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import org.xpathqs.core.reflection.toField
26+
import org.xpathqs.core.selector.base.BaseSelector
27+
import java.lang.reflect.Field
28+
import kotlin.reflect.KProperty
29+
30+
data class ModelAssociation(
31+
val sel: BaseSelector,
32+
val field: Field
33+
) {
34+
constructor(sel: BaseSelector, prop: KProperty<*>) : this(sel, prop.toField())
35+
36+
fun applyTo(source: Any, extractor: ISelectorValueExtractor) {
37+
field.set(source, extractor.apply(this))
38+
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import org.xpathqs.core.selector.base.BaseSelector
26+
import org.xpathqs.core.selector.extensions.simpleName
27+
import java.lang.reflect.Field
28+
29+
class NameAssociation : IModelAssociation {
30+
override fun isSelectorMatchToField(field: Field, sel: BaseSelector) = field.name == sel.simpleName
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
class ValueSetter(
26+
val associations: Collection<ModelAssociation>,
27+
val extractor: ISelectorValueExtractor
28+
) {
29+
fun init(obj: Any) {
30+
associations.forEach {
31+
it.applyTo(obj, extractor)
32+
}
33+
}
34+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2021 Nikita A. Chegodaev
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package org.xpathqs.core.model
24+
25+
import assertk.assertAll
26+
import assertk.assertThat
27+
import assertk.assertions.isEqualTo
28+
import org.junit.jupiter.api.BeforeAll
29+
import org.junit.jupiter.api.Test
30+
import org.xpathqs.core.reflection.SelectorParser
31+
import org.xpathqs.core.selector.Block
32+
import org.xpathqs.core.util.SelectorFactory.tagSelector
33+
34+
data class TestModel(
35+
val name1: String = "",
36+
val name2: String = ""
37+
)
38+
39+
object TestModelPage: Block() {
40+
val name1 = tagSelector("div")
41+
val name2 = tagSelector("p")
42+
}
43+
44+
class XpathExtractor : ISelectorValueExtractor {
45+
override fun apply(assoc: ModelAssociation): String {
46+
return assoc.sel.toXpath()
47+
}
48+
}
49+
50+
internal class ModelTest {
51+
52+
@Test
53+
fun associationTest() {
54+
val res = Model(TestModel::class.java, TestModelPage).associations
55+
56+
val expected = listOf(
57+
ModelAssociation(
58+
TestModelPage.name1,
59+
TestModel::name1
60+
),
61+
ModelAssociation(
62+
TestModelPage.name2,
63+
TestModel::name2
64+
)
65+
)
66+
67+
assertThat(expected)
68+
.isEqualTo(res)
69+
}
70+
71+
@Test
72+
fun applyTest() {
73+
val model = TestModel()
74+
75+
ValueSetter(
76+
Model(TestModel::class.java, TestModelPage).associations,
77+
XpathExtractor()
78+
).init(model)
79+
80+
assertAll {
81+
assertThat(model.name1)
82+
.isEqualTo("//div")
83+
84+
assertThat(model.name2)
85+
.isEqualTo("//p")
86+
}
87+
}
88+
89+
companion object {
90+
@BeforeAll
91+
@JvmStatic
92+
fun init() {
93+
SelectorParser(TestModelPage).parse()
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)