File tree Expand file tree Collapse file tree 3 files changed +123
-0
lines changed
Expand file tree Collapse file tree 3 files changed +123
-0
lines changed Original file line number Diff line number Diff line change 1+ package util.yaml
2+
3+ data class PropertyTree (
4+ val name : String ,
5+ var value : String? = null
6+ ) {
7+
8+ private val childMap = HashMap <String , PropertyTree >()
9+
10+ fun add (propertyIterator : ListIterator <String >, propertyValue : String ) {
11+ val currentProperty = propertyIterator.next().trim()
12+
13+ if (! this .childMap.containsKey(currentProperty)) {
14+ this .childMap[currentProperty] = PropertyTree (currentProperty)
15+ }
16+
17+ if (! propertyIterator.hasNext()) {
18+ this .childMap[currentProperty]!! .value = propertyValue
19+ return
20+ }
21+
22+ this .childMap[currentProperty]!! .add(propertyIterator, propertyValue)
23+ }
24+
25+ fun convertToYamlFormat () = convertToYamlFormat(1 )
26+
27+ private fun convertToYamlFormat (depth : Int ): String {
28+ val stringBuilder = StringBuilder (" $name : " )
29+
30+ if (! this .hasChild()) {
31+ stringBuilder
32+ .append(this .value)
33+ .append(" \n " )
34+ } else {
35+ stringBuilder.append(" \n " )
36+
37+ this .childMap.values.forEach { property ->
38+ repeat(depth) {
39+ stringBuilder.append(" " )
40+ }
41+
42+ stringBuilder.append(property.convertToYamlFormat(depth + 1 ))
43+ }
44+ }
45+
46+ return stringBuilder.toString()
47+ }
48+
49+ private fun hasChild () = this .childMap.isNotEmpty()
50+
51+ }
Original file line number Diff line number Diff line change 1+ package util.yaml
2+
3+ class YamlConverter {
4+
5+ fun convertToYaml (propertiesString : String ): String {
6+ val propertiesMap = HashMap <String , PropertyTree >()
7+
8+ propertiesString.split(" \n " ).forEach {
9+ if (it.isBlank() || it.startsWith(" #" )) {
10+ return @forEach
11+ }
12+
13+ val property = it.split(" =" )
14+
15+ val propertyDetail = property[0 ].split(" ." )
16+
17+ val firstDetail = propertyDetail[0 ].trim()
18+
19+ if (! propertiesMap.containsKey(firstDetail)) {
20+ propertiesMap[firstDetail] = PropertyTree (firstDetail)
21+ }
22+
23+ propertiesMap[firstDetail]!! .add(
24+ propertyDetail.listIterator(1 ),
25+ extractPropertyValue(property.subList(1 , property.size))
26+ )
27+ }
28+
29+ val stringBuilder = StringBuilder ()
30+
31+ propertiesMap.values.forEach {
32+ stringBuilder
33+ .append(it.convertToYamlFormat())
34+ .append(" \n " )
35+ }
36+
37+ return stringBuilder.toString()
38+ }
39+
40+ private fun extractPropertyValue (propertyValueList : List <String >): String {
41+ val stringBuilder = StringBuilder ()
42+
43+ for (i in propertyValueList.indices) {
44+ stringBuilder.append(propertyValueList[i].trim())
45+
46+ if (i < propertyValueList.lastIndex) {
47+ stringBuilder.append(" =" )
48+ }
49+ }
50+
51+ return stringBuilder.toString()
52+ }
53+
54+ }
Original file line number Diff line number Diff line change 1+ package util.yaml
2+
3+ import kotlin.test.Test
4+
5+ class YamlConverterTest {
6+
7+ @Test
8+ fun convertPropertiesToYaml () {
9+ // given
10+ val propertyString = " kotlin.code.style=official"
11+ // when
12+ val yamlConverter = YamlConverter ()
13+ val yamlString = yamlConverter.convertToYaml(propertyString)
14+ // then
15+ require(" kotlin: \n code: \n style: official\n\n " == yamlString)
16+ }
17+
18+ }
You can’t perform that action at this time.
0 commit comments