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

com.regnosys.rosetta.translate.ParserHelper Maven / Gradle / Ivy

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

import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.google.common.collect.ImmutableList;
import com.regnosys.rosetta.common.translation.Path;
import com.regnosys.rosetta.common.translation.Path.PathElement;

public class ParserHelper {

	private static final char BOM = '\uFEFF';
	
	static char[] toCharsfromReader(Reader xmlReader) {
		try {
			return removeBom(IOUtils.toCharArray(xmlReader));
		} catch (IOException e) {
			throw new IngestException("Error reading from file", e);
		}
	}
	
	static MatchResult getMatchType(Collection> matchPaths, Deque path) {
		Path fullMatch = null;
		MatchType matchType = MatchType.NONE;
		for (List possibleMatch : matchPaths) {
			if (path.size() > possibleMatch.size()) {
				// the path we are looking for is longer than the candidate - it can't possibly be a match
				continue;
			}
			int i = 0;
			boolean matchSoFar = true;
			for (PathElement pathVal : path) {
				String matchVal = possibleMatch.get(i++);
				if (!matchVal.equals(pathVal.getPathName())) {
					// they aren't equal - this isn't a match
					matchSoFar = false;
					break;
				}
			}
			if (matchSoFar) {
				if (path.size() == possibleMatch.size()) {
					// you aren't allowed a full match and a partial match
					if (matchType == MatchType.PARTIAL) {
						matchType = MatchType.BOTH;
					}
					else {
						matchType = MatchType.FULL;
					}
					fullMatch = new Path(ImmutableList.copyOf(path));
				} else {
					// this is a partial match - the possible is longer than the current path
					// you aren't allowed a full match and a partial match
					if (matchType == MatchType.FULL || matchType == MatchType.BOTH) {
						matchType = MatchType.BOTH;
					}
					else {
						matchType = MatchType.PARTIAL;
					}
				}
			}
		}
		return new MatchResult(matchType, fullMatch);
	}
	
	private static char[] removeBom(char[] charArray) {
		if (charArray != null && charArray.length > 0 && charArray[0] == BOM)
			return Arrays.copyOfRange(charArray, 1, charArray.length);
		return charArray;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy