001: /*
002: * Copyright 2002-2008 Andy Clark
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.cyberneko.html;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.Properties;
022:
023: /**
024: * Pre-defined HTML entities.
025: *
026: * @author Andy Clark
027: *
028: * @version $Id: HTMLEntities.java,v 1.5 2005/02/14 03:56:54 andyc Exp $
029: */
030: public class HTMLEntities {
031:
032: //
033: // Constants
034: //
035:
036: /** Entities. */
037: protected static final Properties ENTITIES = new Properties();
038:
039: /** Reverse mapping from characters to names. */
040: protected static final IntProperties SEITITNE = new IntProperties();
041:
042: //
043: // Static initialization
044: //
045:
046: static {
047: // load entities
048: load0("res/HTMLlat1.properties");
049: load0("res/HTMLspecial.properties");
050: load0("res/HTMLsymbol.properties");
051: load0("res/XMLbuiltin.properties");
052:
053: // store reverse mappings
054: Enumeration keys = ENTITIES.propertyNames();
055: while (keys.hasMoreElements()) {
056: String key = (String) keys.nextElement();
057: String value = ENTITIES.getProperty(key);
058: if (value.length() == 1) {
059: int ivalue = value.charAt(0);
060: SEITITNE.put(ivalue, key);
061: }
062: }
063: }
064:
065: //
066: // Public static methods
067: //
068:
069: /**
070: * Returns the character associated to the given entity name, or
071: * -1 if the name is not known.
072: */
073: public static int get(String name) {
074: String value = (String) ENTITIES.get(name);
075: return value != null ? value.charAt(0) : -1;
076: } // get(String):char
077:
078: /**
079: * Returns the name associated to the given character or null if
080: * the character is not known.
081: */
082: public static String get(int c) {
083: return SEITITNE.get(c);
084: } // get(int):String
085:
086: //
087: // Private static methods
088: //
089:
090: /** Loads the entity values in the specified resource. */
091: private static void load0(String filename) {
092: try {
093: ENTITIES.load(HTMLEntities.class
094: .getResourceAsStream(filename));
095: } catch (IOException e) {
096: System.err.println("error: unable to load resource \""
097: + filename + "\"");
098: }
099: } // load0(String)
100:
101: //
102: // Classes
103: //
104:
105: static class IntProperties {
106: private Entry[] entries = new Entry[101];
107:
108: public void put(int key, String value) {
109: int hash = key % entries.length;
110: Entry entry = new Entry(key, value, entries[hash]);
111: entries[hash] = entry;
112: }
113:
114: public String get(int key) {
115: int hash = key % entries.length;
116: Entry entry = entries[hash];
117: while (entry != null) {
118: if (entry.key == key) {
119: return entry.value;
120: }
121: entry = entry.next;
122: }
123: return null;
124: }
125:
126: static class Entry {
127: public int key;
128: public String value;
129: public Entry next;
130:
131: public Entry(int key, String value, Entry next) {
132: this .key = key;
133: this .value = value;
134: this .next = next;
135: }
136: }
137: }
138:
139: } // class HTMLEntities
|