/*-
 * Copyright (c) 2012 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.maven.model;

import java.util.regex.Pattern;

import org.fedoraproject.maven.utils.GlobUtils;
import org.fedoraproject.maven.utils.StringSplitter;

public class ArtifactGlob
{
    private final Pattern groupId;

    private final Pattern artifactId;

    private final Pattern version;

    public ArtifactGlob( String glob )
    {
        String[] tok = StringSplitter.split( glob, 3, ':' );
        groupId = GlobUtils.glob2pattern( tok[0] );
        artifactId = GlobUtils.glob2pattern( tok[1] );
        version = GlobUtils.glob2pattern( tok[2] );
    }

    public boolean matches( Artifact artifact )
    {
        return ( groupId == null || groupId.matcher( artifact.getGroupId() ).matches() )
            && ( artifactId == null || artifactId.matcher( artifact.getArtifactId() ).matches() )
            && ( version == null || version.matcher( artifact.getVersion() ).matches() );
    }
}

@@ -635,16 +635,48 @@
 model;%0A%0A
+import java.util.regex.Matcher;%0A
 import j
@@ -863,16 +863,23 @@
  groupId
+Pattern
 ;%0A%0A    p
@@ -909,16 +909,23 @@
 tifactId
+Pattern
 ;%0A%0A    p
@@ -952,16 +952,23 @@
  version
+Pattern
 ;%0A%0A    p
@@ -1083,16 +1083,23 @@
  groupId
+Pattern
  = GlobU
@@ -1145,16 +1145,23 @@
 tifactId
+Pattern
  = GlobU
@@ -1204,16 +1204,23 @@
  version
+Pattern
  = GlobU
@@ -1333,16 +1333,23 @@
  groupId
+Pattern
  == null
@@ -1359,16 +1359,23 @@
  groupId
+Pattern
 .matcher
@@ -1439,16 +1439,23 @@
 tifactId
+Pattern
  == null
@@ -1468,16 +1468,23 @@
 tifactId
+Pattern
 .matcher
@@ -1548,16 +1548,23 @@
  version
+Pattern
  == null
@@ -1574,16 +1574,23 @@
  version
+Pattern
 .matcher
@@ -1633,11 +1633,1362 @@
 );%0A    %7D
+%0A%0A    private String match( String source, Pattern pattern, String target )%0A    %7B%0A        String result = source != null ? source : target;%0A%0A        if ( pattern != null )%0A        %7B%0A            Matcher matcher = pattern.matcher( target );%0A            if ( !matcher.matches() )%0A                return null;%0A            if ( source != null )%0A            %7B%0A                for ( int group = 1; group %3C= matcher.groupCount(); group++ )%0A                    result = result.replace( %22@%22 + group, matcher.group( group ) );%0A            %7D%0A        %7D%0A%0A        return result.trim();%0A    %7D%0A%0A    public Artifact createArtifactFromTemplate( Artifact artifact, String template )%0A    %7B%0A        String%5B%5D tok = StringSplitter.split( template, 3, ':' );%0A%0A        String groupId = match( tok%5B0%5D, groupIdPattern, artifact.getGroupId() );%0A        String artifactId = match( tok%5B1%5D, artifactIdPattern, artifact.getArtifactId() );%0A        String version = match( tok%5B2%5D, versionPattern, artifact.getVersion() );%0A%0A        if ( groupId == null %7C%7C artifactId == null %7C%7C version == null )%0A            return null;%0A%0A        if ( groupId.equals( %22%22 ) )%0A            groupId = null;%0A        if ( artifactId.equals( %22%22 ) )%0A            artifactId = null;%0A        if ( version.equals( %22%22 ) )%0A            version = null;%0A%0A        return new Artifact( groupId, artifactId, version );%0A    %7D
 %0A%7D%0A

Implement creation of artifact from matched pattern