You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/08/18 01:31:00 UTC

cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util XmlUtils.java

bayard      2002/08/17 16:31:00

  Modified:    util/src/java/org/apache/commons/util XmlUtils.java
  Log:
  Brought up to date with my latest version of XmlW. Again, it needs some
  sanity checking to see if all the things it does are necessary
  
  Revision  Changes    Path
  1.5       +48 -2     jakarta-commons-sandbox/util/src/java/org/apache/commons/util/XmlUtils.java
  
  Index: XmlUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/XmlUtils.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XmlUtils.java	16 Jul 2002 14:49:09 -0000	1.4
  +++ XmlUtils.java	17 Aug 2002 23:31:00 -0000	1.5
  @@ -68,6 +68,17 @@
           str = StringUtils.replace(str,"&","&");
           str = StringUtils.replace(str,"<","&lt;");
           str = StringUtils.replace(str,">","&gt;");
  +        str = StringUtils.replace(str,"\"","&quot;");
  +        str = StringUtils.replace(str,"'","&apos;");
  +        return str;
  +    }
  +
  +    static public String unescapeXml(String str) {
  +        str = StringUtils.replace(str,"&amp;","&");
  +        str = StringUtils.replace(str,"&lt;","<");
  +        str = StringUtils.replace(str,"&gt;",">");
  +        str = StringUtils.replace(str,"&quot;","\"");
  +        str = StringUtils.replace(str,"&apos;","'");
           return str;
       }
   
  @@ -96,6 +107,37 @@
           return buffer.toString();
       }
   
  +    static public String getContent(String tag, String text) {
  +        int idx = XmlUtils.getIndexOpeningTag(tag, text);
  +        if(idx == -1) {
  +            return "";
  +        }
  +        text = text.substring(idx);
  +        int end = XmlUtils.getIndexClosingTag(tag, text);
  +        idx = text.indexOf('>');
  +        if(idx == -1) {
  +            return "";
  +        }
  +        return text.substring(idx+1, end);
  +    }
  +
  +    static public int getIndexOpeningTag(String tag, String text) {
  +        return getIndexOpeningTag(tag, text, 0);
  +    }
  +    static private int getIndexOpeningTag(String tag, String text, int start) {
  +        // consider whitespace?
  +        int idx = text.indexOf("<"+tag, start);
  +        if(idx == -1) {
  +            return -1;
  +        }
  +        char next = text.charAt(idx+1+tag.length());
  +        if( (next == '>') || Character.isWhitespace(next) ) {
  +            return idx;
  +        } else {
  +            return getIndexOpeningTag(tag, text, idx+1);
  +        }
  +    }
  +
       // Pass in "para" and a string that starts with 
       // <para> and it will return the index of the matching </para>
       // It assumes well-formed xml. Or well enough.
  @@ -105,12 +147,16 @@
       static public int getIndexClosingTag(String tag, String text, int start) {
           String open = "<"+tag;
           String close = "</"+tag+">";
  +//        System.err.println("OPEN: "+open);
  +//        System.err.println("CLOSE: "+close);
           int closeSz = close.length();
           int nextCloseIdx = text.indexOf(close, start);
  +//        System.err.println("first close: "+nextCloseIdx);
           if(nextCloseIdx == -1) {
               return -1;
           }
           int count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open);
  +//        System.err.println("count: "+count);
           if(count == 0) {
               return -1;  // tag is never opened
           }
  @@ -143,7 +189,7 @@
            if(attrCloseIdx > close) {
                return null;
            }
  -         return text.substring(attrStartIdx, attrCloseIdx);
  +         return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
       }
   
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>