All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.javadpkg.plugin.ReplacementsMaven Maven / Gradle / Ivy

The newest version!
/*
 * dpkg - Debian Package library and the Debian Package Maven plugin
 * (c) Copyright 2016 Gerrit Hohl
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package net.sourceforge.javadpkg.plugin;

import java.util.Map;

import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;

import net.sourceforge.javadpkg.plugin.reflect.PropertyReflection;
import net.sourceforge.javadpkg.plugin.reflect.PropertyReflectionException;
import net.sourceforge.javadpkg.plugin.reflect.PropertyReflectionImpl;
import net.sourceforge.javadpkg.replace.ReplacementException;
import net.sourceforge.javadpkg.replace.Replacements;


/**
 * 

* The replacements which are based on the plug-in properties, the * {@link MavenProject}, the environment variables (see {@link System#getenv()}) * and the system properties (see {@link System#getProperties()}). *

*

* If the value of a variable is requested by calling the * {@link #getValue(String)} method the lookup of the variable is performed the * following way: *

    *
  • Does the variable exist in the plug-in properties? If yes, return the * value.
  • *
  • Does the variable exist in the {@link MavenProject} properties (see * {@link MavenProject#getProperties()})? If yes, return the value.
  • *
  • Does the variable - removing the leading "project" / * "project." - exist in the {@link MavenProject} as property? If yes, * return the value.
  • *
  • Does the variable - removing the leading "env" / * "env." - exist as a environment variable? If yes, return the value. *
  • *
  • Does the variable exist as a system property? If yes, return the value. *
  • *
  • Otherwise return null. *

    *
*

* * @author Gerrit Hohl ([email protected]) * @version 1.0, 04.05.2016 by Gerrit Hohl */ public class ReplacementsMaven implements Replacements { /** The "project" variable. */ private static final String PROJECT_VAR = "project"; /** The prefix for all "project" variables. */ private static final String PROJECT_VAR_PREFIX = PROJECT_VAR + "."; /** The "settings" variable. */ private static final String SETTINGS_VAR = "settings"; /** The prefix for all "settings" variables. */ private static final String SETTINGS_VAR_PREFIX = SETTINGS_VAR + "."; /** The "env" variable. */ private static final String ENV_VAR = "env"; /** The prefix for all "env" variables. */ private static final String ENV_VAR_PREFIX = ENV_VAR + "."; /** The properties of the plug-in configuration. */ private Map pluginProperties; /** The Maven project. */ private MavenProject project; /** The settings of the local Maven installation. */ private Settings settings; /** The environment variables. */ private Map environment; /** The system properties. */ private Map systemProperties; /** The property reflection. */ private PropertyReflection propertyReflection; /** *

* Creates replacements. *

* * @param pluginProperties * The properties of the plug-in configuration. * @param project * The Maven project. * @param settings * The settings of the local Maven installation. * @param environment * The environment variables. * @param systemProperties * The system properties. * @throws IllegalArgumentException * If any of the parameters are null. */ public ReplacementsMaven(Map pluginProperties, MavenProject project, Settings settings, Map environment, Map systemProperties) { super(); if (pluginProperties == null) throw new IllegalArgumentException("Argument pluginProperties is null."); if (project == null) throw new IllegalArgumentException("Argument project is null."); if (settings == null) throw new IllegalArgumentException("Argument settings is null."); if (environment == null) throw new IllegalArgumentException("Argument environment is null."); if (systemProperties == null) throw new IllegalArgumentException("Argument systemProperties is null."); this.pluginProperties = pluginProperties; this.project = project; this.settings = settings; this.environment = environment; this.systemProperties = systemProperties; this.propertyReflection = new PropertyReflectionImpl(); } @Override public String getValue(String name) throws ReplacementException { Object value; if (name == null) throw new IllegalArgumentException("Argument name is null."); // ROADMAP Maybe implement a cache. value = this.getMapValue(this.pluginProperties, null, name); if (value == null) { value = this.getMapValue(this.project.getProperties(), null, name); } if (value == null) { if (PROJECT_VAR.equals(name) || name.startsWith(PROJECT_VAR_PREFIX)) { value = this.getProjectValue(name); } else if (SETTINGS_VAR.equals(name) || name.startsWith(SETTINGS_VAR_PREFIX)) { value = this.getSettingsValue(name); } else if (ENV_VAR.equals(name) || name.startsWith(ENV_VAR_PREFIX)) { value = this.getEnvironmentValue(name); } else { value = this.getMapValue(this.systemProperties, null, name); } } if (value instanceof String) return ((String) value); else if (value != null) return value.toString(); else return null; } /** *

* Returns the value for the specified project property. *

* * @param name * The name. * @return The value or null, if the property isn't set. * @throws ReplacementException * If an error occurs while determine the value. */ private Object getProjectValue(String name) throws ReplacementException { Object value; String path; if (PROJECT_VAR.equals(name)) return this.project; path = name.substring(PROJECT_VAR_PREFIX.length()); try { value = this.propertyReflection.getValue(this.project, path); } catch (PropertyReflectionException e) { throw new ReplacementException("Couldn't determine value for variable |" + name + "|: " + e.getMessage(), e); } return value; } /** *

* Returns the value for the specified settings property. *

* * @param name * The name. * @return The value or null, if the property isn't set. * @throws ReplacementException * If an error occurs while determine the value. */ private Object getSettingsValue(String name) throws ReplacementException { Object value; String path; if (SETTINGS_VAR.equals(name)) return this.settings; path = name.substring(SETTINGS_VAR_PREFIX.length()); try { value = this.propertyReflection.getValue(this.settings, path); } catch (PropertyReflectionException e) { throw new ReplacementException("Couldn't determine value for variable |" + name + "|: " + e.getMessage(), e); } return value; } /** *

* Returns the value for the specified environment property. *

* * @param name * The name. * @return The value or null, if no such property exists. */ private Object getEnvironmentValue(String name) { Object value; if (ENV_VAR.equals(name)) return this.environment; value = this.getMapValue(this.environment, ENV_VAR_PREFIX, name); return value; } /** *

* Returns the value for the specified key. *

* * @param map * The map. * @param prefix * The prefix within the name which must be removed before * accessing the map (optional). * @param name * The name of the key. * @return The value or null, if no key with the specified name * exists. */ private V getMapValue(Map map, String prefix, String name) { Object key; V value; if (map.isEmpty()) return null; // --- Get the key --- if ((prefix == null) || !name.startsWith(prefix)) { key = name; } else { key = name.substring(prefix.length()); } // --- Get the value --- value = map.get(key); return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy