View Javadoc

1   /*
2    * Copyright 2008-2010 Digital Enterprise Research Institute (DERI)
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  
17  package org.deri.any23.extractor;
18  
19  import org.deri.any23.configuration.Configuration;
20  import org.deri.any23.configuration.DefaultConfiguration;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * This class models the parameters to be used to perform an extraction.
27   *
28   * @see org.deri.any23.Any23
29   * @author Michele Mostarda (mostarda@fbk.eu)
30   */
31  public class ExtractionParameters {
32  
33      /**
34       * @param c the underlying configuration.
35       * @return the default extraction parameters.
36       */
37      public static final ExtractionParameters newDefault(Configuration c) {
38          return new ExtractionParameters(c, ValidationMode.None);
39      }
40  
41      /**
42       * Creates the default extraction parameters with {@link DefaultConfiguration}.
43       *
44       * @return the default extraction parameters.
45       */
46      public static final ExtractionParameters newDefault() {
47          return new ExtractionParameters(DefaultConfiguration.singleton(), ValidationMode.None);
48      }
49  
50      /**
51       * Declares the supported validation actions.
52       */
53      public enum ValidationMode {
54          None,
55          Validate,
56          ValidateAndFix
57      }
58  
59      private final Configuration configuration;
60  
61      private final ValidationMode extractionMode;
62  
63      private final Map<String, Boolean> extractionFlags;
64  
65      private final Map<String,String> extractionProperties;
66  
67      /**
68       * Constructor.
69       *
70       * @param configuration underlying configuration.
71       * @param extractionMode specifies the required extraction mode.
72       * @param extractionFlags map of specific flags used for extraction. If not specified they will
73       *        be retrieved by the default {@link org.deri.any23.configuration.Configuration}.
74       * @param extractionProperties map of specific properties used for extraction. If not specified
75       *        they will ne retrieved by the default {@link org.deri.any23.configuration.Configuration}.
76       */
77      public ExtractionParameters(
78              Configuration configuration,
79              ValidationMode extractionMode,
80              Map<String, Boolean> extractionFlags,
81              Map<String,String> extractionProperties
82      ) {
83          if(configuration == null) {
84              throw new NullPointerException("Configuration cannot be null.");
85          }
86          if(extractionMode == null) {
87              throw new NullPointerException("Extraction mode cannot be null.");
88          }
89          this.configuration  = configuration;
90          this.extractionMode = extractionMode;
91          this.extractionFlags =
92                  extractionFlags == null
93                          ?
94                  new HashMap<String,Boolean>()
95                          :
96                  new HashMap<String,Boolean>(extractionFlags);
97          this.extractionProperties =
98                  extractionProperties == null
99                          ?
100                 new HashMap<String,String>()
101                         :
102                 new HashMap<String,String>(extractionProperties);
103     }
104 
105     /**
106      * Constructor.
107      *
108      * @param configuration underlying configuration.
109      * @param extractionMode specifies the required extraction mode.
110      */
111     public ExtractionParameters(Configuration configuration, ValidationMode extractionMode) {
112         this(configuration, extractionMode, null, null);
113     }
114 
115     /**
116      * Constructor, allows to set explicitly the value for flag
117      * {@link SingleDocumentExtraction#METADATA_NESTING_FLAG}.
118      *
119      * @param configuration the underlying configuration.
120      * @param extractionMode specifies the required extraction mode.
121      * @param nesting if <code>true</code> nesting triples will be expressed.
122      */
123     public ExtractionParameters(Configuration configuration, ValidationMode extractionMode, final boolean nesting) {
124         this(
125                 configuration,
126                 extractionMode,
127                 new HashMap<String, Boolean>(){{
128                     put(SingleDocumentExtraction.METADATA_NESTING_FLAG, nesting);
129                 }},
130                 null
131         );
132     }
133 
134     /**
135      * @return <code>true</code> if validation is active.
136      */
137     public boolean isValidate() {
138         return extractionMode == ValidationMode.Validate || extractionMode == ValidationMode.ValidateAndFix;
139     }
140 
141     /**
142      * @return <code>true</code> if fix is active.
143      */
144     public boolean isFix() {
145         return extractionMode == ValidationMode.ValidateAndFix;
146     }
147 
148     /**
149      * Returns the value of the specified extraction flag, if the flag is undefined
150      * it will be retrieved by the default {@link org.deri.any23.configuration.Configuration}.
151      *
152      * @param flagName name of flag.
153      * @return flag value.
154      */
155     public boolean getFlag(String flagName) {
156         final Boolean value = extractionFlags.get(flagName);
157         if(value == null) {
158             return configuration.getFlagProperty(flagName);
159         }
160         return value;
161     }
162 
163     /**
164      * Sets the value for an extraction flag.
165      *
166      * @param flagName flag name.
167      * @param value new flag value.
168      * @return the previous flag value.
169      */
170     public Boolean setFlag(String flagName, boolean value) {
171         checkPropertyExists(flagName);
172         validateValue("flag name", flagName);
173         return extractionFlags.put(flagName, value);
174     }
175 
176     /**
177      * Returns the value of the specified extraction property, if the property is undefined
178      * it will be retrieved by the default {@link org.deri.any23.configuration.Configuration}.
179      *
180      * @param propertyName the property name.
181      * @return the property value.
182      */
183     public String getProperty(String propertyName) {
184         final String propertyValue = extractionProperties.get(propertyName);
185         if(propertyValue == null) {
186             return configuration.getPropertyOrFail(propertyName);
187         }
188         return propertyValue;
189     }
190 
191     /**
192      * Sets the value for an extraction property.
193      *
194      * @param propertyName the property name.
195      * @param propertyValue the property value.
196      * @return the previous property value.
197      */
198     public String setProperty(String propertyName, String propertyValue) {
199         checkPropertyExists(propertyName);
200         validateValue("property name" , propertyName);
201         validateValue("property value", propertyValue);
202         return extractionProperties.put(propertyName, propertyValue);
203     }
204 
205     @Override
206     public boolean equals(Object obj) {
207         if(obj == null) {
208             return false;
209         }
210         if(obj == this) {
211             return true;
212         }
213         if(obj instanceof ExtractionParameters) {
214             ExtractionParameters other = (ExtractionParameters) obj;
215             return
216                     extractionMode == other.extractionMode
217                             &&
218                     extractionFlags.equals( other.extractionFlags)
219                             &&
220                     extractionProperties.equals( other.extractionProperties );
221         }
222         return false;
223     }
224 
225     @Override
226     public int hashCode() {
227         return extractionMode.hashCode() * 2 * extractionFlags.hashCode() * 3 * extractionProperties.hashCode() * 5;
228     }
229 
230     private void checkPropertyExists(String propertyName) {
231         if(! configuration.defineProperty(propertyName) ) {
232             throw new IllegalArgumentException(
233                     String.format("Property '%s' is unknown and cannot be set.", propertyName)
234             );
235         }
236     }
237 
238     private void validateValue(String desc, String value) {
239         if(value == null || value.trim().length() == 0)
240             throw new IllegalArgumentException( String.format("Invalid %s value: '%s', flagName", desc, value) );
241     }
242 }