1. /*
  2. * @(#)Entity.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text.html.parser;
  8. import java.util.Hashtable;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.Reader;
  13. import java.io.CharArrayReader;
  14. import java.net.URL;
  15. /**
  16. * An entity is described in a DTD using the ENTITY construct.
  17. * It defines the type and value of the the entity.
  18. *
  19. * @see DTD
  20. * @version 1.9, 01/23/03
  21. * @author Arthur van Hoff
  22. */
  23. public final
  24. class Entity implements DTDConstants {
  25. public String name;
  26. public int type;
  27. public char data[];
  28. /**
  29. * Creates an entity.
  30. * @param name the name of the entity
  31. * @param type the type of the entity
  32. * @param data the char array of data
  33. */
  34. public Entity(String name, int type, char data[]) {
  35. this.name = name;
  36. this.type = type;
  37. this.data = data;
  38. }
  39. /**
  40. * Gets the name of the entity.
  41. * @return the name of the entity, as a <code>String</code>
  42. */
  43. public String getName() {
  44. return name;
  45. }
  46. /**
  47. * Gets the type of the entity.
  48. * @return the type of the entity
  49. */
  50. public int getType() {
  51. return type & 0xFFFF;
  52. }
  53. /**
  54. * Returns <code>true</code> if it is a parameter entity.
  55. * @return <code>true</code> if it is a parameter entity
  56. */
  57. public boolean isParameter() {
  58. return (type & PARAMETER) != 0;
  59. }
  60. /**
  61. * Returns <code>true</code> if it is a general entity.
  62. * @return <code>true</code> if it is a general entity
  63. */
  64. public boolean isGeneral() {
  65. return (type & GENERAL) != 0;
  66. }
  67. /**
  68. * Returns the <code>data</code>.
  69. * @return the <code>data</code>
  70. */
  71. public char getData()[] {
  72. return data;
  73. }
  74. /**
  75. * Returns the data as a <code>String</code>.
  76. * @return the data as a <code>String</code>
  77. */
  78. public String getString() {
  79. return new String(data, 0, data.length);
  80. }
  81. static Hashtable entityTypes = new Hashtable();
  82. static {
  83. entityTypes.put("PUBLIC", new Integer(PUBLIC));
  84. entityTypes.put("CDATA", new Integer(CDATA));
  85. entityTypes.put("SDATA", new Integer(SDATA));
  86. entityTypes.put("PI", new Integer(PI));
  87. entityTypes.put("STARTTAG", new Integer(STARTTAG));
  88. entityTypes.put("ENDTAG", new Integer(ENDTAG));
  89. entityTypes.put("MS", new Integer(MS));
  90. entityTypes.put("MD", new Integer(MD));
  91. entityTypes.put("SYSTEM", new Integer(SYSTEM));
  92. }
  93. /**
  94. * Converts <code>nm</code> string to the corresponding
  95. * entity type. If the string does not have a corresponding
  96. * entity type, returns the type corresponding to "CDATA".
  97. * Valid entity types are: "PUBLIC", "CDATA", "SDATA", "PI",
  98. * "STARTTAG", "ENDTAG", "MS", "MD", "SYSTEM".
  99. *
  100. * @param nm the string to be converted
  101. * @return the corresponding entity type, or the type corresponding
  102. * to "CDATA", if none exists
  103. */
  104. public static int name2type(String nm) {
  105. Integer i = (Integer)entityTypes.get(nm);
  106. return (i == null) ? CDATA : i.intValue();
  107. }
  108. }