1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/sun/rmi/rmic/iiop/NameContext.java Wed Apr 27 01:21:28 2016 +0800 1.3 @@ -0,0 +1,232 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * Licensed Materials - Property of IBM 1.31 + * RMI-IIOP v1.0 1.32 + * Copyright IBM Corp. 1998 1999 All Rights Reserved 1.33 + * 1.34 + */ 1.35 + 1.36 +package sun.rmi.rmic.iiop; 1.37 + 1.38 +import java.util.Hashtable; 1.39 + 1.40 +/** 1.41 + * A NameContext enables detection of strings which differ only 1.42 + * in case. 1.43 + * 1.44 + * @author Bryan Atsatt 1.45 + */ 1.46 +class NameContext { 1.47 + 1.48 + private Hashtable table; 1.49 + private boolean allowCollisions; 1.50 + 1.51 + /** 1.52 + * Get a context for the given name. Name may be null, in 1.53 + * which case this method will return the default context. 1.54 + */ 1.55 + public static synchronized NameContext forName (String name, 1.56 + boolean allowCollisions, 1.57 + BatchEnvironment env) { 1.58 + 1.59 + NameContext result = null; 1.60 + 1.61 + // Do we need to use the default context? 1.62 + 1.63 + if (name == null) { 1.64 + 1.65 + // Yes. 1.66 + 1.67 + name = "null"; 1.68 + } 1.69 + 1.70 + // Have we initialized our hashtable? 1.71 + 1.72 + if (env.nameContexts == null) { 1.73 + 1.74 + // Nope, so do it... 1.75 + 1.76 + env.nameContexts = new Hashtable(); 1.77 + 1.78 + } else { 1.79 + 1.80 + // Yes, see if we already have the requested 1.81 + // context... 1.82 + 1.83 + result = (NameContext) env.nameContexts.get(name); 1.84 + } 1.85 + 1.86 + // Do we have the requested context? 1.87 + 1.88 + if (result == null) { 1.89 + 1.90 + // Nope, so create and add it... 1.91 + 1.92 + result = new NameContext(allowCollisions); 1.93 + 1.94 + env.nameContexts.put(name,result); 1.95 + } 1.96 + 1.97 + return result; 1.98 + } 1.99 + 1.100 + /** 1.101 + * Construct a context. 1.102 + * @param allowCollisions true if case-sensitive name collisions 1.103 + * are allowed, false if not. 1.104 + */ 1.105 + public NameContext (boolean allowCollisions) { 1.106 + this.allowCollisions = allowCollisions; 1.107 + table = new Hashtable(); 1.108 + } 1.109 + 1.110 + /** 1.111 + * Add a name to this context. If constructed with allowCollisions 1.112 + * false and a collision occurs, this method will throw an exception 1.113 + * in which the message contains the string: "name" and "collision". 1.114 + */ 1.115 + public void assertPut (String name) throws Exception { 1.116 + 1.117 + String message = add(name); 1.118 + 1.119 + if (message != null) { 1.120 + throw new Exception(message); 1.121 + } 1.122 + } 1.123 + 1.124 + /** 1.125 + * Add a name to this context.. 1.126 + */ 1.127 + public void put (String name) { 1.128 + 1.129 + if (allowCollisions == false) { 1.130 + throw new Error("Must use assertPut(name)"); 1.131 + } 1.132 + 1.133 + add(name); 1.134 + } 1.135 + 1.136 + /** 1.137 + * Add a name to this context. If constructed with allowCollisions 1.138 + * false and a collision occurs, this method will return a message 1.139 + * string, otherwise returns null. 1.140 + */ 1.141 + private String add (String name) { 1.142 + 1.143 + // First, create a key by converting name to lowercase... 1.144 + 1.145 + String key = name.toLowerCase(); 1.146 + 1.147 + // Does this key exist in the context? 1.148 + 1.149 + Name value = (Name) table.get(key); 1.150 + 1.151 + if (value != null) { 1.152 + 1.153 + // Yes, so they match if we ignore case. Do they match if 1.154 + // we don't ignore case? 1.155 + 1.156 + if (!name.equals(value.name)) { 1.157 + 1.158 + // No, so this is a case-sensitive match. Are we 1.159 + // supposed to allow this? 1.160 + 1.161 + if (allowCollisions) { 1.162 + 1.163 + // Yes, make sure it knows that it collides... 1.164 + 1.165 + value.collisions = true; 1.166 + 1.167 + } else { 1.168 + 1.169 + // No, so return a message string... 1.170 + 1.171 + return new String("\"" + name + "\" and \"" + value.name + "\""); 1.172 + } 1.173 + } 1.174 + } else { 1.175 + 1.176 + // No, so add it... 1.177 + 1.178 + table.put(key,new Name(name,false)); 1.179 + } 1.180 + 1.181 + return null; 1.182 + } 1.183 + 1.184 + /** 1.185 + * Get a name from the context. If it has collisions, the name 1.186 + * will be converted as specified in section 5.2.7. 1.187 + */ 1.188 + public String get (String name) { 1.189 + 1.190 + Name it = (Name) table.get(name.toLowerCase()); 1.191 + String result = name; 1.192 + 1.193 + // Do we need to mangle it? 1.194 + 1.195 + if (it.collisions) { 1.196 + 1.197 + // Yep, so do it... 1.198 + 1.199 + int length = name.length(); 1.200 + boolean allLower = true; 1.201 + 1.202 + for (int i = 0; i < length; i++) { 1.203 + 1.204 + if (Character.isUpperCase(name.charAt(i))) { 1.205 + result += "_"; 1.206 + result += i; 1.207 + allLower = false; 1.208 + } 1.209 + } 1.210 + 1.211 + if (allLower) { 1.212 + result += "_"; 1.213 + } 1.214 + } 1.215 + 1.216 + return result; 1.217 + } 1.218 + 1.219 + /** 1.220 + * Remove all entries. 1.221 + */ 1.222 + public void clear () { 1.223 + table.clear(); 1.224 + } 1.225 + 1.226 + public class Name { 1.227 + public String name; 1.228 + public boolean collisions; 1.229 + 1.230 + public Name (String name, boolean collisions) { 1.231 + this.name = name; 1.232 + this.collisions = collisions; 1.233 + } 1.234 + } 1.235 +}