Skip to content

Commit 700abac

Browse files
committed
Add a hooked XmlWriter that closes the OutputStreamWriter that is created for the sink. This needs to be done here as appendables are
not closable.
1 parent a9273b6 commit 700abac

File tree

4 files changed

+124
-6
lines changed

4 files changed

+124
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* This file is part of xmlutil.
5+
*
6+
* This file is licenced to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You should have received a copy of the license with the source distribution.
9+
* Alternatively, you may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
package nl.adaptivity.xmlutil.core.kxio
22+
23+
import nl.adaptivity.xmlutil.XmlDelegatingWriter
24+
import nl.adaptivity.xmlutil.XmlWriter
25+
26+
internal fun XmlWriter.onClose(handler: () -> Unit): XmlWriter {
27+
return CloseHandlingWriter(this, handler)
28+
}
29+
30+
internal class CloseHandlingWriter(delegate: XmlWriter, private val closeHandler: ()->Unit): XmlDelegatingWriter(delegate) {
31+
override fun close() {
32+
try {
33+
super.close()
34+
} finally {
35+
closeHandler()
36+
}
37+
}
38+
}

core/kxio/src/jvmMain/kotlin/nl/adaptivity/xmlutil/core/kxio/accessors.jvm.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ public actual fun IXmlStreaming.newWriter(
4040
repairNamespaces: Boolean,
4141
xmlDeclMode: XmlDeclMode,
4242
): XmlWriter {
43-
return OutputStreamWriter(target.asOutputStream(), Charsets.UTF_8).let { output ->
44-
newWriter(output, repairNamespaces, xmlDeclMode)
45-
}
43+
val output = OutputStreamWriter(target.asOutputStream(), Charsets.UTF_8)
44+
return newWriter(output, repairNamespaces, xmlDeclMode).onClose { output.close() }
4645
}
4746

4847
public actual fun IXmlStreaming.newGenericWriter(
4948
target: Sink,
5049
repairNamespaces: Boolean,
5150
xmlDeclMode: XmlDeclMode,
5251
): XmlWriter {
53-
return OutputStreamWriter(target.asOutputStream(), Charsets.UTF_8).let { output ->
54-
newGenericWriter(output, repairNamespaces, xmlDeclMode)
55-
}
52+
val output = OutputStreamWriter(target.asOutputStream(), Charsets.UTF_8)
53+
return newGenericWriter(output, repairNamespaces, xmlDeclMode).onClose { output.close() }
5654
}

serialization-io/build.gradle.kts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2024.
3+
*
4+
* This file is part of xmlutil.
5+
*
6+
* This file is licenced to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You should have received a copy of the license with the source distribution.
9+
* Alternatively, you may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
import net.devrieze.gradle.ext.addNativeTargets
22+
import net.devrieze.gradle.ext.applyDefaultXmlUtilHierarchyTemplate
23+
import net.devrieze.gradle.ext.doPublish
24+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
25+
import org.jetbrains.kotlin.gradle.dsl.HasConfigurableKotlinCompilerOptions
26+
27+
plugins {
28+
alias(libs.plugins.dokka)
29+
id("projectPlugin")
30+
kotlin("multiplatform")
31+
`maven-publish`
32+
signing
33+
idea
34+
}
35+
36+
val autoModuleName = "net.devrieze.xmlutil.serialization.kxio"
37+
38+
kotlin {
39+
explicitApi()
40+
applyDefaultXmlUtilHierarchyTemplate()
41+
42+
jvm()
43+
js {
44+
browser()
45+
compilerOptions {
46+
sourceMap = true
47+
verbose = true
48+
}
49+
}
50+
51+
targets.all {
52+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
53+
if (this is HasConfigurableKotlinCompilerOptions<*>) {
54+
compilerOptions {
55+
freeCompilerArgs.add("-Xexpect-actual-classes")
56+
}
57+
}
58+
}
59+
60+
sourceSets {
61+
val commonMain by getting {
62+
dependencies {
63+
api(projects.serialization)
64+
// api(projects.core.kxio)
65+
}
66+
}
67+
68+
val commonTest by getting {
69+
dependencies {
70+
implementation(kotlin("test"))
71+
implementation(kotlin("test-annotations-common"))
72+
implementation(projects.testutil)
73+
implementation(projects.serialization)
74+
}
75+
}
76+
}
77+
}
78+
79+
addNativeTargets()
80+
81+
doPublish("serialization-io")

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ include("coreAndroid")
6363
include("coreKXIO")
6464

6565
include(":serialization")
66+
include(":serialization-io")
6667
include(":xmlserializable")
6768
include(":testutil")
6869
include(":examples")

0 commit comments

Comments
 (0)