forked from Klortho/DtdAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Attributes.java
executable file
·181 lines (163 loc) · 6.45 KB
/
Attributes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Attributes.java
*
* Created on November 9, 2005, 5:23 PM
*/
package gov.ncbi.pmc.dtdanalyzer;
import java.util.*;
/**
* Holds a collection of Attribute objects and provides accessor methods
*
* @author Demian Hess
*/
public class Attributes {
private HashMap atts = new HashMap(256); // Holds all attributes
private HashMap attsByElement = new HashMap(256); // Organizes attributes by element name
private HashMap attsByName = new HashMap(256); // Organizes attributes by name
private HashMap attNames = new HashMap(256); // Unique set of attribute names
/**
* Adds Attribute to the collection
*
* @param attribute Attribute to be added
*/
public void addAttribute( Attribute attribute ){
atts.put( makeKey(attribute), attribute );
// Keep a list of all unique names
if (! attNames.containsKey(attribute.getName())){
attNames.put(attribute.getName(), attribute.getName());
}//if
// Store this attribute under the name of the element
if ( attsByElement.containsKey(attribute.getParent())){
Collection elementAtts = (Collection)attsByElement.get(attribute.getParent());
elementAtts.add(attribute);
}
// First time this element is seen: need to create container
else{
Collection elementAtts = new HashSet();
elementAtts.add(attribute);
attsByElement.put(attribute.getParent(), elementAtts);
}
// Also store this attribute under its name; the value in the
// collection will be another collection since it is
// possible for several attributes to have the same name in different
// elements
if (attsByName.containsKey(attribute.getName())){
Collection namedAtts = (Collection)attsByName.get(attribute.getName());
namedAtts.add(attribute);
}//if
// First time this attribute name is seen: need to create a container
else{
Collection namedAtts = new HashSet();
namedAtts.add(attribute);
attsByName.put(attribute.getName(), namedAtts);
}//else
}
/**
* Returns a list of unique attribute names in the collection
*
* @return List of attribute names
*/
public String[] getAllAtttributeNames(){
String[] values = new String[0];
if ( ! attNames.isEmpty()){
values = (String [])attNames.keySet().toArray(values);
}//if
return values;
}
/**
* Returns the specified Attribute using the element name and the attribute
* name; null if no matching attribute is in the collection.
*
* @param elementName Element name
* @param attributeName Attribute name
* @return Matching attribute or null
*/
public Attribute getAttribute( String elementName, String attributeName ){
return (Attribute)atts.get( this.makeKey(elementName, attributeName) );
}
/**
* Returns an iterator of all the Attributes in the collection
*
* @return All attributes in the collection
*/
public AttributeIterator getAttributeIterator(){
return new AttributeIterator( atts.values().iterator() );
}
/**
* Returns all attributes declared for a specific element. Attributes will
* be packaged in an AttributeIterator. If no attributes are available,
* the iterator will be empty.
*
* @param eName Element name
* @return Iterator containing all matching attributes
*/
public AttributeIterator getAttributesByElementName( String eName ){
if ( attsByElement.containsKey( eName ) ){
Collection attributes = (Collection)attsByElement.get( eName );
return new AttributeIterator(attributes.iterator());
}//if
else {
return new AttributeIterator( new HashMap().values().iterator());
}//if
}
/**
* Returns all attributes that have the specified name. Note that attributes
* can be declared with the same name as long as they are located in different
* elements. Each attribute returned will thus have a different element name.
*
* @param aName Attribute name
* @return Iterator containing all matching attributes
*/
public AttributeIterator getAttributesByName( String aName ){
if ( attsByName.containsKey( aName ) ){
Collection attributes = (Collection)attsByName.get( aName );
return new AttributeIterator(attributes.iterator());
}//if
else{
return new AttributeIterator( new HashMap().values().iterator());
}//else
}
/**
* @param attribute
* @return */
public boolean hasAttribute( Attribute attribute ){
return atts.containsKey( makeKey( attribute ) );
}
/**
* Indicates whether a specific attribute is in the collection.
*
* @param elementName Name of the parent element
* @param attributeName Name of the attribute
* @return True if in the collection, false otherwise
*/
public boolean hasAttribute( String elementName, String attributeName ){
return atts.containsKey( makeKey( elementName, attributeName ) );
}
/**
* Creates the key used to access an attribute in the underlying
* HashMap. Key is based on the element name plus the attribute name.
* The key should match Attribute.toString(), but this method does
* not rely on that value in case we want to change the way the key is
* formed.
*
* @param eName Name of the parent element
* @param aName Name of the attribute
* @return Key to access the attribute
*/
private String makeKey( String eName, String aName ){
return eName + "/@" + aName;
}
/**
* Creates the key used to access an attribute in the underlying
* HashMap. Key is based on the element name plus the attribute name.
* The key should match Attribute.toString(), but this method does
* not rely on that value in case we want to change the way the key is
* formed.
*
* @param att Attribute for which a key is required
* @return Key to access the attribute
*/
private String makeKey( Attribute att ){
return makeKey( att.getParent(), att.getName() );
}
} // Attributes