diff options
author | Milos Kleint | 2011-02-21 08:16:30 +0000 |
---|---|---|
committer | Milos Kleint | 2011-02-21 08:16:30 +0000 |
commit | 11413b73a1a99a31a64a0a09cbc5a4c847be8d89 (patch) | |
tree | aaf44cc020bbeccde1ab24d26b3a12f12c103a7a | |
parent | 6ebbf4907dd0b0c5d110f7dda28cae363f214adc (diff) | |
download | m2e-core-11413b73a1a99a31a64a0a09cbc5a4c847be8d89.tar.gz m2e-core-11413b73a1a99a31a64a0a09cbc5a4c847be8d89.tar.xz m2e-core-11413b73a1a99a31a64a0a09cbc5a4c847be8d89.zip |
refactoring, introduce constants for elements, move some methods to PomHelper
12 files changed, 157 insertions, 179 deletions
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddDependencyAction.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddDependencyAction.java index 3486fe5d..140172fe 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddDependencyAction.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddDependencyAction.java @@ -28,6 +28,7 @@ import org.eclipse.m2e.core.index.IndexedArtifactFile; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.ui.internal.Messages; import org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog; +import org.eclipse.m2e.core.ui.internal.editing.PomHelper; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IWorkbenchWindow; @@ -80,7 +81,7 @@ public class AddDependencyAction extends MavenActionSupport implements IWorkbenc childEquals("groupId", dependency.getGroupId()), //$NON-NLS-1$ childEquals("artifactId", dependency.getArtifactId()));//$NON-NLS-1$ if (dep == null) { - dep = createDependency(depsEl, dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); + dep = PomHelper.createDependency(depsEl, dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()); } else { //only set version if already exists if (dependency.getVersion() != null) { diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddPluginAction.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddPluginAction.java index bf44cbcf..96c079ff 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddPluginAction.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/AddPluginAction.java @@ -24,6 +24,7 @@ import org.eclipse.m2e.core.index.IndexedArtifactFile; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.ui.internal.Messages; import org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog; +import org.eclipse.m2e.core.ui.internal.editing.PomHelper; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.slf4j.Logger; @@ -62,7 +63,7 @@ public class AddPluginAction extends MavenActionSupport implements IWorkbenchWin performOnDOMDocument(new OperationTuple(file, new Operation() { public void process(Document document) { Element pluginsEl = getChild(document.getDocumentElement(), "build", "plugins"); - createPlugin(pluginsEl, indexedArtifactFile.group, indexedArtifactFile.artifact, indexedArtifactFile.version); + PomHelper.createPlugin(pluginsEl, indexedArtifactFile.group, indexedArtifactFile.artifact, indexedArtifactFile.version); } })); } catch(Exception ex) { diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java index 9eb75d9b..ea74cb1e 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java @@ -10,7 +10,7 @@ package org.eclipse.m2e.core.ui.internal.editing; import org.apache.maven.model.Dependency; import org.eclipse.m2e.core.embedder.ArtifactKey; -import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -32,14 +32,16 @@ public class AddExclusionOperation implements Operation { Element depElement = PomHelper.findDependency(document, dependency); if(depElement == null) { + //TODO we shall not throw exceptions from operations.. throw new IllegalArgumentException("Dependency does not exist in this pom"); } - Element exclusionsElement = PomEdits.getChild(depElement, PomHelper.EXCLUSIONS); + Element exclusionsElement = getChild(depElement, EXCLUSIONS); - Element exclusionElement = PomEdits.createElement(exclusionsElement, PomHelper.EXCLUSION); + Element exclusionElement = createElement(exclusionsElement, EXCLUSION); - PomEdits.createElementWithText(exclusionElement, PomHelper.ARTIFACT_ID, exclusion.getArtifactId()); - PomEdits.createElementWithText(exclusionElement, PomHelper.GROUP_ID, exclusion.getGroupId()); - PomEdits.createElementWithText(exclusionElement, PomHelper.VERSION, exclusion.getVersion()); + createElementWithText(exclusionElement, ARTIFACT_ID, exclusion.getArtifactId()); + createElementWithText(exclusionElement, GROUP_ID, exclusion.getGroupId()); + //TODO mkleint: are there really exclusion versions?? + createElementWithText(exclusionElement, VERSION, exclusion.getVersion()); } } diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java index 46d829d1..7b3b6a3e 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java @@ -40,6 +40,22 @@ import org.w3c.dom.Text; * */ public class PomEdits { + + public static final String DEPENDENCIES = "dependencies"; //$NON-NLS-1$ + public static final String GROUP_ID = "groupId";//$NON-NLS-1$ + public static final String ARTIFACT_ID = "artifactId"; //$NON-NLS-1$ + public static final String DEPENDENCY = "dependency"; //$NON-NLS-1$ + public static final String DEPENDENCY_MANAGEMENT = "dependencyManagement"; //$NON-NLS-1$ + public static final String EXCLUSIONS = "exclusions"; //$NON-NLS-1$ + public static final String EXCLUSION = "exclusion"; //$NON-NLS-1$ + public static final String VERSION = "version"; //$NON-NLS-1$ + public static final String PLUGIN = "plugin"; //$NON-NLS-1$ + public static final String CONFIGURATION = "configuration";//$NON-NLS-1$ + public static final String PLUGINS = "plugins";//$NON-NLS-1$ + public static final String PLUGIN_MANAGEMENT = "pluginManagement";//$NON-NLS-1$ + public static final String BUILD = "build";//$NON-NLS-1$ + public static final String PARENT = "parent";//$NON-NLS-1$ + public static final String RELATIVE_PATH = "relativePath";//$NON-NLS-1$ public static Element findChild(Element parent, String name) { @@ -111,76 +127,8 @@ public class PomEdits { } /** - * node is expected to be the node containing <dependencies> node, so <project>, <dependencyManagement> etc.. - * @param node - * @return - */ - public static List<Element> findDependencies(Element node) { - return findChilds(findChild(node, "dependencies"), "dependency"); - } - - /** for the root <project> node (or equivalent) finds or creates the <dm> and <dependencies> sections. - * returns the <dependencies> section element. - * - * @param root - * @return - */ - public static Element getManagedDependencies(Element root) { - Element toRet = getChild(root, "dependencyManagement"); - toRet = getChild(toRet, "dependencies"); - return toRet; - } - - /** - * creates and adds new dependency to the parent. - * @param parentList - * @param groupId null or value - * @param artifactId never null - * @param version null or value - * @return - */ - public static Element createDependency(Element parentList, String groupId, String artifactId, String version) { - Document doc = parentList.getOwnerDocument(); - Element dep = doc.createElement("dependency"); - parentList.appendChild(dep); - - if (groupId != null) { - createElementWithText(dep, "groupId", groupId); - } - createElementWithText(dep, "artifactId", artifactId); - if (version != null) { - createElementWithText(dep, "version", version); - } - format(dep); - return dep; - } - - /** - * creates and adds new plugin to the parent. Formats the result. - * @param parentList - * @param groupId null or value - * @param artifactId never null - * @param version null or value - * @return - */ - public static Element createPlugin(Element parentList, String groupId, String artifactId, String version) { - Document doc = parentList.getOwnerDocument(); - Element plug = doc.createElement("plugin"); - parentList.appendChild(plug); - - if (groupId != null) { - createElementWithText(plug, "groupId", groupId); - } - createElementWithText(plug, "artifactId", artifactId); - if (version != null) { - createElementWithText(plug, "version", version); - } - format(plug); - return plug; - } - - /** * helper method, creates a subelement with text embedded. does not format the result. + * primarily to be used in cases like <code><goals><goal>xxx</goal></goals></code> * @param parent * @param name * @param value @@ -195,7 +143,7 @@ public class PomEdits { } /** - * helper method, creates a subelement + * helper method, creates a subelement, does not format result. * * @param parent the parent element * @param name the name of the new element diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java index 12bb6fe7..5812013a 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java @@ -8,9 +8,9 @@ package org.eclipse.m2e.core.ui.internal.editing; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.childEquals; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; + +import java.util.List; import org.apache.maven.model.Dependency; import org.eclipse.core.resources.IFile; @@ -33,24 +33,14 @@ import org.w3c.dom.Element; public final class PomHelper { - private static final Logger LOG = LoggerFactory.getLogger(PomHelper.class); - - public static final String DEPENDENCIES = "dependencies"; //$NON-NLS-1$ - - public static final String GROUP_ID = "groupId";//$NON-NLS-1$ - - public static final String ARTIFACT_ID = "artifactId"; //$NON-NLS-1$ - - public static final String DEPENDENCY = "dependency"; //$NON-NLS-1$ - public static final String EXCLUSIONS = "exclusions"; //$NON-NLS-1$ - - public static final String EXCLUSION = "exclusion"; //$NON-NLS-1$ + private static final Logger LOG = LoggerFactory.getLogger(PomHelper.class); - public static final String VERSION = "version"; //$NON-NLS-1$ /* - * Return the Element matching the dependency or null. + * Return the Element matching the dependency or null, + * PLEASE NOTE: the dependency values are resolved, while the xml content is not, which makes the method + * not as reliable as the signature suggests */ public static Element findDependency(Document document, Dependency dependency) { Element dependenciesElement = findChild(document.getDocumentElement(), DEPENDENCIES); @@ -81,4 +71,59 @@ public final class PomHelper { } } } + + /** + * creates and adds new plugin to the parent. Formats the result. + * @param parentList + * @param groupId null or value + * @param artifactId never null + * @param version null or value + * @return + */ + public static Element createPlugin(Element parentList, String groupId, String artifactId, String version) { + Document doc = parentList.getOwnerDocument(); + Element plug = doc.createElement(PLUGIN); + parentList.appendChild(plug); + + if (groupId != null) { + createElementWithText(plug, GROUP_ID, groupId); + } + createElementWithText(plug, ARTIFACT_ID, artifactId); + if (version != null) { + createElementWithText(plug, VERSION, version); + } + format(plug); + return plug; + } + + /** + * creates and adds new dependency to the parent. formats the result. + * @param parentList + * @param groupId null or value + * @param artifactId never null + * @param version null or value + * @return + */ + public static Element createDependency(Element parentList, String groupId, String artifactId, String version) { + Element dep = createElement(parentList, DEPENDENCY); + + if (groupId != null) { + createElementWithText(dep, GROUP_ID, groupId); + } + createElementWithText(dep, ARTIFACT_ID, artifactId); + if (version != null) { + createElementWithText(dep, VERSION, version); + } + format(dep); + return dep; + } + + /** + * node is expected to be the node containing <dependencies> node, so <project>, <dependencyManagement> etc.. + * @param node + * @return + */ + public static List<Element> findDependencies(Element node) { + return findChilds(findChild(node, "dependencies"), "dependency"); + } } diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java index 9ca8eabb..45711d9d 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java @@ -9,7 +9,7 @@ package org.eclipse.m2e.core.ui.internal.editing; import org.apache.maven.model.Dependency; -import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -27,14 +27,15 @@ public class RemoveDependencyOperation implements Operation { public void process(Document document) { Element dependencyElement = PomHelper.findDependency(document, dependency); if(dependencyElement == null) { + //TODO we shall not throw exceptions from operations.. throw new IllegalArgumentException("Dependency does not exist in pom"); } - Element dependencies = PomEdits.findChild(document.getDocumentElement(), PomHelper.DEPENDENCIES); - PomEdits.removeChild(dependencies, dependencyElement); + Element dependencies = findChild(document.getDocumentElement(), DEPENDENCIES); + removeChild(dependencies, dependencyElement); // Remove dependencies element if it is empty - if(PomEdits.findDependencies(document.getDocumentElement()).isEmpty()) { - PomEdits.removeChild(document.getDocumentElement(), dependencies); + if(PomHelper.findDependencies(document.getDocumentElement()).isEmpty()) { + removeChild(document.getDocumentElement(), dependencies); } } } diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertArtifactProposal.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertArtifactProposal.java index ab812e06..9a356077 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertArtifactProposal.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/InsertArtifactProposal.java @@ -11,13 +11,7 @@ package org.eclipse.m2e.editor.xml; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.elementAtOffset; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.format; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.insertAt; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.setText; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; import java.io.IOException; @@ -107,13 +101,13 @@ public class InsertArtifactProposal implements ICompletionProposal, ICompletionP final int fOffset = offset; performOnDOMDocument(new OperationTuple(document, new Operation() { public void process(Document doc) { - Element parent = insertAt(doc.createElement("parent"), fOffset); - setText(getChild(parent, "groupId"), af.group); - setText(getChild(parent, "artifactId"), af.artifact); - setText(getChild(parent, "version"), af.version); + Element parent = insertAt(doc.createElement(PARENT), fOffset); + setText(getChild(parent, GROUP_ID), af.group); + setText(getChild(parent, ARTIFACT_ID), af.artifact); + setText(getChild(parent, VERSION), af.version); String relativePath = PomContentAssistProcessor.findRelativePath(sourceViewer, af.group, af.artifact, af.version); if (relativePath != null) { - setText(getChild(parent, "relativePath"), relativePath); + setText(getChild(parent, RELATIVE_PATH), relativePath); } format(parent); generatedOffset = ((IndexedRegion)parent).getStartOffset(); @@ -142,14 +136,12 @@ public class InsertArtifactProposal implements ICompletionProposal, ICompletionP Element plugin = null; Element toFormat = null; if("project".equals(currentName)) { //$NON-NLS-1$ - Element build = findChild(currentNode, "build"); + Element build = findChild(currentNode, BUILD); if(build == null) { - build = insertAt(doc.createElement("build"), fOffset); + build = insertAt(doc.createElement(BUILD), fOffset); toFormat = build; } - Element plugins = getChild(build, "plugins"); - plugin = doc.createElement("plugin"); - plugins.appendChild(plugin); + plugin = createElement(getChild(build, PLUGINS), PLUGIN); } if("build".equals(currentName) || "pluginManagement".equals(currentName)) { //$NON-NLS-1$ //$NON-NLS-2$ Element plugins = findChild(currentNode, "plugins"); @@ -157,8 +149,7 @@ public class InsertArtifactProposal implements ICompletionProposal, ICompletionP plugins = insertAt(doc.createElement("plugins"), fOffset); toFormat = plugins; } - plugin = doc.createElement("plugin"); - plugins.appendChild(plugin); + plugin = createElement(plugins, PLUGIN); } if("plugins".equals(currentName)) { plugin = insertAt(doc.createElement("plugin"), fOffset); diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingOperation.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingOperation.java index c9e75330..3441d068 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingOperation.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingOperation.java @@ -11,15 +11,7 @@ package org.eclipse.m2e.editor.xml.internal.lifecycle; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.childEquals; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.createElementWithText; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.createPlugin; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChilds; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.format; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getTextValue; -import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.setText; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; import java.util.ArrayList; import java.util.Arrays; @@ -37,8 +29,11 @@ import org.w3c.dom.Element; import org.eclipse.m2e.core.internal.lifecycle.LifecycleMappingFactory; import org.eclipse.m2e.core.internal.lifecycle.model.PluginExecutionAction; import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; +import org.eclipse.m2e.core.ui.internal.editing.PomHelper; public class LifecycleMappingOperation implements Operation { + + private static final Logger log = LoggerFactory.getLogger(LifecycleMappingOperation.class); private static final String LIFECYCLE_PLUGIN_VERSION = LifecycleMappingFactory.LIFECYCLE_MAPPING_PLUGIN_VERSION; @@ -66,20 +61,20 @@ public class LifecycleMappingOperation implements Operation { public void process(Document document) { Element root = document.getDocumentElement(); - Element managedPlugins = getChild(root, "build", "pluginManagement", "plugins"); + Element managedPlugins = getChild(root, BUILD, PLUGIN_MANAGEMENT, PLUGINS); //now find the lifecycle stuff if it's there. - Element lifecyclePlugin = findChild(managedPlugins, "plugin", - childEquals("groupId", LIFECYCLE_PLUGIN_GROUPID), - childEquals("artifactId", LIFECYCLE_PLUGIN_ARTIFACTID)); + Element lifecyclePlugin = findChild(managedPlugins, PLUGIN, + childEquals(GROUP_ID, LIFECYCLE_PLUGIN_GROUPID), + childEquals(ARTIFACT_ID, LIFECYCLE_PLUGIN_ARTIFACTID)); if (lifecyclePlugin == null) { //not found, create - lifecyclePlugin = createPlugin(managedPlugins, LIFECYCLE_PLUGIN_GROUPID, LIFECYCLE_PLUGIN_ARTIFACTID, LIFECYCLE_PLUGIN_VERSION); + lifecyclePlugin = PomHelper.createPlugin(managedPlugins, LIFECYCLE_PLUGIN_GROUPID, LIFECYCLE_PLUGIN_ARTIFACTID, LIFECYCLE_PLUGIN_VERSION); Comment comment = document.createComment("TODO TEXT. This plugin's configuration is used in m2e only."); managedPlugins.insertBefore(comment, lifecyclePlugin); format(comment); } - Element pluginExecutions = getChild(lifecyclePlugin, "configuration", "lifecycleMappingMetadata", "pluginExecutions"); + Element pluginExecutions = getChild(lifecyclePlugin, CONFIGURATION, "lifecycleMappingMetadata", "pluginExecutions"); //now find the plugin execution for the plugin we have.. Element execution = null; for (Element exec : findChilds(pluginExecutions, "pluginExecution")) { diff --git a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingProposal.java b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingProposal.java index 73367286..8fc9676d 100644 --- a/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingProposal.java +++ b/org.eclipse.m2e.editor.xml/src/main/java/org/eclipse/m2e/editor/xml/internal/lifecycle/LifecycleMappingProposal.java @@ -1,3 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2008-2011 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.editor.xml.internal.lifecycle; import java.io.IOException; @@ -23,7 +35,7 @@ import org.eclipse.ui.texteditor.MarkerAnnotation; import org.eclipse.m2e.core.core.IMavenConstants; import org.eclipse.m2e.core.internal.lifecycle.model.PluginExecutionAction; -import org.eclipse.m2e.core.ui.internal.editing.PomEdits; +import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*; import org.eclipse.m2e.editor.xml.internal.Messages; public class LifecycleMappingProposal implements ICompletionProposal, ICompletionProposalExtension5, IMarkerResolution { @@ -48,7 +60,7 @@ public class LifecycleMappingProposal implements ICompletionProposal, ICompletio public void apply(final IDocument doc) { try { - PomEdits.performOnDOMDocument(new PomEdits.OperationTuple(doc, createOperation())); + performOnDOMDocument(new OperationTuple(doc, createOperation())); marker.delete(); } catch(IOException e) { log.error("Error generating code in pom.xml", e); //$NON-NLS-1$ @@ -116,7 +128,7 @@ public class LifecycleMappingProposal implements ICompletionProposal, ICompletio public void run(final IMarker marker) { try { - PomEdits.performOnDOMDocument(new PomEdits.OperationTuple((IFile) marker.getResource(), createOperation())); + performOnDOMDocument(new OperationTuple((IFile) marker.getResource(), createOperation())); marker.delete(); } catch(IOException e) { log.error("Error generating code in pom.xml", e); //$NON-NLS-1$ diff --git a/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/dialogs/ManageDependenciesDialog.java b/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/dialogs/ManageDependenciesDialog.java index 2cb044e3..2c583931 100644 --- a/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/dialogs/ManageDependenciesDialog.java +++ b/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/dialogs/ManageDependenciesDialog.java @@ -39,6 +39,7 @@ import org.eclipse.m2e.core.ui.internal.dialogs.AbstractMavenDialog; import org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation; import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple; +import org.eclipse.m2e.core.ui.internal.editing.PomHelper; import org.eclipse.m2e.editor.MavenEditorPlugin; import org.eclipse.m2e.editor.composites.DependencyLabelProvider; import org.eclipse.m2e.editor.composites.ListEditorContentProvider; @@ -289,7 +290,7 @@ public class ManageDependenciesDialog extends AbstractMavenDialog { } //TODO is the version is defined by property expression, we should make sure the property is defined in the current project for (Dependency modelDependency : modelDependencies) { - createDependency(managedDepsElement, modelDependency.getGroupId(), modelDependency.getArtifactId(), modelDependency.getVersion()); + PomHelper.createDependency(managedDepsElement, modelDependency.getGroupId(), modelDependency.getArtifactId(), modelDependency.getVersion()); } } }; diff --git a/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/pom/OverviewPage.java b/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/pom/OverviewPage.java index 8c594d45..e00bb6bb 100644 --- a/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/pom/OverviewPage.java +++ b/org.eclipse.m2e.editor/src/org/eclipse/m2e/editor/pom/OverviewPage.java @@ -1395,17 +1395,17 @@ public class OverviewPage extends MavenPomEditorPage { performOnDOMDocument(new OperationTuple(pomFile, new Operation() { public void process(Document document) { Element root = document.getDocumentElement(); - Element parent = getChild(root, "parent"); - setText(getChild(parent, "groupId"), parentGroupId); - setText(getChild(parent, "artifactId"), parentArtifactId); - setText(getChild(parent, "version"), parentVersion); - setText(getChild(parent, "relativePath"), relativePath); - Element grId = findChild(root, "groupId"); + Element parent = getChild(root, PARENT); + setText(getChild(parent, GROUP_ID), parentGroupId); + setText(getChild(parent, ARTIFACT_ID), parentArtifactId); + setText(getChild(parent, VERSION), parentVersion); + setText(getChild(parent, RELATIVE_PATH), relativePath); + Element grId = findChild(root, GROUP_ID); String grIdText = getTextValue(grId); if (grIdText != null && grIdText.equals(parentGroupId)) { removeChild(root, grId); } - Element ver = findChild(root, "version"); + Element ver = findChild(root, VERSION); String verText = getTextValue(ver); if (verText != null && verText.equals(parentVersion)) { removeChild(root, ver); diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java index 1422b2d4..aef020c7 100644 --- a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java +++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java @@ -26,6 +26,7 @@ import org.eclipse.ltk.core.refactoring.Refactoring; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.m2e.core.embedder.ArtifactKey; import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation; +import org.eclipse.m2e.core.ui.internal.editing.PomHelper; import org.eclipse.m2e.refactoring.ChangeCreator; import org.eclipse.wst.sse.core.StructuredModelManager; import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; @@ -82,32 +83,12 @@ public class DependencySetRefactoring extends Refactoring { * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor) */ public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { - CompositeChange res = new CompositeChange(getName()); - IStructuredModel model = null; - try { - model = StructuredModelManager.getModelManager().getModelForRead(file); - IDocument document = model.getStructuredDocument(); - IStructuredModel tempModel = StructuredModelManager.getModelManager().createUnManagedStructuredModelFor( - "org.eclipse.m2e.core.pomFile"); - tempModel.getStructuredDocument().setText(StructuredModelManager.getModelManager(), document.get()); - IDocument tempDocument = tempModel.getStructuredDocument(); - List<Operation> operations = new ArrayList<Operation>(); - for (ArtifactKey key : keys) { - operations.add(new OneDependency(key)); - } - CompoundOperation compound = new CompoundOperation(operations.toArray(new Operation[0])); - performOnDOMDocument(new OperationTuple((IDOMModel) tempModel, compound)); - - ChangeCreator chc = new ChangeCreator(file, document, tempDocument, getName()); - res.add(chc.createChange()); - } catch(Exception exc) { - LOG.error("", exc); - } finally { - if(model != null) { - model.releaseFromRead(); - } + List<Operation> operations = new ArrayList<Operation>(); + for (ArtifactKey key : keys) { + operations.add(new OneDependency(key)); } - return res; + CompoundOperation compound = new CompoundOperation(operations.toArray(new Operation[0])); + return PomHelper.createChange(file, compound, getName()); } private static class OneDependency implements Operation { @@ -126,23 +107,23 @@ public class DependencySetRefactoring extends Refactoring { */ public void process(Document document) { //TODO handle activated profiles? - Element deps = findChild(document.getDocumentElement(), "dependencies"); - Element existing = findChild(deps, "dependency", childEquals("groupId", groupId), - childEquals("artifactId", artifactId)); + Element deps = findChild(document.getDocumentElement(), DEPENDENCIES); + Element existing = findChild(deps, DEPENDENCY, childEquals(GROUP_ID, groupId), + childEquals(ARTIFACT_ID, artifactId)); if(existing != null) { //it's a direct dependency //TODO check the version value.. not to overwrite the existing version.. //even better, have the action only available on transitive dependencies - setText(getChild(existing, "version"), version); + setText(getChild(existing, VERSION), version); } else { //is transitive dependency - Element dm = getChild(document.getDocumentElement(), "dependencyManagement", "dependencies"); - existing = findChild(dm, "dependency", childEquals("groupId", groupId), - childEquals("artifactId", artifactId)); + Element dm = getChild(document.getDocumentElement(), DEPENDENCY_MANAGEMENT, DEPENDENCIES); + existing = findChild(dm, DEPENDENCY, childEquals(GROUP_ID, groupId), + childEquals(ARTIFACT_ID, artifactId)); if(existing != null) { - setText(getChild(existing, "version"), version); + setText(getChild(existing, VERSION), version); } else { - createDependency(dm, groupId, artifactId, version); + PomHelper.createDependency(dm, groupId, artifactId, version); } } |