You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mu...@apache.org on 2024/06/27 06:33:06 UTC

(xalan-java) branch xalan-j_xslt3.0 updated: making few changes to implementation for xpath3.1 functions fn:json-to-xml, fn:xml-to-json, along with minor changes to comments within code

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

mukulg pushed a commit to branch xalan-j_xslt3.0
in repository https://gitbox.apache.org/repos/asf/xalan-java.git


The following commit(s) were added to refs/heads/xalan-j_xslt3.0 by this push:
     new e55ef6e61 making few changes to implementation for xpath3.1 functions fn:json-to-xml, fn:xml-to-json, along with minor changes to comments within code
e55ef6e61 is described below

commit e55ef6e61e6638f22e18bcf207f19144638fd623
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Thu Jun 27 12:02:51 2024 +0530

    making few changes to implementation for xpath3.1 functions fn:json-to-xml, fn:xml-to-json, along with minor changes to comments within code
---
 src/org/apache/xalan/templates/ElemFunction.java   | 10 ++++--
 src/org/apache/xalan/templates/ElemSequence.java   | 22 ++++++++++---
 .../apache/xpath/functions/json/FuncJsonToXml.java | 38 ++++++++++++----------
 .../apache/xpath/functions/json/FuncXmlToJson.java |  5 +++
 4 files changed, 49 insertions(+), 26 deletions(-)

diff --git a/src/org/apache/xalan/templates/ElemFunction.java b/src/org/apache/xalan/templates/ElemFunction.java
index 851844d5f..4d5e639c0 100644
--- a/src/org/apache/xalan/templates/ElemFunction.java
+++ b/src/org/apache/xalan/templates/ElemFunction.java
@@ -56,6 +56,7 @@ import xml.xpath31.processor.types.XSInt;
 import xml.xpath31.processor.types.XSInteger;
 import xml.xpath31.processor.types.XSLong;
 import xml.xpath31.processor.types.XSString;
+import xml.xpath31.processor.types.XSTime;
 import xml.xpath31.processor.types.XSYearMonthDuration;
 
 /**
@@ -417,8 +418,8 @@ public class ElemFunction extends ElemTemplate
   }
   
   /**
-   * Given XalanJ's integer code value of, an XML Schema built-in data type and a 
-   * string representation of a data value, construct XalanJ's typed data object 
+   * Given Xalan-J's integer code value of, an XML Schema built-in data type and a 
+   * string representation of a data value, construct Xalan-J's typed data object 
    * corresponding to the data type's integer code value. 
    */
   private XObject getXSTypedAtomicValue(String strVal, int sequenceType) throws TransformerException {
@@ -439,7 +440,7 @@ public class ElemFunction extends ElemTemplate
          result = XSDateTime.parseDateTime(strVal);
       }
       else if (sequenceType == SequenceTypeSupport.XS_TIME) {
-         // TO DO
+    	 result = XSTime.parseTime(strVal);
       }
       else if (sequenceType == SequenceTypeSupport.XS_DURATION) {
          result = XSDuration.parseDuration(strVal);
@@ -468,6 +469,9 @@ public class ElemFunction extends ElemTemplate
       else if (sequenceType == SequenceTypeSupport.XS_FLOAT) {
          result = new XSFloat(strVal); 
       }
+      else {
+    	 result = new XSString(strVal); 
+      }
      
       return result;
   }
diff --git a/src/org/apache/xalan/templates/ElemSequence.java b/src/org/apache/xalan/templates/ElemSequence.java
index 426f8c3ef..9a1c05ca9 100644
--- a/src/org/apache/xalan/templates/ElemSequence.java
+++ b/src/org/apache/xalan/templates/ElemSequence.java
@@ -89,13 +89,13 @@ public class ElemSequence extends ElemTemplateElement
   
   /**
    * We use this string value constant to add as a suffix to, a string value that
-   * is getting serialized by XalanJ serializer, when XSL serialization is
+   * is getting serialized by Xalan-J serializer, when XSL serialization is
    * getting done during evaluation of xsl:sequence instruction. This helps us
    * construct later during XSL transformation process, a sequence of XDM atomic
    * values.
    * 
    * We're assuming that, this string value is significantly random, and is unlikely
-   * to exist within an input document that is getting transformed by XalanJ.
+   * to exist within an input document that is getting transformed by Xalan-J.
    */
   public static String STRING_VAL_SERIALIZATION_SUFFIX = "XSL_SEQ" + ElemCopyOf.SPACE_CHAR + "XSL_SEQ";
 
