KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > AliasMapper


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.internal.core;
12
13 import java.io.*;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Vector JavaDoc;
16 import org.eclipse.osgi.framework.debug.Debug;
17
18 /**
19  * This class maps aliases.
20  */

21 public class AliasMapper {
22     private static Hashtable JavaDoc processorAliasTable;
23     private static Hashtable JavaDoc osnameAliasTable;
24
25     /**
26      * Constructor.
27      *
28      */

29     public AliasMapper() {
30     }
31
32     /**
33      * Return the master alias for the processor.
34      *
35      * @param processor Input name
36      * @return aliased name (if any)
37      */

38     public String JavaDoc aliasProcessor(String JavaDoc processor) {
39         processor = processor.toLowerCase();
40         if (processorAliasTable == null) {
41             InputStream in = getClass().getResourceAsStream(Constants.OSGI_PROCESSOR_ALIASES);
42             if (in != null) {
43                 try {
44                     processorAliasTable = initAliases(in);
45                 } finally {
46                     try {
47                         in.close();
48                     } catch (IOException ee) {
49                     }
50                 }
51             }
52         }
53         if (processorAliasTable != null) {
54             String JavaDoc alias = (String JavaDoc) processorAliasTable.get(processor);
55             if (alias != null) {
56                 processor = alias;
57             }
58         }
59         return (processor);
60     }
61
62     /**
63      * Return the master alias for the osname.
64      *
65      * @param osname Input name
66      * @return aliased name (if any)
67      */

68     public Object JavaDoc aliasOSName(String JavaDoc osname) {
69         osname = osname.toLowerCase();
70         if (osnameAliasTable == null) {
71             InputStream in = getClass().getResourceAsStream(Constants.OSGI_OSNAME_ALIASES);
72             if (in != null) {
73                 try {
74                     osnameAliasTable = initAliases(in);
75                 } finally {
76                     try {
77                         in.close();
78                     } catch (IOException ee) {
79                     }
80                 }
81             }
82         }
83         if (osnameAliasTable != null) {
84             Object JavaDoc aliasObject = osnameAliasTable.get(osname);
85             //String alias = (String) osnameAliasTable.get(osname);
86
if (aliasObject != null)
87                 if (aliasObject instanceof String JavaDoc) {
88                     osname = (String JavaDoc) aliasObject;
89                 } else {
90                     return (Vector JavaDoc) aliasObject;
91                 }
92         }
93         return (osname);
94     }
95
96     /**
97      * Read alias data and populate a Hashtable.
98      *
99      * @param in InputStream from which to read alias data.
100      * @return Hashtable of aliases.
101      */

102     protected static Hashtable JavaDoc initAliases(InputStream in) {
103         Hashtable JavaDoc aliases = new Hashtable JavaDoc(37);
104         try {
105             BufferedReader br;
106             try {
107                 br = new BufferedReader(new InputStreamReader(in, "UTF8")); //$NON-NLS-1$
108
} catch (UnsupportedEncodingException e) {
109                 br = new BufferedReader(new InputStreamReader(in));
110             }
111             while (true) {
112                 String JavaDoc line = br.readLine();
113                 if (line == null) /* EOF */{
114                     break; /* done */
115                 }
116                 Tokenizer tokenizer = new Tokenizer(line);
117                 String JavaDoc master = tokenizer.getString("# \t"); //$NON-NLS-1$
118
if (master != null) {
119                     aliases.put(master.toLowerCase(), master);
120                     parseloop: while (true) {
121                         String JavaDoc alias = tokenizer.getString("# \t"); //$NON-NLS-1$
122
if (alias == null) {
123                             break parseloop;
124                         }
125                         String JavaDoc lowerCaseAlias = alias.toLowerCase();
126                         Object JavaDoc storedMaster = aliases.get(lowerCaseAlias);
127                         if (storedMaster == null) {
128                             aliases.put(lowerCaseAlias, master);
129                         } else if (storedMaster instanceof String JavaDoc) {
130                             Vector JavaDoc newMaster = new Vector JavaDoc();
131                             newMaster.add(storedMaster);
132                             newMaster.add(master);
133                             aliases.put(lowerCaseAlias, newMaster);
134                         } else {
135                             ((Vector JavaDoc) storedMaster).add(master);
136                             aliases.put(lowerCaseAlias, storedMaster);
137                         }
138                     }
139                 }
140             }
141         } catch (IOException e) {
142             if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
143                 Debug.printStackTrace(e);
144             }
145         }
146         return (aliases);
147     }
148 }
149
Popular Tags