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 2023/05/19 15:46:19 UTC

[xalan-java] branch xalan-j_xslt3.0 updated: implementing xsl:analyze-string 'regex' attribute as an avt, as defined by xslt 3.0 spec. also doing, few other minor refactoring within xalanj codebase on this dev branch.

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 a42ddb58 implementing xsl:analyze-string 'regex' attribute as an avt, as defined by xslt 3.0 spec. also doing, few other minor refactoring within xalanj codebase on this dev branch.
a42ddb58 is described below

commit a42ddb58fda514d02d10f09a3a74cb2ddd384b9a
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Fri May 19 21:16:02 2023 +0530

    implementing xsl:analyze-string 'regex' attribute as an avt, as defined by xslt 3.0 spec. also doing, few other minor refactoring within xalanj codebase on this dev branch.
---
 src/org/apache/xalan/processor/XSLTSchema.java     | 11 ++--
 .../apache/xalan/templates/ElemAnalyzeString.java  | 59 +++++++++++++---------
 src/org/apache/xalan/templates/FuncRegexGroup.java |  4 +-
 src/org/apache/xpath/functions/FuncMatches.java    |  4 +-
 src/org/apache/xpath/functions/FuncReplace.java    |  4 +-
 ...ionSupport.java => RegexEvaluationSupport.java} |  2 +-
 6 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/src/org/apache/xalan/processor/XSLTSchema.java b/src/org/apache/xalan/processor/XSLTSchema.java
index 3faa0c65..b53b9adc 100644
--- a/src/org/apache/xalan/processor/XSLTSchema.java
+++ b/src/org/apache/xalan/processor/XSLTSchema.java
@@ -203,10 +203,9 @@ public class XSLTSchema extends XSLTElementDef
                                             XSLTAttributeDef.T_EXPR, true, false, XSLTAttributeDef.ERROR);
     
     // Required.                                       
-    // xsl:analyze-string                             
-    XSLTAttributeDef regexAttrRequired = new XSLTAttributeDef(null,
-                                              "regex",
-                                              XSLTAttributeDef.T_STRING, true, false, XSLTAttributeDef.ERROR);
+    // xsl:analyze-string
+    XSLTAttributeDef regexAVTRequired = new XSLTAttributeDef(null, "regex",
+                                              XSLTAttributeDef.T_AVT, true, true, XSLTAttributeDef.ERROR);
     
     // Optional.                                       
     // xsl:analyze-string                              
@@ -498,8 +497,8 @@ public class XSLTSchema extends XSLTElementDef
     XSLTElementDef xslAnalyzeString = new XSLTElementDef(this,
                                                 Constants.S_XSLNAMESPACEURL, "analyze-string",
                                                 null /*alias */, templateElements,
-                                                new XSLTAttributeDef[]{ selectAttrRequired, 
-                                                                        regexAttrRequired, flagsAttrOpt, spaceAttr }, 
+                                                new XSLTAttributeDef[]{ regexAVTRequired, selectAttrRequired, 
+                                                                        flagsAttrOpt, spaceAttr }, 
                                                 new ProcessorTemplateElem(),
                                                 ElemAnalyzeString.class /* class object */, true, false, true, 20, true);
     
diff --git a/src/org/apache/xalan/templates/ElemAnalyzeString.java b/src/org/apache/xalan/templates/ElemAnalyzeString.java
index 29039493..1d6d8bfa 100644
--- a/src/org/apache/xalan/templates/ElemAnalyzeString.java
+++ b/src/org/apache/xalan/templates/ElemAnalyzeString.java
@@ -30,7 +30,7 @@ import org.apache.xpath.Expression;
 import org.apache.xpath.ExpressionOwner;
 import org.apache.xpath.XPath;
 import org.apache.xpath.XPathContext;
-import org.apache.xpath.functions.RegExFunctionSupport;
+import org.apache.xpath.functions.RegexEvaluationSupport;
 import org.apache.xpath.objects.XObject;
 import org.apache.xpath.regex.Matcher;
 
@@ -64,8 +64,8 @@ import org.apache.xpath.regex.Matcher;
  * This XSLT instruction is intended to process, all the 'non overlapping' substrings 
  * of the input string that match the provided regular expression.
  * 
- * The XSLT function fn:regex-group may be used optionally along with XSLT instruction 
- * xsl:analyze-string.    // revisit
+ * The XSLT function fn:regex-group may be used optionally, along with XSLT instruction 
+ * xsl:analyze-string.
  * 
  * With xsl:analyze-string element, we're presently not implementing xsl:fallback 
  * elements. xsl:fallback elements don't seem to have much useful use cases within 
@@ -87,14 +87,14 @@ public class ElemAnalyzeString extends ElemTemplateElement implements Expression
   protected Expression m_selectExpression = null;
   
   /**
-   * The "regex" expression.
+   * The "regex" avt expression.
   */
-  protected String m_regex = null;
+  protected AVT m_regex = null;
   
   /**
-   * The regex "flags" expression.
+   * The regex "flags" string value.
   */
