Author: jonathan
Date: Mon Oct 13 15:47:00 2008
New Revision: 22382
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=22382

Log:
Improved removal of tags after the display limit has been reached.

Modified:
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/QueryParamUtils.java

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/QueryParamUtils.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/QueryParamUtils.java?rev=22382&r1=22381&r2=22382&view=diff
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/QueryParamUtils.java
        (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/QueryParamUtils.java
        Mon Oct 13 15:47:00 2008
@@ -149,24 +149,51 @@
             - entities e.g. < are counted as 4 charactets instead of one.
     */
     public static String trimVisibleChars(String markup, int length) {
-        String trimmedMarkup = "";
-        int charactersDisplayed = 0;
-        boolean parsingTag = false;
-        boolean completingWord = true;
+        String trimmedMarkup = "";          // The result string
+        int charactersDisplayed = 0;        // How many visible characters 
have been displayed so far
+        boolean underDisplayLimit = true;   // State variable - are we under 
the visible display limit or not (length + word completion)
+        boolean parsingTag = false;         // State variable - are we parsing 
inside a tag?
+        boolean closeTag = false;           // State variable - are we parsing 
inside a close tag?
+        int tagDepth = 0;                   // How many ancestors we have so 
far
+        int tagDepthLimit = 0;              // How many ancestors are 
displayable.  Anything deeper than this will be suppressed
+                                            //  when the display limit has 
been reached.
 
         // Walk through the string a character at a time.
         for (int i=0; i<markup.length(); i++) {
             char c = markup.charAt(i);
             if (parsingTag) {
-                // preserve all markup (anything between < and >)
-                trimmedMarkup += c;
+                // preserve markup (anything between < and >) when we're under 
the visible length limit,
+                //  or we're closing off existing markup.  i.e. don't go any 
deeper.
+                if (underDisplayLimit || (!underDisplayLimit && tagDepth <= 
tagDepthLimit)) {
+                    trimmedMarkup += c;
+                }
+                // we're at the end of a tag
                 if (c == '>') {
+                    // if we've just completed a close tag (indicated by 
closeTag), or we're at the end of a minimized tag, comment, or pi
+                    //  indicate that the depth has decreased.  And even as 
you pop out remember never to go any deeper.
+                    if (closeTag || markup.charAt(i - 1) == '/' || 
markup.charAt(i - 1) == '-' || markup.charAt(i - 1) == '?') {
+                        tagDepth--;
+                        if (tagDepth < tagDepthLimit) {
+                            tagDepthLimit--;
+                        }
+                    }
+                    // update state
                     parsingTag = false;
+                    closeTag = false;
                 }
             } else {
                 if (c == '<') {
                     // entering a tag.  Anything from now till > will be 
passed through.
-                    trimmedMarkup += c;
+                    if (markup.charAt(i + 1) == '/') {
+                        closeTag = true;
+                    } else {
+                        tagDepth++;
+                    }
+
+                    if (underDisplayLimit || (!underDisplayLimit && tagDepth 
<= tagDepthLimit)) {
+                        trimmedMarkup += c;
+                    }
+                                    
                     parsingTag = true;
                 } else {
                     if (c == ' ' && i > 0 && markup.charAt(i-1) == ' ') {
@@ -179,7 +206,7 @@
 
                         } else {
                             // now we're past the trimming mark, but we still 
might want to show a few characters.
-                            if (completingWord) {
+                            if (underDisplayLimit) {
                                 // if we're in the middle of a word, keep a 
few more characters.
                                 if (('0' <= c && c <= '9') || ('A' <= c && c 
<= 'Z') || ('a' <= c && c <= 'z')) {
                                     trimmedMarkup += c;
@@ -187,7 +214,8 @@
                                 } else {
                                     // now we just completed the word, no 
other visible characters will be displayed.
                                     // add an ellipsis to indicate that 
trimming occurred.
-                                    completingWord = false;
+                                    underDisplayLimit = false;
+                                    tagDepthLimit = tagDepth;
                                     trimmedMarkup += "&hellip;";  //&hellip;
                                     charactersDisplayed++;
                                 }

_______________________________________________
Mashup-dev mailing list
[email protected]
http://mailman.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to