Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0449a81268b74980e55fd0cf32cc240b56a5c923 (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
/*******************************************************************************
 * 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.translators;

import java.util.Collections;
import java.util.List;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.wst.common.internal.emf.utilities.ExtendedEcoreUtil;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * Handles notifications from the DOM that a simple text value has changed.
 * 
 * @author Mike Poindexter
 */
class ValueUpdateAdapter extends TranslatorAdapter implements INodeAdapter {
	/**
     * 
     */
	private EObject modelObject;

	private EStructuralFeature feature;

	private List<Node> linkedWhitespaceNodes = Collections.emptyList();

	public ValueUpdateAdapter(SSESyncResource resource, Element node,
			EObject object, EStructuralFeature feature) {
		super(resource);
		this.node = node;
		this.modelObject = object;
		this.feature = feature;
	}

	public boolean isAdapterForType(Object type) {
		return ValueUpdateAdapter.class.equals(type);
	}

	public void notifyChanged(INodeNotifier notifier, int eventType,
			Object changedFeature, Object oldValue, Object newValue, int pos) {
		if (resource.isProcessEvents()) {
			try {
				resource.setProcessEvents(false);
				if (newValue instanceof Text) {
					changedFeature = newValue;
					newValue = ((Text) newValue).getData();
				}
				if (changedFeature instanceof Text) {
					if (null == newValue) {
						ExtendedEcoreUtil.eUnsetOrRemove(modelObject, feature,
								oldValue);
					} else {
						ExtendedEcoreUtil.eSetOrAdd(modelObject, feature,
								newValue.toString().trim());
					}
				}
			} finally {
				resource.setProcessEvents(true);
			}

		}

	}

	@Override
	public void load() {
		Object value = getElementText(node);
		if (feature instanceof EAttribute) {
			EAttribute ea = (EAttribute) feature;
			value = EcoreUtil.createFromString(ea.getEAttributeType(), value
					.toString());
		}

		modelObject.eSet(feature, value);
	}

	@Override
	public void save() {
		setElementTextValue(node, null, modelObject.eGet(feature));
	}

	@Override
	public void update(Object oldValue, Object newValue, int index) {
		setElementTextValue(node, oldValue, modelObject.eGet(feature));
	}

	/**
	 * Sets the text value of an existing node, attempting to preserve
	 * whitespace
	 * 
	 * @param element
	 * @param oldValue
	 * @param newValue
	 */
	private void setElementTextValue(Element element, Object oldValue,
			Object newValue) {
		newValue = newValue == null ? "" : newValue.toString(); //$NON-NLS-1$
		boolean replacedChild = false;

		if (oldValue != null) {
			// First try to find a text node with the old value and set it (to
			// preserve whitespace)
			NodeList children = element.getChildNodes();
			int nChildren = children.getLength();
			for (int i = 0; i < nChildren; i++) {
				Node child = children.item(i);
				if (child instanceof Text) {
					String value = ((Text) child).getData();
					int oldIdx = value.indexOf(oldValue.toString());
					if (oldIdx > -1) {
						String replacement = value.substring(0, oldIdx)
								+ newValue.toString()
								+ value.substring(oldIdx
										+ oldValue.toString().length());
						((Text) child).setData(replacement);
						replacedChild = true;
					}
				}
			}
		}

		// If for some reason we couldn't find a text to update, just clear the
		// element contents and put in our text.
		if (!replacedChild) {
			while (element.getFirstChild() != null) {
				element.removeChild(element.getFirstChild());
			}
			Text text = node.getOwnerDocument().createTextNode(
					newValue.toString());
			element.appendChild(text);
		}
	}

	public List<Node> getLinkedWhitespaceNodes() {
		return linkedWhitespaceNodes;
	}

	public void setLinkedWhitespaceNodes(List<Node> linkedWhitespaceNodes) {
		this.linkedWhitespaceNodes = linkedWhitespaceNodes;
	}
}

Back to the top