Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 584ef5dfab07cdde301d1e7f8daa857c42fd0ec4 (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
/*******************************************************************************
 * Copyright (c) 2008-2010 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.scm;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.scm.internal.Messages;

/**
 * An SCM URL wrapper used to adapt 3rd party resources:
 * 
 * <pre>
 * scm:{scm_provider}:{scm_provider_specific_part}
 * </pre>
 * 
 * @see http://maven.apache.org/scm/scm-url-format.html
 * @see org.eclipse.core.runtime.IAdapterManager
 *
 * @author Eugene Kuleshov
 */
public class ScmUrl {
  private final String scmUrl;
  private final String scmParentUrl;
  private final ScmTag tag;

  public ScmUrl(String scmUrl) {
    this(scmUrl, null);
  }
  
  public ScmUrl(String scmUrl, String scmParentUrl) {
    this(scmUrl, null, null);
  }

  public ScmUrl(String scmUrl, String scmParentUrl, ScmTag tag) {
    this.scmUrl = scmUrl;
    this.scmParentUrl = scmParentUrl;
    this.tag = tag;
  }
  
  /**
   * Return SCM url
   */
  public String getUrl() {
    return scmUrl;
  }
  
  /**
   * Return SCM url for the parent folder
   */
  public String getParentUrl() {
    return scmParentUrl;
  }
  
  /**
   * Return SCM tag
   */
  public ScmTag getTag() {
    return this.tag;
  }
  
  /**
   * Return provider-specific part of the SCM url
   * 
   * @return
   */
  public String getProviderUrl() {
    try {
      String type = ScmUrl.getType(scmUrl);
      return scmUrl.substring(type.length() + 5);
      
    } catch(CoreException ex) {
      return null;
    }
  }

  public static synchronized String getType(String url) throws CoreException {
    if(!url.startsWith("scm:")) { //$NON-NLS-1$
      throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, NLS.bind(Messages.ScmUrl_error, url), null));
    }
    int n = url.indexOf(":", 4); //$NON-NLS-1$
    if(n == -1) {
      throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, NLS.bind(Messages.ScmUrl_error, url), null));
    }
    return url.substring(4, n);
  }

}

Back to the top