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

org.eclipse.osgi.internal.framework.AliasMapper Maven / Gradle / Ivy

Go to download

AspectJ tools most notably contains the AspectJ compiler (AJC). AJC applies aspects to Java classes during compilation, fully replacing Javac for plain Java classes and also compiling native AspectJ or annotation-based @AspectJ syntax. Furthermore, AJC can weave aspects into existing class files in a post-compile binary weaving step. This library is a superset of AspectJ weaver and hence also of AspectJ runtime.

There is a newer version: 1.9.22.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.framework;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Map.Entry;
import org.eclipse.osgi.internal.debug.Debug;
import org.eclipse.osgi.internal.util.Tokenizer;

/**
 * This class maps aliases.
 */
public class AliasMapper {
	private static final Map> processorAliasTable = new HashMap<>();
	private static final Map processorCanonicalTable = new HashMap<>();
	private static final Map> osnameAliasTable = new HashMap<>();
	private static final Map osnameCanonicalTable = new HashMap<>();
	static {
		getTables("osname.aliases", osnameAliasTable, osnameCanonicalTable); //$NON-NLS-1$
		getTables("processor.aliases", processorAliasTable, processorCanonicalTable); //$NON-NLS-1$
	}

	private static void getTables(String resourceName, Map> aliasTable, Map canonicalTable) {
		InputStream in = AliasMapper.class.getResourceAsStream(resourceName);
		if (in != null) {
			try {
				initAliases(in, aliasTable, canonicalTable);
			} finally {
				try {
					in.close();
				} catch (IOException ee) {
					// nothing
				}
			}
		}
	}

	/**
	 * Return the master alias for the processor.
	 *
	 * @param processor Input name
	 * @return aliased name (if any)
	 */
	public Collection getProcessorAliases(String processor) {
		return getAlias(processor.toLowerCase(), processorAliasTable);
	}

	/**
	 * Return the master alias for the osname.
	 *
	 * @param osname Input name
	 * @return aliased name (if any)
	 */
	public Collection getOSNameAliases(String osname) {
		return getAlias(osname.toLowerCase(), osnameAliasTable);
	}

	public String getCanonicalOSName(String osname) {
		String lowerName = osname.toLowerCase();
		String result = osnameCanonicalTable.get(lowerName);
		if (result == null) {
			if (lowerName.startsWith("windows")) { //$NON-NLS-1$
				return "win32"; //$NON-NLS-1$
			}
		}
		return result == null ? osname : result;
	}

	public String getCanonicalProcessor(String processor) {
		String result = processorCanonicalTable.get(processor.toLowerCase());
		return result == null ? processor : result;
	}

	private Collection getAlias(String name, Map> aliasMap) {
		if (name == null) {
			return Collections.emptyList();
		}
		Collection aliases = aliasMap == null ? null : aliasMap.get(name);
		if (aliases != null) {
			return Collections.unmodifiableCollection(aliases);
		}
		return Collections.singletonList(name.toLowerCase());
	}

	/**
	 * Read alias data and populate a Map.
	 *
	 * @param in InputStream from which to read alias data.
	 * @return Map of aliases.
	 */
	private static Map> initAliases(InputStream in, Map> aliasTable, Map canonicalTable) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
			Map> multiMaster = new HashMap<>();
			while (true) {
				String line = br.readLine();
				if (line == null) /* EOF */ {
					break; /* done */
				}
				Tokenizer tokenizer = new Tokenizer(line);
				String master = tokenizer.getString("# \t"); //$NON-NLS-1$
				if (master != null) {
					String masterLower = master.toLowerCase();
					canonicalTable.put(masterLower, master);
					Collection aliasLine = new ArrayList<>(1);
					aliasLine.add(master);
					parseloop: while (true) {
						String alias = tokenizer.getString("# \t"); //$NON-NLS-1$
						if (alias == null) {
							break parseloop;
						}
						aliasLine.add(alias);
						String aliasLower = alias.toLowerCase();
						if (!canonicalTable.containsKey(aliasLower)) {
							canonicalTable.put(aliasLower, master);
						} else {
							// the alias has multiple masters just make its canonical name be the alias
							String existingMaster = canonicalTable.put(aliasLower, alias);
							Set masters = multiMaster.get(aliasLower);
							if (masters == null) {
								masters = new HashSet<>();
								multiMaster.put(aliasLower, masters);
								masters.add(existingMaster.toLowerCase());
							}
							masters.add(masterLower);
						}
					}
					aliasTable.put(masterLower, aliasLine);
				}
			}
			Map> multiMasterAliases = new HashMap<>(multiMaster.size());
			for (Entry> entry : multiMaster.entrySet()) {
				Set aliases = new HashSet<>();
				for (String master : entry.getValue()) {
					aliases.addAll(aliasTable.get(master));
				}
				multiMasterAliases.put(entry.getKey(), aliases);
			}
			aliasTable.putAll(multiMasterAliases);
		} catch (IOException e) {
			Debug.printStackTrace(e);
		}
		return aliasTable;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy