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;
 
 import java.io.File;
 import java.util.List;
 import java.util.Set;
 
 
Renders the build report.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class BuildReportRenderer
 { // NOPMD
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
  
The normalizer to be applied to file name value to remove the base dir prefix.
 
   private final FilePathNormalizer filePathNormalizer;

  
The sink to write to.
 
   private final Sink sink;

  
The resource bundle to access localized messages.
 
   private final ResourceBundle messages;

  
The properties file to read the build information from.
 
   private final File buildMetaDataPropertiesFile;

  
The list of a system properties or environment variables to be selected by the user to include into the build meta data properties.

The name is the name of the property, the section is relevant for placing the property in one of the following sections:

  • build.scm
  • build.dateAndVersion
  • build.runtime
  • build.java
  • build.maven
  • build.misc

If no valid section is given, the property is silently rendered in the build.misc section.

Parameter:
  private final List<Propertyproperties;
  // ****************************** Initializer *******************************
  // ****************************** Constructors ******************************

  
Default constructor.

Parameters:
filePathNormalizer the normalizer to be applied to file name value to remove the base dir prefix.
messages the resource bundle to access localized messages.
sink the sink to write to.
buildMetaDataPropertiesFile the properties file to read the build information from.
properties the list of a system properties or environment variables to be selected by the user to include into the build meta data properties.
  public BuildReportRenderer(final FilePathNormalizer filePathNormalizer,
      final ResourceBundle messagesfinal Sink sink,
      final File buildMetaDataPropertiesFilefinal List<Propertyproperties)
  {
    this. = filePathNormalizer;
    this. = sink;
    this. = messages;
    this. = buildMetaDataPropertiesFile;
    this. = properties;
  }
  // ****************************** Inner Classes *****************************
  // ********************************* Methods ********************************
  // --- init -----------------------------------------------------------------
  // --- get&set --------------------------------------------------------------
  // --- business -------------------------------------------------------------

  
Renders the report to the instance's sink.

Throws:
org.apache.maven.reporting.MavenReportException if the report cannot be rendered.
  public void renderReport() throws MavenReportException
  {
    .head();
    .title();
    .text(.getString("report.name"));
    .title_();
    .head_();
    .body();
    renderBody();
    .body_();
    .flush();
    .close();
  }

  
Renders the body of the report.

Throws:
org.apache.maven.reporting.MavenReportException if the report cannot be rendered.
  private void renderBody() throws MavenReportException
  {
    .section1();
    .text(.getString("report.name"));
    .paragraph();
    .text(.getString("report.description"));
    .paragraph_();
    final Properties buildMetaDataProperties = readBuildMetaDataProperties();
    renderSections(buildMetaDataProperties);
    renderFooter();
    .section1_();
  }
  private void renderSections(final Properties buildMetaDataProperties)
  {
    for (final Section section : .)
    {
      final List<Stringproperties = section.getProperties();
      if (hasPropertiesProvided(buildMetaDataPropertiesproperties))
      {
        final String sectionKey = section.getTitleKey();
        .sectionTitle2();
        .text(.getString(sectionKey));
        .sectionTitle2_();
        renderTableStart();
        for (final String key : properties)
        {
          renderCell(buildMetaDataPropertieskey);
        }
        renderSelectedPropertiesForSection(buildMetaDataPropertiessectionKey);
        renderTableEnd();
      }
    }
    renderNonStandardProperties(buildMetaDataProperties);
  }
  private boolean hasPropertiesProvided(
      final Properties buildMetaDataPropertiesfinal List<Stringproperties)
  {
    for (final String key : properties)
    {
      final Object value = buildMetaDataProperties.get(key);
      if (value != null && StringUtils.isNotBlank(String.valueOf(value)))
      {
        return true;
      }
    }
    final Set<StringselectedProperties = createSelectedProperties();
    for (final String key : selectedProperties)
    {
      final Object value = buildMetaDataProperties.get(key);
      if (value != null && StringUtils.isNotBlank(String.valueOf(value)))
      {
        return true;
      }
    }
    return false;
  }
      final Properties buildMetaDataPropertiesfinal String sectionKey)
  {
    if ( != null && !.isEmpty())
    {
      for (final Property property : )
      {
        if (sectionKey.equals(property.getSection()))
        {
          final String key = property.getName();
          renderCell(buildMetaDataPropertieskey);
        }
      }
    }
  }
  private void renderNonStandardProperties(
      final Properties buildMetaDataProperties)
  {
    final Properties nonStandardProperties =
        Constant.calcNonStandardProperties(buildMetaDataProperties);
    if (!nonStandardProperties.isEmpty())
    {
      .sectionTitle2();
      .sectionTitle2_();
      renderTableStart();
      for (final Enumeration<Objecten = nonStandardProperties.keys(); en
          .hasMoreElements();)
      {
        final String key = String.valueOf(en.nextElement());
        if (Constant.isIntendedForMiscSection(key))
        {
          renderCell(nonStandardPropertieskey);
        }
      }
      renderTableEnd();
    }
  }
  {
    final Set<StringselectedProperties = new HashSet<String>();
    if ( != null)
    {
      for (final Property property : )
      {
        selectedProperties.add(property.getName());
      }
    }
    return selectedProperties;
  }
  private void renderTableEnd()
  {
    .table_();
  }
  private void renderTableStart()
  {
    .table();
    .tableRow();
    .tableHeaderCell("200");
    final String topicLabel = .getString("report.table.header.topic");
    .text(topicLabel);
    final String valueLabel = .getString("report.table.header.value");
    .text(valueLabel);
    .tableRow_();
  }

  
