]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-889: When converting json to xml, convert properties named '.' to xml element...
authorRay Lee <ray.lee@lyrasis.org>
Mon, 27 Jul 2020 22:10:18 +0000 (18:10 -0400)
committerRay Lee <ray.lee@lyrasis.org>
Mon, 27 Jul 2020 22:10:18 +0000 (18:10 -0400)
services/common/src/main/java/org/collectionspace/services/common/xmljson/ConversionUtils.java
services/common/src/main/java/org/collectionspace/services/common/xmljson/JsonToXmlStreamConverter.java
services/common/src/test/java/org/collectionspace/services/common/xmljson/test/JsonToXmlStreamConverterTest.java
services/common/src/test/resources/test-data/xmljson/export-invocation.json [new file with mode: 0644]
services/common/src/test/resources/test-data/xmljson/export-invocation.xml [new file with mode: 0644]

index e3e17ec1ad3b775a53c507055d3d009eeb4a6a18..0f22031edc0b4690c4029d9556fb1528b2ec36e0 100644 (file)
@@ -13,28 +13,28 @@ public class ConversionUtils {
      * field names.
      */
     public static final String XML_ATTRIBUTE_PREFIX = "@";
-    
+
     /**
      * Prefix to prepend to XML namespace prefixes when converting to
      * JSON field names.
      */
     public static final String XML_NAMESPACE_PREFIX = XML_ATTRIBUTE_PREFIX + "xmlns:";
-    
+
     /**
      * Converts an XML attribute name to a JSON field name.
-     * 
+     *
      * @param xmlAttributeName the XML attribute name
      * @return the JSON field name
      */
     public static String xmlAttributeNameToJsonFieldName(String xmlAttributeName) {
         return XML_ATTRIBUTE_PREFIX + xmlAttributeName;
     }
-    
+
     /**
      * Converts a JSON field name to an XML attribute name.
      * The field name must be in the expected format, as determined by
      * isXmlAttribute().
-     * 
+     *
      * @param jsonFieldName the JSON field name
      * @return the XML attribute name
      */
@@ -44,19 +44,19 @@ public class ConversionUtils {
 
     /**
      * Converts an XML namespace prefix to a JSON field name.
-     * 
+     *
      * @param xmlNamespacePrefix the XML namespace prefix
      * @return the JSON field name
      */
     public static String xmlNamespacePrefixToJsonFieldName(String xmlNamespacePrefix) {
         return XML_NAMESPACE_PREFIX + xmlNamespacePrefix;
     }
-    
+
     /**
      * Converts a JSON field name to an XML namespace prefix.
      * The field name must be in the expected format, as determined by
      * isXmlNamespace().
-     * 
+     *
      * @param jsonFieldName the JSON field name
      * @return the XML namespace prefix
      */
@@ -67,7 +67,7 @@ public class ConversionUtils {
     /**
      * Determines if a JSON field name represents an XML
      * attribute.
-     *  
+     *
      * @param jsonFieldName the field name to test
      * @return true if the field name represents an XML attribute,
      *         false otherwise
@@ -75,11 +75,11 @@ public class ConversionUtils {
     public static boolean isXmlAttribute(String jsonFieldName) {
         return jsonFieldName.startsWith(XML_ATTRIBUTE_PREFIX);
     }
-    
+
     /**
      * Determines if a JSON field name represents an XML
      * namespace prefix.
-     * 
+     *
      * @param jsonFieldName the field name to test
      * @return true if the field name represents an XML namespace prefix,
      *         false otherwise
@@ -87,21 +87,32 @@ public class ConversionUtils {
     public static boolean isXmlNamespace(String jsonFieldName) {
         return jsonFieldName.startsWith(XML_NAMESPACE_PREFIX);
     }
-    
+
+    /**
+     * Determines if a JSON field name represents XML element content.
+     *
+     * @param jsonFieldName the field name to test
+     * @return true if the field name represents XML element content,
+     *         false otherwise
+     */
+    public static boolean isXmlContent(String jsonFieldName) {
+        return jsonFieldName.equals(".");
+    }
+
     /**
      * Converts an XML element QName to a JSON field name.
-     * 
+     *
      * @param name the XML element QName
      * @return the JSON field name
      */
     public static String jsonFieldNameFromXMLQName(QName name) {
         String prefix = name.getPrefix();
         String localPart = name.getLocalPart();
-        
+
         if (StringUtils.isNotEmpty(prefix)) {
             return prefix + ":" + localPart;
         }
-        
+
         return localPart;
     }
 }
index 2e0575a5dfd267a46cff817fd83198cdda2b7e64..e6130b2473eefcb0244843ae93c7b335516990a2 100644 (file)
@@ -29,6 +29,8 @@ import com.fasterxml.jackson.core.JsonToken;
  * <ul>
  * <li>JSON fields starting with "@xmlns:" are converted XML namespace declarations.</li>
  * <li>JSON fields starting with "@" are converted to XML attributes.</li>
+ * <li>JSON fields named "." are converted to XML element content. This is useful when the desired
+ *     XML output is an element with one or more attributes, and some text content.<li>
  * <li>Other JSON fields are converted to identically-named XML elements.</li>
  * <li>The contents of JSON objects are converted to XML child elements.</li>
  * <li>The contents of JSON arrays are expanded into multiple XML elements, each
@@ -268,6 +270,9 @@ public class JsonToXmlStreamConverter {
                 xmlWriter.writeAttribute(localName, value);
             }
         }
+        else if (field.isScalar() && isXmlContent(name)) {
+            xmlWriter.writeCharacters(value);
+        }
         else {
             // It doesn't look like an XML attribute or namespace declaration.
             // Output an XML element with the same name as the field, whose
index fc5c4c378a853aaa4f4714a6b9db22eaebcd98de..208d1ae19d845e40c2fdbc87a02aadc20eac5fbf 100644 (file)
@@ -19,7 +19,7 @@ import com.fasterxml.jackson.core.JsonParseException;
 
 public class JsonToXmlStreamConverterTest {
     public final String FILE_PATH = "test-data/xmljson/";
-    
+
     @Test
     public void testConvert() throws XMLStreamException, JsonParseException, IOException {
         testConvert("record");
@@ -31,9 +31,10 @@ public class JsonToXmlStreamConverterTest {
         testConvert("numeric-json");
         testConvert("boolean-json");
         testConvert("single-list-item-json");
+        testConvert("export-invocation");
         testConvertThrows("empty-json", XMLStreamException.class);
     }
-    
+
     private void testConvert(String fileName) throws XMLStreamException, IOException {
         System.out.println("---------------------------------------------------------");
         System.out.println("Converting JSON to XML: " + fileName);
@@ -42,44 +43,44 @@ public class JsonToXmlStreamConverterTest {
         ClassLoader classLoader = getClass().getClassLoader();
         File jsonFile = new File(classLoader.getResource(FILE_PATH + fileName + ".json").getFile());
         File xmlFile = new File(classLoader.getResource(FILE_PATH + fileName + ".xml").getFile());
-        
+
         FileInputStream in = new FileInputStream(jsonFile);
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        
+
         JsonToXmlStreamConverter converter = new JsonToXmlStreamConverter(in, out);
         converter.convert();
-        
+
         System.out.println(out.toString("UTF-8"));
-        
+
         Diff diff = DiffBuilder
                 .compare(Input.fromStream(out.toInputStream()))
                 .withTest(Input.fromFile(xmlFile))
                 .ignoreComments()
                 .ignoreWhitespace()
                 .build();
-        
+
         System.out.println(diff.toString());
-        
+
         assertFalse(diff.hasDifferences());
     }
-    
+
     private void testConvertThrows(String fileName, Class<?> exceptionClass) throws XMLStreamException, IOException {
         boolean caught = false;
-        
+
         try {
             testConvert(fileName);
         }
         catch(XMLStreamException|IOException e) {
             if (e.getClass().isAssignableFrom(exceptionClass)) {
                 caught = true;
-                
+
                 System.out.println(e.toString());
             }
             else {
                 throw e;
             }
         }
-        
+
         assertTrue(caught);
     }
 }
diff --git a/services/common/src/test/resources/test-data/xmljson/export-invocation.json b/services/common/src/test/resources/test-data/xmljson/export-invocation.json
new file mode 100644 (file)
index 0000000..5c6514f
--- /dev/null
@@ -0,0 +1,23 @@
+{
+       "ns2:invocationContext": {
+               "@xmlns:ns2": "http://collectionspace.org/services/common/invocable",
+               "mode": "single",
+               "singleCSID": "dc929ede-081f-4eaa-a439-358b89887c91",
+               "docType": "CollectionObject",
+               "includeFields": {
+                       "field": [
+                               "collectionobjects_common:objectNumber",
+                               {
+                                       "@name": "fieldCollectionDateGroup",
+                                       ".": "collectionobjects_common:fieldCollectionDateGroup/dateDisplayDate"
+                               },
+                               "collectionobjects_common:titleGroupList/titleGroup/title",
+                               "collectionobjects_common:titleGroupList/titleGroup/titleLanguage",
+                               "collectionobjects_common:titleGroupList/titleGroup/titleType",
+                               "collectionobjects_common:titleGroupList/titleGroup/titleTranslationSubGroupList/titleTranslationSubGroup/titleTranslation",
+                               "collectionobjects_common:titleGroupList/titleGroup/titleTranslationSubGroupList/titleTranslationSubGroup/titleTranslationLanguage"
+                       ]
+               },
+               "outputMIME": "text/csv"
+       }
+}
diff --git a/services/common/src/test/resources/test-data/xmljson/export-invocation.xml b/services/common/src/test/resources/test-data/xmljson/export-invocation.xml
new file mode 100644 (file)
index 0000000..07cc148
--- /dev/null
@@ -0,0 +1,15 @@
+<ns2:invocationContext xmlns:ns2="http://collectionspace.org/services/common/invocable">
+       <mode>single</mode>
+       <singleCSID>dc929ede-081f-4eaa-a439-358b89887c91</singleCSID>
+       <docType>CollectionObject</docType>
+       <includeFields>
+               <field>collectionobjects_common:objectNumber</field>
+               <field name="fieldCollectionDateGroup">collectionobjects_common:fieldCollectionDateGroup/dateDisplayDate</field>
+               <field>collectionobjects_common:titleGroupList/titleGroup/title</field>
+               <field>collectionobjects_common:titleGroupList/titleGroup/titleLanguage</field>
+               <field>collectionobjects_common:titleGroupList/titleGroup/titleType</field>
+               <field>collectionobjects_common:titleGroupList/titleGroup/titleTranslationSubGroupList/titleTranslationSubGroup/titleTranslation</field>
+               <field>collectionobjects_common:titleGroupList/titleGroup/titleTranslationSubGroupList/titleTranslationSubGroup/titleTranslationLanguage</field>
+       </includeFields>
+       <outputMIME>text/csv</outputMIME>
+</ns2:invocationContext>