zamf commented on code in PR #2769: URL: https://github.com/apache/tika/pull/2769#discussion_r3092858439
########## tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-ocr-encode-module/src/main/java/org/apache/tika/parser/ocrencode/EncodeOCRConfig.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.tika.parser.ocrencode; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashSet; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.tika.exception.TikaException; + +/** + * Configuration for EncodeOCRParser. This class is not thread safe and must be + * synchronized externally. + * <p> + * This class will remember all set* field forever, and on + * {@link #cloneAndUpdate(EncodeOCRConfig)}, it will update all the fields that + * have been set on the "update" config. Create a new update config for each + * parse unless you're only changing the same field(s) with every parse. + */ +public class EncodeOCRConfig implements Serializable { + + private static final long serialVersionUID = -1761942486845717891L; + + private static final Logger LOG = LoggerFactory.getLogger( + EncodeOCRConfig.class + ); + + // Maximum file size to submit file to ocr. + private long maxFileSizeToOcr = Integer.MAX_VALUE; + // Minimum file size to submit file to ocr. + private long minFileSizeToOcr = 0; + private boolean skipOcr = false; + private int maxImagesToOcr = 50; + private Set<String> userConfigured = new HashSet<>(); + private boolean inlineContent = false; + + public void setInlineContent(boolean inlineContent) { + this.inlineContent = inlineContent; + userConfigured.add("inlineContent"); + } + + public boolean isInlineContent() { + return inlineContent; + } + + /** + * @see #setMinFileSizeToOcr(long minFileSizeToOcr) + */ + public long getMinFileSizeToOcr() { + return minFileSizeToOcr; + } + + /** + * Set minimum file size to submit file to ocr. Default is 0. + */ + public void setMinFileSizeToOcr(long minFileSizeToOcr) { + this.minFileSizeToOcr = minFileSizeToOcr; + userConfigured.add("minFileSizeToOcr"); + } + + /** + * @see #setMaxFileSizeToOcr(long maxFileSizeToOcr) + */ + public long getMaxFileSizeToOcr() { + return maxFileSizeToOcr; + } + + /** + * Set maximum file size to submit file to ocr. Default is + * Integer.MAX_VALUE. + */ + public void setMaxFileSizeToOcr(long maxFileSizeToOcr) { + this.maxFileSizeToOcr = maxFileSizeToOcr; + userConfigured.add("maxFileSizeToOcr"); + } + + public boolean isSkipOcr() { + return skipOcr; + } + + /** + * If you want to turn off OCR at run time for a specific file, set this to + * <code>true</code> + * + * @param skipOcr + */ + public void setSkipOcr(boolean skipOcr) { + this.skipOcr = skipOcr; + userConfigured.add("skipOcr"); + } + + public int getMaxImagesToOcr() { + return maxImagesToOcr; + } + + /** + * Sets the maximum number of images to process with OCR per parse. Defaults + * to {@link Integer#MAX_VALUE}, which effectively disables the limit. + * + * @param maxImagesToOcr maximum number of images to OCR; must be >= 0 + */ + public void setMaxImagesToOcr(int maxImagesToOcr) { + if (maxImagesToOcr < 0) { + throw new IllegalArgumentException( + "maxImagesToOcr must be >= 0" + ); + } + this.maxImagesToOcr = maxImagesToOcr; + userConfigured.add("maxImagesToOcr"); + } + + public EncodeOCRConfig cloneAndUpdate(EncodeOCRConfig updates) + throws TikaException { + EncodeOCRConfig updated = new EncodeOCRConfig(); + for (Field field : this.getClass().getDeclaredFields()) { + if (Modifier.isFinal(field.getModifiers())) { + continue; + } else if (Modifier.isStatic(field.getModifiers())) { + continue; + } + if ("userConfigured".equals(field.getName())) { + continue; + } + if (updates.userConfigured.contains(field.getName())) { + try { + field.set(updated, field.get(updates)); + } catch (IllegalAccessException e) { + throw new TikaException( + "can't update " + field.getName(), + e + ); + } + } else { + try { + field.set(updated, field.get(this)); + } catch (IllegalAccessException e) { + throw new TikaException( + "can't update " + field.getName(), + e + ); + } + } + } Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
