XMLTransformer.kt 2.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * xml-data-transform
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
Jonas Waeber's avatar
Jonas Waeber committed
18
package org.memobase.xml
19

Jonas Waeber's avatar
Jonas Waeber committed
20
21
22
23
import ch.memobase.settings.HeaderMetadata
import java.io.ByteArrayInputStream
import java.io.InputStream
import javax.xml.transform.stream.StreamSource
24
25
26
import net.sf.saxon.s9api.Processor
import net.sf.saxon.s9api.SAXDestination
import net.sf.saxon.s9api.StaticError
Jonas Waeber's avatar
Jonas Waeber committed
27
28
import org.memobase.utils.MissingIdentifierException
import org.memobase.utils.XsltException
29

Jonas Waeber's avatar
Jonas Waeber committed
30
class XMLTransformer {
31
32
    private val processor = Processor(false)

Jonas Waeber's avatar
Jonas Waeber committed
33
34
35
36
    fun xsltFunction(xsltData: ByteArray): ByteArray {
        return xsltData
    }

Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41
42
    fun applyXSLT(
        key: String,
        headers: HeaderMetadata,
        data: InputStream,
        xsltFile: ByteArray
    ): Pair<String, SAXContentHandler> {
Jonas Waeber's avatar
Jonas Waeber committed
43
        val contentHandler = SAXContentHandler(key, headers.xmlIdentifierFieldName, headers.xmlRecordTag)
44
45
46
        val errorList = mutableListOf<StaticError>()
        val xsltCompiler = processor.newXsltCompiler()
        xsltCompiler.setErrorList(errorList)
Jonas Waeber's avatar
Jonas Waeber committed
47
        val source = StreamSource(ByteArrayInputStream(xsltFile))
48
        val executable = xsltCompiler.compile(source)
Jonas Waeber's avatar
Jonas Waeber committed
49
50
        if (errorList.isNotEmpty()) {
            throw XsltException(errorList)
51
        }
Jonas Waeber's avatar
Jonas Waeber committed
52
        val transformer = executable.load()
Jonas Waeber's avatar
Jonas Waeber committed
53
54
55
56
        data.use { stream ->
            transformer.setSource(StreamSource(stream))
            transformer.destination = SAXDestination(contentHandler)
            transformer.transform()
57
        }
Jonas Waeber's avatar
Jonas Waeber committed
58
        if (contentHandler.identifier == null || contentHandler.identifier?.isEmpty() == true) {
Jonas Waeber's avatar
Jonas Waeber committed
59
            throw MissingIdentifierException(key, headers.xmlIdentifierFieldName)
60
        } else {
Jonas Waeber's avatar
Jonas Waeber committed
61
            return Pair(contentHandler.identifier!!, contentHandler)
62
63
64
        }
    }
}