Renders a single cell of the table.

Parameters:
buildMetaDataProperties build meta data properties to access the data to be rendered.
key the key to the data to be rendered.
  private void renderCell(final Properties buildMetaDataProperties,
      final String key)
  {
    final Object value = buildMetaDataProperties.get(key);
    if (value != null)
    {
      .tableRow();
      .tableCell();
      .text(getLabel(key));
      .tableCell_();
      .tableCell();
      {
        renderMultiTupleValue(buildMetaDataPropertiesvalue,
            .);
      }
      {
        final String filesValue = Constant.prettifyFilesValue(value);
        renderMultiValue(filesValue.);
      }
      else if (..equals(key))
      {
        renderMultiValue(value.);
      }
      else if (..equals(key))
      {
        renderMultiValue(value);
      }
      else
      {
        renderSingleValue(value);
      }
      .tableCell_();
      .tableRow_();
    }
  }
  private void renderSingleValue(final Object value)
  {
    final String stringValue = String.valueOf(value);
    if (stringValue != null && !isLink(stringValue))
    {
      .text(stringValue);
    }
    else
    {
      .link(stringValue);
      .text(stringValue);
      .link_();
    }
  }
  private boolean isLink(final String input)
  {
    return (input.startsWith("http://") || input.startsWith("https://"));
  }
  private void renderMultiTupleValue(final Properties buildMetaDataProperties,
      final Object valuefinal String subKeyPrefix)
  {
    final String stringValue = Constant.prettify((Stringvalue);
    if (hasMultipleValues(stringValue))
    {
      final StringTokenizer tokenizer = new StringTokenizer(stringValue",");
      while (tokenizer.hasMoreTokens())
      {
        final String profileName = tokenizer.nextToken().trim();
        final String subKey = subKeyPrefix + '.' + profileName;
        final Object subValue = buildMetaDataProperties.get(subKey);
        final String item = profileName + ':' + subValue;
        .listItem();
        .text(item);
        .listItem_();
      }
      .numberedList_();
    }
    else
    {
      .text(String.valueOf(value));
    }
  }
  private void renderMultiValue(final Object valuefinal Normalizer normalizer)
  {
    final String stringValue = Constant.prettify(ObjectUtils.toString(value));
    if (hasMultipleValues(stringValue))
    {
      final StringTokenizer tokenizer = new StringTokenizer(stringValue",");
      while (tokenizer.hasMoreTokens())
      {
        final String subValue = tokenizer.nextToken().trim();
        final String textValue = normalizer.normalize(subValue);
        .listItem();
        .text(textValue);
        .listItem_();
      }
      .numberedList_();
    }
    else
    {
      final String textValue = normalizer.normalize(stringValue);
      .text(textValue);
    }
  }
  private boolean hasMultipleValues(final String stringValue)
  {
    return stringValue.indexOf(',') != -1;
  }
  private String getLabel(final String key)
  {
    try
    {
      return .getString(key);
    }
    catch (final MissingResourceException e)
    {
      if ( != null)
      {
        for (final Property property : )
        {
          final String label = property.getLabel();
          if (StringUtils.isNotBlank(label)
              && key.equals(property.getMappedName()))
          {
            return label;
          }
        }
      }
      return key;
    }
  }

  
Renders the footer text.
  private void renderFooter()
  {
    final String footerText = .getString("report.footer");
    if (StringUtils.isNotBlank(footerText))
    {
      .rawText(footerText);
    }
  }

  
Reads the build meta data properties from the well known location.

Returns:
the read properties.
Throws:
org.apache.maven.reporting.MavenReportException if the properties cannot be read.
  {
    final Properties buildMetaDataProperties = new Properties();
    InputStream inStream = null;
    try
    {
      inStream =
          new BufferedInputStream(new FileInputStream(
              this.));
      buildMetaDataProperties.load(inStream);
    }
    catch (final IOException e)
    {
      throw new MavenReportException("Cannot read build properties file '"
                                     + this. + "'.",
          e);
    }
    finally
    {
      IOUtil.close(inStream);
    }
    return buildMetaDataProperties;
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X