-  protected String m_flags = null;
+  protected String m_regex_flags = null;
 
   /**
    * Set the "select" attribute.
@@ -117,37 +117,37 @@ public class ElemAnalyzeString extends ElemTemplateElement implements Expression
   /**
    * Set the "regex" attribute.
    *
-   * @param regex The string value for the "regex" attribute.
+   * @param regex The avt value for the "regex" attribute.
    */
-  public void setRegex(String regex) {
+  public void setRegex(AVT regex) {
       m_regex = regex;   
   }
 
   /**
    * Get the "regex" attribute.
    *
-   * @return The string value for the "regex" attribute.
+   * @return The avt value for the "regex" attribute.
    */
-  public String getRegex() {
+  public AVT getRegex() {
       return m_regex;
   }
   
   /**
-   * Set the "flags" attribute.
+   * Set the regex "flags" attribute.
    *
    * @param regex The string value for the "flags" attribute.
    */
   public void setFlags(String flags) {
-      m_flags = flags;   
+      m_regex_flags = flags;   
   }
 
   /**
-   * Get the "flags" attribute.
+   * Get the regex "flags" attribute.
    *
    * @return The string value for the "flags" attribute.
    */
   public String getFlags() {
-      return m_flags;
+      return m_regex_flags;
   }
 
   /**
@@ -233,17 +233,20 @@ public class ElemAnalyzeString extends ElemTemplateElement implements Expression
        
        String strToBeAnalyzed = xpathEvalResult.str();
        
-       long currentTimeMills = System.currentTimeMillis();
-       String tempReplStr = (Long.valueOf(currentTimeMills)).toString();
+       if (m_regex == null) {
+           throw new javax.xml.transform.TransformerException("XTSE0010 : xsl:analyze-string element must "
+                                                                                 + "have an 'regex' attribute.", xctxt.getSAXLocator());   
+       }
        
-       if (m_flags != null && !RegExFunctionSupport.isFlagStrValid(m_flags)) {
-           throw new javax.xml.transform.TransformerException("XTDE1145 : Invalid regular expression flags are present, with "
-                                                                                     + "xsl:analyze-string element.", xctxt.getSAXLocator());    
+       if (m_regex_flags != null && !RegexEvaluationSupport.isFlagStrValid(m_regex_flags)) {
+           throw new javax.xml.transform.TransformerException("XTDE1145 : Invalid regular expression flag(s) are present, with "
+                   + "xsl:analyze-string element.", xctxt.getSAXLocator());    
        }
+
+       String effectiveRegexStrValue = m_regex.evaluate(xctxt, xctxt.getContextNode(), this);
        
-       Matcher regexMatcher = RegExFunctionSupport.compileAndExecute(RegExFunctionSupport.trfPatternStrForSubtraction(
-                                                                                             m_regex), m_flags, strToBeAnalyzed);       
-       String tempReplacedStr = regexMatcher.replaceAll(tempReplStr);
+       Matcher regexMatcher = RegexEvaluationSupport.compileAndExecute(RegexEvaluationSupport.trfPatternStrForSubtraction(
+                                                                                    effectiveRegexStrValue), m_regex_flags, strToBeAnalyzed);
        
        ElemTemplateElement templateElem1 = this.m_firstChild;
        ElemTemplateElement templateElem2 = null;
@@ -281,6 +284,12 @@ public class ElemAnalyzeString extends ElemTemplateElement implements Expression
            strToBeAnalyzedMatchingSubsequences.add(str);
        }
        
+       // we use a fairly random string like the string value of 'System.currentTimeMillis()',
+       // to help us pre-process the input string to be analyzed. 
+       long currentTimeMills = System.currentTimeMillis();
+       String tempReplStr = (Long.valueOf(currentTimeMills)).toString();
+       String tempReplacedStr = regexMatcher.replaceAll(tempReplStr);
+       
        String[] strParts = tempReplacedStr.split(tempReplStr);
        int i2 = 0;
        for (int idx = 0; idx < strParts.length; idx++) {           
@@ -303,8 +312,8 @@ public class ElemAnalyzeString extends ElemTemplateElement implements Expression
                                                                  tempReplacedStr.endsWith(tempReplStr))) {
                       ((ElemMatchingSubstring)templateElem1).setStrValue(
                                                                  strToBeAnalyzedMatchingSubsequences.get(i2));
-                      ((ElemMatchingSubstring)templateElem1).setRegex(m_regex);
-                      ((ElemMatchingSubstring)templateElem1).setFlags(m_flags);
+                      ((ElemMatchingSubstring)templateElem1).setRegex(effectiveRegexStrValue);
+                      ((ElemMatchingSubstring)templateElem1).setFlags(m_regex_flags);
                       xctxt.setSAXLocator(templateElem1);
                       transformer.setCurrentElement(templateElem1);                   
                       templateElem1.execute(transformer);
diff --git a/src/org/apache/xalan/templates/FuncRegexGroup.java b/src/org/apache/xalan/templates/FuncRegexGroup.java
index 1abe30e0..5faddcca 100644
--- a/src/org/apache/xalan/templates/FuncRegexGroup.java
+++ b/src/org/apache/xalan/templates/FuncRegexGroup.java
@@ -25,7 +25,7 @@ import java.util.Map;
 import org.apache.xpath.Expression;
 import org.apache.xpath.XPathContext;
 import org.apache.xpath.functions.FunctionOneArg;
-import org.apache.xpath.functions.RegExFunctionSupport;
+import org.apache.xpath.functions.RegexEvaluationSupport;
 import org.apache.xpath.objects.XNumber;
 import org.apache.xpath.objects.XObject;
 import org.apache.xpath.objects.XString;
@@ -90,7 +90,7 @@ public class FuncRegexGroup extends FunctionOneArg
         String regex = customDataMap.get(ElemMatchingSubstring.REGEX);
         String regexFlags = customDataMap.get(ElemMatchingSubstring.REGEX_FLAGS);
         
-        Matcher regexMatcher = RegExFunctionSupport.compileAndExecute(RegExFunctionSupport.trfPatternStrForSubtraction(
+        Matcher regexMatcher = RegexEvaluationSupport.compileAndExecute(RegexEvaluationSupport.trfPatternStrForSubtraction(
                                                                                    regex), regexFlags, strValue);
         String regexGrpStr = null;
         if (regexMatcher.matches()) {
diff --git a/src/org/apache/xpath/functions/FuncMatches.java b/src/org/apache/xpath/functions/FuncMatches.java
index 1f252490..c20e24c4 100644
--- a/src/org/apache/xpath/functions/FuncMatches.java
+++ b/src/org/apache/xpath/functions/FuncMatches.java
@@ -63,7 +63,7 @@ public class FuncMatches extends Function3Args {
         
         if (m_arg2 != null) {
            flags = m_arg2.execute(xctxt).xstr();
-           if (!RegExFunctionSupport.isFlagStrValid(flags.toString())) {               
+           if (!RegexEvaluationSupport.isFlagStrValid(flags.toString())) {               
               throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.
                                                           ER_INVALID_REGEX_FLAGS, new Object[]{ FUNCTION_NAME }),
                                                                      srcLocator); 
@@ -73,7 +73,7 @@ public class FuncMatches extends Function3Args {
         boolean result = false;
         
         try {                
-            Matcher matcher = RegExFunctionSupport.regex(RegExFunctionSupport.trfPatternStrForSubtraction(pattern.toString()), 
+            Matcher matcher = RegexEvaluationSupport.regex(RegexEvaluationSupport.trfPatternStrForSubtraction(pattern.toString()), 
                                                             flags != null ? flags.toString() : null, inputStr.toString());
             while (matcher.find()) {
                result = true;
diff --git a/src/org/apache/xpath/functions/FuncReplace.java b/src/org/apache/xpath/functions/FuncReplace.java
index 71eabdfc..3173880c 100644
--- a/src/org/apache/xpath/functions/FuncReplace.java
+++ b/src/org/apache/xpath/functions/FuncReplace.java
@@ -64,7 +64,7 @@ public class FuncReplace extends Function4Args {
         
         if (m_arg3 != null) {
            flags = m_arg3.execute(xctxt).xstr();
-           if (!RegExFunctionSupport.isFlagStrValid(flags.toString())) {
+           if (!RegexEvaluationSupport.isFlagStrValid(flags.toString())) {
                throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.
                                                                         ER_INVALID_REGEX_FLAGS, new Object[]{ FUNCTION_NAME }),
                                                                                srcLocator);     
@@ -74,7 +74,7 @@ public class FuncReplace extends Function4Args {
         String resultStr = null;
         
         try {
-            Matcher matcher = RegExFunctionSupport.regex(RegExFunctionSupport.trfPatternStrForSubtraction(pattern.toString()), 
+            Matcher matcher = RegexEvaluationSupport.regex(RegexEvaluationSupport.trfPatternStrForSubtraction(pattern.toString()), 
                                                            flags != null ? flags.toString() : null, inputStr.toString());
             resultStr = matcher.replaceAll(replacement.toString());
         }
diff --git a/src/org/apache/xpath/functions/RegExFunctionSupport.java b/src/org/apache/xpath/functions/RegexEvaluationSupport.java
similarity index 98%
rename from src/org/apache/xpath/functions/RegExFunctionSupport.java
rename to src/org/apache/xpath/functions/RegexEvaluationSupport.java
index 7d57bc82..1d72638a 100644
--- a/src/org/apache/xpath/functions/RegExFunctionSupport.java
+++ b/src/org/apache/xpath/functions/RegexEvaluationSupport.java
@@ -32,7 +32,7 @@ import org.apache.xpath.regex.Pattern;
  * 
  * @xsl.usage advanced
  */
-public class RegExFunctionSupport {
+public class RegexEvaluationSupport {
 	
     private static final String validRegexflags = "smixq";
 	


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