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