All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.translate.basic.BasicParseHandler Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate.basic;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;


import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.regnosys.rosetta.translate.ParseHandler;

public abstract class BasicParseHandler extends ParseHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(BasicParseHandler.class);

	public static class SetResult {
		private final SetResultStatus status;
		private final Object oldValue;
		private final Object newValue;

		public SetResult(SetResultStatus status, Object oldValue, Object newValue) {
			this.status = status;
			this.oldValue = oldValue;
			this.newValue = newValue;
		}

		public SetResultStatus getStatus() {
			return status;
		}

		public Object getOldValue() {
			return oldValue;
		}
		
		public Object getNewValue() {
			return newValue;
		}

		@Override
		public String toString() {
			return "SetResult [status=" + status + ", oldValue=" + oldValue + ", newValue=" + newValue + "]";
		}
	}

	public enum SetResultStatus {
		SUCCESS,
		DUPLICATE,
		PARSING,
		MAPPING
	}

	protected Consumer parentSetter;
	protected Supplier> parentSupplier;
	private final boolean allowsMultiple;
	private final boolean isCondition;
	private final String patternMatch;
	private final String patternReplace;
	private final boolean removeHtml;

	public BasicParseHandler(boolean allowsMultiple) {
		this.allowsMultiple = allowsMultiple;
		this.isCondition = false;
		this.patternMatch = null;
		this.patternReplace = null;
		this.removeHtml = false;
	}
	
	public BasicParseHandler(boolean allowsMultiple, boolean isCondition) {
		this.allowsMultiple = allowsMultiple;
		this.isCondition = isCondition;
		this.patternMatch = null;
		this.patternReplace = null;
		this.removeHtml = false;
	}

	public BasicParseHandler(boolean allowsMultiple, boolean isCondition, boolean removeHtml) {
		this.allowsMultiple = allowsMultiple;
		this.isCondition = isCondition;
		this.patternMatch = null;
		this.patternReplace = null;
		this.removeHtml = removeHtml;
	}
	
	public BasicParseHandler(boolean allowsMultiple, boolean isCondition, boolean removeHtml, String patternMatch, String patternReplace) {
		this.allowsMultiple = allowsMultiple;
		this.isCondition = isCondition;
		this.patternMatch = patternMatch;
		this.patternReplace = patternReplace;
		this.removeHtml = removeHtml;
	}

	/**
	 * @param string representation of the value to set this field to
	 * @return false if the value was already set (the supplier does not return null)
	 *         If the value was already set it is NOT changed
	 */
	public SetResult setValue(String value) {
		try {
			if (patternMatch!=null) {
				value = value.replaceAll(patternMatch, patternReplace);
			}
			if (removeHtml) {
				value = Jsoup.parse(value).text();
			}
			T newValue = convert(value);
			return setTypedValue(newValue);
		} catch (RuntimeException e) {
			LOGGER.info("Error setting a value [" + value + "]: " + e.getMessage());
			return new SetResult(SetResultStatus.PARSING, null, null);
		}
	}

	public SetResult setTypedValue(T newValue) {
		Optional prevValue = parentSupplier.get();
		if (prevValue.isPresent()) {
			return new SetResult(SetResultStatus.DUPLICATE, prevValue.orElse(null), newValue);
		}
		parentSetter.accept(newValue);
		return new SetResult(SetResultStatus.SUCCESS, null, newValue);
	}

	public void setParentSetter(Consumer parentSetter) {
		this.parentSetter = parentSetter;
	}

	public void setParentSupplier(Supplier> parentSupplier) {
		this.parentSupplier = parentSupplier;
	}

	protected abstract T convert(String value);

	public boolean allowsMultiple() {
		return allowsMultiple;
	}

	public boolean isCondition() {
		return isCondition;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy