Last active
March 29, 2023 13:13
-
-
Save NosearY/cb88b14cae600672425138177e64fcf8 to your computer and use it in GitHub Desktop.
XML Stream Marshaller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jakarta.xml.bind.JAXBContext | |
import jakarta.xml.bind.JAXBElement | |
import jakarta.xml.bind.Marshaller | |
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
import java.io.FileOutputStream | |
import javax.xml.namespace.QName | |
import javax.xml.stream.XMLOutputFactory | |
import javax.xml.stream.XMLStreamWriter | |
object JaxbXmlStreamMarshaller { | |
private val logger: Logger = LoggerFactory.getLogger(this::class.java) | |
private val caResponseJaxbContext = JAXBContext.newInstance(CorpAction::class.java) | |
private val marshaller = caResponseJaxbContext.createMarshaller().also { | |
it.setProperty(Marshaller.JAXB_FRAGMENT, true) | |
} | |
private lateinit var xmlOut: XMLStreamWriter | |
fun open(fileName: String) { | |
xmlOut = XMLOutputFactory.newFactory().createXMLStreamWriter(FileOutputStream(fileName)); | |
xmlOut.writeStartDocument(); | |
xmlOut.writeStartElement("", "CAResponse", "http://www.cstools.broker.com") | |
} | |
fun write(corpAction: CorpAction) { | |
val element: JAXBElement<CorpAction> = JAXBElement<CorpAction>(QName.valueOf(CorpAction::class.simpleName), CorpAction::class.java, corpAction) | |
marshaller.marshal(element, xmlOut) | |
} | |
fun close() { | |
xmlOut.writeEndDocument() | |
xmlOut.close() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jakarta.xml.bind.JAXBContext | |
import jakarta.xml.bind.Marshaller | |
import jakarta.xml.bind.UnmarshalException | |
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
import org.springframework.util.ResourceUtils | |
import java.io.OutputStream | |
import java.io.Reader | |
import javax.xml.XMLConstants | |
import javax.xml.validation.Schema | |
import javax.xml.validation.SchemaFactory | |
object JaxbXmlUnmarshaller { | |
private val logger: Logger = LoggerFactory.getLogger(this::class.java) | |
private val caResponseJaxbContext = JAXBContext.newInstance(CAResponse::class.java) | |
private var caResponseSchema: Schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) | |
.newSchema(ResourceUtils.getFile("classpath:xsd/ca_v20230217.xsd")) | |
fun unmarshall(reader: Reader): CAResponse { | |
val unmarshaller = caResponseJaxbContext.createUnmarshaller() | |
unmarshaller.schema = caResponseSchema | |
return try { | |
unmarshaller.unmarshal(reader) as CAResponse | |
} catch (e: UnmarshalException) { | |
logger.error("Unable to parse corporate action xml", e) | |
throw XmlCorporateActionParsingException(ignorable = false, e.message) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `generate test data`() { | |
// given | |
val pathToXml = "corporateactionfiles/minimal/corpaction_CASH_DIVIDEND.xml" | |
val parsedXml = parseXml(pathToXml) | |
val b = parsedXml.corpAction[0] | |
val numOfCustomer = 10_500 * 10 | |
val numOfCaEvent = 100 | |
JaxbXmlStreamMarshaller.open("/tmp/ca-large.xml") | |
for (i in 1..numOfCaEvent * numOfCustomer) { | |
JaxbXmlStreamMarshaller.write(b.also { | |
it.id += i % numOfCaEvent | |
}) | |
} | |
JaxbXmlStreamMarshaller.close() | |
} | |
/* | |
<CAResponse> | |
<CorpActon></CorpAction> | |
<CorpActon></CorpAction> | |
<CorpActon></CorpAction> | |
... | |
</CAResponse> | |
*/ | |
private fun parseXml(pathToXml: String): CAResponse { | |
return JaxbXmlUnmarshaller.unmarshall( | |
InputStreamReader( | |
ByteArrayInputStream( | |
ClassPathResource(pathToXml).inputStream.readAllBytes() | |
) | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment