Coverage Report - de.smartics.properties.reports.PropertiesPropertyReport
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesPropertyReport
0%
0/27
0%
0/8
2.2
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package de.smartics.properties.reports;
 17  
 
 18  
 import java.io.OutputStream;
 19  
 
 20  
 import org.apache.commons.configuration.ConfigurationException;
 21  
 import org.apache.commons.configuration.PropertiesConfiguration;
 22  
 
 23  
 import de.smartics.properties.api.core.domain.PropertyDescriptor;
 24  
 import de.smartics.properties.api.core.domain.PropertyExpression;
 25  
 import de.smartics.properties.report.app.ReportException;
 26  
 import de.smartics.properties.report.data.PropertyReportItem;
 27  
 import de.smartics.properties.report.data.PropertyReportSet;
 28  
 import de.smartics.util.lang.Arg;
 29  
 
 30  
 /**
 31  
  * Stores every report item in a property set.
 32  
  */
 33  
 public final class PropertiesPropertyReport extends AbstractPropertyReport
 34  
 {
 35  
   // ********************************* Fields *********************************
 36  
 
 37  
   // --- constants ------------------------------------------------------------
 38  
 
 39  
   // --- members --------------------------------------------------------------
 40  
 
 41  
   /**
 42  
    * The current comment to prepend to the collected properties.
 43  
    */
 44  
   private String currentComment;
 45  
 
 46  
   /**
 47  
    * The currently collected properties.
 48  
    */
 49  0
   private final PropertiesConfiguration currentProperties =
 50  
       new PropertiesConfiguration();
 51  
 
 52  
   /**
 53  
    * The stream to write to.
 54  
    */
 55  
   private final OutputStream output;
 56  
 
 57  
   // ****************************** Initializer *******************************
 58  
 
 59  
   // ****************************** Constructors ******************************
 60  
 
 61  
   /**
 62  
    * Constructor.
 63  
    *
 64  
    * @param output the stream to write to.
 65  
    * @see de.smartics.properties.reports.AbstractPropertyReport#AbstractPropertyReport()
 66  
    */
 67  
   public PropertiesPropertyReport(final OutputStream output)
 68  0
   {
 69  0
     this.output = Arg.checkNotNull("output", output);
 70  0
   }
 71  
 
 72  
   // ****************************** Inner Classes *****************************
 73  
 
 74  
   // ********************************* Methods ********************************
 75  
 
 76  
   // --- init -----------------------------------------------------------------
 77  
 
 78  
   // --- get&set --------------------------------------------------------------
 79  
 
 80  
   // --- business -------------------------------------------------------------
 81  
 
 82  
   @Override
 83  
   public void handle(final PropertyReportSet reportSet) throws ReportException
 84  
   {
 85  0
     flush(reportSet.getName());
 86  
 
 87  0
     final StringBuilder buffer = new StringBuilder(256);
 88  0
     buffer.append(reportSet.getName()).append('\n')
 89  
         .append(reportSet.getComment());
 90  0
     currentComment = buffer.toString();
 91  0
   }
 92  
 
 93  
   private void flush(final String name) throws ReportException
 94  
   {
 95  0
     if (!currentProperties.isEmpty())
 96  
     {
 97  
       try
 98  
       {
 99  0
         currentProperties.setHeader(currentComment);
 100  0
         currentProperties.save(output, "ISO8859-1");
 101  
       }
 102  0
       catch (final ConfigurationException e)
 103  
       {
 104  0
         final String message =
 105  
             name != null ? "Cannot write report set '" + name + "'."
 106  
                 : "Cannot flush.";
 107  0
         throw new ReportException(message, e);
 108  0
       }
 109  
 
 110  0
       currentProperties.clear();
 111  
     }
 112  0
   }
 113  
 
 114  
   @Override
 115  
   public void handle(final PropertyReportItem item) throws ReportException
 116  
   {
 117  0
     super.handle(item);
 118  
 
 119  0
     final PropertyDescriptor descriptor = item.getDescriptor();
 120  0
     final String key = descriptor.getKey().toString();
 121  
 
 122  0
     final PropertyExpression expression = descriptor.getDefaultExpression();
 123  0
     final String value =
 124  
         expression != null && expression.getExpression() != null ? expression
 125  
             .getExpression() : "";
 126  
 
 127  0
     currentProperties.setProperty(key, value);
 128  0
   }
 129  
 
 130  
   /**
 131  
    * Flushes the latest report set and items.
 132  
    */
 133  
   public void flush()
 134  
   {
 135  0
     flush(null);
 136  0
   }
 137  
 
 138  
   // --- object basics --------------------------------------------------------
 139  
 
 140  
 }