Java String Strip stripBadHTMLTags(String s)

Here you can find the source of stripBadHTMLTags(String s)

Description

strip Bad HTML Tags

License

Apache License

Declaration

public static String stripBadHTMLTags(String s) 

Method Source Code


//package com.java2s;
/*/*from  w w w. j a  v  a  2s  .  c  om*/
Copyright 2001-2015 Bo Zimmerman
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.util.*;

public class Main {
    public static String stripBadHTMLTags(String s) {
        final StringBuffer buf = new StringBuffer(s);
        final Vector quotes = new Vector();
        int i = -1;
        int start = -1;
        StringBuffer bit = null;
        String lastTag = null;
        while ((++i) < buf.length()) {
            switch (buf.charAt(i)) {
            case '<':
                if (quotes.size() > 0)
                    break;
                bit = new StringBuffer("");
                lastTag = null;
                start = i;
                break;
            case '>':
                if (bit != null)
                    lastTag = bit.toString();
                if ((quotes.size() == 0) && (start >= 0) && (i - start > 0) && (lastTag != null)
                        && (lastTag.trim().equalsIgnoreCase("FONT"))) {
                    final int distance = (i - start) + 1;
                    buf.delete(start, i + 1);
                    i = i - distance;
                }
                bit = null;
                lastTag = null;
                start = -1;
                break;
            case ' ':
                if (bit != null) {
                    lastTag = bit.toString();
                    bit = null;
                }
                break;
            case '"':
            case '\'':
                if (start < 0)
                    break;
                if ((quotes.size() > 0) && (((Character) quotes.lastElement()).charValue() == buf.charAt(i)))
                    quotes.removeElementAt(quotes.size() - 1);
                else
                    quotes.addElement(new Character(buf.charAt(i)));
                break;
            default:
                if (bit != null)
                    bit.append(buf.charAt(i));
                break;
            }
        }
        return buf.toString();
    }
}

Related

  1. collectionToString(Collection collection, boolean stripPackageNames)
  2. strip(final String s)
  3. strip(String string, String token)
  4. stripClassFromClassName(String className)
  5. stripExtension(String filename)
  6. stripLastElement(String str)
  7. stripLeadingSlash(String str)