FileDocCategorySizeDatePackage
Entities.javaAPI DocExample10983Tue Jun 24 13:23:04 BST 2003org.apache.lucene.demo.html

Entities

public class Entities extends Object

Fields Summary
static final Hashtable
decoder
static final String[]
encoder
Constructors Summary
Methods Summary
static final voidadd(java.lang.String entity, int value)

    decoder.put(entity, (new Character((char)value)).toString());
    if (value < 0x100)
      encoder[value] = entity;
  
static final java.lang.Stringdecode(java.lang.String entity)


       
    if (entity.charAt(entity.length()-1) == ';")  // remove trailing semicolon
      entity = entity.substring(0, entity.length()-1);
    if (entity.charAt(1) == '#") {
      int start = 2;
      int radix = 10;
      if (entity.charAt(2) == 'X" || entity.charAt(2) == 'x") {
	start++;
	radix = 16;
      }
      Character c =
	new Character((char)Integer.parseInt(entity.substring(start), radix));
      return c.toString();
    } else {
      String s = (String)decoder.get(entity);
      if (s != null)
	return s;
      else return "";
    }
  
public static final java.lang.Stringencode(java.lang.String s)

    int length = s.length();
    StringBuffer buffer = new StringBuffer(length * 2);
    for (int i = 0; i < length; i++) {
      char c = s.charAt(i);
      int j = (int)c;
      if (j < 0x100 && encoder[j] != null) {
	buffer.append(encoder[j]);		  // have a named encoding
	buffer.append(';");
      } else if (j < 0x80) {
	buffer.append(c);			  // use ASCII value
      } else {
	buffer.append("&#");			  // use numeric encoding
	buffer.append((int)c);
	buffer.append(';");
      }
    }
    return buffer.toString();