KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > htmlcleaner > GeckoCorruptTagCleaner


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.htmlcleaner;
17
18 class GeckoCorruptTagCleaner {
19     /**
20      * Removes invalid tags produced by gecko editor.
21      *
22      * <p>The Gecko Midas editor (or Mozilla Composer for that matter) sometimes
23      * leaves things like '&lt;&gt;' or '&lt; /&gt;' (for those reading the
24      * source: it leaves the unescaped tags), thus tags without names. This
25      * is easily reproduceable by hitting enter twice followed by backspace twice
26      * when in the middle of a paragraph or header.
27      */

28     public static String JavaDoc clean(String JavaDoc input) {
29         char[] inputChars = input.toCharArray();
30         StringBuffer JavaDoc result = new StringBuffer JavaDoc(input.length());
31
32         int i = 0;
33         while(i < inputChars.length) {
34             char c = inputChars[i];
35             if (c == '<' && i + 3 < inputChars.length) {
36                 if (inputChars[i + 1] == '>') {
37                     i = i + 2;
38                     continue;
39                 } else if (c == '<' && inputChars[i + 1] == ' ' && inputChars[i + 2] == '/' && inputChars[i + 3] == '>') {
40                     i = i + 4;
41                     continue;
42                 } else if (c == '<' && inputChars[i + 1] == '<') {
43                     i = i + 1;
44                     continue;
45                 }
46             }
47             result.append(c);
48             i = i + 1;
49         }
50
51         return result.toString();
52     }
53 }
54
Popular Tags