Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2014 smartics, Kronseder & Reiner GmbH
   *
   * 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 com.redhat.rcm.maven.plugin.buildmetadata.maven;
 
 
Fetches properties from a Maven project. This includes properties of the Maven project instance as well as properties from its properties section.

It is allowed to change the project passed to the helper instance after creation of the instance as long as the caller makes sure that it is not changed within a call of this instance. No synchronization is cared for by this instance.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class MavenPropertyHelper
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
  
All properties referring to the Maven project instance start with this prefix.

The value of this constant is .

 
   private static final String PROJECT_PROPERTY_NAME_PREFIX = "project.";

  
The length of the project property prefix.

The value of this constant is .

 
   private static final int PROJECT_PROPERTY_NAME_PREFIX_LENGTH =
 
   // --- members --------------------------------------------------------------
 
  
The project to fetch properties from.
 
   private final MavenProject project;
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Default constructor.

Parameters:
project the project to fetch properties from.
 
   public MavenPropertyHelper(final MavenProject project)
   {
     if (project == null)
     {
       throw new NullPointerException(
           "The property helper requires the project reference to be not 'null'.");
     }
     this. = project;
   }
 
   // ****************************** Inner Classes *****************************
 
   // ********************************* Methods ********************************
 
   // --- init -----------------------------------------------------------------
 
   // --- get&set --------------------------------------------------------------
 
   // --- business -------------------------------------------------------------
 
  
Returns the property value specified by the given name.

Parameters:
name the name of the property to return.
Returns:
the property value for the given name.
  public String getProperty(final String name)
  {
    if (name == null)
    {
      throw new NullPointerException(
          "Name of requested property must not be 'null'");
    }
    String value = null;
    if (isProjectProperty(name))
    {
      value = getProjectProperty(name);
    }
    if (value == null)
    {
      value = getPropertiesProperty(name);
    }
    return value;
  }
  private boolean isProjectProperty(final String name)
  {
    return name != null && name.length() >= 
           && name.startsWith();
  }

  
Returns a property from the project instance.
  private String getProjectProperty(final String name)
  {
    final String projectName =
    if (PropertyUtils.isReadable(projectName))
    {
      try
      {
        return getProjectProperty(projectName);
      }
      catch (final Exception e)
      {
        throw new IllegalStateException("Cannot access project property '"
                                        + name + "'.");
      }
    }
    return null;
  }
  private static String getProjectProperty(final MavenProject project,
      final String projectNamethrows IllegalAccessException,
  {
    if (project != null)
    {
      final Object value = PropertyUtils.getProperty(projectprojectName);
      if (value != null)
      {
        return String.valueOf(value);
      }
      else
      {
        return getProjectProperty(project.getParent(), projectName);
      }
    }
    return null;
  }

  
Returns a property from the properties section.
  private String getPropertiesProperty(final String name)
  {
    return getPropertiesProperty(name);
  }
  private static String getPropertiesProperty(final MavenProject project,
      final String name)
  {
    String value = null;
    final Properties properties = project.getProperties();
    if (properties != null)
    {
      value = properties.getProperty(name);
    }
    if (value == null)
    {
      value = getPropertiesPropertyFromParent(projectname);
    }
    return value;
  }
      final MavenProject projectfinal String name)
  {
    final String value;
    final MavenProject parentProject = project.getParent();
    if (parentProject != null)
    {
      value = getPropertiesProperty(parentProjectname);
    }
    else
    {
      value = null;
    }
    return value;
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X