001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.text.html.parser;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.BitSet;
022: import java.util.List;
023:
024: import javax.swing.text.html.HTML;
025:
026: public final class Element implements DTDConstants, Serializable {
027: public int index;
028:
029: public String name;
030:
031: public boolean oStart;
032:
033: public boolean oEnd;
034:
035: public BitSet inclusions;
036:
037: public BitSet exclusions;
038:
039: public int type = DTDConstants.ANY;
040:
041: public ContentModel content;
042:
043: public AttributeList atts;
044:
045: public Object data;
046:
047: private final String SCRIPT_TAG_NAME = "script";
048:
049: Element(final int index, final String name, final boolean oStart,
050: final boolean oEnd, final BitSet exclusions,
051: final BitSet inclusions, final int type,
052: final ContentModel content, final AttributeList atts,
053: final Object data) {
054: this .index = index;
055: this .name = name;
056: this .oStart = oStart;
057: this .oEnd = oEnd;
058: this .inclusions = inclusions;
059: this .exclusions = exclusions;
060: this .type = type;
061: this .content = content;
062: this .atts = atts;
063: this .data = data;
064: }
065:
066: Element() {
067: }
068:
069: public static int name2type(final String name) {
070: if (name.equals("ANY")) {
071: return DTDConstants.ANY;
072: } else if (name.equals("CDATA")) {
073: return DTDConstants.CDATA;
074: } else if (name.equals("EMPTY")) {
075: return DTDConstants.EMPTY;
076: } else if (name.equals("RCDATA")) {
077: return DTDConstants.RCDATA;
078: } else {
079: return 0;
080: }
081: }
082:
083: public AttributeList getAttributeByValue(final String value) {
084: AttributeList currentAtts = this .atts;
085: while (currentAtts != null) {
086: if (currentAtts.containsValue(value)) {
087: return currentAtts;
088: }
089: currentAtts = currentAtts.next;
090: }
091: return null;
092: }
093:
094: public AttributeList getAttribute(final String name) {
095: AttributeList currentAtts = this .atts;
096: while (currentAtts != null) {
097: // we change the order of the comparision to force a
098: // NullPointerException if currentAtts.getName() is null (same as RI)
099: if (currentAtts.getName().equals(name)) {
100: return currentAtts;
101: }
102: currentAtts = currentAtts.next;
103: }
104: return null;
105: }
106:
107: public String toString() {
108: return name; // (same as RI)
109: }
110:
111: public boolean isEmpty() {
112: return type == DTDConstants.EMPTY;
113: }
114:
115: public int getIndex() {
116: return index;
117: }
118:
119: public AttributeList getAttributes() {
120: return atts;
121: }
122:
123: public ContentModel getContent() {
124: return content;
125: }
126:
127: public int getType() {
128: return type;
129: }
130:
131: public boolean omitEnd() {
132: return oEnd;
133: }
134:
135: public boolean omitStart() {
136: return oStart;
137: }
138:
139: public String getName() {
140: return name;
141: }
142:
143: final void updateElement(final int index, final String name,
144: final boolean oStart, final boolean oEnd,
145: final BitSet exclusions, final BitSet inclusions,
146: final int type, final ContentModel content,
147: final AttributeList atts, final Object data) {
148: this .index = index;
149: this .name = name;
150: this .oStart = oStart;
151: this .oEnd = oEnd;
152: this .inclusions = inclusions;
153: this .exclusions = exclusions;
154: this .type = type;
155: this .content = content;
156: this .atts = atts;
157: this .data = data;
158: }
159:
160: /**
161: * Returns a list of required attributes for the {@link Element}.
162: *
163: * @return a {@link List} with all the required attributes for the
164: * {@link Element}.
165: */
166: final List<Object> getRequiredAttributes() {
167: List<Object> reqAtts = new ArrayList<Object>();
168: AttributeList attList = atts;
169: while (attList != null) {
170: if (attList.getModifier() == DTDConstants.REQUIRED) {
171: Object attr = HTML.getAttributeKey(attList.getName());
172: reqAtts.add(attr == null ? attList.getName() : attr);
173: }
174: attList = attList.getNext();
175: }
176: return reqAtts;
177: }
178:
179: final boolean hasRequiredAttributes() {
180: boolean flag = false;
181: AttributeList attList = atts;
182: while (attList != null) {
183: if (attList.getModifier() == DTDConstants.REQUIRED) {
184: flag = true;
185: break;
186: }
187: attList = attList.getNext();
188: }
189: return flag;
190: }
191:
192: final boolean isScript() {
193: return name.equalsIgnoreCase(SCRIPT_TAG_NAME);
194: }
195: }
|