1. /*
  2. * @(#)Element.java 1.6 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text.html.parser;
  11. import java.util.Hashtable;
  12. import java.util.BitSet;
  13. import java.io.*;
  14. /**
  15. * An element as described in a DTD using the ELEMENT construct.
  16. * This is essentiall the description of a tag. It describes the
  17. * type, content model, attributes, attribute types etc. It is used
  18. * to correctly parse a document by the Parser.
  19. *
  20. * @see DTD
  21. * @see AttributeList
  22. * @version 1.6, 02/02/00
  23. * @author Arthur van Hoff
  24. */
  25. public final
  26. class Element implements DTDConstants, Serializable {
  27. public int index;
  28. public String name;
  29. public boolean oStart;
  30. public boolean oEnd;
  31. public BitSet inclusions;
  32. public BitSet exclusions;
  33. public int type = ANY;
  34. public ContentModel content;
  35. public AttributeList atts;
  36. static int maxIndex = 0;
  37. /**
  38. * A field to store user data. Mostly used to store
  39. * style sheets.
  40. */
  41. public Object data;
  42. Element() {
  43. }
  44. /**
  45. * Create a new element.
  46. */
  47. Element(String name, int index) {
  48. this.name = name;
  49. this.index = index;
  50. maxIndex = Math.max(maxIndex, index);
  51. }
  52. /**
  53. * Get the name of the element.
  54. */
  55. public String getName() {
  56. return name;
  57. }
  58. /**
  59. * Return true if the start tag can be omitted.
  60. */
  61. public boolean omitStart() {
  62. return oStart;
  63. }
  64. /**
  65. * Return true if the end tag can be omitted.
  66. */
  67. public boolean omitEnd() {
  68. return oEnd;
  69. }
  70. /**
  71. * Get type.
  72. */
  73. public int getType() {
  74. return type;
  75. }
  76. /**
  77. * Get content model
  78. */
  79. public ContentModel getContent() {
  80. return content;
  81. }
  82. /**
  83. * Get the attributes.
  84. */
  85. public AttributeList getAttributes() {
  86. return atts;
  87. }
  88. /**
  89. * Get index.
  90. */
  91. public int getIndex() {
  92. return index;
  93. }
  94. /**
  95. * Check if empty
  96. */
  97. public boolean isEmpty() {
  98. return type == EMPTY;
  99. }
  100. /**
  101. * Convert to a string.
  102. */
  103. public String toString() {
  104. return name;
  105. }
  106. /**
  107. * Get an attribute by name.
  108. */
  109. public AttributeList getAttribute(String name) {
  110. for (AttributeList a = atts ; a != null ; a = a.next) {
  111. if (a.name.equals(name)) {
  112. return a;
  113. }
  114. }
  115. return null;
  116. }
  117. /**
  118. * Get an attribute by value.
  119. */
  120. public AttributeList getAttributeByValue(String name) {
  121. for (AttributeList a = atts ; a != null ; a = a.next) {
  122. if ((a.values != null) && a.values.contains(name)) {
  123. return a;
  124. }
  125. }
  126. return null;
  127. }
  128. static Hashtable contentTypes = new Hashtable();
  129. static {
  130. contentTypes.put("CDATA", new Integer(CDATA));
  131. contentTypes.put("RCDATA", new Integer(RCDATA));
  132. contentTypes.put("EMPTY", new Integer(EMPTY));
  133. contentTypes.put("ANY", new Integer(ANY));
  134. }
  135. public static int name2type(String nm) {
  136. Integer val = (Integer)contentTypes.get(nm);
  137. return (val != null) ? val.intValue() : 0;
  138. }
  139. }