xalan-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mk...@apache.org
Subject cvs commit: xml-xalan/java/src/org/apache/xalan/lib ExsltStrings.java
Date Mon, 16 Sep 2002 15:56:50 GMT
mkwan       2002/09/16 08:56:50

  Modified:    java/src/org/apache/xalan/lib ExsltStrings.java
  Log:
  Make the tokenize function conformed with the EXSLT spec. Reuse
  the Document object to reduce memory usage.
  
  Revision  Changes    Path
  1.2       +31 -13    xml-xalan/java/src/org/apache/xalan/lib/ExsltStrings.java
  
  Index: ExsltStrings.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/lib/ExsltStrings.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExsltStrings.java	6 Sep 2002 16:54:20 -0000	1.1
  +++ ExsltStrings.java	16 Sep 2002 15:56:50 -0000	1.2
  @@ -83,6 +83,8 @@
    */
   public class ExsltStrings
   {
  +  // Reuse the Document object to reduce memory usage.
  +  private static Document lDoc = null;
   
     /**
      * The str:align function aligns a string within another string. 
  @@ -222,12 +224,10 @@
      */
     public static NodeList split(String str, String pattern)
     {
  -    Document lDoc = null;
       try
       {
  -      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  -      DocumentBuilder db = dbf.newDocumentBuilder();
  -      lDoc = db.newDocument();
  +      if (lDoc == null)
  +        lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
       }
       catch(ParserConfigurationException pce)
       {
  @@ -295,29 +295,47 @@
      * (i.e. whitespace characters). 
      *
      * If the second argument is an empty string, the function returns a set of token 
  -   * elements, each of which holds a single character. 
  +   * elements, each of which holds a single character.
  +   *
  +   * Note: This one is different from the tokenize extension function in the Xalan
  +   * namespace. The one in Xalan returns a set of Text nodes, while this one wraps
  +   * the Text nodes inside the token Element nodes.
      */
     public static NodeList tokenize(String toTokenize, String delims)
     {
  -    Document lDoc;
  -
       try
       {
  -      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  -      DocumentBuilder db = dbf.newDocumentBuilder();
  -      lDoc = db.newDocument();
  +      if (lDoc == null)
  +        lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
       }
       catch(ParserConfigurationException pce)
       {
         throw new org.apache.xml.utils.WrappedRuntimeException(pce);
       }
   
  -    StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
       NodeSet resultSet = new NodeSet();
  +    
  +    if (delims != null && delims.length() > 0)
  +    {
  +      StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
   
  -    while (lTokenizer.hasMoreTokens())
  +      while (lTokenizer.hasMoreTokens())
  +      {
  +        Element element = lDoc.createElement("token");
  +        element.appendChild(lDoc.createTextNode(lTokenizer.nextToken()));
  +        resultSet.addNode(element);      
  +      }
  +    }
  +    // If the delimiter is an empty string, create one token Element for 
  +    // every single character.
  +    else
       {
  -      resultSet.addNode(lDoc.createTextNode(lTokenizer.nextToken()));
  +      for (int i = 0; i < toTokenize.length(); i++)
  +      {
  +        Element element = lDoc.createElement("token");
  +        element.appendChild(lDoc.createTextNode(toTokenize.substring(i, i+1)));
  +        resultSet.addNode(element);              
  +      }
       }
   
       return resultSet;
  
  
  

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


Mime
View raw message