View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  /*
13   * This file is protected by the Mozilla Public License located in
14   * euromath2-bin.zip file, downloadable from
15   * http://sourceforge.net/projects/euromath2/.
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  	 * (non-Javadoc)
49  	 * @see java.lang.Object#toString()
50  	 */
51  	@Override
52  	public String toString() {
53  		return original.toString();
54  	}
55  	/*
56  	 * (non-Javadoc)
57  	 * @see java.util.List#add(int, E)
58  	 */
59  	public void add(int index, ElementLoc element) {
60  		throw new UnsupportedOperationException();
61  	}
62  	/*
63  	 * (non-Javadoc)
64  	 * @see java.util.List#add(E)
65  	 */
66  	public boolean add(ElementLoc o) {
67  		throw new UnsupportedOperationException();
68  	}
69  	/*
70  	 * (non-Javadoc)
71  	 * @see java.util.List#addAll(int, java.util.Collection)
72  	 */
73  	public boolean addAll(int index, Collection< ? extends ElementLoc> c) {
74  		throw new UnsupportedOperationException();
75  	}
76  	/*
77  	 * (non-Javadoc)
78  	 * @see java.util.List#addAll(java.util.Collection)
79  	 */
80  	public boolean addAll(Collection< ? extends ElementLoc> c) {
81  		throw new UnsupportedOperationException();
82  	}
83  	/*
84  	 * (non-Javadoc)
85  	 * @see java.util.List#clear()
86  	 */
87  	public void clear() {
88  		throw new UnsupportedOperationException();
89  	}
90  	/*
91  	 * (non-Javadoc)
92  	 * @see java.util.List#contains(java.lang.Object)
93  	 */
94  	public boolean contains(Object o) {
95  		return list.contains(o);
96  	}
97  	/*
98  	 * (non-Javadoc)
99  	 * @see java.util.List#containsAll(java.util.Collection)
100 	 */
101 	public boolean containsAll(Collection< ? > c) {
102 		return list.containsAll(c);
103 	}
104 	/*
105 	 * (non-Javadoc)
106 	 * @see java.util.List#equals(java.lang.Object)
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 	 * (non-Javadoc)
116 	 * @see java.util.List#get(int)
117 	 */
118 	public ElementLoc get(int index) {
119 		return list.get(index);
120 	}
121 	/*
122 	 * (non-Javadoc)
123 	 * @see java.util.List#hashCode()
124 	 */
125 	@Override
126 	public int hashCode() {
127 		return list.hashCode();
128 	}
129 	/*
130 	 * (non-Javadoc)
131 	 * @see java.util.List#indexOf(java.lang.Object)
132 	 */
133 	public int indexOf(Object o) {
134 		return list.indexOf(o);
135 	}
136 	/*
137 	 * (non-Javadoc)
138 	 * @see java.util.List#isEmpty()
139 	 */
140 	public boolean isEmpty() {
141 		return list.isEmpty();
142 	}
143 	/*
144 	 * (non-Javadoc)
145 	 * @see java.util.List#iterator()
146 	 */
147 	public Iterator<ElementLoc> iterator() {
148 		return list.iterator();
149 	}
150 	/*
151 	 * (non-Javadoc)
152 	 * @see java.util.List#lastIndexOf(java.lang.Object)
153 	 */
154 	public int lastIndexOf(Object o) {
155 		return list.lastIndexOf(o);
156 	}
157 	/*
158 	 * (non-Javadoc)
159 	 * @see java.util.List#listIterator()
160 	 */
161 	public ListIterator<ElementLoc> listIterator() {
162 		return list.listIterator();
163 	}
164 	/*
165 	 * (non-Javadoc)
166 	 * @see java.util.List#listIterator(int)
167 	 */
168 	public ListIterator<ElementLoc> listIterator(int index) {
169 		return list.listIterator(index);
170 	}
171 	/*
172 	 * (non-Javadoc)
173 	 * @see java.util.List#remove(int)
174 	 */
175 	public ElementLoc remove(int index) {
176 		throw new UnsupportedOperationException();
177 	}
178 	/*
179 	 * (non-Javadoc)
180 	 * @see java.util.List#remove(java.lang.Object)
181 	 */
182 	public boolean remove(Object o) {
183 		throw new UnsupportedOperationException();
184 	}
185 	/*
186 	 * (non-Javadoc)
187 	 * @see java.util.List#removeAll(java.util.Collection)
188 	 */
189 	public boolean removeAll(Collection< ? > c) {
190 		throw new UnsupportedOperationException();
191 	}
192 	/*
193 	 * (non-Javadoc)
194 	 * @see java.util.List#retainAll(java.util.Collection)
195 	 */
196 	public boolean retainAll(Collection< ? > c) {
197 		throw new UnsupportedOperationException();
198 	}
199 	/*
200 	 * (non-Javadoc)
201 	 * @see java.util.List#set(int, E)
202 	 */
203 	public ElementLoc set(int index, ElementLoc element) {
204 		throw new UnsupportedOperationException();
205 	}
206 	/*
207 	 * (non-Javadoc)
208 	 * @see java.util.List#size()
209 	 */
210 	public int size() {
211 		return list.size();
212 	}
213 	/*
214 	 * (non-Javadoc)
215 	 * @see java.util.List#subList(int, int)
216 	 */
217 	public List<ElementLoc> subList(int fromIndex, int toIndex) {
218 		return list.subList(fromIndex, toIndex);
219 	}
220 	/*
221 	 * (non-Javadoc)
222 	 * @see java.util.List#toArray()
223 	 */
224 	public Object[] toArray() {
225 		return list.toArray();
226 	}
227 	/*
228 	 * (non-Javadoc)
229 	 * @see java.util.List#toArray(T[])
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");//$NON-NLS-1$
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 }