001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.corba.se.idl.toJavaPortable;
027:
028: import com.sun.tools.corba.se.idl.toJavaPortable.NameModifier;
029:
030: public class NameModifierImpl implements NameModifier {
031: private String prefix;
032: private String suffix;
033:
034: public NameModifierImpl() {
035: this .prefix = null;
036: this .suffix = null;
037: }
038:
039: public NameModifierImpl(String prefix, String suffix) {
040: this .prefix = prefix;
041: this .suffix = suffix;
042: }
043:
044: /** Construct a NameModifier from a pattern of the form xxx%xxx.
045: * The pattern must consist of characters chosen from the
046: * set [A-Za-z0-9%$_]. In addition, the pattern must contain
047: * exactly one % character. Finally, if % is not the first char in
048: * the pattern, the pattern must not start with a number.
049: * <p>
050: * The semantics of makeName are very simply: just replace the
051: * % character with the base in the pattern and return the result.
052: */
053: public NameModifierImpl(String pattern) {
054: int first = pattern.indexOf('%');
055: int last = pattern.lastIndexOf('%');
056:
057: if (first != last)
058: throw new IllegalArgumentException(Util
059: .getMessage("NameModifier.TooManyPercent"));
060:
061: if (first == -1)
062: throw new IllegalArgumentException(Util
063: .getMessage("NameModifier.NoPercent"));
064:
065: for (int ctr = 0; ctr < pattern.length(); ctr++) {
066: char ch = pattern.charAt(ctr);
067: if (invalidChar(ch, ctr == 0)) {
068: char[] chars = new char[] { ch };
069: throw new IllegalArgumentException(Util.getMessage(
070: "NameModifier.InvalidChar", new String(chars)));
071: }
072: }
073:
074: // at this point, 0 <= first && first < pattern.length()
075: prefix = pattern.substring(0, first);
076: suffix = pattern.substring(first + 1);
077: }
078:
079: /** Return true if ch is invalid as a character in an
080: * identifier. If ch is a number, it is invalid only if
081: * isFirst is true.
082: */
083: private boolean invalidChar(char ch, boolean isFirst) {
084: if (('A' <= ch) && (ch <= 'Z'))
085: return false;
086: else if (('a' <= ch) && (ch <= 'z'))
087: return false;
088: else if (('0' <= ch) && (ch <= '9'))
089: return isFirst;
090: else if (ch == '%')
091: return false;
092: else if (ch == '$')
093: return false;
094: else if (ch == '_')
095: return false;
096: else
097: return true;
098: }
099:
100: public String makeName(String base) {
101: StringBuffer sb = new StringBuffer();
102:
103: if (prefix != null)
104: sb.append(prefix);
105:
106: sb.append(base);
107:
108: if (suffix != null)
109: sb.append(suffix);
110:
111: return sb.toString();
112: }
113: }
|