01: /*
02: * $Id: ElementDefinition.java,v 1.3 2004/07/08 08:03:04 yuvalo Exp $
03: *
04: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.bluecast.xml;
20:
21: import com.bluecast.util.*;
22: import java.util.*;
23:
24: /**
25: * A class to hold information about an element defined
26: * within an XML document type declaration.
27: *
28: * @author Yuval Oren, yuval@bluecast.com
29: * @version $Revision: 1.3 $
30: */
31: final public class ElementDefinition {
32: String name;
33: AttributeDefinition[] attributes;
34: Map attributeMap;
35: int size = 0;
36:
37: public ElementDefinition() {
38: this (null);
39: }
40:
41: public ElementDefinition(String name) {
42: this .name = name;
43: attributes = new AttributeDefinition[4];
44: attributeMap = new HashMap();
45: size = 0;
46: }
47:
48: final public String getName() {
49: return name;
50: }
51:
52: final public void setName(String name) {
53: this .name = name;
54: }
55:
56: final public AttributeDefinition[] getAttributes() {
57: return attributes;
58: }
59:
60: final public int getAttributeCount() {
61: return size;
62: }
63:
64: final public IndexedObject getIndexedAttribute(String name) {
65: return (IndexedObject) attributeMap.get(name);
66: }
67:
68: final public AttributeDefinition getAttribute(int index) {
69: return attributes[index];
70: }
71:
72: final public void addAttribute(AttributeDefinition attrib)
73: throws DuplicateKeyException {
74: Object newObj = new IndexedObjectImpl(size, attrib);
75: Object oldObj = attributeMap.put(attrib.getQName(), newObj);
76: // If there was already an attribute with this name, put the original back
77: // and throw an exception.
78: if (oldObj != null) {
79: attributeMap.put(attrib.getQName(), oldObj);
80: throw new DuplicateKeyException("attribute '"
81: + attrib.getQName()
82: + "' is already defined for element '" + name
83: + "'.");
84: } else {
85: if (size >= attributes.length) {
86: AttributeDefinition[] newAttributes = new AttributeDefinition[size * 2];
87: System.arraycopy(attributes, 0, newAttributes, 0, size);
88: attributes = newAttributes;
89: }
90: attributes[size++] = attrib;
91: }
92: }
93: }
|