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
|
/*******************************************************************************
* Copyright (c) 2004, 2005 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.ws.internal.explorer.platform.engine.transformer;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.eclipse.wst.ws.internal.datamodel.Element;
import org.eclipse.wst.ws.internal.datamodel.Rel;
import org.eclipse.wst.ws.internal.explorer.platform.constants.ActionInputs;
import org.eclipse.wst.ws.internal.explorer.platform.constants.ModelConstants;
import org.eclipse.wst.ws.internal.explorer.platform.datamodel.TreeElement;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Controller;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Node;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.NodeManager;
public class NodeIdTransformer implements ITransformer
{
protected Controller controller;
public NodeIdTransformer(Controller controller)
{
this.controller = controller;
}
public Hashtable normalize(Hashtable properties)
{
Vector rels = new Vector();
try
{
int nodeId = Integer.parseInt((String)properties.get(ActionInputs.NODEID));
NodeManager nodeManager = controller.getCurrentPerspective().getNodeManager();
Node rootNode = nodeManager.getRootNode();
Node node = nodeManager.getNode(nodeId);
int depth = 0;
if (node != null)
depth = node.getNodeDepth();
while (rels.size() < depth && node != null && node != rootNode)
{
Node parentNode = node.getParent();
Element element = node.getTreeElement();
Rel rel = getRel(parentNode.getTreeElement(), element);
StringBuffer relValue = new StringBuffer(rel.getName());
relValue.append(ModelConstants.REL_LOCALNAME_SEPARATOR);
relValue.append(element.getName());
rels.insertElementAt(relValue.toString(), 0);
node = parentNode;
}
}
catch (NumberFormatException nfe)
{
}
int numRelationships = rels.size();
if (numRelationships == 1)
properties.put(ModelConstants.REL_ID, rels.get(0).toString());
else if (numRelationships > 1)
properties.put(ModelConstants.REL_ID, (String[])rels.toArray(new String[0]));
return properties;
}
private Rel getRel(Element sourceElement, Element targetElement)
{
Enumeration rels = sourceElement.getRels();
while (rels.hasMoreElements())
{
Rel rel = (Rel)rels.nextElement();
Enumeration targetElements = rel.getTargetElements();
while (targetElements.hasMoreElements())
{
if (targetElements.nextElement() == targetElement)
return rel;
}
}
return null;
}
public Hashtable deNormalize(Hashtable properties)
{
NodeManager nodeManager = controller.getCurrentPerspective().getNodeManager();
Node node = nodeManager.getRootNode();
Object value = properties.get(ModelConstants.REL_ID);
String[] relationships = null;
if (value instanceof String[])
relationships = (String[])value;
else if (value != null)
relationships = new String[] {value.toString()};
if (relationships != null)
{
for (int i = 0; i < relationships.length; i++)
{
int index = relationships[i].indexOf(ModelConstants.REL_LOCALNAME_SEPARATOR);
if (index == -1)
return deNormalizeAsSelectedNode(properties);
String rel = (index != -1) ? relationships[i].substring(0, index) : relationships[i];
String localname = (index != -1) ? relationships[i].substring(index + 1, relationships[i].length()) : null;
if (localname == null)
return deNormalizeAsSelectedNode(properties);
node = getNode(node, rel, localname);
if (node == null)
return deNormalizeAsSelectedNode(properties);
}
properties.put(ActionInputs.NODEID, String.valueOf(node.getNodeId()));
return properties;
}
else
return deNormalizeAsSelectedNode(properties);
}
private Hashtable deNormalizeAsSelectedNode(Hashtable properties)
{
return (new CurrentNodeSelectionTransformer(controller)).deNormalize(properties);
}
private Node getNode(Node parent, String rel, String localname)
{
Element parentElement = parent.getTreeElement();
Enumeration e = parentElement.getElements(rel);
while (e.hasMoreElements())
{
Element element = (Element)e.nextElement();
if (localname.equals(element.getName()))
return parent.getChildNode((TreeElement)element);
}
return null;
}
}
|