You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2022/12/02 05:11:47 UTC

[GitHub] [daffodil] tuxji commented on a diff in pull request #821: Add support for new tunable "xmlOutputStyle"

tuxji commented on code in PR #821:
URL: https://github.com/apache/daffodil/pull/821#discussion_r1037784480


##########
daffodil-propgen/src/main/resources/org/apache/daffodil/xsd/dafext.xsd:
##########
@@ -559,6 +559,17 @@
             </xs:restriction>
           </xs:simpleType>
         </xs:element>
+        <xs:element name="xmlOutputStyle" type="daf:TunableXMLOutputStyle" default="standard" minOccurs="0">
+          <xs:annotation>
+            <xs:documentation>
+              values is a whitespace separated list of tokens drawn from this set.
+              Values are:
+              - standard: (Use if data is not being pretty printed, or will not be re-read in, or if whitespace is fungible in the actual data format. This applies to xs:string and its derivatives),
+              - prettyPrintSafe: preserves the XML Infoset exactly including whitespace characters. This XML can be pretty printed without indentation changes modifying element values.
+              - other values are reserved for future use.

Review Comment:
   Other values aren't possible since the type is an enumeration.



##########
daffodil-sapi/src/test/resources/test/sapi/mySchemaCDATAString.dfdl.xsd:
##########
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+  targetNamespace="http://example.com" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://example.com">
+
+  <annotation>
+    <appinfo source="http://www.ogf.org/dfdl/">
+      <dfdl:format ref="tns:GeneralFormat" />
+    </appinfo>
+  </annotation>
+
+  <include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+
+  <element name="string" type="xsd:string" dfdl:lengthKind="delimited" dfdl:terminator="$"/>
+
+</schema>

