You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/03/06 15:02:54 UTC

[causeway] branch master updated: CAUSEWAY-3304: JaxbUtils: adds generateSchema(..) to generate XSD from Java types

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/master by this push:
     new cee8c951be CAUSEWAY-3304: JaxbUtils: adds generateSchema(..) to generate XSD from Java types
cee8c951be is described below

commit cee8c951be08ad8a9cf620dd0c301789f055e989
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Mar 6 16:02:50 2023 +0100

    CAUSEWAY-3304: JaxbUtils: adds generateSchema(..) to generate XSD from
    Java types
---
 .../org/apache/causeway/commons/io/JaxbUtils.java  | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/commons/src/main/java/org/apache/causeway/commons/io/JaxbUtils.java b/commons/src/main/java/org/apache/causeway/commons/io/JaxbUtils.java
index f049a9b690..4236a694a5 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/JaxbUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/JaxbUtils.java
@@ -36,8 +36,11 @@ import javax.xml.bind.JAXBContextFactory;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
+import javax.xml.bind.SchemaOutputResolver;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.namespace.QName;
+import javax.xml.transform.Result;
+import javax.xml.transform.stream.StreamResult;
 
 import org.springframework.lang.Nullable;
 
@@ -267,8 +270,6 @@ public class JaxbUtils {
         return sb.toString();
     }
 
-    // -- CUSTOMIZERS
-
     // -- MAPPER FACTORY
 
     private JaxbOptions createOptions(
@@ -281,6 +282,24 @@ public class JaxbUtils {
         return opts.build();
     }
 
+    // -- GENERATE XSD
+
+    /**
+     * Generates the schema documents for given {@link JAXBContext} and writes them to given
+     * {@link DataSink}.
+     */
+    public void generateSchema(final @NonNull JAXBContext jaxbContext, final DataSink dataSink) {
+        dataSink.writeAll(os->{
+            val schemaOutputResolver = new SchemaOutputResolver() {
+                @Override
+                public Result createOutput(final String namespaceURI, final String suggestedFileName) {
+                    return new StreamResult(os);
+                }
+            };
+            jaxbContext.generateSchema(schemaOutputResolver);
+        });
+    }
+
     // -- JAXB CONTEXT FACTORIES AND CACHING
 
     /** not cached */