Package aQute.maven.ns

Source Code of aQute.maven.ns.MavenNamespace

package aQute.maven.ns;

import java.util.*;
import java.util.regex.*;

import aQute.maven.MavenRepository.Coordinates;
import aQute.maven.*;
import aQute.maven.Pom.Dependency;

/**
* 1.0 x >= 1.0 * The default Maven meaning for 1.0 is everything (,) but with
* 1.0 recommended. Obviously this doesn't work for enforcing versions here, so
* it has been redefined as a minimum version. (,1.0] x <= 1.0 (,1.0) x < 1.0
* [1.0] x == 1.0 [1.0,) x >= 1.0 (1.0,) x > 1.0 (1.0,2.0) 1.0 < x < 2.0
* [1.0,2.0] 1.0 <= x <= 2.0 (,1.0],[1.2,) x <= 1.0 or x >= 1.2. Multiple sets
* are comma-separated (,1.1),(1.1,) x != 1.1
*/

public class MavenNamespace {
  public static final String  NAME    = "x-maven";
  public static final String  GROUPID    = "g";
  public static final String  ARTIFACTID  = "a";
  public static final String  VERSION    = "v";
  public static final String  PACKAGING  = "p";
  public static final String  CLASSIFIER  = "c";

  public static Map<String,Object> createRequirement(Dependency dep) throws Exception {
    Map<String,Object> ps = new HashMap<String,Object>();
    StringBuilder sb = new StringBuilder("(&(g=");
    sb.append(dep.groupId).append(")(a=").append(dep.artifactId).append(")");

    if (dep.version == null)
      dep.version = "0";

    MavenVersionRange mr = MavenVersionRange.parse(dep.version);
    if (mr != null)
      mr.toFilter(sb);
    else
      sb.append(dep.version);

    if (dep.classifier == null) {
      sb.append("(!(c=*)");
    } else {
      sb.append(")(c=").append(dep.classifier);
    }
    sb.append("))");
    StringBuilder sb2 = new StringBuilder();
    sb2.append(dep.groupId).append(":").append(dep.artifactId);
    if (dep.classifier != null && !dep.classifier.isEmpty())
      sb2.append(":").append(dep.classifier);
    sb2.append("@").append(dep.version);

    ps.put("name:", sb2.toString());
    ps.put("filter:", sb.toString());
    // TODO do we need another effective time?
    ps.put("effective:", dep.scope == null ? "maven" : dep.scope);

    if (dep.optional)
      ps.put("resolution:", "optional");
    return ps;
  }

  static Pattern  FILTER_PATTERN      = Pattern.compile("\\((g|a|v|c)=\\s*([-\\w\\d._]+)\\s*\\)");
  static Pattern  COORDINATE_PATTERN    = Pattern
                          .compile("([-_.\\d\\w]+):([-_.\\d\\w]+)(?:([-_.\\d\\w]+))?@([\\[\\]\\(\\)-_.\\d\\w]+)");
  static Pattern  COORDINATE_PATTERN_OLD  = Pattern
                          .compile("([-_.\\d\\w]+):([-_.\\d\\w]+)(?::([-_.\\d\\w]+))?(?:(.+))");

  public static Coordinates getCoordinatesFromReq(Map<String,Object> ps) {
    String filter = (String) ps.get("filter:");
    Coordinates c = new Coordinates();
    // String coordinates = (String) ps.get("name:");
    // if (coordinates != null) {
    // Matcher m2 = COORDINATE_PATTERN.matcher(coordinates);
    // if (!m2.matches())
    // m2 = COORDINATE_PATTERN_OLD.matcher(coordinates);
    //
    // if (m2.matches()) {
    // c.groupId = m2.group(1);
    // c.artifactId = m2.group(2);
    // c.classifier = m2.group(3);
    // c.version = m2.group(4);
    // return c;
    // }
    // }
    //
    Matcher m = FILTER_PATTERN.matcher(filter);
    while (m.find()) {
      String key = m.group(1);
      String value = m.group(2);
      if (key.equals("a"))
        c.artifactId = value;
      else if (key.equals("g"))
        c.groupId = value;
      else if (key.equals("c"))
        c.classifier = value;
      else if (c.version == null && key.equals("v"))
        c.version = value;
      else
        ; // ignore
    }

    return c;
  }

}
TOP

Related Classes of aQute.maven.ns.MavenNamespace

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.