KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > HtmlCharacterEntityReferencesTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.web.util;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.StreamTokenizer JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import junit.framework.TestCase;
28
29 /**
30  * @author Martin Kersten
31  * @author Juergen Hoeller
32  */

33 public class HtmlCharacterEntityReferencesTests extends TestCase {
34
35     private static final String JavaDoc DTD_FILE = "HtmlCharacterEntityReferences.dtd";
36
37     public void testSupportsAllCharacterEntityReferencesDefinedByHtml() {
38         HtmlCharacterEntityReferences entityReferences = new HtmlCharacterEntityReferences();
39         Map JavaDoc referenceCharactersMap = getReferenceCharacterMap();
40
41         for (int character = 0; character < 64000; character++) {
42             if (referenceCharactersMap.containsKey(new Integer JavaDoc(character))) {
43                 String JavaDoc referenceName = (String JavaDoc) referenceCharactersMap.get(new Integer JavaDoc(character));
44                 String JavaDoc fullReference =
45                         HtmlCharacterEntityReferences.REFERENCE_START +
46                         referenceName +
47                         HtmlCharacterEntityReferences.REFERENCE_END;
48                 assertTrue("The unicode character " + character + " should be mapped to a reference",
49                         entityReferences.isMappedToReference((char) character));
50                 assertEquals("The reference of unicode character " + character + " should be entity " + referenceName,
51                         fullReference, entityReferences.convertToReference((char) character));
52                 assertEquals("The entity reference [" + referenceName + "] should be mapped to unicode character " + character,
53                         (char) character, entityReferences.convertToCharacter(referenceName));
54             }
55             else {
56                 assertFalse("The unicode character " + character + " should not be mapped to a reference",
57                         entityReferences.isMappedToReference((char) character));
58                 assertNull("No entity reference of unicode character " + character + " should exist",
59                         entityReferences.convertToReference((char) character));
60             }
61         }
62
63         assertEquals("The registered entity count of entityReferences should match the number of entity references",
64                 referenceCharactersMap.size(), entityReferences.getSupportedReferenceCount());
65         assertEquals("The HTML 4.0 Standard defines 252 entity references so do entityReferences",
66                 252, entityReferences.getSupportedReferenceCount());
67
68         assertEquals("Invalid entity reference names should not be convertable",
69                 (char) -1, entityReferences.convertToCharacter("invalid"));
70     }
71
72     private Map JavaDoc getReferenceCharacterMap() {
73         CharacterEntityResourceIterator entityIterator = new CharacterEntityResourceIterator();
74         Map JavaDoc referencedCharactersMap = new HashMap JavaDoc();
75         while (entityIterator.hasNext()) {
76             int character = entityIterator.getReferredCharacter();
77             String JavaDoc entityName = entityIterator.nextEntry();
78             referencedCharactersMap.put(new Integer JavaDoc(character), entityName);
79         }
80         return referencedCharactersMap;
81     }
82
83
84     private static class CharacterEntityResourceIterator {
85
86         private final StreamTokenizer JavaDoc tokenizer;
87
88         private String JavaDoc currentEntityName = null;
89
90         private int referredCharacter = -1;
91
92         public CharacterEntityResourceIterator() {
93             try {
94                 InputStream JavaDoc inputStream = getClass().getResourceAsStream(DTD_FILE);
95                 if (inputStream == null) {
96                     throw new IOException JavaDoc("Cannot find definition resource [" + DTD_FILE + "]");
97                 }
98                 tokenizer = new StreamTokenizer JavaDoc(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream, "UTF-8")));
99             }
100             catch (IOException JavaDoc ex) {
101                 throw new IllegalStateException JavaDoc("Failed to open definition resource [" + DTD_FILE + "]");
102             }
103         }
104
105         public boolean hasNext() {
106             return (currentEntityName != null ? true : readNextEntity());
107         }
108
109         public String JavaDoc nextEntry() {
110             if (hasNext()) {
111                 String JavaDoc entityName = currentEntityName;
112                 currentEntityName = null;
113                 return entityName;
114             }
115             return null;
116         }
117
118         public int getReferredCharacter() {
119             return referredCharacter;
120         }
121
122         private boolean readNextEntity() {
123             try {
124                 while (navigateToNextEntity()) {
125                     String JavaDoc entityName = nextWordToken();
126                     if ("CDATA".equals(nextWordToken())) {
127                         int referredCharacter = nextReferredCharacterId();
128                         if (entityName != null && referredCharacter != -1) {
129                             this.currentEntityName = entityName;
130                             this.referredCharacter = referredCharacter;
131                             return true;
132                         }
133                     }
134                 }
135                 return false;
136             }
137             catch (IOException JavaDoc ex) {
138                 throw new IllegalStateException JavaDoc("Could not parse defintion resource: " + ex.getMessage());
139             }
140         }
141
142         private boolean navigateToNextEntity() throws IOException JavaDoc {
143             while (tokenizer.nextToken() != StreamTokenizer.TT_WORD || !"ENTITY".equals(tokenizer.sval)) {
144                 if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
145                     return false;
146                 }
147             }
148             return true;
149         }
150
151         private int nextReferredCharacterId() throws IOException JavaDoc {
152             String JavaDoc reference = nextWordToken();
153             if (reference != null && reference.startsWith("&#") && reference.endsWith(";")) {
154                 return Integer.parseInt(reference.substring(2, reference.length() - 1));
155             }
156             return -1;
157         }
158
159         private String JavaDoc nextWordToken() throws IOException JavaDoc {
160             tokenizer.nextToken();
161             return tokenizer.sval;
162         }
163     }
164
165 }
166
Popular Tags