You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by si...@apache.org on 2008/05/15 04:53:13 UTC

svn commit: r656493 [3/3] - in /maven/sandbox/branches/MNG-3536/maven-project/src: main/java/org/apache/maven/project/ main/java/org/apache/maven/project/interpolation/ main/java/org/apache/maven/project/interpolation/policies/ test/java/org/apache/mav...

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/DefaultModelPropertyInterpolator.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/DefaultModelPropertyInterpolator.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/DefaultModelPropertyInterpolator.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/DefaultModelPropertyInterpolator.java Wed May 14 19:53:13 2008
@@ -0,0 +1,122 @@
+package org.apache.maven.project.interpolation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.codehaus.plexus.logging.LogEnabled;
+import org.codehaus.plexus.logging.Logger;
+
+import java.util.List;
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.io.StringWriter;
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * Default implementation of the model property interpolator.
+ */
+public class DefaultModelPropertyInterpolator implements ModelPropertyInterpolator, LogEnabled {
+
+    private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(pom\\.|project\\.|env\\.)?([^}]+)\\}");
+
+    private Logger logger;
+
+    public void enableLogging(Logger logger) {
+        if(logger == null) {
+            throw new IllegalArgumentException("logger");
+        }
+        this.logger = logger;
+    }
+    /**
+     * @see DefaultModelPropertyInterpolator#interpolate(org.apache.maven.model.Model, java.util.List) 
+     */
+    public Model interpolate(Model model, List policies) throws ModelInterpolationException {
+        if(model == null) {
+            throw new IllegalArgumentException("model");
+        }
+
+        if(policies == null || policies.size() == 0) {
+            return model;
+        }
+
+        StringWriter pomStringWriter = new StringWriter();
+        MavenXpp3Writer writer = new MavenXpp3Writer();
+        try {
+            writer.write(pomStringWriter, model);
+        }
+        catch (IOException e) {
+            throw new ModelInterpolationException("Cannot serialize project model for interpolation.", e);
+        }
+
+        String result = interpolateString(model, policies, pomStringWriter.toString());
+      // System.out.println(result);
+        
+        MavenXpp3Reader modelReader = new MavenXpp3Reader();
+        try {
+            return modelReader.read(new StringReader(result));
+        }
+        catch (IOException e) {
+            throw new ModelInterpolationException(
+                    "Cannot read project model from interpolating filter of serialized version.", e);
+        }
+        catch (XmlPullParserException e) {
+            throw new ModelInterpolationException(
+                    "Cannot read project model from interpolating filter of serialized version.", e);
+        }
+    }
+
+    private String interpolateString(Model model, List policies, String value) throws ModelInterpolationException {
+        List modelProperties = new ArrayList();
+        Matcher matcher = EXPRESSION_PATTERN.matcher(value);
+        while (matcher.find()) {
+            ModelProperty modelProperty = new ModelProperty();
+            modelProperty.setKey(matcher.group(0));
+            modelProperty.setExpression(matcher.group(2));
+            if (!modelProperties.contains(modelProperty)) {
+                modelProperties.add(modelProperty);
+            }
+        }
+
+        for (Iterator j = modelProperties.iterator(); j.hasNext();) {
+            ModelProperty modelProperty = (ModelProperty) j.next();
+            for (Iterator i = policies.iterator(); i.hasNext();) {
+                ModelPropertyPolicy policy = (ModelPropertyPolicy) i.next();
+                policy.evaluate(modelProperty, model);
+                if (modelProperty.getValue() != null) {
+              //  System.out.println("REPLACE: Key = " + modelProperty.getKey() + ": Value = " + modelProperty.getValue());
+                    String result = StringUtils.replace(value, modelProperty.getKey(), modelProperty.getValue());
+                    if (!result.equals(value)) {     //&& EXPRESSION_PATTERN.matcher(modelProperty.getValue()).matches()
+                        String s = interpolateString(model, policies, modelProperty.getValue());
+                     //   System.out.println("REPLACE: Key-1 = " + modelProperty.getKey() + ": Value = " + s);
+                        value = StringUtils.replace(value, modelProperty.getKey(), s);
+                    }
+                }
+            }
+        }
+        return value;
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelProperty.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelProperty.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelProperty.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelProperty.java Wed May 14 19:53:13 2008
@@ -0,0 +1,80 @@
+package org.apache.maven.project.interpolation;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+/**
+ * Provides methods for model properties.
+ */
+public final class ModelProperty {
+
+    private String value;
+
+    private String key;
+
+    private String expression;
+
+    private boolean isProjectOrPomProperty;
+
+    public String getExpression() {
+        return expression;
+    }
+
+    public void setExpression(String expression) {
+        this.expression = expression;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public boolean isProjectOrPomProperty() {
+        return isProjectOrPomProperty;
+    }
+
+    public void setProjectOrPomProperty(boolean projectOrPomProperty) {
+        isProjectOrPomProperty = projectOrPomProperty;
+    }
+
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        ModelProperty that = (ModelProperty) o;
+
+        if (key != null ? !key.equals(that.key) : that.key != null) return false;
+
+        return true;
+    }
+
+    public int hashCode() {
+        return (key != null ? key.hashCode() : 0);
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyInterpolator.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyInterpolator.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyInterpolator.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyInterpolator.java Wed May 14 19:53:13 2008
@@ -0,0 +1,43 @@
+package org.apache.maven.project.interpolation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.model.Model;
+
+import java.util.List;
+
+/**
+ * Provides services for interpolating the properties of a model.
+ */
+public interface ModelPropertyInterpolator {
+
+    String ROLE = ModelPropertyInterpolator.class.getName();
+
+    /**
+     * Interpolates properties within the specified model, using the list of specified model property policies
+     *
+     * @param model    the model to interpolate properties of. This value may not be null.
+     * @param policies the model property policies used to interpolate the model properties. If this value is null or
+     *                 empty, this method returns an unmodified model.
+     * @return model with interpolated properties
+     * @throws ModelInterpolationException if there is a problem interpolating properties
+     */
+    Model interpolate(Model model, List policies) throws ModelInterpolationException;
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,38 @@
+package org.apache.maven.project.interpolation;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.model.Model;
+
+/**
+ * Provides services for interpolating model properties.
+ */
+public interface ModelPropertyPolicy {
+
+    /**
+     * Interpolates the specified model using the specified model property.
+     *
+     * @param mp model property
+     * @param model the model to interpolate the properties of
+     * @throws ModelInterpolationException if there is a problem in interpolating the model properties
+     */
+    void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException;
+
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,179 @@
+package org.apache.maven.project.interpolation.policies;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.project.path.PathTranslator;
+import org.apache.maven.project.path.DefaultPathTranslator;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Build;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.io.File;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.net.MalformedURLException;
+
+/**
+ * Interpolates the build properties: build.directory, build.outputDirectory, build.testOutputDirectory,
+ * build.sourceDirectory, build.testSourceDirectory, basedir.
+ */
+public class BuildPropertyPolicy implements ModelPropertyPolicy {
+
+    /**
+     * Project directory for the build.
+     */
+    private File projectDir;
+
+    /**
+     * Expression that matches pom, project, env properties
+     */
+    private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{(pom\\.|project\\.|env\\.)?([^}]+)\\}");
+
+    /**
+     * Constructor
+     *
+     * @param projectDir the project directory for the build. May not be null.
+     */
+    public BuildPropertyPolicy(File projectDir) {
+        if (projectDir == null) {
+            throw new IllegalArgumentException("projectDir");
+        }
+        this.projectDir = projectDir;
+    }
+
+    /**
+     * Interpolates the build properties and resolves absolute path.
+     *
+     * @param mp    model property
+     * @param model model. May not be null.
+     * @throws ModelInterpolationException
+     */
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if (model == null) {
+            throw new IllegalArgumentException("model");
+        }
+        if (mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+
+        String expression = mp.getExpression();
+        if (expression == null) {
+            throw new IllegalArgumentException("mp.expression");
+        }
+
+        Build build = model.getBuild();
+        if (mp.getValue() == null) {
+            //  System.out.println("Aligning path: Expression = " + expression);
+            if (expression.equals("basedir")) {
+                mp.setValue(projectDir.getAbsolutePath());
+            } else if (expression.equals("build.directory")) {
+                setPath(mp, build.getDirectory());
+            } else if (expression.equals("build.outputDirectory")) {
+                setPath(mp, build.getOutputDirectory());
+            } else if (expression.equals("build.testOutputDirectory")) {
+                setPath(mp, build.getTestOutputDirectory());
+            } else if (expression.equals("build.sourceDirectory")) {
+                setPath(mp, build.getSourceDirectory());
+            } else if (expression.equals("build.testSourceDirectory")) {
+                setPath(mp, build.getTestSourceDirectory());
+            } else if (expression.equals("build.scriptSourceDirectory")) {
+                setPath(mp, build.getScriptSourceDirectory());
+            } else if (expression.equals("reporting.outputDirectory")) {
+                setPath(mp, model.getReporting().getOutputDirectory());
+            }
+
+        } else {
+            System.out.println("Matched value: Expression = " + expression);
+            List modelProperties = new ArrayList();
+            Matcher matcher = EXPRESSION_PATTERN.matcher(mp.getValue());
+            while (matcher.find()) {
+                ModelProperty modelProperty = new ModelProperty();
+                modelProperty.setKey(matcher.group(0));
+                modelProperty.setExpression(matcher.group(2));
+                //mp.getExpression().equals(mp.getExpression()) && 
+                if (!modelProperties.contains(modelProperty)) {
+                    modelProperties.add(modelProperty);
+                }
+            }
+
+            for (Iterator j = modelProperties.iterator(); j.hasNext();) {
+                ModelProperty modelProperty = (ModelProperty) j.next();
+                //     System.out.println("Found property. KEY = " + modelProperty.getKey() + ": VALUE = " + modelProperty.getValue());
+                if (expression.equals("basedir")) {
+                    modelProperty.setValue(projectDir.getPath());
+                } else if (expression.equals("build.directory")) {
+                    modelProperty.setValue(build.getDirectory());
+                } else if (expression.equals("build.outputDirectory")) {
+                    modelProperty.setValue(build.getOutputDirectory());
+                } else if (expression.equals("build.testOutputDirectory")) {
+                    modelProperty.setValue(build.getTestOutputDirectory());
+                } else if (expression.equals("build.sourceDirectory")) {
+                    modelProperty.setValue(build.getSourceDirectory());
+                } else if (expression.equals("build.testSourceDirectory")) {
+                    modelProperty.setValue(build.getTestSourceDirectory());
+                } else if (expression.equals("build.scriptSourceDirectory")) {
+                    modelProperty.setValue(build.getScriptSourceDirectory());
+                } else if (expression.equals("reporting.outputDirectory")) {
+                    modelProperty.setValue(model.getReporting().getOutputDirectory());
+                }
+                if (mp.getValue() != null && modelProperty.getValue() != null) {
+                  //  System.out.println("Replace values: OLD VALUE = " + mp.getValue() + ": KEY = "
+                  //          + modelProperty.getKey() + ": NEW VALUE =" + modelProperty.getValue());
+                    mp.setValue(StringUtils.replace(mp.getValue(), modelProperty.getKey(), modelProperty.getValue()));
+                    break;
+                }
+                System.out.println(mp.getValue());
+            }
+        }
+    }
+
+    /**
+     * Aligns the value of the model property to the project directory.
+     *
+     * @param mp        model property
+     * @param directory directory to align to the project directory
+     */
+    private void setPath(ModelProperty mp, String directory) throws ModelInterpolationException {
+        if (directory == null) {
+            throw new IllegalArgumentException("directory");
+        }
+        PathTranslator pathTranslator = new DefaultPathTranslator();
+        if (!directory.startsWith("${")) {
+            /*
+                            File filePath = new File(pathTranslator.alignToBaseDirectory(directory, projectDir));
+                String path = (!directory.contains("${")) ? filePath.toURI().toURL().toExternalForm() : filePath.getAbsolutePath();
+                */
+            mp.setValue(replaceFileSeparators(pathTranslator.alignToBaseDirectory(directory, projectDir)));
+
+        } else {
+            mp.setValue(replaceFileSeparators(directory));
+        }
+    }
+
+    private static String replaceFileSeparators(String b) {
+        return b.replace("/", File.separator).replace("\\", File.separator);
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/EnvironmentalPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/EnvironmentalPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/EnvironmentalPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/EnvironmentalPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,62 @@
+package org.apache.maven.project.interpolation.policies;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+import org.apache.maven.model.Model;
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+
+import java.util.Properties;
+
+/**
+ * Interpolates the environmental properties of the model.
+ */
+public class EnvironmentalPropertyPolicy implements ModelPropertyPolicy {
+
+    /**
+     * The environmental variables
+     */
+    private Properties envars;
+
+    /**
+     * Constructor
+     *
+     * @param environmentalVariables the environmental variables
+     */
+    public EnvironmentalPropertyPolicy(Properties environmentalVariables) {
+        if(environmentalVariables == null) {
+            throw new IllegalArgumentException("environmentalVariables");
+        }
+        envars = environmentalVariables;
+    }
+
+    /**
+     * Interpolates the environmental properties of the model.
+     *
+     * @param mp model property
+     * @param model model. May be null.
+     */
+    public void evaluate(ModelProperty mp, Model model) {
+        if(mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+        if (mp.getValue() == null) {
+            mp.setValue(envars.getProperty(mp.getExpression()));
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/PropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/PropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/PropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/PropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,43 @@
+package org.apache.maven.project.interpolation.policies;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+import org.apache.maven.model.Model;
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+
+/**
+ * Interpolates the project.properties within the model.
+ */
+public class PropertyPolicy implements ModelPropertyPolicy {
+
+    public void evaluate(ModelProperty mp, Model model) {
+        if(model == null) {
+            throw new IllegalArgumentException("model");
+        }
+        if(mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+
+     //   System.out.println("MODEL PROP: VALUE = " + mp.getValue() + ": KEY = " + mp.getKey() + ": EXPR =" + mp.getExpression());
+        if (mp.getValue() == null) {            
+            mp.setValue(model.getProperties().getProperty(mp.getExpression()));
+         //   System.out.println("SET VALUE = " + mp.getValue());
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/ReflectionPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/ReflectionPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/ReflectionPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/ReflectionPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,48 @@
+package org.apache.maven.project.interpolation.policies;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.model.Model;
+import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
+
+/**
+ * Substitutes values from model into their respective property. For example,
+ * build.sourceDirectory = model.getBuild().getSourceDirectory().
+ */
+public class ReflectionPropertyPolicy implements ModelPropertyPolicy {
+
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if(model == null) {
+            throw new IllegalArgumentException("model");
+        }
+        if(mp == null) {
+            throw new IllegalArgumentException("mp");
+        }        
+        if(mp.getValue() == null) {
+            try {
+                mp.setValue((String) ReflectionValueExtractor.evaluate(mp.getExpression(), model, false));
+            } catch (Exception e) {
+                throw new ModelInterpolationException("", e);
+            }
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SelfReferencePolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SelfReferencePolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SelfReferencePolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SelfReferencePolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,49 @@
+package org.apache.maven.project.interpolation.policies;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.model.Model;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+
+/**
+ * Evaluates whether a model property references itself.
+ */
+public class SelfReferencePolicy implements ModelPropertyPolicy {
+    /**
+     *  Evaluates whether a model property references itself.
+     *
+     * @param mp model property
+     * @param model model. May be null.
+     * @throws ModelInterpolationException if a model property references itself
+     */
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if(mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+
+        if (String.valueOf(mp.getValue()).indexOf(mp.getKey()) > -1) {
+            if (((mp.getKey().startsWith("${pom.") || mp.getKey().startsWith("${project.")))) {
+                throw new ModelInterpolationException(mp.getKey(), "Expression value '" + mp.getValue()
+                        + "' references itself in '" + model.getId() + "'.");
+            }
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SystemPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SystemPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SystemPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/SystemPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,61 @@
+package org.apache.maven.project.interpolation.policies;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.model.Model;
+
+import java.util.Map;
+
+/**
+ * Interpolates the system properties within the model.
+ */
+public class SystemPropertyPolicy implements ModelPropertyPolicy {
+
+    private Map properties;
+
+    /**
+     * Constructor
+     *
+     * @param properties system properties
+     */
+    public SystemPropertyPolicy(Map properties) {
+        if (properties == null) {
+            throw new IllegalArgumentException("properties");
+        }
+        this.properties = properties;
+    }
+
+    /**
+     * Interpolates the system properties within the model.
+     *
+     * @param mp    model property
+     * @param model model. May be null.
+     * @throws ModelInterpolationException
+     */
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if (mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+        mp.setValue((String) properties.get(mp.getExpression()));
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/TimestampPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/TimestampPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/TimestampPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/TimestampPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,33 @@
+package org.apache.maven.project.interpolation.policies;
+
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.model.Model;
+
+import java.util.Date;
+import java.text.DateFormat;
+
+public class TimestampPolicy implements ModelPropertyPolicy {
+
+    private String date;
+
+    public TimestampPolicy() {
+        DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG,
+                DateFormat.LONG);
+        date = dateFormatter.format(new Date());
+    }
+
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if (model == null) {
+            throw new IllegalArgumentException("model");
+        }
+        if (mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+
+        if (mp.getValue() == null && mp.getExpression().equals("build.timestamp")) {
+            mp.setValue(date);
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/UserPropertyPolicy.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/UserPropertyPolicy.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/UserPropertyPolicy.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/main/java/org/apache/maven/project/interpolation/policies/UserPropertyPolicy.java Wed May 14 19:53:13 2008
@@ -0,0 +1,60 @@
+package org.apache.maven.project.interpolation.policies;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.project.interpolation.ModelPropertyPolicy;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.model.Model;
+
+import java.util.Map;
+
+/**
+ * Interpolates user defined properties from the command line.
+ */
+public class UserPropertyPolicy implements ModelPropertyPolicy {
+
+    /**
+     * User properties specified on the command line
+     */
+    private Map userProperties;
+
+    /**
+     * Constructor
+     *
+     * @param userProperties the user properties defined on the command line
+     */
+    public UserPropertyPolicy(Map userProperties) {
+        if(userProperties == null) {
+            throw new IllegalArgumentException("userProperties");
+        }
+        this.userProperties = userProperties;
+    }
+
+    public void evaluate(ModelProperty mp, Model model) throws ModelInterpolationException {
+        if(mp == null) {
+            throw new IllegalArgumentException("mp");
+        }
+        
+        if (mp.getValue() == null) {
+            mp.setValue((String) userProperties.get(mp.getExpression()));
+        }
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/ModelPropertyInterpolatorTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/ModelPropertyInterpolatorTest.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/ModelPropertyInterpolatorTest.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/ModelPropertyInterpolatorTest.java Wed May 14 19:53:13 2008
@@ -0,0 +1,42 @@
+package org.apache.maven.project.interpolation;
+
+import junit.framework.TestCase;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Build;
+import org.apache.maven.project.interpolation.policies.PropertyPolicy;
+import org.apache.maven.project.interpolation.policies.BuildPropertyPolicy;
+import org.apache.maven.project.path.PathTranslator;
+import org.apache.maven.project.path.DefaultPathTranslator;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.util.Properties;
+import java.util.List;
+import java.util.ArrayList;
+import java.io.File;
+
+public class ModelPropertyInterpolatorTest extends TestCase {
+    
+    public void testPropertyInterpolation() throws Exception {
+
+        Properties properties = new Properties();
+        properties.setProperty("model.txt", "file:${project.build.sourceDirectory}/test.txt");
+
+        Build build = new Build();
+        build.setSourceDirectory("${project.basedir}/src/main/uml");
+
+        Model model = new Model();
+        model.setBuild(build);
+        model.setProperties(properties);
+
+        List policies = new ArrayList();
+        policies.add(new BuildPropertyPolicy(new File("/tmp")));
+
+        DefaultModelPropertyInterpolator modelInterpolator = new DefaultModelPropertyInterpolator();
+        model = modelInterpolator.interpolate(model, policies);
+                 System.out.println(FileUtils.normalize(model.getProperties().getProperty("model.txt")));
+        
+        PathTranslator pathTranslator = new DefaultPathTranslator();
+        assertEquals(new File("file:"+pathTranslator.alignToBaseDirectory("src/main/uml/test.txt",
+                new File("/tmp"))), new File(model.getProperties().getProperty("model.txt")));
+    }
+}

Added: maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicyTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicyTest.java?rev=656493&view=auto
==============================================================================
--- maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicyTest.java (added)
+++ maven/sandbox/branches/MNG-3536/maven-project/src/test/java/org/apache/maven/project/interpolation/policies/BuildPropertyPolicyTest.java Wed May 14 19:53:13 2008
@@ -0,0 +1,115 @@
+package org.apache.maven.project.interpolation.policies;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+
+import org.apache.maven.project.interpolation.ModelInterpolationException;
+import org.apache.maven.project.interpolation.ModelProperty;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Build;
+
+public class BuildPropertyPolicyTest extends TestCase {
+
+    public void testNullProjectDirectory() {
+        try {
+            new BuildPropertyPolicy(null);
+        } catch (IllegalArgumentException e) {
+            return;
+        }
+        fail("Should throw illegal argument exception on null project directory");
+    }
+
+    public void testEvaluateWithNullModel() throws ModelInterpolationException {
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        try {
+            policy.evaluate(new ModelProperty(), null);
+        } catch (IllegalArgumentException e) {
+            return;
+        }
+        fail("Should throw illegal argument exception on null model");
+    }
+
+    public void testEvaluateWithModelProperty() throws ModelInterpolationException {
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        try {
+            policy.evaluate(null, new Model());
+        } catch (IllegalArgumentException e) {
+            return;
+        }
+        fail("Should throw illegal argument exception on null model property");
+    }
+
+    public void testAlignPathofBuildSourceDirectory() throws ModelInterpolationException {
+        ModelProperty property = new ModelProperty();
+        property.setExpression("build.sourceDirectory");
+
+        Build build = new Build();
+        build.setSourceDirectory("src/main/java");
+        Model model = new Model();
+        model.setBuild(build);
+
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        policy.evaluate(property, model);
+        assertEquals(new File("/tmp/src/main/java").getAbsolutePath(), property.getValue());
+    }
+
+    public void testResolvedBuildSourceDirectory() throws ModelInterpolationException {
+        ModelProperty property = new ModelProperty();
+        property.setExpression("build.sourceDirectory");
+        property.setValue("/tmp/src/main/java");
+
+        Build build = new Build();
+        Model model = new Model();
+        model.setBuild(build);
+
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        policy.evaluate(property, model);
+        assertEquals(new File("/tmp/src/main/java"), new File(property.getValue()));
+    }
+
+    public void testInterpolatedBuildSourceDirectory() throws ModelInterpolationException {
+        ModelProperty property = new ModelProperty();
+        property.setExpression("build.sourceDirectory");
+        property.setValue("/tmp/${project.build.sourceDirectory}");
+
+        Build build = new Build();
+        build.setSourceDirectory("src/main/java");
+        Model model = new Model();
+        model.setBuild(build);
+
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        policy.evaluate(property, model);
+        assertEquals(new File("/tmp/src/main/java"), new File(property.getValue()));
+    }
+
+    public void testMultipleInterpolatedBuildSourceDirectory() throws ModelInterpolationException {
+        ModelProperty property = new ModelProperty();
+        property.setExpression("basedir");
+        property.setValue("${basedir}/${project.build.sourceDirectory}");
+
+        Build build = new Build();
+        build.setSourceDirectory("src/main/java");
+        Model model = new Model();
+        model.setBuild(build);
+
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        policy.evaluate(property, model);
+        assertEquals(new File("/tmp/${project.build.sourceDirectory}"), new File(property.getValue()));
+    }
+      
+    public void testUnknownProperty() throws ModelInterpolationException {
+        ModelProperty property = new ModelProperty();
+        property.setExpression("foobar");
+        property.setValue("${basedir}/${project.build.sourceDirectory}");
+
+        Build build = new Build();
+        build.setSourceDirectory("src/main/java");
+        Model model = new Model();
+        model.setBuild(build);
+
+        BuildPropertyPolicy policy = new BuildPropertyPolicy(new File("/tmp"));
+        policy.evaluate(property, model);
+        assertEquals(new File("${basedir}/${project.build.sourceDirectory}"), new File(property.getValue()));
+    }
+}