Skip to content
Permalink
Browse files
Produce best-of-breed repid to class name conversion
  • Loading branch information
ngmr committed May 27, 2015
1 parent 3407444 commit 9b0f17fe032a7499101f2c70aadab26f95384c7e
Showing 2 changed files with 56 additions and 187 deletions.
@@ -363,136 +363,4 @@ public ValueDescriptor getDescriptor(Class<?> clz, String repid,

return clzdesc;
}

public static String idToClass(String repid) {
// debug
logger.finer("idToClass " + repid);

if (repid.startsWith("IDL:")) {

ByteString id = new ByteString(repid);

try {
int end = id.lastIndexOf(':');
ByteString s = end < 0 ? id.substring(4) : id.substring(4, end);

ByteBuffer bb = new ByteBuffer();

//
// reverse order of dot-separated name components up
// till the first slash.
//
int firstSlash = s.indexOf('/');
if (firstSlash > 0) {
ByteString prefix = s.substring(0, firstSlash);
ByteString[] elems = prefix.split('.');

for (int i = elems.length - 1; i >= 0; i--) {
bb.append(fixName(elems[i]));
bb.append('.');
}

s = s.substring(firstSlash + 1);
}

//
// Append slash-separated name components ...
//
ByteString[] elems = s.split('/');
for (int i = 0; i < elems.length; i++) {
bb.append(fixName(elems[i]));
if (i != elems.length - 1)
bb.append('.');
}

String result = bb.toString();

logger.finer("idToClassName " + repid + " => " + result);

return result;
} catch (IndexOutOfBoundsException ex) {
logger.log(Level.FINE, "idToClass " + ex.getMessage(), ex);
return null;
}

} else if (repid.startsWith("RMI:")) {
int end = repid.indexOf(':', 4);
return end < 0 ? repid.substring(4) : repid.substring(4, end);
}

return null;
}

static String fixName(String name) {
return (new ByteString(name)).toString();
}

static ByteString fixName(ByteString name) {
if (keyWords.contains(name)) {
ByteBuffer buf = new ByteBuffer();
buf.append('_');
buf.append(name);
return buf.toByteString();
}

ByteString result = name;
ByteString current = name;

boolean match = true;
while (match) {

int len = current.length();
match = false;

for (ByteString reservedPostfix : reservedPostfixes) {
if (current.endsWith(reservedPostfix)) {
ByteBuffer buf = new ByteBuffer();
buf.append('_');
buf.append(result);
result = buf.toByteString();

int resultLen = reservedPostfix.length();
if (len > resultLen)
current = current.substring(0, len - resultLen);
else
current = new ByteString("");

match = true;
break;
}
}

}

return name;
}

private static final Set<ByteString> keyWords;
private static final Set<ByteString> reservedPostfixes;

static {
keyWords = createByteStringSet(
"abstract", "boolean", "break", "byte", "case",
"catch", "char", "class", "clone", "const", "continue",
"default", "do", "double", "else", "equals", "extends",
"false", "final", "finalize", "finally", "float", "for",
"getClass", "goto", "hashCode", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new",
"notify", "notifyAll", "null", "package", "private",
"protected", "public", "return", "short", "static", "super",
"switch", "synchronized", "this", "throw", "throws",
"toString", "transient", "true", "try", "void", "volatile",
"wait", "while");
reservedPostfixes = createByteStringSet(
"Helper", "Holder", "Operations", "POA", "POATie", "Package", "ValueFactory");
}

private static Set<ByteString> createByteStringSet(String...words) {
final Set<ByteString> set = new HashSet<>(words.length);
for (String word : words) {
set.add(new ByteString(word));
}
return Collections.unmodifiableSet(set);
}

}
@@ -1,5 +1,6 @@
package org.apache.yoko.util.cmsf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -8,6 +9,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import javax.rmi.CORBA.Util;

@@ -90,84 +92,83 @@ private static Class<?> toClass(final QueryImpl query) {
return result;
}

private static final Pattern dotPattern = Pattern.compile("\\.");
private static final Pattern slashPattern = Pattern.compile("/");

private static String toClassName(QueryImpl query) {
final String repid = query.repid;
final String suffix = query.suffix;

String result = null;

if (repid.startsWith("IDL:")) {
try {
StringBuffer buf = new StringBuffer();

int end = repid.lastIndexOf(':');
String s;
if (end < 0)
s = repid.substring(4);
else
s = repid.substring(4, end);

//
// If the ID contains a prefix, then fix each of the
// dotted components of the prefix
//
int slash = s.indexOf('/');
if (slash > 0) {
String prefix = s.substring(0, slash);
String rest = s.substring(slash + 1);
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
prefix, ".");
while (tokenizer.hasMoreTokens()) {
String tok = tokenizer.nextToken();
buf.append(fixName(tok));
buf.append('.');
}
s = rest;
}

//
// "Fix" the remainder of the ID
//
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
s, "/");
while (tokenizer.hasMoreTokens()) {
String tok = tokenizer.nextToken();
buf.append(fixName(tok));
if (tokenizer.hasMoreTokens())
buf.append('.');
}

result = buf.toString() + suffix;
} catch (IndexOutOfBoundsException ex) // if id has bad format
{
result = null;
}
}
else if (repid.startsWith ("RMI:")) {
int end = repid.indexOf (':', 4);
result = end < 0
? repid.substring (4)
: repid.substring (4, end);
result = idlToClassName(repid);
} else if (repid.startsWith("RMI:")) {
result = rmiToClassName(repid);
}
if (result != null) {
result += suffix;
result = removeUnicodeEscapes(result);
}
return result;
}

private static String rmiToClassName(final String repid) {
String result;
final int end = repid.indexOf (':', 4);
result = end < 0 ? repid.substring (4) : repid.substring (4, end);
return result;
}

private static String idlToClassName(final String repid) {
try {
final StringBuilder sb = new StringBuilder(repid.length());

final int end = repid.lastIndexOf(':');
String s = end < 0 ? repid.substring(4) : repid.substring(4, end);

//
// reverse order of dot-separated name components up
// till the first slash.
//
final int firstSlash = s.indexOf('/');
if (firstSlash > 0) {
String prefix = s.substring(0, firstSlash);
String[] elems = dotPattern.split(prefix);
Collections.reverse(Arrays.asList(elems)); //reverses the order in the underlying array - i.e. 'elems'
for (String elem: elems) {
sb.append(fixName(elem));
sb.append('.');
}

s = s.substring(firstSlash + 1);
}

//
// Append slash-separated name components ...
//
for (String elem: slashPattern.split(s)) {
sb.append(fixName(elem)).append('.');
}
sb.deleteCharAt(sb.length() - 1); // eliminate final '.'

return sb.toString();
} catch (IndexOutOfBoundsException ex) {
// id has bad format
return null;
}
}

private static String removeUnicodeEscapes(String in) {
// if no escape sequences are in the string, this is easy
int escape = in.indexOf("\\U");
if (escape < 0) {
return in;
}

// get a string buffer at least as long as the input string
StringBuffer out = new StringBuffer(in.length());
StringBuilder out = new StringBuilder(in.length());
int start = 0;

while (escape >= 0) {
// add the next real segment to the buffer
out.append(in.substring(start, escape));
// step over the escape sequence
escape += 2;

0 comments on commit 9b0f17f

Please sign in to comment.