Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2013 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.io;
 
 import java.io.File;
 
 
Helper to handle the build meta data properties file.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class BuildPropertiesFileHelper
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
  
The logger to use.
 
   private final Log log;

  
The file to write to.
 
   private final File propertiesOutputFile;

  
The normalizer to be applied to file name value to remove the base dir prefix.
 
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Default constructor.

Parameters:
log the logger to use.
propertiesOutputFile the file to write to.
filePathNormalizer the normalizer to be applied to file name value to remove the base dir prefix.
 
   public BuildPropertiesFileHelper(final Log log,
       final File propertiesOutputFile,
       final FilePathNormalizer filePathNormalizer)
   {
     this. = log;
     this. = propertiesOutputFile;
     this. = filePathNormalizer;
   }
 
   // ****************************** Inner Classes *****************************
 
   // ********************************* Methods ********************************
 
   // --- init -----------------------------------------------------------------
 
   // --- get&set --------------------------------------------------------------
 
   // --- business -------------------------------------------------------------

  
Writes the build meta data properties to the target file.

Parameters:
buildMetaDataProperties the properties to write.
Returns:
the reference to the written file.
Throws:
org.apache.maven.plugin.MojoExecutionException on any problem encountered while writing the properties.
  public File writePropertiesFile(final Properties buildMetaDataProperties)
  {
    final File buildMetaDataFile =
    if (.isInfoEnabled())
    {
      .info("Writing properties '" + buildMetaDataFile.getAbsolutePath()
               + "'...");
    }
    OutputStream out = null;
    try
    {
      out = new BufferedOutputStream(new FileOutputStream(buildMetaDataFile));
      final String comments = "Created by buildmetadata-maven-plugin.";
      final Properties sortedBuildMetaDataProperties =
          SortedProperties.createSorted(buildMetaDataProperties);
      normalizeProperties(sortedBuildMetaDataProperties);
      sortedBuildMetaDataProperties.store(outcomments);
    }
    catch (final FileNotFoundException e)
    {
      final String message =
          "Cannot find file '" + buildMetaDataFile
              + "' to write properties to.";
      throw MojoUtils.createException(emessage);
    }
    catch (final IOException e)
    {
      final String message =
          "Cannot write properties to file '" + buildMetaDataFile + "'.";
      throw MojoUtils.createException(emessage);
    }
    finally
    {
      IOUtil.close(out);
    }
    return buildMetaDataFile;
  }
  private void normalizeProperties(final Properties buildMetaDataProperties)
  {
    final String filters =
        buildMetaDataProperties.getProperty(.);
    if (filters != null)
    {
      final String slashedFilters = filters.trim().replace('\\''/');
      final String slashedBaseDir =
          .getBaseDir().replace('\\''/');
      final String normBaseDir =
          slashedBaseDir.endsWith("/") ? slashedBaseDir : slashedBaseDir + '/';
      final String normFilters =
          StringUtils.replace(slashedFiltersnormBaseDir"");
      buildMetaDataProperties.setProperty(.,
          normFilters);
    }
  }

  
Creates the properties file for the build meta data. If the directory to place it in is not present, it will be created.

Returns:
the file to write the build properties to.
Throws:
org.apache.maven.plugin.MojoExecutionException if the output directory is not present and cannot be created.
  private File createBuildMetaDataFile(final File propertiesOutputFile)
  {
    final File outputDirectory = propertiesOutputFile.getParentFile();
    if (!outputDirectory.exists())
    {
      final boolean created = outputDirectory.mkdirs();
      if (!created)
      {
        throw new MojoExecutionException("Cannot create output directory '"
                                         + outputDirectory + "'.");
      }
    }
    return propertiesOutputFile;
  }

  
Reads the build properties file from stream. The properties file is passed to this instance via the constructor propertiesOutputFile.

Parameters:
buildMetaDataProperties the properties instance to append the read properties to.
Throws:
org.apache.maven.plugin.MojoExecutionException if the properties cannot be read.
  public void readBuildPropertiesFile(final Properties buildMetaDataProperties)
  {
    InputStream inStream = null;
    try
    {
      inStream =
      buildMetaDataProperties.load(inStream);
    }
    catch (final IOException e)
    {
      throw new MojoExecutionException(
          "Cannot read provided properties file: "
              + .getAbsolutePath(), e);
    }
    finally
    {
      IOUtil.close(inStream);
    }
  }

  
Fetches the project properties and if null returns a new empty properties instance that is associated with the project.

Parameters:
project the project whose properties are requested.
Returns:
the properties of the project.
  public Properties getProjectProperties(final MavenProject project)
  {
    Properties projectProperties = project.getProperties();
    if (projectProperties == null)
    {
      projectProperties = new Properties();
      project.getModel().setProperties(projectProperties);
    }
    return projectProperties;
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X