Difference between revisions of "Extend OCR field parsers"

From OpenKM Documentation
Jump to: navigation, search
 
Line 25: Line 25:
 
  * StringParser
 
  * StringParser
 
  *  
 
  *  
* @author jllort
 
 
  *  
 
  *  
 
  */
 
  */

Latest revision as of 19:02, 7 September 2013


Nota clasica.png From OpenKM 6.4+ the Plug-in system of OpenKM allows you to quickly expand the functionality offered by the platform, extending the available OCR field parsers without having to rebuild the system to add/change the existing functionality.


Step 1 - Create class

  • Tthe new class should be under com.openkm.ocr.template.parser ( this is mandatory )
  • Important do not forget to set the tag @PluginImplementation before the class definition


package com.openkm.ocr.template.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import com.openkm.dao.bean.OCRTemplateField;
import com.openkm.ocr.template.OCRParserEmptyValueException;
import com.openkm.ocr.template.OCRTemplateException;
import com.openkm.ocr.template.OCRTemplateParser;

/**
 * StringParser
 * 
 * 
 */
@PluginImplementation
public class StringParser implements OCRTemplateParser {
	
	@Override
	public Object parse(OCRTemplateField otf, String text) throws OCRTemplateException, OCRParserEmptyValueException {
		if (text == null || text.equals("")) {
			throw new OCRParserEmptyValueException("Empty value");
		}
		
		if (otf.getPattern() == null || otf.getPattern().equals("")) {
			return text != null ? text.trim() : null;
		} else {
			Pattern pattern = Pattern.compile(otf.getPattern(), Pattern.UNICODE_CASE);
			Matcher matcher = pattern.matcher(text);
			
			if (matcher.matches()) {
				return text != null ? text.trim() : null;
			} else {
				throw new OCRTemplateException("Bad format, parse exception");
			}
		}
	}
	
	@Override
	public String getName() {
		return "String";
	}

	@Override
	public boolean isPatternRequired() {
		return false;
	}
}


Step 2 - Publish

Create your own jar and copy into $TOMCAT_HOME/lib folder.