Review Comment:
   Last line doesn't have a newline.



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala:
##########
@@ -156,8 +158,11 @@ class XMLTextInfosetOutputter private (writer: java.io.Writer, pretty: Boolean)
 
     if (!isNilled(simple) && simple.hasValue) {
       if (simple.erd.optPrimType.get == NodeInfo.String) {
-        val simpleVal = simple.dataValueAsString
-        if (simple.erd.runtimeProperties.get(XMLTextInfoset.stringAsXml) == "true") {
+        var simpleVal = simple.dataValueAsString
+        if (xmlOutputStyle == XMLOutputStyle.PrettyPrintSafe){
+          simpleVal = "<![CDATA[%s]]>".format(remapped(simpleVal).replaceAll("]]>", "]]]]><![CDATA[>"))

Review Comment:
   I was going to ask if you did, but I see you do have a test to check that this replaceAll call actually produces the correct XML you expect it to produce.  



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/infoset/XMLTextInfosetOutputter.scala:
##########
@@ -156,8 +158,11 @@ class XMLTextInfosetOutputter private (writer: java.io.Writer, pretty: Boolean)
 
     if (!isNilled(simple) && simple.hasValue) {
       if (simple.erd.optPrimType.get == NodeInfo.String) {
-        val simpleVal = simple.dataValueAsString
-        if (simple.erd.runtimeProperties.get(XMLTextInfoset.stringAsXml) == "true") {
+        var simpleVal = simple.dataValueAsString
+        if (xmlOutputStyle == XMLOutputStyle.PrettyPrintSafe){
+          simpleVal = "<![CDATA[%s]]>".format(remapped(simpleVal).replaceAll("]]>", "]]]]><![CDATA[>"))
+          writer.write(simpleVal)
+        } else if (simple.erd.runtimeProperties.get(XMLTextInfoset.stringAsXml) == "true") {
           writeStringAsXml(simpleVal)
         } else {
           writer.write(scala.xml.Utility.escape(remapped(simpleVal)))

Review Comment:
   For easier maintainability, I suggest mutating simpleVal and calling write.write(simpleVal) here too.



##########
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/DataProcessor.scala:
##########
@@ -372,7 +372,7 @@ class DataProcessor private (
       validationMode match {
         case ValidationMode.Full | ValidationMode.Custom(_) =>
           val bos = new java.io.ByteArrayOutputStream()
-          val xmlOutputter = new XMLTextInfosetOutputter(bos, false)
+          val xmlOutputter = new XMLTextInfosetOutputter(bos, false, tunables.xmlOutputStyle)

Review Comment:
   I think Steve suggested only passing xmlOutputStyle to XMLTextInfosetOutputter programmatically in the API for now and finding a better way in another issue/PR for the CLI to pass a custom parameter to outputters than to add another tunable parameter.  Did I get that right, Steve, or are tunables probably as a good way as another?



##########
daffodil-japi/src/test/java/org/apache/daffodil/example/TestJavaAPI.java:
##########
@@ -873,7 +873,7 @@ public void testJavaAPI20() throws IOException, ClassNotFoundException {
         InputSourceDataInputStream disDP = new InputSourceDataInputStream(fisDP);
         InputSourceDataInputStream disSAX = new InputSourceDataInputStream(fisSAX);
         ByteArrayOutputStream xmlBos = new ByteArrayOutputStream();
-        XMLTextInfosetOutputter outputter = new XMLTextInfosetOutputter(xmlBos, true);
+        XMLTextInfosetOutputter outputter = new XMLTextInfosetOutputter(xmlBos, true, null);

Review Comment:
   I'm surprised the Java compiler allowed null here instead of XMLOutputStyle.standard or XMLOutputStyle.prettyPrintSafe.  I don't think null can be a correct value even if the compiler doesn't complain (see <https://stackoverflow.com/questions/13059528/instantiate-a-scala-class-from-java-and-use-the-default-parameters-of-the-const>).  
   
   The Java-API XMLTextInfosetOutputter has two overloaded 2-arg and 3-arg constructors.  There's no need to even call the 3-arg constructor here unless you want to cover it with a test, but this test didn't even throw a NullException (was it exhaustive enough?).



##########
daffodil-sapi/src/test/resources/test/sapi/mySchemaCDATAFloat.dfdl.xsd:
##########
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+      http://www.apache.org/licenses/LICENSE-2.0
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+  targetNamespace="http://example.com" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://example.com">
+
+  <annotation>
+    <appinfo source="http://www.ogf.org/dfdl/">
+      <dfdl:format ref="tns:GeneralFormat" />
+    </appinfo>
+  </annotation>
+
+  <include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+
+  <element name="float" type="xsd:float"
+        dfdl:representation="text"
+        dfdl:textNumberRep="standard"
+        dfdl:lengthKind="delimited"
+        dfdl:encoding="UTF-8"
+        dfdl:textNumberPattern="0.000"
+        dfdl:textStandardDecimalSeparator="." />
+
+</schema>

Review Comment:
   Last line doesn't have a newline.



##########
daffodil-schematron/src/test/scala/org/apache/daffodil/validation/schematron/EmbeddedTesting.scala:
##########
@@ -52,8 +54,7 @@ trait EmbeddedTesting {
       val bos = new ByteArrayOutputStream()
       val r1 = dp.parse(
         new InputSourceDataInputStream(new ByteArrayInputStream(bytes)),
-        new XMLTextInfosetOutputter(bos, true))
-
+        new XMLTextInfosetOutputter(bos, true, XMLOutputStyle.Standard))

Review Comment:
   Likewise, why even pass XMLOutputStyle.Standard here when it's the default?  I also like pretty = true better (self-documenting) than true by itself.



##########
daffodil-sapi/src/test/scala/org/apache/daffodil/example/TestScalaAPI.scala:
##########
@@ -889,7 +891,7 @@ class TestScalaAPI {
     val inputSAX = new InputSourceDataInputStream(fisSAX)
     val inputDP = new InputSourceDataInputStream(fisDP)
     val bosDP = new ByteArrayOutputStream()
-    val outputter = new XMLTextInfosetOutputter(bosDP, pretty = true)
+    val outputter = new XMLTextInfosetOutputter(bosDP, true, XMLOutputStyle.Standard)

Review Comment:
   Why even pass XMLOutputStyle.Standard when it's the default?  I also like pretty = true better (self-documenting) than true by itself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org