Skip to main content
summaryrefslogtreecommitdiffstats
blob: 669cedef474eef8c6f782f559a7defffb3c716f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.framework.internal.core;

import java.io.*;
import java.util.Hashtable;
import java.util.Vector;
import org.eclipse.osgi.framework.debug.Debug;

/**
 * This class maps aliases.
 */
public class AliasMapper {
	private static Hashtable processorAliasTable;
	private static Hashtable osnameAliasTable;

	/**
	 * Constructor.
	 *
	 */
	public AliasMapper() {
	}

	/**
	 * Return the master alias for the processor.
	 *
	 * @param processor Input name
	 * @return aliased name (if any)
	 */
	public String aliasProcessor(String processor) {
		processor = processor.toLowerCase();
		if (processorAliasTable == null) {
			InputStream in = getClass().getResourceAsStream(Constants.OSGI_PROCESSOR_ALIASES);
			if (in != null) {
				try {
					processorAliasTable = initAliases(in);
				} finally {
					try {
						in.close();
					} catch (IOException ee) {
					}
				}
			}
		}
		if (processorAliasTable != null) {
			String alias = (String) processorAliasTable.get(processor);
			if (alias != null) {
				processor = alias;
			}
		}
		return (processor);
	}

	/**
	 * Return the master alias for the osname.
	 *
	 * @param osname Input name
	 * @return aliased name (if any)
	 */
	public Object aliasOSName(String osname) {
		osname = osname.toLowerCase();
		if (osnameAliasTable == null) {
			InputStream in = getClass().getResourceAsStream(Constants.OSGI_OSNAME_ALIASES);
			if (in != null) {
				try {
					osnameAliasTable = initAliases(in);
				} finally {
					try {
						in.close();
					} catch (IOException ee) {
					}
				}
			}
		}
		if (osnameAliasTable != null) {
			Object aliasObject = osnameAliasTable.get(osname);
			//String alias = (String) osnameAliasTable.get(osname);
			if (aliasObject != null)
				if (aliasObject instanceof String) {
					osname = (String) aliasObject;
				} else {
					return (Vector) aliasObject;
				}
		}
		return (osname);
	}

	/**
	 * Read alias data and populate a Hashtable.
	 *
	 * @param in InputStream from which to read alias data.
	 * @return Hashtable of aliases.
	 */
	protected static Hashtable initAliases(InputStream in) {
		Hashtable aliases = new Hashtable(37);
		try {
			BufferedReader br;
			try {
				br = new BufferedReader(new InputStreamReader(in, "UTF8")); //$NON-NLS-1$
			} catch (UnsupportedEncodingException e) {
				br = new BufferedReader(new InputStreamReader(in));
			}
			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) {
					aliases.put(master.toLowerCase(), master);
					parseloop: while (true) {
						String alias = tokenizer.getString("# \t"); //$NON-NLS-1$
						if (alias == null) {
							break parseloop;
						}
						String lowerCaseAlias = alias.toLowerCase();
						Object storedMaster = aliases.get(lowerCaseAlias);
						if (storedMaster == null) {
							aliases.put(lowerCaseAlias, master);
						} else if (storedMaster instanceof String) {
							Vector newMaster = new Vector();
							newMaster.add(storedMaster);
							newMaster.add(master);
							aliases.put(lowerCaseAlias, newMaster);
						} else {
							((Vector) storedMaster).add(master);
							aliases.put(lowerCaseAlias, storedMaster);
						}
					}
				}
			}
		} catch (IOException e) {
			if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
				Debug.printStackTrace(e);
			}
		}
		return (aliases);
	}
}

Back to the top