~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/XmlStorageElement.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2007, 2010 Intel Corporation and others.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *     Intel Corporation - Initial API and implementation
 
10
 *     Anton Leherbauer (Wind River Systems) - Bug 253911
 
11
 *******************************************************************************/
 
12
package org.eclipse.cdt.core.settings.model.util;
 
13
 
 
14
import java.util.ArrayList;
 
15
import java.util.Arrays;
 
16
import java.util.HashSet;
 
17
import java.util.List;
 
18
import java.util.Set;
 
19
 
 
20
import org.eclipse.cdt.core.settings.model.ICStorageElement;
 
21
import org.eclipse.core.runtime.CoreException;
 
22
import org.w3c.dom.Document;
 
23
import org.w3c.dom.Element;
 
24
import org.w3c.dom.NamedNodeMap;
 
25
import org.w3c.dom.Node;
 
26
import org.w3c.dom.NodeList;
 
27
import org.w3c.dom.Text;
 
28
 
 
29
/**
 
30
 * @deprecated
 
31
 * @noextend This class is not intended to be subclassed by clients.
 
32
 * @noinstantiate This class is not intended to be instantiated by clients.
 
33
 */
 
34
@Deprecated
 
35
public class XmlStorageElement implements ICStorageElement {
 
36
 
 
37
        Element fElement;
 
38
        private ICStorageElement fParent;
 
39
        private List<XmlStorageElement> fChildList = new ArrayList<XmlStorageElement>();
 
40
        private boolean fChildrenCreated;
 
41
        private String[] fAttributeFilters;
 
42
        private String[] fChildFilters;
 
43
        private boolean fParentRefAlowed;
 
44
 
 
45
        public XmlStorageElement(Element element){
 
46
                this(element, null, false);
 
47
        }
 
48
 
 
49
        public XmlStorageElement(Element element, ICStorageElement parent, boolean alowReferencingParent){
 
50
                this(element, parent, alowReferencingParent, null, null);
 
51
        }
 
52
 
 
53
        public XmlStorageElement(Element element,
 
54
                        ICStorageElement parent,
 
55
                        boolean alowReferencingParent,
 
56
                        String[] attributeFilters,
 
57
                        String[] childFilters){
 
58
                fElement = element;
 
59
                fParent = parent;
 
60
                fParentRefAlowed = alowReferencingParent;
 
61
                
 
62
                if(attributeFilters != null && attributeFilters.length != 0)
 
63
                        fAttributeFilters = attributeFilters.clone();
 
64
                
 
65
                if(childFilters != null && childFilters.length != 0)
 
66
                        fChildFilters = childFilters.clone();
 
67
        }
 
68
        
 
69
//      public String[] getAttributeFilters(){
 
70
//              if(fAttributeFilters != null)
 
71
//                      return (String[])fAttributeFilters.clone();
 
72
//              return null;
 
73
//      }
 
74
 
 
75
//      public String[] getChildFilters(){
 
76
//              if(fChildFilters != null)
 
77
//                      return (String[])fChildFilters.clone();
 
78
//              return null;
 
79
//      }
 
80
//      
 
81
//      public boolean isParentRefAlowed(){
 
82
//              return fParentRefAlowed;
 
83
//      }
 
84
 
 
85
        private void createChildren(){
 
86
                if(fChildrenCreated)
 
87
                        return;
 
88
                
 
89
                fChildrenCreated = true;
 
90
                NodeList list = fElement.getChildNodes();
 
91
                int size = list.getLength();
 
92
                for(int i = 0; i < size; i++){
 
93
                        Node node = list.item(i);
 
94
                        if(node.getNodeType() == Node.ELEMENT_NODE
 
95
                                        && isChildAlowed(node.getNodeName())){
 
96
                                createAddChild((Element)node, true, null, null);
 
97
                        }
 
98
                }
 
99
        }
 
100
        
 
101
        private XmlStorageElement createAddChild(Element element,
 
102
                        boolean alowReferencingParent,
 
103
                        String[] attributeFilters,
 
104
                        String[] childFilters){
 
105
                XmlStorageElement child = createChild(element, alowReferencingParent, attributeFilters, childFilters);
 
106
                fChildList.add(child);
 
107
                return child; 
 
108
        }
 
109
        
 
110
        protected XmlStorageElement createChild(Element element,
 
111
                        boolean alowReferencingParent,
 
112
                        String[] attributeFilters,
 
113
                        String[] childFilters){
 
114
                return new XmlStorageElement(element, this, alowReferencingParent, attributeFilters, childFilters);
 
115
        }
 
116
 
 
117
        public ICStorageElement[] getChildren() {
 
118
                return getChildren(XmlStorageElement.class);
 
119
        }
 
120
 
 
121
        protected ICStorageElement[] getChildren(Class<XmlStorageElement> clazz){
 
122
                return getChildren(clazz, true);
 
123
        }
 
124
 
 
125
        protected ICStorageElement[] getChildren(boolean load){
 
126
                return getChildren(XmlStorageElement.class, load);
 
127
        }
 
128
 
 
129
        protected ICStorageElement[] getChildren(Class<XmlStorageElement> clazz, boolean load){
 
130
                if(load)
 
131
                        createChildren();
 
132
 
 
133
                ICStorageElement[] children = (ICStorageElement[])java.lang.reflect.Array.newInstance(
 
134
                                clazz, fChildList.size());
 
135
 
 
136
                return fChildList.toArray(children);
 
137
        }
 
138
 
 
139
        public ICStorageElement getParent() {
 
140
                return fParentRefAlowed ? fParent : null;
 
141
        }
 
142
 
 
143
        public String getAttribute(String name) {
 
144
                if(isPropertyAlowed(name) && fElement.hasAttribute(name))
 
145
                        return fElement.getAttribute(name);
 
146
                return null;
 
147
        }
 
148
        
 
149
        private boolean isPropertyAlowed(String name){
 
150
                if(fAttributeFilters != null){
 
151
                        return checkString(name, fAttributeFilters);
 
152
                }
 
153
                return true;
 
154
        }
 
155
 
 
156
        private boolean isChildAlowed(String name){
 
157
                if(fChildFilters != null){
 
158
                        return checkString(name, fChildFilters);
 
159
                }
 
160
                return true;
 
161
        }
 
162
 
 
163
        private boolean checkString(String name, String array[]){
 
164
                if(array.length > 0){
 
165
                        for(int i = 0; i < array.length; i++){
 
166
                                if(name.equals(array[i]))
 
167
                                        return false;
 
168
                        }
 
169
                }
 
170
                return true;
 
171
        }
 
172
 
 
173
//      protected void childRemoved(ICStorageElement child) {
 
174
//              fChildList.remove(child);
 
175
//      }
 
176
        
 
177
        protected void removed(){
 
178
//              fElement.getParentNode().removeChild(fElement);
 
179
                fElement = null;
 
180
//              if(fParent != null)
 
181
//                      ((XmlStorageElement)fParent).childRemoved(this);
 
182
        }
 
183
        
 
184
 
 
185
        public void removeChild(ICStorageElement el) {
 
186
                if(el instanceof XmlStorageElement){
 
187
                        ICStorageElement[] children = getChildren();
 
188
                        for(int i = 0; i < children.length; i++){
 
189
                                if(children[i] == el){
 
190
                                        XmlStorageElement xmlEl = (XmlStorageElement)el;
 
191
                                        Node nextSibling = xmlEl.fElement.getNextSibling();
 
192
                                        fElement.removeChild(xmlEl.fElement);
 
193
                                        if (nextSibling != null && nextSibling.getNodeType() == Node.TEXT_NODE) {
 
194
                                                String value = nextSibling.getNodeValue();
 
195
                                                if (value != null && value.trim().length() == 0) {
 
196
                                                        // remove whitespace
 
197
                                                        fElement.removeChild(nextSibling);
 
198
                                                }
 
199
                                        }
 
200
                                        fChildList.remove(el);
 
201
                                        xmlEl.removed();
 
202
                                }
 
203
                        }
 
204
                }
 
205
                
 
206
        }
 
207
 
 
208
        public void removeAttribute(String name) {
 
209
                if(isPropertyAlowed(name))
 
210
                        fElement.removeAttribute(name);
 
211
        }
 
212
 
 
213
        public void setAttribute(String name, String value) {
 
214
                if(isPropertyAlowed(name))
 
215
                        fElement.setAttribute(name, value);
 
216
        }
 
217
        
 
218
        public void clear(){
 
219
                createChildren();
 
220
 
 
221
                ICStorageElement children[] = fChildList.toArray(new ICStorageElement[fChildList.size()]);
 
222
                for(int i = 0; i < children.length; i++){
 
223
                        removeChild(children[i]);
 
224
                }
 
225
                
 
226
                NamedNodeMap map = fElement.getAttributes();
 
227
                for(int i = 0; i < map.getLength(); i++){
 
228
                        Node attr = map.item(i);
 
229
                        if(isPropertyAlowed(attr.getNodeName()))
 
230
                                map.removeNamedItem(attr.getNodeName());
 
231
                }
 
232
                
 
233
                NodeList list = fElement.getChildNodes();
 
234
                for(int i = 0; i < list.getLength(); i++){
 
235
                        Node node = list.item(i);
 
236
                        if(node.getNodeType() == Node.TEXT_NODE)
 
237
                                fElement.removeChild(node);
 
238
                }
 
239
        }
 
240
        
 
241
        public ICStorageElement createChild(String name, 
 
242
                        boolean alowReferencingParent,
 
243
                        String[] attributeFilters,
 
244
                        String[] childFilters) {
 
245
                if(!isChildAlowed(name))
 
246
                        return null;
 
247
                Element childElement = fElement.getOwnerDocument().createElement(name);
 
248
                fElement.appendChild(childElement);
 
249
                return createAddChild(childElement, alowReferencingParent, attributeFilters, childFilters);
 
250
        }
 
251
 
 
252
        public String getName() {
 
253
                return fElement.getNodeName();
 
254
        }
 
255
 
 
256
        public ICStorageElement createChild(String name) {
 
257
                return createChild(name, true, null, null);
 
258
        }
 
259
 
 
260
        public String getValue() {
 
261
                Text text = getTextChild();
 
262
                if(text != null)
 
263
                        return text.getData();
 
264
                return null;
 
265
        }
 
266
 
 
267
        public void setValue(String value) {
 
268
                Text text = getTextChild();
 
269
                if(value != null){
 
270
                        if(text == null){
 
271
                                text = fElement.getOwnerDocument().createTextNode(value);
 
272
                                fElement.appendChild(text);
 
273
                        } else {
 
274
                                text.setData(value);
 
275
                        }
 
276
                } else {
 
277
                        if(text != null){
 
278
                                fElement.removeChild(text);
 
279
                        }
 
280
                }
 
281
        }
 
282
        
 
283
        private Text getTextChild(){
 
284
                NodeList nodes = fElement.getChildNodes();
 
285
                Text text = null;
 
286
                for(int i = 0; i < nodes.getLength(); i++){
 
287
                        Node node = nodes.item(i);
 
288
                        if(node.getNodeType() == Node.TEXT_NODE){
 
289
                                text = (Text)node;
 
290
                                break;
 
291
                        }
 
292
                }
 
293
 
 
294
                return text;
 
295
        }
 
296
        
 
297
        public ICStorageElement importChild(ICStorageElement el) throws UnsupportedOperationException {
 
298
                return addChild(el, true, null, null);
 
299
        }
 
300
 
 
301
        public ICStorageElement addChild(ICStorageElement el, 
 
302
                        boolean alowReferencingParent,
 
303
                        String[] attributeFilters,
 
304
                        String[] childFilters) throws UnsupportedOperationException {
 
305
                
 
306
                if(!isChildAlowed(el.getName()))
 
307
                        return null;
 
308
 
 
309
                if(el instanceof XmlStorageElement){
 
310
                        XmlStorageElement xmlStEl = (XmlStorageElement)el;
 
311
                        Element xmlEl = xmlStEl.fElement;
 
312
                        Document thisDoc = fElement.getOwnerDocument();
 
313
                        Document otherDoc = xmlEl.getOwnerDocument();
 
314
                        if(!thisDoc.equals(otherDoc)){
 
315
                                xmlEl = (Element)thisDoc.importNode(xmlEl, true);
 
316
                        } else {
 
317
                                xmlEl = (Element)xmlEl.cloneNode(true);
 
318
                        }
 
319
                        
 
320
                        xmlEl = (Element)fElement.appendChild(xmlEl);
 
321
                        return createAddChild(xmlEl, alowReferencingParent, attributeFilters, childFilters);
 
322
                } else {
 
323
                        throw new UnsupportedOperationException();
 
324
                }
 
325
        }
 
326
        
 
327
        public String[] getAttributeFilters(){
 
328
                if(fAttributeFilters != null)
 
329
                        return fAttributeFilters.clone();
 
330
                return new String[0];
 
331
        }
 
332
 
 
333
        public String[] getChildFilters(){
 
334
                if(fChildFilters != null)
 
335
                        return fChildFilters.clone();
 
336
                return new String[0];
 
337
        }
 
338
 
 
339
        public boolean isParentRefAlowed(){
 
340
                return fParentRefAlowed;
 
341
        }
 
342
        
 
343
        public boolean matches(ICStorageElement el){
 
344
                if(!getName().equals(el.getName()))
 
345
                        return false;
 
346
                
 
347
                if (!valuesMatch(getValue(), el.getValue()))
 
348
                        return false;
 
349
 
 
350
                
 
351
                String[] attrs = getAttributeNames();
 
352
                String[] otherAttrs = el.getAttributeNames();
 
353
                if(attrs.length != otherAttrs.length)
 
354
                        return false;
 
355
                
 
356
                if(attrs.length != 0){
 
357
                        Set<String> set = new HashSet<String>(Arrays.asList(attrs));
 
358
                        set.removeAll(Arrays.asList(otherAttrs));
 
359
                        if(set.size() != 0)
 
360
                                return false;
 
361
 
 
362
                        for(int i = 0; i < attrs.length; i++){
 
363
                                if(!getAttribute(attrs[i]).equals(el.getAttribute(attrs[i])))
 
364
                                        return false;
 
365
                        }
 
366
 
 
367
                }
 
368
                
 
369
                
 
370
                XmlStorageElement[] children = (XmlStorageElement[])getChildren();
 
371
                ICStorageElement[] otherChildren = el.getChildren();
 
372
                
 
373
                if(children.length != otherChildren.length)
 
374
                        return false;
 
375
                
 
376
                if(children.length != 0){
 
377
                        for(int i = 0; i < children.length; i++){
 
378
                                if(!children[i].matches(otherChildren[i]))
 
379
                                        return false;
 
380
                        }
 
381
                }
 
382
                
 
383
                return true;
 
384
        }
 
385
 
 
386
        private static boolean valuesMatch(String value, String other) {
 
387
                if(value == null) {
 
388
                        return other == null || other.trim().length() == 0;
 
389
                } else if (other == null) {
 
390
                        return value.trim().length() == 0;
 
391
                } else {
 
392
                        return value.trim().equals(other.trim());
 
393
                }
 
394
        }
 
395
 
 
396
        public String[] getAttributeNames() {
 
397
                NamedNodeMap nodeMap = fElement.getAttributes();
 
398
                int length = nodeMap.getLength();
 
399
                List<String> list = new ArrayList<String>(length);
 
400
                for(int i = 0; i < length; i++){
 
401
                        Node node = nodeMap.item(i);
 
402
                        String name = node.getNodeName();
 
403
                        if(isPropertyAlowed(name))
 
404
                                list.add(name);
 
405
                }
 
406
                return list.toArray(new String[list.size()]);
 
407
        }
 
408
 
 
409
        /**
 
410
         * @since 5.1
 
411
         */
 
412
        public ICStorageElement createCopy() throws UnsupportedOperationException, CoreException {
 
413
                // todo Auto-generated method stub
 
414
                return null;
 
415
        }
 
416
 
 
417
        /**
 
418
         * @since 5.1
 
419
         */
 
420
        public boolean equals(ICStorageElement other) {
 
421
                // todo Auto-generated method stub
 
422
                return false;
 
423
        }
 
424
 
 
425
        /**
 
426
         * @since 5.1
 
427
         */
 
428
        public ICStorageElement[] getChildrenByName(String name) {
 
429
                // todo Auto-generated method stub
 
430
                return null;
 
431
        }
 
432
 
 
433
        /**
 
434
         * @since 5.1
 
435
         */
 
436
        public boolean hasAttribute(String name) {
 
437
                // todo Auto-generated method stub
 
438
                return false;
 
439
        }
 
440
 
 
441
        /**
 
442
         * @since 5.1
 
443
         */
 
444
        public boolean hasChildren() {
 
445
                // todo Auto-generated method stub
 
446
                return false;
 
447
        }
 
448
 
 
449
}