Thu, 24 May 2018 16:41:12 +0800
Merge
1 /*
2 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 /*
27 * Licensed Materials - Property of IBM
28 * RMI-IIOP v1.0
29 * Copyright IBM Corp. 1998 1999 All Rights Reserved
30 *
31 */
33 package sun.rmi.rmic.iiop;
35 import java.util.Hashtable;
37 /**
38 * A NameContext enables detection of strings which differ only
39 * in case.
40 *
41 * @author Bryan Atsatt
42 */
43 class NameContext {
45 private Hashtable table;
46 private boolean allowCollisions;
48 /**
49 * Get a context for the given name. Name may be null, in
50 * which case this method will return the default context.
51 */
52 public static synchronized NameContext forName (String name,
53 boolean allowCollisions,
54 BatchEnvironment env) {
56 NameContext result = null;
58 // Do we need to use the default context?
60 if (name == null) {
62 // Yes.
64 name = "null";
65 }
67 // Have we initialized our hashtable?
69 if (env.nameContexts == null) {
71 // Nope, so do it...
73 env.nameContexts = new Hashtable();
75 } else {
77 // Yes, see if we already have the requested
78 // context...
80 result = (NameContext) env.nameContexts.get(name);
81 }
83 // Do we have the requested context?
85 if (result == null) {
87 // Nope, so create and add it...
89 result = new NameContext(allowCollisions);
91 env.nameContexts.put(name,result);
92 }
94 return result;
95 }
97 /**
98 * Construct a context.
99 * @param allowCollisions true if case-sensitive name collisions
100 * are allowed, false if not.
101 */
102 public NameContext (boolean allowCollisions) {
103 this.allowCollisions = allowCollisions;
104 table = new Hashtable();
105 }
107 /**
108 * Add a name to this context. If constructed with allowCollisions
109 * false and a collision occurs, this method will throw an exception
110 * in which the message contains the string: "name" and "collision".
111 */
112 public void assertPut (String name) throws Exception {
114 String message = add(name);
116 if (message != null) {
117 throw new Exception(message);
118 }
119 }
121 /**
122 * Add a name to this context..
123 */
124 public void put (String name) {
126 if (allowCollisions == false) {
127 throw new Error("Must use assertPut(name)");
128 }
130 add(name);
131 }
133 /**
134 * Add a name to this context. If constructed with allowCollisions
135 * false and a collision occurs, this method will return a message
136 * string, otherwise returns null.
137 */
138 private String add (String name) {
140 // First, create a key by converting name to lowercase...
142 String key = name.toLowerCase();
144 // Does this key exist in the context?
146 Name value = (Name) table.get(key);
148 if (value != null) {
150 // Yes, so they match if we ignore case. Do they match if
151 // we don't ignore case?
153 if (!name.equals(value.name)) {
155 // No, so this is a case-sensitive match. Are we
156 // supposed to allow this?
158 if (allowCollisions) {
160 // Yes, make sure it knows that it collides...
162 value.collisions = true;
164 } else {
166 // No, so return a message string...
168 return new String("\"" + name + "\" and \"" + value.name + "\"");
169 }
170 }
171 } else {
173 // No, so add it...
175 table.put(key,new Name(name,false));
176 }
178 return null;
179 }
181 /**
182 * Get a name from the context. If it has collisions, the name
183 * will be converted as specified in section 5.2.7.
184 */
185 public String get (String name) {
187 Name it = (Name) table.get(name.toLowerCase());
188 String result = name;
190 // Do we need to mangle it?
192 if (it.collisions) {
194 // Yep, so do it...
196 int length = name.length();
197 boolean allLower = true;
199 for (int i = 0; i < length; i++) {
201 if (Character.isUpperCase(name.charAt(i))) {
202 result += "_";
203 result += i;
204 allLower = false;
205 }
206 }
208 if (allLower) {
209 result += "_";
210 }
211 }
213 return result;
214 }
216 /**
217 * Remove all entries.
218 */
219 public void clear () {
220 table.clear();
221 }
223 public class Name {
224 public String name;
225 public boolean collisions;
227 public Name (String name, boolean collisions) {
228 this.name = name;
229 this.collisions = collisions;
230 }
231 }
232 }