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

com.regnosys.ingest.test.framework.util.PathUtils Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
/*
 * Copyright (C) 2022 REGnosys Limited. All rights reserved This software is the confidential and proprietary information of REGnosys Limited.
 *
 * You shall not disclose such confidential information and shall use it only in accordance with the terms of the license agreement you entered into with REGnosys Limited.
 *
 * See https://ui.rc.rosetta-technology.io/#/documents/rosetta-core/RC-1
 */
package com.regnosys.granite.util;

import com.google.common.base.MoreObjects;
import com.regnosys.granite.ingestor.RosettaIngestionException;
import com.regnosys.rosetta.common.translation.Path;
import com.regnosys.rosetta.common.translation.Path.PathElement;
import com.rosetta.model.lib.path.RosettaPath;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PathUtils {

	public static RosettaPath toHierarchicalPath(Path path) {
		List collect = path.getElements().stream()
												.map(x -> RosettaPath.Element.create(x.getPathName(), toOptionalInt(x), x.getMetas()))
												.collect(Collectors.toList());

		return RosettaPath.createPathFromElements(collect);
	}

	private static OptionalInt toOptionalInt(PathElement x) {
		return x.getIndex().map(OptionalInt::of).orElse(OptionalInt.empty());
	}

	/**
	 * Parse contents of excluded paths file into list of path objects.
	 */
	public static List getExcludedPaths(String excludedPathsFile) {
		return Optional.ofNullable(excludedPathsFile)
					   .map(PathUtils::parseExcludedPathsFile)
					   .orElse(Collections.emptyList());
	}

	private static List parseExcludedPathsFile(String excludedPathsFile) {
		ClassLoader loader = MoreObjects.firstNonNull(
				Thread.currentThread().getContextClassLoader(),
				PathUtils.class.getClassLoader());
		URL resource = loader.getResource(excludedPathsFile);
		if (resource == null) {
			return Collections.emptyList();
		}

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
				Stream stream = reader.lines()) {
			return stream
				.filter(s -> !s.isEmpty())
				.map(p -> Path.parse(p, true)).collect(Collectors.toList());
		} catch (IOException e) {
			throw new RosettaIngestionException(String.format("Unable to load excluded xml paths for resource %s", excludedPathsFile), e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy