Coverage Report - org.apache.any23.extractor.ExtractionParameters
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtractionParameters
0%
0/48
0%
0/36
2.6
ExtractionParameters$1
0%
0/3
N/A
2.6
ExtractionParameters$ValidationMode
0%
0/4
N/A
2.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.extractor;
 19  
 
 20  
 import org.apache.any23.configuration.Configuration;
 21  
 import org.apache.any23.configuration.DefaultConfiguration;
 22  
 
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * This class models the parameters to be used to perform an extraction.
 28  
  *
 29  
  * @see org.apache.any23.Any23
 30  
  * @author Michele Mostarda (mostarda@fbk.eu)
 31  
  */
 32  
 public class ExtractionParameters {
 33  
 
 34  
     /**
 35  
      * @param c the underlying configuration.
 36  
      * @return the default extraction parameters.
 37  
      */
 38  
     public static final ExtractionParameters newDefault(Configuration c) {
 39  0
         return new ExtractionParameters(c, ValidationMode.None);
 40  
     }
 41  
 
 42  
     /**
 43  
      * Creates the default extraction parameters with {@link org.apache.any23.configuration.DefaultConfiguration}.
 44  
      *
 45  
      * @return the default extraction parameters.
 46  
      */
 47  
     public static final ExtractionParameters newDefault() {
 48  0
         return new ExtractionParameters(DefaultConfiguration.singleton(), ValidationMode.None);
 49  
     }
 50  
 
 51  
     /**
 52  
      * Declares the supported validation actions.
 53  
      */
 54  0
     public enum ValidationMode {
 55  0
         None,
 56  0
         Validate,
 57  0
         ValidateAndFix
 58  
     }
 59  
 
 60  
     private final Configuration configuration;
 61  
 
 62  
     private final ValidationMode extractionMode;
 63  
 
 64  
     private final Map<String, Boolean> extractionFlags;
 65  
 
 66  
     private final Map<String,String> extractionProperties;
 67  
 
 68  
     /**
 69  
      * Constructor.
 70  
      *
 71  
      * @param configuration underlying configuration.
 72  
      * @param extractionMode specifies the required extraction mode.
 73  
      * @param extractionFlags map of specific flags used for extraction. If not specified they will
 74  
      *        be retrieved by the default {@link org.apache.any23.configuration.Configuration}.
 75  
      * @param extractionProperties map of specific properties used for extraction. If not specified
 76  
      *        they will ne retrieved by the default {@link org.apache.any23.configuration.Configuration}.
 77  
      */
 78  
     public ExtractionParameters(
 79  
             Configuration configuration,
 80  
             ValidationMode extractionMode,
 81  
             Map<String, Boolean> extractionFlags,
 82  
             Map<String,String> extractionProperties
 83  0
     ) {
 84  0
         if(configuration == null) {
 85  0
             throw new NullPointerException("Configuration cannot be null.");
 86  
         }
 87  0
         if(extractionMode == null) {
 88  0
             throw new NullPointerException("Extraction mode cannot be null.");
 89  
         }
 90  0
         this.configuration  = configuration;
 91  0
         this.extractionMode = extractionMode;
 92  0
         this.extractionFlags =
 93  
                 extractionFlags == null
 94  
                         ?
 95  
                 new HashMap<String,Boolean>()
 96  
                         :
 97  
                 new HashMap<String,Boolean>(extractionFlags);
 98  0
         this.extractionProperties =
 99  
                 extractionProperties == null
 100  
                         ?
 101  
                 new HashMap<String,String>()
 102  
                         :
 103  
                 new HashMap<String,String>(extractionProperties);
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Constructor.
 108  
      *
 109  
      * @param configuration underlying configuration.
 110  
      * @param extractionMode specifies the required extraction mode.
 111  
      */
 112  
     public ExtractionParameters(Configuration configuration, ValidationMode extractionMode) {
 113  0
         this(configuration, extractionMode, null, null);
 114  0
     }
 115  
 
 116  
     /**
 117  
      * Constructor, allows to set explicitly the value for flag
 118  
      * {@link SingleDocumentExtraction#METADATA_NESTING_FLAG}.
 119  
      *
 120  
      * @param configuration the underlying configuration.
 121  
      * @param extractionMode specifies the required extraction mode.
 122  
      * @param nesting if <code>true</code> nesting triples will be expressed.
 123  
      */
 124  
     public ExtractionParameters(Configuration configuration, ValidationMode extractionMode, final boolean nesting) {
 125  0
         this(
 126  
                 configuration,
 127  
                 extractionMode,
 128  0
                 new HashMap<String, Boolean>(){{
 129  0
                     put(SingleDocumentExtraction.METADATA_NESTING_FLAG, nesting);
 130  0
                 }},
 131  
                 null
 132  
         );
 133  0
     }
 134  
 
 135  
     /**
 136  
      * @return <code>true</code> if validation is active.
 137  
      */
 138  
     public boolean isValidate() {
 139  0
         return extractionMode == ValidationMode.Validate || extractionMode == ValidationMode.ValidateAndFix;
 140  
     }
 141  
 
 142  
     /**
 143  
      * @return <code>true</code> if fix is active.
 144  
      */
 145  
     public boolean isFix() {
 146  0
         return extractionMode == ValidationMode.ValidateAndFix;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Returns the value of the specified extraction flag, if the flag is undefined
 151  
      * it will be retrieved by the default {@link org.apache.any23.configuration.Configuration}.
 152  
      *
 153  
      * @param flagName name of flag.
 154  
      * @return flag value.
 155  
      */
 156  
     public boolean getFlag(String flagName) {
 157  0
         final Boolean value = extractionFlags.get(flagName);
 158  0
         if(value == null) {
 159  0
             return configuration.getFlagProperty(flagName);
 160  
         }
 161  0
         return value;
 162  
     }
 163  
 
 164  
     /**
 165  
      * Sets the value for an extraction flag.
 166  
      *
 167  
      * @param flagName flag name.
 168  
      * @param value new flag value.
 169  
      * @return the previous flag value.
 170  
      */
 171  
     public Boolean setFlag(String flagName, boolean value) {
 172  0
         checkPropertyExists(flagName);
 173  0
         validateValue("flag name", flagName);
 174  0
         return extractionFlags.put(flagName, value);
 175  
     }
 176  
 
 177  
     /**
 178  
      * Returns the value of the specified extraction property, if the property is undefined
 179  
      * it will be retrieved by the default {@link org.apache.any23.configuration.Configuration}.
 180  
      *
 181  
      * @param propertyName the property name.
 182  
      * @return the property value.
 183  
      * @throws IllegalArgumentException if the property name is not defined in configuration.
 184  
      */
 185  
     public String getProperty(String propertyName) {
 186  0
         final String propertyValue = extractionProperties.get(propertyName);
 187  0
         if(propertyValue == null) {
 188  0
             return configuration.getPropertyOrFail(propertyName);
 189  
         }
 190  0
         return propertyValue;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Sets the value for an extraction property.
 195  
      *
 196  
      * @param propertyName the property name.
 197  
      * @param propertyValue the property value.
 198  
      * @return the previous property value.
 199  
      */
 200  
     public String setProperty(String propertyName, String propertyValue) {
 201  0
         checkPropertyExists(propertyName);
 202  0
         validateValue("property name" , propertyName);
 203  0
         validateValue("property value", propertyValue);
 204  0
         return extractionProperties.put(propertyName, propertyValue);
 205  
     }
 206  
 
 207  
     @Override
 208  
     public boolean equals(Object obj) {
 209  0
         if(obj == null) {
 210  0
             return false;
 211  
         }
 212  0
         if(obj == this) {
 213  0
             return true;
 214  
         }
 215  0
         if(obj instanceof ExtractionParameters) {
 216  0
             ExtractionParameters other = (ExtractionParameters) obj;
 217  0
             return
 218  
                     extractionMode == other.extractionMode
 219  
                             &&
 220  
                     extractionFlags.equals( other.extractionFlags)
 221  
                             &&
 222  
                     extractionProperties.equals( other.extractionProperties );
 223  
         }
 224  0
         return false;
 225  
     }
 226  
 
 227  
     @Override
 228  
     public int hashCode() {
 229  0
         return extractionMode.hashCode() * 2 * extractionFlags.hashCode() * 3 * extractionProperties.hashCode() * 5;
 230  
     }
 231  
 
 232  
     private void checkPropertyExists(String propertyName) {
 233  0
         if(! configuration.defineProperty(propertyName) ) {
 234  0
             throw new IllegalArgumentException(
 235  
                     String.format("Property '%s' is unknown and cannot be set.", propertyName)
 236  
             );
 237  
         }
 238  0
     }
 239  
 
 240  
     private void validateValue(String desc, String value) {
 241  0
         if(value == null || value.trim().length() == 0)
 242  0
             throw new IllegalArgumentException( String.format("Invalid %s: '%s'", desc, value) );
 243  0
     }
 244  
 }