Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97c765f67d471bb69a2d9729d0ded6336e1a1cb5 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 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
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.editing;

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;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.TextChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;

import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public final class PomHelper {


  private static final Logger LOG = LoggerFactory.getLogger(PomHelper.class);


  /*
   * 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);
    return findChild(dependenciesElement, DEPENDENCY, childEquals(GROUP_ID, dependency.getGroupId()),
        childEquals(ARTIFACT_ID, dependency.getArtifactId()));
  }

  private static boolean isOpened(IDocument document) {
    for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
      for (IWorkbenchPage page : window.getPages()) {
        //page.getEditors() - this call restores all the pages content, didn't feel like doing so, unless
        // we can bring to life only the maven pom editors..
        for (IEditorReference ref : page.getEditorReferences()) {
          IEditorPart editor = ref.getEditor(false);
          if (editor != null) {
            IDocument doc = (IDocument) editor.getAdapter(IDocument.class);
            if (doc !=null && doc.equals(document)) {
              return true;
            }
          }
        }
      }
    }
    return false;
  }
  /**
   * 
   * @param file
   * @param operation
   * @param label
   * @param forceSave if true will save all files, no matter if associated document is opened in editor area or not.
   * @return
   * @throws CoreException
   */
  @SuppressWarnings("restriction")
  public static TextChange createChange(IFile file, Operation operation, String label, boolean forceSave) throws CoreException {
    IStructuredModel model = null;
    try {
      model = StructuredModelManager.getModelManager().getModelForRead(file);
      IDocument document = model.getStructuredDocument();
      boolean existing = isOpened(document);
      IStructuredModel tempModel = StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(
          "org.eclipse.m2e.core.pomFile"); //$NON-NLS-1$
      tempModel.getStructuredDocument().setText(StructuredModelManager.getModelManager(), document.get());
      IDocument tempDocument = tempModel.getStructuredDocument();
      performOnDOMDocument(new OperationTuple((IDOMModel) tempModel, operation));

      TextChange change = new ChangeCreator(!forceSave && existing ? null : file, document, tempDocument, label).createChange();
      if (forceSave) assert change instanceof TextFileChange; //if assert not fullfilled, we will not get the file saved..
      
      if (forceSave && change instanceof TextFileChange) {
        ((TextFileChange)change).setSaveMode(TextFileChange.FORCE_SAVE);
      }
      return change;
    } catch(Exception exc) {
      LOG.error(Messages.PomHelper_errorCreatingChange, exc);
      throw new CoreException(new Status(IStatus.ERROR, M2EUIPluginActivator.PLUGIN_ID,
          Messages.PomHelper_errorCreatingChange, exc));
    } finally {
      if(model != null) {
        model.releaseFromRead();
      }
    }
  }

  /**
   * by default will create a change that won't save files with opened documents
   * @param file
   * @param operation
   * @param label
   * @return
   * @throws CoreException
   */
  public static TextChange createChange(IFile file, Operation operation, String label) throws CoreException {
    return createChange(file, operation, label, false);
  }

  /**
   * 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);
  }
  
  /**
   * null in any value parameter mean remove the element.
   * @param depsEl
   * @param groupId
   * @param artifactId
   * @param version
   * @param type
   * @param scope
   * @param classifier
   * @return the root xml element of the dependency
   */
  public static Element addOrUpdateDependency(Element depsEl, String groupId, String artifactId, String version, String type, String scope, String classifier) {
    Element dep = findChild(depsEl, DEPENDENCY,
        childEquals(GROUP_ID, groupId), 
        childEquals(ARTIFACT_ID, artifactId));
    if (dep == null) {
      dep = createDependency(depsEl, groupId, artifactId, version);
    } else {
      //only set version if already exists
      if (version != null) {
        setText(getChild(dep, VERSION), version);
      } else {
        removeChild(dep, findChild(dep, VERSION));
      }
    }
    if (type != null //
        && !"jar".equals(type) // //$NON-NLS-1$
        && !"null".equals(type)) { // guard against MNGECLIPSE-622 //$NON-NLS-1$
      
      setText(getChild(dep, TYPE), type);
    } else {
      removeChild(dep, findChild(dep, TYPE));
    }
    
    if (classifier != null) {
      setText(getChild(dep, CLASSIFIER), classifier);
    } else {
      removeChild(dep, findChild(dep, CLASSIFIER));
    }
    
    if (scope != null && !"compile".equals(scope)) { //$NON-NLS-1$
      setText(getChild(dep, SCOPE), scope);
    } else {
      removeChild(dep, findChild(dep, SCOPE));
    }
    return dep;
  }
}

Back to the top