KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cyberneko > html > HTMLEntities


1 /*
2  * (C) Copyright 2002-2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7
8 package org.cyberneko.html;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Properties JavaDoc;
13                            
14 /**
15  * Pre-defined HTML entities.
16  *
17  * @author Andy Clark
18  *
19  * @version $Id: HTMLEntities.java,v 1.5 2005/02/14 03:56:54 andyc Exp $
20  */

21 public class HTMLEntities {
22
23     //
24
// Constants
25
//
26

27     /** Entities. */
28     protected static final Properties JavaDoc ENTITIES = new Properties JavaDoc();
29
30     /** Reverse mapping from characters to names. */
31     protected static final IntProperties SEITITNE = new IntProperties();
32
33     //
34
// Static initialization
35
//
36

37     static {
38         // load entities
39
load0("res/HTMLlat1.properties");
40         load0("res/HTMLspecial.properties");
41         load0("res/HTMLsymbol.properties");
42         load0("res/XMLbuiltin.properties");
43
44         // store reverse mappings
45
Enumeration JavaDoc keys = ENTITIES.propertyNames();
46         while (keys.hasMoreElements()) {
47             String JavaDoc key = (String JavaDoc)keys.nextElement();
48             String JavaDoc value = ENTITIES.getProperty(key);
49             if (value.length() == 1) {
50                 int ivalue = value.charAt(0);
51                 SEITITNE.put(ivalue, key);
52             }
53         }
54     }
55
56     //
57
// Public static methods
58
//
59

60     /**
61      * Returns the character associated to the given entity name, or
62      * -1 if the name is not known.
63      */

64     public static int get(String JavaDoc name) {
65         String JavaDoc value = (String JavaDoc)ENTITIES.get(name);
66         return value != null ? value.charAt(0) : -1;
67     } // get(String):char
68

69     /**
70      * Returns the name associated to the given character or null if
71      * the character is not known.
72      */

73     public static String JavaDoc get(int c) {
74         return SEITITNE.get(c);
75     } // get(int):String
76

77     //
78
// Private static methods
79
//
80

81     /** Loads the entity values in the specified resource. */
82     private static void load0(String JavaDoc filename) {
83         try {
84             ENTITIES.load(HTMLEntities.class.getResourceAsStream(filename));
85         }
86         catch (IOException JavaDoc e) {
87             System.err.println("error: unable to load resource \""+filename+"\"");
88         }
89     } // load0(String)
90

91     //
92
// Classes
93
//
94

95     static class IntProperties {
96         private int top = 0;
97         private Entry[] entries = new Entry[101];
98         public void put(int key, String JavaDoc value) {
99             int hash = key % entries.length;
100             Entry entry = new Entry(key, value, entries[hash]);
101             entries[hash] = entry;
102         }
103         public String JavaDoc get(int key) {
104             int hash = key % entries.length;
105             Entry entry = entries[hash];
106             while (entry != null) {
107                 if (entry.key == key) {
108                     return entry.value;
109                 }
110                 entry = entry.next;
111             }
112             return null;
113         }
114         static class Entry {
115             public int key;
116             public String JavaDoc value;
117             public Entry next;
118             public Entry(int key, String JavaDoc value, Entry next) {
119                 this.key = key;
120                 this.value = value;
121                 this.next = next;
122             }
123         }
124     }
125
126 } // class HTMLEntities
127
Popular Tags