KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > PDEHTMLHelper


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * PDEHTMLHelper
18  *
19  */

20 public class PDEHTMLHelper {
21
22     public final static HashMap JavaDoc fgEntityLookup = new HashMap JavaDoc(6);
23     static {
24         fgEntityLookup.put("lt", "<"); //$NON-NLS-1$ //$NON-NLS-2$
25
fgEntityLookup.put("gt", ">"); //$NON-NLS-1$ //$NON-NLS-2$
26
fgEntityLookup.put("nbsp", " "); //$NON-NLS-1$ //$NON-NLS-2$
27
fgEntityLookup.put("amp", "&"); //$NON-NLS-1$ //$NON-NLS-2$
28
fgEntityLookup.put("apos", "'"); //$NON-NLS-1$ //$NON-NLS-2$
29
fgEntityLookup.put("quot", "\""); //$NON-NLS-1$ //$NON-NLS-2$
30
}
31     
32     public static String JavaDoc stripTags(String JavaDoc html) {
33         if (html == null) {
34             return null;
35         }
36         int length = html.length();
37         boolean write = true;
38         char oldChar = ' ';
39         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(length);
40
41         boolean processingEntity = false;
42         StringBuffer JavaDoc entityBuffer = null;
43         
44         for (int i = 0; i < length; i++) {
45             char curr = html.charAt(i);
46             
47             // Detect predefined character entities
48
if (curr == '&') {
49                 // Process predefined character entity found
50
processingEntity = true;
51                 entityBuffer = new StringBuffer JavaDoc();
52                 continue;
53             } else if (processingEntity && (curr == ';')) {
54                 // End of predefined character entity found
55
processingEntity = false;
56                 // Resolve the entity
57
String JavaDoc entity = ((String JavaDoc)fgEntityLookup.get(entityBuffer.toString()));
58                 if (entity == null) {
59                     // If the entity is not found or supported, ignore it
60
continue;
61                 }
62                 // Present the resolved character for writing
63
curr = entity.charAt(0);
64             } else if (processingEntity) {
65                 // Collect predefined character entity name character by
66
// character
67
entityBuffer.append(curr);
68                 continue;
69             }
70             
71             if (curr == '<') {
72                 write = false;
73             } else if (curr == '>') {
74                 write = true;
75             } else if (write && curr != '\r' && curr != '\n' && curr != '\t') {
76                 if (!(curr == ' ') || !(oldChar == curr)) { // skip multiple spaces
77
sb.append(curr);
78                     oldChar = curr;
79                 }
80             }
81         }
82         if (isAllWhitespace(sb.toString())) {
83             return null;
84         }
85         return sb.toString();
86     }
87     
88     public static boolean isAllWhitespace(String JavaDoc string) {
89         if (string == null) {
90             return false;
91         }
92         char[] characters = string.toCharArray();
93         for (int i = 0; i < characters.length; i++) {
94             if (!Character.isWhitespace(characters[i])) {
95                 return false;
96             }
97         }
98         return true;
99     }
100     
101 }
102
Popular Tags