1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package sk.uniba.euromath.document.schema;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.ListIterator;
24 import javax.xml.namespace.QName;
25 import sk.baka.ikslibs.ptr.DOMPoint;
26 import sk.uniba.euromath.document.schema.plug.IInsertListP;
27 /***
28 * List of <code>ElementLoc</code> objects, must be maintained ordered by
29 * <code>InsertPoint</code>.
30 * @author Martin Vysny
31 */
32 public final class InsertList implements List<ElementLoc> {
33 private final List<ElementLoc> list;
34 private final IInsertListP original;
35 /***
36 * Constructor of the adapter.
37 * @param list original insert list.
38 */
39 InsertList(IInsertListP list) {
40 super();
41 original = list;
42 List<ElementLoc> _list = new ArrayList<ElementLoc>(list.getLength());
43 for (int i = 0; i < list.getLength(); i++)
44 _list.add(new ElementLoc(list.get(i)));
45 this.list = Collections.unmodifiableList(_list);
46 }
47
48
49
50
51 @Override
52 public String toString() {
53 return original.toString();
54 }
55
56
57
58
59 public void add(int index, ElementLoc element) {
60 throw new UnsupportedOperationException();
61 }
62
63
64
65
66 public boolean add(ElementLoc o) {
67 throw new UnsupportedOperationException();
68 }
69
70
71
72
73 public boolean addAll(int index, Collection< ? extends ElementLoc> c) {
74 throw new UnsupportedOperationException();
75 }
76
77
78
79
80 public boolean addAll(Collection< ? extends ElementLoc> c) {
81 throw new UnsupportedOperationException();
82 }
83
84
85
86
87 public void clear() {
88 throw new UnsupportedOperationException();
89 }
90
91
92
93
94 public boolean contains(Object o) {
95 return list.contains(o);
96 }
97
98
99
100
101 public boolean containsAll(Collection< ? > c) {
102 return list.containsAll(c);
103 }
104
105
106
107
108 @Override
109 public boolean equals(Object o) {
110 if ((o == null) || !(o instanceof InsertList))
111 return false;
112 return list.equals(((InsertList) o).list);
113 }
114
115
116
117
118 public ElementLoc get(int index) {
119 return list.get(index);
120 }
121
122
123
124
125 @Override
126 public int hashCode() {
127 return list.hashCode();
128 }
129
130
131
132
133 public int indexOf(Object o) {
134 return list.indexOf(o);
135 }
136
137
138
139
140 public boolean isEmpty() {
141 return list.isEmpty();
142 }
143
144
145
146
147 public Iterator<ElementLoc> iterator() {
148 return list.iterator();
149 }
150
151
152
153
154 public int lastIndexOf(Object o) {
155 return list.lastIndexOf(o);
156 }
157
158
159
160
161 public ListIterator<ElementLoc> listIterator() {
162 return list.listIterator();
163 }
164
165
166
167
168 public ListIterator<ElementLoc> listIterator(int index) {
169 return list.listIterator(index);
170 }
171
172
173
174
175 public ElementLoc remove(int index) {
176 throw new UnsupportedOperationException();
177 }
178
179
180
181
182 public boolean remove(Object o) {
183 throw new UnsupportedOperationException();
184 }
185
186
187
188
189 public boolean removeAll(Collection< ? > c) {
190 throw new UnsupportedOperationException();
191 }
192
193
194
195
196 public boolean retainAll(Collection< ? > c) {
197 throw new UnsupportedOperationException();
198 }
199
200
201
202
203 public ElementLoc set(int index, ElementLoc element) {
204 throw new UnsupportedOperationException();
205 }
206
207
208
209
210 public int size() {
211 return list.size();
212 }
213
214
215
216
217 public List<ElementLoc> subList(int fromIndex, int toIndex) {
218 return list.subList(fromIndex, toIndex);
219 }
220
221
222
223
224 public Object[] toArray() {
225 return list.toArray();
226 }
227
228
229
230
231 public <T> T[] toArray(T[] a) {
232 return list.toArray(a);
233 }
234 /***
235 * Checks if this insertlist is able to create element/attribute with given
236 * qname.
237 * @param qname qname to check. May be <code>null</code> - in such case
238 * the qname does not matter.
239 * @param ip the insertpoint where the element will be inserted.
240 * @return <code>true</code> if at least one elementloc contains rule for
241 * given qname. If qname is <code>null</code> then returns
242 * <code>true</code> if any elementloc can be found at given insertpoint.
243 */
244 public boolean canCreate(final QName qname, final DOMPoint ip) {
245 return getFirstAt(ip, qname) != null;
246 }
247 /***
248 * Finds first elementloc bound to be inserted at given position.
249 * @param ip the position.
250 * @param qname if not <code>null</code> then the elementloc must contain
251 * rule for given qname.
252 * @return elementloc instance or <code>null</code> if no such object
253 * exist.
254 */
255 public ElementLoc getFirstAt(final DOMPoint ip, final QName qname) {
256 if (ip == null)
257 throw new IllegalArgumentException("ip is null");
258 for (final ElementLoc loc : list) {
259 final int cmp = ip.compareTo(loc.ip);
260 if (cmp == 0) {
261 if (qname == null)
262 return loc;
263 if (loc.nameList.hasRule(qname, true))
264 return loc;
265 }
266 if (cmp > 0)
267 return null;
268 }
269 return null;
270 }
271 /***
272 * Returns namelist of first elementloc, located at given insertpoint.
273 * @param ip insertpoint.
274 * @return namelist or <code>null</code> if no rule exists for given
275 * insertpoint.
276 */
277 public INameList<NewElementRule> getFirstNamelistAt(final DOMPoint ip) {
278 final ElementLoc loc = getFirstAt(ip, null);
279 if (loc == null)
280 return null;
281 return loc.nameList;
282 }
283 }