iterate(new DefaultIterator() {
@Override
public void execute2(String filename, Document doc) throws Exception {
// will do both links and nodes
IterableElementList nodes = xpath(doc, "/GenEditorGenerator/diagram/topLevelNodes");
nodes.addAll(xpath(doc, "/GenEditorGenerator/diagram/links"));
assertFalse(filename + ": No nodes found", nodes.isEmpty());
boolean changed = false;
for (Element node : nodes) {
// find the meta element for this node
Element meta = xpathFirst(node, "modelFacet/metaClass");
// iaml.genmodel#//model/CompositeOperation
String metaHref = meta.getAttribute("href");
// CompositeOperation
metaHref = metaHref.substring(metaHref.lastIndexOf("/") + 1);
EClass metaElement = resolveSimpleEClass(metaHref);
try {
RootElementPair pair = getBestPair(rootElements, metaElement);
if (pair == null)
continue; // can't open this element
// there must be a behaviour for this meta element!
changed |= checkOrAddOpenBehaviour(doc, node, pair);
// check readonly labels contained within
IterableElementList labels = xpath(node, "labels[@readOnly='true']");
for (Element label : labels) {
try {
changed |= checkOrAddOpenBehaviour(doc, label, pair);
} catch (Throwable t) {
String labelName = label.getAttribute("editPartClassName");
throw new RuntimeException("Label " + labelName + ": " + t.getMessage(), t);
}
}
} catch (Throwable t) {
throw new RuntimeException(node.getAttribute("editPartClassName") + ": " + t.getMessage(), t);
}
}
if (changed) {
saveDocument(doc, filename);
}
}
/**
* Checks the given node (can either be a /node, /link or /label)
* that it contains the given OpenDiagramBehaviour. Adds it if it
* doesn't exist, otherwise check that it matches what it should be.
*
* @param doc The root document
* @param node The node to check for behaviour containment
* @param pair The RootElementPair pointing to the editor it should open
* @return true if the document has been modified, false otherwise
* @throws XPathExpressionException
*/
private boolean checkOrAddOpenBehaviour(Document doc, Element node, RootElementPair pair) throws XPathExpressionException {
IterableElementList beh = xpath(node, "behaviour");
assertFalse("Too many behaviours for " + node, beh.size() > 1);
String policyName = camelCase(getName(pair.getFilename())) + "OpenDiagramEditPolicy";
String diagramKind = pair.getModelID();
String editorId = getPluginName(pair.getFilename()) + ".part.IamlDiagramEditorID";
if (beh.size() == 1) {
// check it is correct
Element b = beh.item(0);
assertEquals("gmfgen:OpenDiagramBehaviour", b.getAttribute("xsi:type"));
assertEquals(policyName, b.getAttribute("editPolicyClassName"));
assertEquals(diagramKind, b.getAttribute("diagramKind"));
assertEquals(editorId, b.getAttribute("editorID"));
} else if (beh.size() == 0) {
// there is none; insert one
log("Adding new behaviour " + policyName);
Element behaviour = doc.createElement("behaviour");
behaviour.setAttribute("xsi:type", "gmfgen:OpenDiagramBehaviour");