diff --git a/multiapps-common/src/main/java/module-info.java b/multiapps-common/src/main/java/module-info.java index 6aa3890b..312d0f6f 100644 --- a/multiapps-common/src/main/java/module-info.java +++ b/multiapps-common/src/main/java/module-info.java @@ -16,6 +16,5 @@ requires static java.compiler; requires static org.immutables.value; - requires java.xml; requires jakarta.xml.bind; } diff --git a/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/Messages.java b/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/Messages.java index c5ca0fa3..1d66aec4 100644 --- a/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/Messages.java +++ b/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/Messages.java @@ -11,13 +11,6 @@ private Messages() { public static final String CANNOT_CONVERT_JSON_STREAM_TO_LIST = "Error while converting JSON input stream to list: {0}"; public static final String CANNOT_CONVERT_JSON_STRING_TO_LIST = "Error while converting JSON string \"{0}\" to list: {1}"; public static final String CANNOT_PARSE_JSON_STRING_TO_TYPE = "Error while parsing JSON string \"{0}\" to type \"{1}\" "; - public static final String COULD_NOT_CREATE_DOCUMENT_BUILDER_FACTORY = "Could not create document builder factory"; - public static final String CANNOT_PARSE_XML_STREAM = "Error while parsing XML input stream"; - public static final String COULD_NOT_CREATE_JAXB_UNMARSHALLER = "Could not create JAXB unmarshaller"; - public static final String COULD_NOT_CREATE_JAXB_MARSHALLER = "Could not create JAXB marshaller"; - public static final String UNABLE_TO_PARSE_SCHEMA = "Could not parse schema at location \"{0}\""; - public static final String UNABLE_TO_UNMARSHAL_OBJECT = "An unexpected error occurred while unmarshalling the XML content"; - public static final String UNABLE_TO_MARSHAL_OBJECT = "An unexpected error occurred while marshalling the object"; public static final String ERROR_PARSING_YAML_STREAM = "Error while parsing YAML stream: {0}"; public static final String ERROR_PARSING_YAML_STRING = "Error while parsing YAML string: %n%s%n%s"; public static final String COULD_NOT_PARSE_BOOLEAN_FLAG = "Cannot parse \"{0}\" flag - expected a boolean format."; diff --git a/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/util/XmlUtil.java b/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/util/XmlUtil.java deleted file mode 100644 index 3134d701..00000000 --- a/multiapps-common/src/main/java/org/cloudfoundry/multiapps/common/util/XmlUtil.java +++ /dev/null @@ -1,196 +0,0 @@ -package org.cloudfoundry.multiapps.common.util; - -import jakarta.xml.bind.JAXBContext; -import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Marshaller; -import jakarta.xml.bind.Unmarshaller; -import org.cloudfoundry.multiapps.common.Messages; -import org.cloudfoundry.multiapps.common.ParsingException; -import org.cloudfoundry.multiapps.common.SLException; -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.validation.SchemaFactory; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URL; -import java.text.MessageFormat; -import java.util.Map; - -public class XmlUtil { - - private static final String EXTERNAL_GENERAL_ENTITIES_FEATURE = "http://xml.org/sax/features/external-general-entities"; - private static final String EXTERNAL_PARAMETER_ENTITIES_FEATURE = "http://xml.org/sax/features/external-parameter-entities"; - private static final String DISALLOW_DOCTYPE_DECLARATION_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl"; - - private XmlUtil() { - } - - public static String toXml(T object) throws SLException { - return toXml(object, false); - } - - public static String toXml(T object, Map properties) throws SLException { - return toXml(object, true, properties); - } - - public static String toXml(T object, boolean formattedOutput) throws SLException { - try { - Writer buffer = new StringWriter(); - getMarshaller(getContext(object.getClass()), formattedOutput).marshal(object, buffer); - - return buffer.toString(); - } catch (JAXBException e) { - handleMarshallingException(e); - return null; - } - } - - public static String toXml(T object, boolean formattedOutput, Map properties) throws SLException { - try { - Writer buffer = new StringWriter(); - getMarshaller(getContext(object.getClass()), properties).marshal(object, buffer); - return buffer.toString(); - } catch (JAXBException e) { - handleMarshallingException(e); - return null; - } - } - - public static T fromXml(InputStream xml, Class clazz, URL schemaLocation) throws ParsingException { - return fromXml(getAsDocument(xml), clazz, schemaLocation); - } - - public static T fromXml(InputStream xml, Class clazz) throws ParsingException { - return fromXml(xml, clazz, null); - } - - public static T fromXml(String xml, Class clazz, URL schemaLocation) throws ParsingException { - return fromXml(getAsDocument(xml), clazz, schemaLocation); - } - - public static T fromXml(String xml, Class clazz) throws ParsingException { - return fromXml(xml, clazz, null); - } - - private static T fromXml(Document xml, Class clazz, URL schemaLocation) throws ParsingException { - try { - return getUnmarshaller(getContext(clazz), schemaLocation).unmarshal(xml, clazz) - .getValue(); - } catch (SAXException | JAXBException e) { - handleUnmarshallingException(e, schemaLocation); - return null; - } - } - - private static JAXBContext getContext(Class clazz) throws JAXBException { - return JAXBContext.newInstance(clazz); - } - - private static Marshaller getMarshaller(JAXBContext context, boolean formattedOutput) { - try { - return formattedOutput ? getFormattingMarshaller(context) : getRegularMarshaller(context); - } catch (JAXBException e) { - throw new SLException(Messages.COULD_NOT_CREATE_JAXB_MARSHALLER, e); - } - } - - private static Marshaller getMarshaller(JAXBContext context, Map properties) { - try { - return properties.isEmpty() ? getRegularMarshaller(context) : getPropertyMarshaller(context, properties); - } catch (JAXBException e) { - throw new SLException(Messages.COULD_NOT_CREATE_JAXB_MARSHALLER, e); - } - } - - private static Marshaller getPropertyMarshaller(JAXBContext context, Map properties) throws JAXBException { - Marshaller marshaller = getRegularMarshaller(context); - for (Map.Entry property : properties.entrySet()) { - marshaller.setProperty(property.getKey(), property.getValue()); - } - return marshaller; - } - - private static Marshaller getFormattingMarshaller(JAXBContext context) throws JAXBException { - Marshaller marshaller = getRegularMarshaller(context); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - return marshaller; - } - - private static Marshaller getRegularMarshaller(JAXBContext context) throws JAXBException { - return context.createMarshaller(); - } - - private static Document getAsDocument(InputStream xml) { - return getAsDocument(new InputSource(xml)); - } - - private static Document getAsDocument(String xml) { - return getAsDocument(new InputSource(new StringReader(xml))); - } - - private static Document getAsDocument(InputSource xml) { - try { - return getDocumentBuilder().parse(xml); - } catch (SAXException | IOException e) { - throw new ParsingException(e, Messages.CANNOT_PARSE_XML_STREAM); - } - } - - private static DocumentBuilder getDocumentBuilder() { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - try { - factory.setFeature(DISALLOW_DOCTYPE_DECLARATION_FEATURE, true); - factory.setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false); - factory.setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false); - return factory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new ParsingException(Messages.COULD_NOT_CREATE_DOCUMENT_BUILDER_FACTORY, e); - } - } - - private static Unmarshaller getValidatingUnmarshaller(JAXBContext context, URL schemaLocation) throws SAXException, JAXBException { - Unmarshaller unmarshaller = getRegularUnmarshaller(context); - unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) - .newSchema(schemaLocation)); - return unmarshaller; - } - - private static Unmarshaller getUnmarshaller(JAXBContext context, URL schemaLocation) throws SAXException { - try { - return (schemaLocation != null) ? getValidatingUnmarshaller(context, schemaLocation) : getRegularUnmarshaller(context); - } catch (JAXBException e) { - throw new ParsingException(Messages.COULD_NOT_CREATE_JAXB_UNMARSHALLER, e); - } - } - - private static void handleUnmarshallingException(Exception e, URL schemaLocation) { - if (e instanceof SAXException) { - throw new ParsingException(MessageFormat.format(Messages.UNABLE_TO_PARSE_SCHEMA, schemaLocation), e); - } else if (e instanceof JAXBException && e.getCause() instanceof SAXParseException) { - Throwable cause = e.getCause(); - throw new ParsingException(cause.getMessage(), cause); - } else if (e instanceof JAXBException) { - throw new ParsingException(Messages.UNABLE_TO_UNMARSHAL_OBJECT, e); - } - } - - private static Unmarshaller getRegularUnmarshaller(JAXBContext context) throws JAXBException { - return context.createUnmarshaller(); - } - - private static void handleMarshallingException(Exception e) { - throw new SLException(e, Messages.UNABLE_TO_MARSHAL_OBJECT); - } - -} diff --git a/pom.xml b/pom.xml index b845c340..20cb851d 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,6 @@ 2.12.1 3.1.1 2.21.2 - 4.0.7 4.0.5 1.28.0 @@ -238,12 +237,6 @@ jakarta.xml.bind-api ${jakarta.xml.bind-api.version} - - - org.glassfish.jaxb - jaxb-runtime - ${jaxb-runtime.version} - tools.jackson.core