Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/main/java/org/entur/siri/XmlInputFactoryProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/

package org.entur.siri;

import javax.xml.stream.XMLInputFactory;

/**
* Provides a shared {@link XMLInputFactory} when the StAX provider is Woodstox
* (thread-safe after initialization), otherwise creates a new factory per call.
*/
public class XmlInputFactoryProvider {

private static final XMLInputFactory SHARED_XML_INPUT_FACTORY = createSharedFactory();

/**
* Returns a shared instance if the StAX provider is Woodstox (thread-safe after
* initialization), otherwise creates a new factory per call (spec-safe default).
*/
public static XMLInputFactory getXmlInputFactory() {
if (SHARED_XML_INPUT_FACTORY != null) {
return SHARED_XML_INPUT_FACTORY;
}
return XMLInputFactory.newInstance();
}

private static XMLInputFactory createSharedFactory() {
XMLInputFactory factory = XMLInputFactory.newInstance();
// Woodstox factories are thread-safe after initialization.
// https://github.com/FasterXML/Woodstox4/blob/master/release-notes/USAGE
if (factory.getClass().getName().startsWith("com.ctc.wstx.")) {
return factory;
}
return null;
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/entur/siri21/util/SiriXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.entur.siri21.util;

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
import org.entur.siri.XmlInputFactoryProvider;
import uk.org.siri.siri21.Siri;

import jakarta.xml.bind.JAXBContext;
Expand Down Expand Up @@ -51,16 +52,16 @@ private static void init() throws JAXBException {

public static Siri parseXml(String xml) throws JAXBException, XMLStreamException {
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XMLInputFactory xmlif = XMLInputFactory.newInstance();

XMLInputFactory xmlif = XmlInputFactoryProvider.getXmlInputFactory();
XMLStreamReader xmler = xmlif.createXMLStreamReader(new StringReader(xml));

return (Siri) jaxbUnmarshaller.unmarshal(xmler);
}

public static Siri parseXml(InputStream inputStream) throws JAXBException, XMLStreamException {
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLInputFactory xmlif = XmlInputFactoryProvider.getXmlInputFactory();
XMLStreamReader xmler = xmlif.createXMLStreamReader(inputStream);

return (Siri) jaxbUnmarshaller.unmarshal(xmler);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/rutebanken/siri20/util/SiriXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

import org.entur.siri.XmlInputFactoryProvider;
import uk.org.siri.siri20.Siri;

public class SiriXml {
Expand All @@ -53,16 +54,16 @@ private static void init() throws JAXBException {

public static Siri parseXml(String xml) throws JAXBException, XMLStreamException {
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XMLInputFactory xmlif = XMLInputFactory.newInstance();

XMLInputFactory xmlif = XmlInputFactoryProvider.getXmlInputFactory();
XMLStreamReader xmler = xmlif.createXMLStreamReader(new StringReader(xml));

return (Siri) jaxbUnmarshaller.unmarshal(xmler);
}

public static Siri parseXml(InputStream inputStream) throws JAXBException, XMLStreamException {
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLInputFactory xmlif = XmlInputFactoryProvider.getXmlInputFactory();
XMLStreamReader xmler = xmlif.createXMLStreamReader(inputStream);

return (Siri) jaxbUnmarshaller.unmarshal(xmler);
Expand Down
Loading