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