package hudson.mvn;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.util.ArgumentListBuilder;
import hudson.util.FormValidation;
import hudson.util.IOUtils;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

/**
 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
 */
public class FilePathSettingsProvider extends SettingsProvider {

    private final String path;

    @DataBoundConstructor
    public FilePathSettingsProvider(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    @Override
    public void configure(ArgumentListBuilder margs, AbstractBuild project) throws IOException, InterruptedException {
        if (StringUtils.isEmpty(path)) return;
        if (IOUtils.isAbsolute(path)) {
            margs.add("-s").add(path);
        } else {
            FilePath mrSettings = project.getModuleRoot().child(path);
            FilePath wsSettings = project.getWorkspace().child(path);
            if (!wsSettings.exists() && mrSettings.exists())
                wsSettings = mrSettings;

            margs.add("-s").add(wsSettings.getRemote());
        }
    }


    @Extension(ordinal = 10)
    public static class DescriptorImpl extends SettingsProviderDescriptor {

        /**
         * Check that the provided file is a relative path. And check that it exists, just in case.
         */
        public FormValidation doCheck(@AncestorInPath AbstractProject job, @QueryParameter String value) throws IOException, ServletException {
            String v = fixEmpty(value);
            if ((v == null) || (v.length() == 0)) {
                // Null values are allowed.
                return FormValidation.ok();
            }
            if ((v.startsWith("/")) || (v.startsWith("\\")) || (v.matches("^\\w\\:\\\\.*"))) {
                return FormValidation.error(Messages.MavenModuleSet_AlternateSettingsRelativePath());
            }

            Run lb = job.getLastBuild();
            if (lb!=null) {
                FilePath ws = lb.getWorkspace();
                if(ws!=null)
                    return ws.validateRelativePath(value,true,true);
            }
            return FormValidation.ok();
        }

        @Override
        public String getDisplayName() {
            return "File in project workspace";
        }
    }
}

@@ -1796,32 +1796,105 @@
 vletException %7B%0A
+        %09%0A        %09// TODO why not support an absolute path?%0A        %09%0A//
             Stri
@@ -1913,24 +1913,26 @@
 pty(value);%0A
+//
             
@@ -1967,24 +1967,26 @@
 () == 0)) %7B%0A
+//
             
@@ -2017,16 +2017,18 @@
 llowed.%0A
+//
         
@@ -2055,32 +2055,34 @@
 alidation.ok();%0A
+//
             %7D%0A  
@@ -2071,32 +2071,34 @@
 //            %7D%0A
+//
             if (
@@ -2172,24 +2172,26 @@
 %5C%5C%5C.*%22))) %7B%0A
+//
             
@@ -2276,24 +2276,26 @@
 ivePath());%0A
+//
             
@@ -2288,33 +2288,37 @@
 //            %7D%0A
-%0A
+//%0A//
             Run 
@@ -2338,24 +2338,26 @@
 astBuild();%0A
+//
             
@@ -2368,24 +2368,26 @@
 lb!=null) %7B%0A
+//
             
@@ -2419,24 +2419,26 @@
 orkspace();%0A
+//
             
@@ -2454,16 +2454,18 @@
 !=null)%0A
+//
         
@@ -2525,16 +2525,18 @@
 ,true);%0A
+//
         

disable file path validation