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
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;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.HashSet;
17
import java.util.List;
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;
31
* @noextend This class is not intended to be subclassed by clients.
32
* @noinstantiate This class is not intended to be instantiated by clients.
35
public class XmlStorageElement implements ICStorageElement {
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;
45
public XmlStorageElement(Element element){
46
this(element, null, false);
49
public XmlStorageElement(Element element, ICStorageElement parent, boolean alowReferencingParent){
50
this(element, parent, alowReferencingParent, null, null);
53
public XmlStorageElement(Element element,
54
ICStorageElement parent,
55
boolean alowReferencingParent,
56
String[] attributeFilters,
57
String[] childFilters){
60
fParentRefAlowed = alowReferencingParent;
62
if(attributeFilters != null && attributeFilters.length != 0)
63
fAttributeFilters = attributeFilters.clone();
65
if(childFilters != null && childFilters.length != 0)
66
fChildFilters = childFilters.clone();
69
// public String[] getAttributeFilters(){
70
// if(fAttributeFilters != null)
71
// return (String[])fAttributeFilters.clone();
75
// public String[] getChildFilters(){
76
// if(fChildFilters != null)
77
// return (String[])fChildFilters.clone();
81
// public boolean isParentRefAlowed(){
82
// return fParentRefAlowed;
85
private void createChildren(){
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);
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);
110
protected XmlStorageElement createChild(Element element,
111
boolean alowReferencingParent,
112
String[] attributeFilters,
113
String[] childFilters){
114
return new XmlStorageElement(element, this, alowReferencingParent, attributeFilters, childFilters);
117
public ICStorageElement[] getChildren() {
118
return getChildren(XmlStorageElement.class);
121
protected ICStorageElement[] getChildren(Class<XmlStorageElement> clazz){
122
return getChildren(clazz, true);
125
protected ICStorageElement[] getChildren(boolean load){
126
return getChildren(XmlStorageElement.class, load);
129
protected ICStorageElement[] getChildren(Class<XmlStorageElement> clazz, boolean load){
133
ICStorageElement[] children = (ICStorageElement[])java.lang.reflect.Array.newInstance(
134
clazz, fChildList.size());
136
return fChildList.toArray(children);
139
public ICStorageElement getParent() {
140
return fParentRefAlowed ? fParent : null;
143
public String getAttribute(String name) {
144
if(isPropertyAlowed(name) && fElement.hasAttribute(name))
145
return fElement.getAttribute(name);
149
private boolean isPropertyAlowed(String name){
150
if(fAttributeFilters != null){
151
return checkString(name, fAttributeFilters);
156
private boolean isChildAlowed(String name){
157
if(fChildFilters != null){
158
return checkString(name, fChildFilters);
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]))
173
// protected void childRemoved(ICStorageElement child) {
174
// fChildList.remove(child);
177
protected void removed(){
178
// fElement.getParentNode().removeChild(fElement);
180
// if(fParent != null)
181
// ((XmlStorageElement)fParent).childRemoved(this);
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) {
197
fElement.removeChild(nextSibling);
200
fChildList.remove(el);
208
public void removeAttribute(String name) {
209
if(isPropertyAlowed(name))
210
fElement.removeAttribute(name);
213
public void setAttribute(String name, String value) {
214
if(isPropertyAlowed(name))
215
fElement.setAttribute(name, value);
221
ICStorageElement children[] = fChildList.toArray(new ICStorageElement[fChildList.size()]);
222
for(int i = 0; i < children.length; i++){
223
removeChild(children[i]);
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());
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);
241
public ICStorageElement createChild(String name,
242
boolean alowReferencingParent,
243
String[] attributeFilters,
244
String[] childFilters) {
245
if(!isChildAlowed(name))
247
Element childElement = fElement.getOwnerDocument().createElement(name);
248
fElement.appendChild(childElement);
249
return createAddChild(childElement, alowReferencingParent, attributeFilters, childFilters);
252
public String getName() {
253
return fElement.getNodeName();
256
public ICStorageElement createChild(String name) {
257
return createChild(name, true, null, null);
260
public String getValue() {
261
Text text = getTextChild();
263
return text.getData();
267
public void setValue(String value) {
268
Text text = getTextChild();
271
text = fElement.getOwnerDocument().createTextNode(value);
272
fElement.appendChild(text);
278
fElement.removeChild(text);
283
private Text getTextChild(){
284
NodeList nodes = fElement.getChildNodes();
286
for(int i = 0; i < nodes.getLength(); i++){
287
Node node = nodes.item(i);
288
if(node.getNodeType() == Node.TEXT_NODE){
297
public ICStorageElement importChild(ICStorageElement el) throws UnsupportedOperationException {
298
return addChild(el, true, null, null);
301
public ICStorageElement addChild(ICStorageElement el,
302
boolean alowReferencingParent,
303
String[] attributeFilters,
304
String[] childFilters) throws UnsupportedOperationException {
306
if(!isChildAlowed(el.getName()))
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);
317
xmlEl = (Element)xmlEl.cloneNode(true);
320
xmlEl = (Element)fElement.appendChild(xmlEl);
321
return createAddChild(xmlEl, alowReferencingParent, attributeFilters, childFilters);
323
throw new UnsupportedOperationException();
327
public String[] getAttributeFilters(){
328
if(fAttributeFilters != null)
329
return fAttributeFilters.clone();
330
return new String[0];
333
public String[] getChildFilters(){
334
if(fChildFilters != null)
335
return fChildFilters.clone();
336
return new String[0];
339
public boolean isParentRefAlowed(){
340
return fParentRefAlowed;
343
public boolean matches(ICStorageElement el){
344
if(!getName().equals(el.getName()))
347
if (!valuesMatch(getValue(), el.getValue()))
351
String[] attrs = getAttributeNames();
352
String[] otherAttrs = el.getAttributeNames();
353
if(attrs.length != otherAttrs.length)
356
if(attrs.length != 0){
357
Set<String> set = new HashSet<String>(Arrays.asList(attrs));
358
set.removeAll(Arrays.asList(otherAttrs));
362
for(int i = 0; i < attrs.length; i++){
363
if(!getAttribute(attrs[i]).equals(el.getAttribute(attrs[i])))
370
XmlStorageElement[] children = (XmlStorageElement[])getChildren();
371
ICStorageElement[] otherChildren = el.getChildren();
373
if(children.length != otherChildren.length)
376
if(children.length != 0){
377
for(int i = 0; i < children.length; i++){
378
if(!children[i].matches(otherChildren[i]))
386
private static boolean valuesMatch(String value, String other) {
388
return other == null || other.trim().length() == 0;
389
} else if (other == null) {
390
return value.trim().length() == 0;
392
return value.trim().equals(other.trim());
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))
406
return list.toArray(new String[list.size()]);
412
public ICStorageElement createCopy() throws UnsupportedOperationException, CoreException {
413
// todo Auto-generated method stub
420
public boolean equals(ICStorageElement other) {
421
// todo Auto-generated method stub
428
public ICStorageElement[] getChildrenByName(String name) {
429
// todo Auto-generated method stub
436
public boolean hasAttribute(String name) {
437
// todo Auto-generated method stub
444
public boolean hasChildren() {
445
// todo Auto-generated method stub