| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class ExtractionParameters { |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public static final ExtractionParameters newDefault(Configuration c) { |
| 39 | 0 | return new ExtractionParameters(c, ValidationMode.None); |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public static final ExtractionParameters newDefault() { |
| 48 | 0 | return new ExtractionParameters(DefaultConfiguration.singleton(), ValidationMode.None); |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 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 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 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 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public ExtractionParameters(Configuration configuration, ValidationMode extractionMode) { |
| 113 | 0 | this(configuration, extractionMode, null, null); |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 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 | |
|
| 137 | |
|
| 138 | |
public boolean isValidate() { |
| 139 | 0 | return extractionMode == ValidationMode.Validate || extractionMode == ValidationMode.ValidateAndFix; |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public boolean isFix() { |
| 146 | 0 | return extractionMode == ValidationMode.ValidateAndFix; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 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 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 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 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 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 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 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 | |
} |