Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 065da65054e30da6d6ff54f9239b6032d6568d08 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.model.edit.pom.impl;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.m2e.model.edit.pom.Configuration;
import org.eclipse.m2e.model.edit.pom.PomPackage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * <!-- begin-user-doc --> An implementation of the model object '
 * <em><b>Configuration</b></em>'. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.eclipse.m2e.model.edit.pom.impl.ConfigurationImpl#getConfigurationNode <em>Configuration Node</em>}</li>
 * </ul>
 * </p>
 *
 * @generated NOT
 */
public class ConfigurationImpl extends EObjectImpl implements Configuration {
  private Node configurationNode;
	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	protected ConfigurationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return PomPackage.Literals.CONFIGURATION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Node getConfigurationNode() {
		return configurationNode;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setConfigurationNode(Node newConfigurationNode) {
	  this.configurationNode = newConfigurationNode;
	}


	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (configurationNode: "); //$NON-NLS-1$
		result.append(configurationNode);
		result.append(')');
		return result.toString();
	}
	
	public String getStringValue(String xpath) throws RuntimeException {
	  
    NodeList children = configurationNode.getChildNodes();
    for (int i=0; i<children.getLength(); i++) {
      Node current = children.item(i);
      if (current.getNodeName().equals(xpath)) {
        return children.item(i).getChildNodes().item(0).getNodeValue();
      }
    }
    return null;
  }
  
  public void setStringValue(String xpath, String value) throws RuntimeException {
    NodeList children = configurationNode.getChildNodes();
    boolean set = false;
    for (int i=0; i<children.getLength(); i++) {
      Node current = children.item(i);
      if (current.getNodeName().equals(xpath)) {
        while(current.getFirstChild() != null) {
          current.removeChild(current.getFirstChild());
        }
        current.appendChild(current.getOwnerDocument().createTextNode(value));
        set = true;
        break;
      }
    }
    if(!set) {
      Element e = configurationNode.getOwnerDocument().createElement(xpath);
      e.appendChild(configurationNode.getOwnerDocument().createTextNode(value));
      configurationNode.appendChild(e);
    }
  }
  
  public List<String> getListValue(String xpath) throws RuntimeException {
    NodeList children = configurationNode.getChildNodes();
    for (int i=0; i<children.getLength(); i++) {
      Node current = children.item(i);
      if (current.getNodeName().equals(xpath)) {
        NodeList items = current.getChildNodes();
        List<String> res = new ArrayList<String>(); 
        for (int j=0; j<items.getLength(); j++) {
          if (items.item(j).getNodeType() == Node.ELEMENT_NODE) {
            res.add(items.item(j).getChildNodes().item(0).getNodeValue());
          }
        }
        return res;
      }
    }
    return null;
  }

  public List<Node> getListNodes(String xpath) {
    return getListNodes(configurationNode, xpath);
  }

  public List<Node> getListNodes(Node node, String xpath) {
    NodeList children = node.getChildNodes();
    for (int i=0; i<children.getLength(); i++) {
      Node current = children.item(i);
      if (current.getNodeName().equals(xpath)) {
        List<Node> res = new ArrayList<Node>();
        NodeList items = current.getChildNodes();
        for (int j=0; j<items.getLength(); j++) {
          if (items.item(j).getNodeType() == Node.ELEMENT_NODE) {
            res.add(items.item(j).getChildNodes().item(0));
          }
        }
        return res;
      }
    }
    return null;
  }

  public Node getNode(String xpath) {
    return getNode(configurationNode, xpath);
  }
  
  public Node getNode(Node node, String xpath) {
    NodeList children = node.getChildNodes();
    for (int i=0; i<children.getLength(); i++) {
      Node current = children.item(i);
      if (current.getNodeName().equals(xpath)) {
        return current;
      }
    }
    return null;
  }
  
  public void setNodeValues(String xpath, String names[], String[] values) {
    setNodeValues(configurationNode, xpath, names, values);
  }

  public void setNodeValues(String xpath, String name, String[] values) {
    String[] names = new String[values.length];
    for (int i=0; i<names.length; i++) {
      names[i] = name;
    }
    setNodeValues(xpath, names, values);
  }

  public Node createNode(String xpath) {
    Node element = configurationNode.getOwnerDocument().createElement(xpath);
    configurationNode.appendChild(element);
    return element;
  }

  public void removeNode(String xpath) {
    configurationNode.removeChild(getNode(xpath));
  }

  public void setNodeValues(Node node, String xpath, String[] names, String[] values) {
    Node parent = getNode(node, xpath);

    if (parent == null) {
      //create node
      parent = node.getOwnerDocument().createElement(xpath);
      node.appendChild(parent);
    }
    
    List<Node> nodes = getListNodes(node, xpath);
    
    //append missing nodes if required
    int diff = values.length - nodes.size();
    for (int i=0; i<diff; i++) {
      Node element = parent.getOwnerDocument().createElement(names[i]);
      parent.appendChild(element);
      Text text = parent.getOwnerDocument().createTextNode(""); //$NON-NLS-1$
      element.appendChild(text);
      nodes.add(text);
    }

    //remove extra nodes if required
    for (int i=0; i<-diff; i++) {
      Node element = nodes.remove(nodes.size() - 1 - i);
      parent.removeChild(element.getParentNode());
    }
    
    //set values
    for (int i=0; i<nodes.size(); i++) {
      nodes.get(i).setNodeValue(values[i]);
    }
  }

  public void doNotify(int eventType, Object changedFeature, Object oldValue, Object newValue) {
    // A catch-all notificator. 
    // The configuration section can differ with every plugin, so we cannot really have a
    // static EMF model. So we'll just notify the subscribers and let them act accordingly.
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, eventType, PomPackage.CONFIGURATION, oldValue, newValue));
  }
} // ConfigurationImpl

Back to the top