result = new AtomicReference<>();
+ Display.getDefault().syncExec(() -> result.set(supplier.get()));
+ return result.get();
+ }
+
+ /**
+ * Waits until the condition holds.
+ *
+ * @param condition
+ * the condition to wait for
+ * @return {@code false} if that did not happen before the timeout
+ */
+ private boolean waitFor(final BooleanSupplier condition) throws InterruptedException {
+ long deadline = System.currentTimeMillis() + TIMEOUT_MILLIS;
+ while (!condition.getAsBoolean()) {
+ if (System.currentTimeMillis() >= deadline) {
+ return false;
+ }
+ Thread.sleep(POLL_MILLIS);
+ }
+ return true;
+ }
+
+ private void assertAutoBuildSettles() throws Exception {
+ boolean settled = waitForAutoBuild();
+ int builds = autoBuilds.get();
+ assertTrue(settled, "auto-build settles within " + TIMEOUT_MILLIS + " ms; runs of the auto-build job so far: " + builds);
+ assertTrue(builds <= MAX_AUTO_BUILDS, "at most " + MAX_AUTO_BUILDS + " runs of the auto-build job: " + builds);
+ }
+
+ private void assertHasExtensions(final String catalog) throws Exception {
+ String pluginXml = getContents(pluginXml());
+ assertTrue(pluginXml.contains(catalogReference(catalog)), "plugin.xml has the extensions of " + catalog + ":\n" + pluginXml);
+ }
+
+ /**
+ * Waits until no auto-build has been pending for the quiet period.
+ *
+ * @return {@code false} if that did not happen before the timeout
+ */
+ private boolean waitForAutoBuild() throws InterruptedException {
+ long deadline = System.currentTimeMillis() + TIMEOUT_MILLIS;
+ long quietSince = System.currentTimeMillis();
+ int seenBuilds = autoBuilds.get();
+ while (System.currentTimeMillis() < deadline) {
+ Job[] pending = Job.getJobManager().find(ResourcesPlugin.FAMILY_AUTO_BUILD);
+ if (pending.length > 0 || autoBuilds.get() != seenBuilds) {
+ for (Job job : pending) {
+ job.join(Math.max(1, deadline - System.currentTimeMillis()), null);
+ }
+ seenBuilds = autoBuilds.get();
+ quietSince = System.currentTimeMillis();
+ } else if (System.currentTimeMillis() - quietSince >= QUIET_MILLIS) {
+ return true;
+ } else {
+ Thread.sleep(POLL_MILLIS);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * The end of the attribute value with which an extension in {@code plugin.xml} refers to the given catalog.
+ *
+ * @param catalog
+ * the name of the catalog
+ * @return the text to look for
+ */
+ private static String catalogReference(final String catalog) {
+ return "/" + catalog + ".check\"";
+ }
+
+ private static String catalogPath(final String catalog) {
+ return "src/autobuild/" + catalog + ".check";
+ }
+
+ private static String catalogSource(final String catalog, final boolean withGrammar) {
+ return String.format(CATALOG, catalog, withGrammar ? FOR_GRAMMAR : "", String.format(CHECK, catalog, catalog, catalog));
+ }
+
+ private static IProject project() {
+ return ResourcesPlugin.getWorkspace().getRoot().getProject(PluginTestProjectManager.TEST_PROJECT_NAME);
+ }
+
+ private static IFile pluginXml() {
+ return project().getFile(PLUGIN_XML);
+ }
+
+ private static int count(final String text, final String part) {
+ int count = 0;
+ for (int i = text.indexOf(part); i >= 0; i = text.indexOf(part, i + 1)) {
+ count++;
+ }
+ return count;
+ }
+}
diff --git a/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java
index 0805628c6..c91d67e6d 100644
--- a/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java
+++ b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/CheckExtensionGenerator.java
@@ -34,7 +34,6 @@
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EValidator;
-import org.eclipse.pde.core.IBaseModel;
import org.eclipse.pde.core.plugin.IFragmentModel;
import org.eclipse.pde.core.plugin.IPlugin;
import org.eclipse.pde.core.plugin.IPluginAttribute;
@@ -54,10 +53,6 @@
import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.eclipse.pde.internal.ui.PDEPlugin;
-import org.eclipse.pde.internal.ui.util.ModelModification;
-import org.eclipse.pde.internal.ui.util.PDEModelUtility;
-import org.eclipse.swt.SWTException;
-import org.eclipse.ui.PlatformUI;
import org.eclipse.xtext.builder.IXtextBuilderParticipant.IBuildContext;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IResourceDescription.Delta;
@@ -342,17 +337,20 @@ void changePluginXmlFile(final IBuildContext context, final Delta delta, final I
}
IFile file = getPluginFile(uri);
- if (!validPluginFile(file)) {
- createNewFile(catalog, file, monitor);
- } else {
- if (!catalogValidates(catalog)) {
- removeExtensions(file, catalog, monitor);
+ try {
+ if (!validPluginFile(file)) {
+ createNewFile(catalog, file, monitor);
} else {
- updateExtensions(file, catalog, monitor);
- // TODO improve by only updating the manifest if the validator extension has changed..
+ if (!catalogValidates(catalog)) {
+ removeExtensions(file, catalog, monitor);
+ } else {
+ updateExtensions(file, catalog, monitor);
+ // TODO improve by only updating the manifest if the validator extension has changed..
+ }
}
+ } finally {
+ mergeManifest(catalog, monitor); // also when the plug-in file could not be edited
}
- mergeManifest(catalog, monitor);
}
/**
@@ -405,24 +403,12 @@ private void updateExtensions(final IFile pluginXmlFile, final CheckCatalog cata
manager.sortAllExtensions(pluginModel, monitor);
ModelSaver.saveIfChanged(pluginModel);
} else {
- final ModelModification modification = new ModelModification(pluginXmlFile) {
- @Override
- protected void modifyModel(final IBaseModel model, final IProgressMonitor monitor) throws CoreException {// NOPMD NPath complexity
- if (!(model instanceof IPluginModelBase)) {
- return;
- }
- IPluginModelBase pluginModel = (IPluginModelBase) model;
- manager.updateExtensions(catalog, pluginModel, monitor);
- manager.addExtensions(catalog, pluginModel, monitor);
- manager.sortAllExtensions(pluginModel, monitor);
- }
-
- @Override
- public boolean saveOpenEditor() {
- return false; // do nothing with any open editor...
- }
- };
- modifyModel(modification, monitor);
+ checkCanceled(monitor);
+ PluginXmlEditor.modify(pluginXmlFile, pluginModel -> {
+ manager.updateExtensions(catalog, pluginModel, monitor);
+ manager.addExtensions(catalog, pluginModel, monitor);
+ manager.sortAllExtensions(pluginModel, monitor);
+ });
}
}
@@ -460,26 +446,14 @@ private void removeExtensions(final IFile file, final Object object, final IProg
}
ModelSaver.saveIfChanged(pluginModel);
} else {
- final ModelModification mod = new ModelModification(file) {
- @Override
- protected void modifyModel(final IBaseModel model, final IProgressMonitor monitor) throws CoreException {
- if (!(model instanceof IPluginModelBase)) {
- return;
- }
- IPluginModelBase pluginModel = (IPluginModelBase) model;
- if (object instanceof IEObjectDescription) {
- manager.removeExtensions((IEObjectDescription) object, pluginModel, monitor); // called when catalog is deleted
- } else if (object instanceof CheckCatalog) {
- manager.removeExtensions((CheckCatalog) object, pluginModel, monitor); // called in order to remove extensions in an invalid model
- }
+ checkCanceled(monitor);
+ PluginXmlEditor.modify(file, pluginModel -> {
+ if (object instanceof IEObjectDescription) {
+ manager.removeExtensions((IEObjectDescription) object, pluginModel, monitor); // called when catalog is deleted
+ } else if (object instanceof CheckCatalog) {
+ manager.removeExtensions((CheckCatalog) object, pluginModel, monitor); // called in order to remove extensions in an invalid model
}
-
- @Override
- public boolean saveOpenEditor() {
- return false; // prevent modifications to open plugin manifest editor; the editor will become dirty once the modification is performed
- }
- };
- modifyModel(mod, monitor);
+ });
}
}
@@ -518,34 +492,17 @@ private void mergeManifest(final CheckCatalog catalog, final IProgressMonitor mo
}
/**
- * Performs modifications to the Plugin Model.
+ * Stops the build before the {@code plugin.xml} file is edited if it was canceled.
*
- * @param modification
- * a modification
* @param monitor
* progress monitor
* @throws OperationCanceledException
* if the operation was canceled
*/
- void modifyModel(final ModelModification modification, final IProgressMonitor monitor) {
+ private static void checkCanceled(final IProgressMonitor monitor) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
- try {
- PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
- @Override
- public void run() {
- PDEModelUtility.modifyModel(modification, monitor);
- }
- });
- } catch (SWTException e) {
- // If the build was cancelled while in syncExec() it will throw an SWTException
- if (monitor.isCanceled()) {
- throw new OperationCanceledException(); // NOPMD PreserveStackTrace - SWTException is just the cancellation signal
- } else {
- throw e;
- }
- }
}
/**
diff --git a/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/PluginXmlEditor.java b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/PluginXmlEditor.java
new file mode 100644
index 000000000..eb2eac989
--- /dev/null
+++ b/com.avaloq.tools.ddk.check.ui/src/com/avaloq/tools/ddk/check/ui/builder/PluginXmlEditor.java
@@ -0,0 +1,165 @@
+/*******************************************************************************
+ * Copyright (c) 2026 Avaloq Group AG 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:
+ * Avaloq Group AG - initial API and implementation
+ *******************************************************************************/
+package com.avaloq.tools.ddk.check.ui.builder;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.pde.core.plugin.IPluginModelBase;
+import org.eclipse.pde.internal.core.text.plugin.PluginModel;
+import org.eclipse.pde.internal.core.text.plugin.XMLTextChangeListener;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.TextEdit;
+
+import com.avaloq.tools.ddk.check.ui.internal.Activator;
+
+
+/**
+ * Edits a {@code plugin.xml} file on the calling thread.
+ *
+ * The changes are made to PDE's text model of the file and applied as text edits, as {@code PDEModelUtility} does when no editor is open, so the parts of
+ * the file that do not change keep their formatting and comments. The edits are applied to a private copy of the content and not to the shared file buffer,
+ * so this needs neither the UI thread nor a workbench and can run inside a build. An editor that has the file open sees the write as any other change on
+ * disk.
+ *
+ */
+@SuppressWarnings("restriction")
+final class PluginXmlEditor {
+
+ /**
+ * A change to the plug-in model of a {@code plugin.xml} file.
+ */
+ @FunctionalInterface
+ interface Modification {
+ /**
+ * Changes the given model.
+ *
+ * @param model
+ * the model to change
+ * @throws CoreException
+ * if the model cannot be changed
+ */
+ void apply(IPluginModelBase model) throws CoreException;
+ }
+
+ private PluginXmlEditor() {
+ // static helpers only
+ }
+
+ /**
+ * Applies a modification to the given {@code plugin.xml} file and writes the file if that changes its content.
+ *
+ * @param file
+ * the {@code plugin.xml} file, must exist
+ * @param modification
+ * the modification to apply
+ * @return {@code true} if the file was written
+ * @throws CoreException
+ * if the file cannot be read, parsed, edited or written
+ */
+ static boolean modify(final IFile file, final Modification modification) throws CoreException {
+ final Charset charset = getCharset(file);
+ final String original = read(file, charset);
+ final IDocument document = new Document(original);
+ final PluginModel model = new PluginModel(document, true);
+ try {
+ model.setUnderlyingResource(file);
+ model.setCharset(charset);
+ model.load();
+ if (!model.isLoaded()) {
+ throw new CoreException(error("Could not parse " + file.getFullPath(), null)); //$NON-NLS-1$
+ }
+ final XMLTextChangeListener listener = new XMLTextChangeListener(document);
+ model.addModelChangedListener(listener);
+
+ modification.apply(model);
+
+ final TextEdit[] edits = listener.getTextOperations();
+ if (edits.length == 0) {
+ return false;
+ }
+ final MultiTextEdit edit = new MultiTextEdit();
+ edit.addChildren(edits);
+ edit.apply(document);
+ } catch (MalformedTreeException | BadLocationException e) {
+ throw new CoreException(error("Could not edit " + file.getFullPath(), e)); //$NON-NLS-1$
+ } finally {
+ model.dispose();
+ }
+ final String changed = document.get();
+ if (changed.equals(original)) {
+ return false;
+ }
+ file.setContents(new ByteArrayInputStream(encode(changed, charset, file)), IResource.FORCE | IResource.KEEP_HISTORY, null);
+ return true;
+ }
+
+ /**
+ * Encodes the content, and fails instead of replacing a character that the charset of the file cannot represent.
+ *
+ * @param content
+ * the content to encode
+ * @param charset
+ * the charset of the file
+ * @param file
+ * the file, for the error message
+ * @return the encoded content
+ * @throws CoreException
+ * if the charset cannot represent the content
+ */
+ private static byte[] encode(final String content, final Charset charset, final IFile file) throws CoreException {
+ try {
+ ByteBuffer bytes = charset.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT).encode(CharBuffer.wrap(content));
+ byte[] result = new byte[bytes.remaining()];
+ bytes.get(result);
+ return result;
+ } catch (CharacterCodingException e) {
+ throw new CoreException(error("Could not encode " + file.getFullPath() + " as " + charset, e)); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ }
+
+ private static Charset getCharset(final IFile file) throws CoreException {
+ String name = file.getCharset();
+ try {
+ return Charset.forName(name);
+ } catch (IllegalArgumentException e) { // an illegal or unsupported charset name
+ throw new CoreException(error("Unknown charset " + name + " of " + file.getFullPath(), e)); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ }
+
+ private static String read(final IFile file, final Charset charset) throws CoreException {
+ try (InputStream stream = file.getContents(true)) {
+ return new String(stream.readAllBytes(), charset);
+ } catch (IOException e) {
+ throw new CoreException(error("Could not read " + file.getFullPath(), e)); //$NON-NLS-1$
+ }
+ }
+
+ private static IStatus error(final String message, final Throwable cause) {
+ return new Status(IStatus.ERROR, Activator.getPluginId(), IStatus.ERROR, message, cause);
+ }
+
+}