@@ -171,7 +171,19 @@ public class ElemSequence extends ElemTemplateElement
     Expression selectExpression = null;
  
     try {        
-      if (m_selectPattern != null) {          
+      if (m_selectPattern != null) {
+    	 ElemTemplateElement xslSeqChildElem = getFirstChildElem();
+    	 while (xslSeqChildElem != null) {
+    	   if (!(xslSeqChildElem instanceof ElemFallback)) {
+    		  throw new javax.xml.transform.TransformerException("XTSE3185 : An xsl:sequence element cannot "
+    		  		                                                   + "have both \"select\" attribute, and child content "
+    		  		                                                   + "other than xsl:fallback element(s).", srcLocator); 
+    	   }
+    	   else {
+    		  xslSeqChildElem = xslSeqChildElem.getNextSiblingElem(); 
+    	   }
+    	}
+    	  
         selectExpression = m_selectPattern.getExpression();
         
         if (selectExpression instanceof FuncExtFunction) {
@@ -331,7 +343,7 @@ public class ElemSequence extends ElemTemplateElement
   
           switch (xObjectType) {           
               case XObject.CLASS_NODESET :          
-                  ElemCopyOf.copyOfActionOnNodeSet((XNodeSet)xslSequenceVal, transformer, handler, xctxt);          
+                ElemCopyOf.copyOfActionOnNodeSet((XNodeSet)xslSequenceVal, transformer, handler, xctxt);          
                 break;
               case XObject.CLASS_RTREEFRAG :
                 SerializerUtils.outputResultTreeFragment(handler, xslSequenceVal, xctxt);
@@ -436,7 +448,7 @@ public class ElemSequence extends ElemTemplateElement
   }
   
   /**
-   * This method determines that, should a XalanJ serialization suffix be added, when
+   * This method determines that, should a Xalan-J serialization suffix be added, when
    * emitting xdm atomic values to an XSLT result sequence.
    */
   private boolean isToAddStrValSerializationSuffix(XPathContext xctxt) throws TransformerException {
diff --git a/src/org/apache/xpath/functions/json/FuncJsonToXml.java b/src/org/apache/xpath/functions/json/FuncJsonToXml.java
index 553f67482..6ce26aa03 100644
--- a/src/org/apache/xpath/functions/json/FuncJsonToXml.java
+++ b/src/org/apache/xpath/functions/json/FuncJsonToXml.java
@@ -227,9 +227,6 @@ public class FuncJsonToXml extends FunctionMultiArgs
         Document document = dBuilder.newDocument();
         
         constructXmlDom(jsonObj, document, document, null);
-        
-        Element docElem = document.getDocumentElement();
-        docElem.setAttribute("xmlns", FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI);
          
         DTMManager dtmMgr = xctxt.getDTMManager();             
         DTM dtm = dtmMgr.getDTM(new DOMSource(document), true, null, false, false);            
@@ -256,9 +253,8 @@ public class FuncJsonToXml extends FunctionMultiArgs
     private void constructXmlDom(Object jsonObj, Document document, Node parentNode, String keyVal) {
     	
     	if (jsonObj instanceof JSONObject) {
-    		Element mapElem = document.createElement(XSLJsonConstants.MAP);
-    		parentNode.appendChild(mapElem);
-    		
+    		Element mapElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.MAP);
+    		parentNode.appendChild(mapElem);    		
     		if (keyVal != null) {
     		   mapElem.setAttribute(XSLJsonConstants.KEY, keyVal);	
     		}
@@ -268,28 +264,28 @@ public class FuncJsonToXml extends FunctionMultiArgs
 	      	   String key = jsonKeys.next();
 	      	   Object value = ((JSONObject)jsonObj).get(key);        	  
 	      	   if (value instanceof String) {
-	      		 Element strElem = document.createElement(XSLJsonConstants.STRING);
+	      		 Element strElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.STRING);
 	      		 strElem.setAttribute(XSLJsonConstants.KEY, key);
 	      		 Text text = document.createTextNode((String)value);
 	      		 strElem.appendChild(text);
 	      		 mapElem.appendChild(strElem);
 	      	   }
 	      	   else if (value instanceof Number) {
-	      		 Element numberElem = document.createElement(XSLJsonConstants.NUMBER);
+	      		 Element numberElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.NUMBER);
 	      		 numberElem.setAttribute(XSLJsonConstants.KEY, key);
 	      		 Text text = document.createTextNode(value.toString());
 	      		 numberElem.appendChild(text);
 	      		 mapElem.appendChild(numberElem);
 	      	   }
 	      	   else if (value instanceof Boolean) {
-	      		 Element boolElem = document.createElement(XSLJsonConstants.BOOLEAN);
+	      		 Element boolElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.BOOLEAN);
 	      		 boolElem.setAttribute(XSLJsonConstants.KEY, key);
 	      		 Text text = document.createTextNode(value.toString());
 	      		 boolElem.appendChild(text);
 	      		 mapElem.appendChild(boolElem);
 	      	   }	      	   
 	      	   else if (value instanceof JSONArray) {
-	      		  Element arrayElem = document.createElement(XSLJsonConstants.ARRAY);
+	      		  Element arrayElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.ARRAY);
 	      		  arrayElem.setAttribute(XSLJsonConstants.KEY, key);
 	      		  mapElem.appendChild(arrayElem);
 	      		  JSONArray jsonArr = (JSONArray)value;
@@ -297,19 +293,19 @@ public class FuncJsonToXml extends FunctionMultiArgs
 	    		  for (int idx = 0; idx < arrLen; idx++) {
 	    			  Object arrItem = jsonArr.get(idx);
 	    			  if (arrItem instanceof String) {
-	    				  Element strElem = document.createElement(XSLJsonConstants.STRING);
+	    				  Element strElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.STRING);
 	    				  Text text = document.createTextNode((String)arrItem);
 	    				  strElem.appendChild(text);
 	    				  arrayElem.appendChild(strElem);	 
 	    			  }
 	    			  else if (arrItem instanceof Number) {
-	    				  Element numberElem = document.createElement(XSLJsonConstants.NUMBER);
+	    				  Element numberElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.NUMBER);
 	    				  Text text = document.createTextNode(arrItem.toString());
 	    				  numberElem.appendChild(text);
 	    				  arrayElem.appendChild(numberElem); 
 	    			  }
 	    			  else if (arrItem instanceof Boolean) {
-	    				  Element boolElem = document.createElement(XSLJsonConstants.BOOLEAN);
+	    				  Element boolElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.BOOLEAN);
 	    				  Text text = document.createTextNode(arrItem.toString());
 	    				  boolElem.appendChild(text);
 	    				  arrayElem.appendChild(boolElem);  
@@ -329,14 +325,20 @@ public class FuncJsonToXml extends FunctionMultiArgs
 	      		  constructXmlDom(value, document, mapElem, key); 
 	      	   }
 	      	   else if (JSONObject.NULL.equals(value)) {
-	      		  Element nullElem = document.createElement("null");
+	      		  Element nullElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, "null");
 	      		  nullElem.setAttribute(XSLJsonConstants.KEY, key);
 	      		  mapElem.appendChild(nullElem);
 	      	   }
 	        }	    		    	
     	}
     	else if (jsonObj instanceof JSONArray) {
-    		Element arrayElem = document.createElement(XSLJsonConstants.ARRAY);
+    		Element arrayElem = null;
+    		if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
+    		   arrayElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.ARRAY);
+     		}
+     		else {
+     		   arrayElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.ARRAY);
+     		}
      		parentNode.appendChild(arrayElem);
      		
      		if (keyVal != null) {
@@ -348,19 +350,19 @@ public class FuncJsonToXml extends FunctionMultiArgs
     		for (int idx = 0; idx < arrLen; idx++) {
     		   Object arrItem = jsonArr.get(idx);
     		   if (arrItem instanceof String) {
-    			  Element strElem = document.createElement(XSLJsonConstants.STRING);
+    			  Element strElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.STRING);
   	      		  Text text = document.createTextNode((String)arrItem);
   	      		  strElem.appendChild(text);
   	      		  arrayElem.appendChild(strElem);	 
     		   }
     		   else if (arrItem instanceof Number) {
-    			  Element numberElem = document.createElement(XSLJsonConstants.NUMBER);
+    			  Element numberElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.NUMBER);
    	      		  Text text = document.createTextNode(arrItem.toString());
    	      		  numberElem.appendChild(text);
    	      		  arrayElem.appendChild(numberElem); 
     		   }
     		   else if (arrItem instanceof Boolean) {
-    			  Element boolElem = document.createElement(XSLJsonConstants.BOOLEAN);
+    			  Element boolElem = document.createElementNS(FunctionTable.XPATH_BUILT_IN_FUNCS_NS_URI, XSLJsonConstants.BOOLEAN);
     	          Text text = document.createTextNode(arrItem.toString());
     	          boolElem.appendChild(text);
     	          arrayElem.appendChild(boolElem);  
diff --git a/src/org/apache/xpath/functions/json/FuncXmlToJson.java b/src/org/apache/xpath/functions/json/FuncXmlToJson.java
index 60f74dd25..d1843c8ec 100644
--- a/src/org/apache/xpath/functions/json/FuncXmlToJson.java
+++ b/src/org/apache/xpath/functions/json/FuncXmlToJson.java
@@ -162,7 +162,12 @@ public class FuncXmlToJson extends FunctionMultiArgs
         DTMIterator dtmIter = arg0NodeSet.iterRaw();
    	    int nodeHandle = dtmIter.nextNode();
    	    DTM dtm = xctxt.getDTM(nodeHandle);
+   	    
    	    Node node = dtm.getNode(nodeHandle);
+   	    if (node.getNodeType() == Node.DOCUMENT_NODE) {
+   	       node = node.getFirstChild();
+   	    }
+   	    
    	    String xmlDocStr = null;
    	    
    	    try {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xalan.apache.org
For additional commands, e-mail: commits-help@xalan.apache.org