| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
package uk.co.badgersinfoil.asxsd; |
| 6 |
|
|
| 7 |
|
import java.util.Arrays; |
| 8 |
|
import java.util.HashSet; |
| 9 |
|
import java.util.Set; |
| 10 |
|
import org.eclipse.emf.common.util.URI; |
| 11 |
|
import org.eclipse.xsd.XSDAttributeDeclaration; |
| 12 |
|
import uk.co.badgersinfoil.metaas.ActionScriptFactory; |
| 13 |
|
|
| 14 |
|
|
| 15 |
0 |
public class StringUtils { |
| 16 |
1 |
private static Set KEYWORDS = new HashSet(); |
| 17 |
|
static { |
| 18 |
1 |
KEYWORDS.addAll(Arrays.asList(new String[] { |
| 19 |
|
"private", |
| 20 |
|
"public", |
| 21 |
|
"protected" |
| 22 |
|
})); |
| 23 |
1 |
} |
| 24 |
|
|
| 25 |
|
public static String toPackageName(String targetNamespace) { |
| 26 |
75 |
URI uri = URI.createURI(targetNamespace); |
| 27 |
75 |
if (uri.isHierarchical()) { |
| 28 |
74 |
String name = reverseJoin(sanitize(uri.host().split("\\.")), "."); |
| 29 |
74 |
if (uri.hasPath()) { |
| 30 |
|
|
| 31 |
|
|
| 32 |
74 |
String path = uri.path().replaceAll("/+", "/").replaceFirst("\\A/", ""); |
| 33 |
74 |
if (path.length() > 0) { |
| 34 |
73 |
name = name + "." + join(sanitize(path.split("/")), "."); |
| 35 |
|
} |
| 36 |
|
} |
| 37 |
74 |
return name; |
| 38 |
|
} |
| 39 |
1 |
if ("urn".equalsIgnoreCase(uri.scheme())) { |
| 40 |
1 |
return join(sanitize(uri.opaquePart().split(":")), "."); |
| 41 |
|
} |
| 42 |
0 |
throw new IllegalArgumentException("unhandled scheme in uri: "+targetNamespace); |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
public static String[] sanitize(String[] strings) { |
| 46 |
414 |
for (int i=0; i<strings.length; i++) { |
| 47 |
266 |
strings[i] = sanitize(strings[i]); |
| 48 |
|
} |
| 49 |
148 |
return strings; |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
public static String sanitize(String string) { |
| 53 |
935 |
StringBuffer result = new StringBuffer(); |
| 54 |
935 |
if (!Character.isJavaIdentifierStart(string.charAt(0)) |
| 55 |
|
&& Character.isJavaIdentifierPart(string.charAt(0))) |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
|
|
| 59 |
22 |
result.append("_"); |
| 60 |
|
} |
| 61 |
6957 |
for (int i=0; i<string.length(); i++) { |
| 62 |
6022 |
char c = string.charAt(i); |
| 63 |
6022 |
if (Character.isJavaIdentifierPart(c)) { |
| 64 |
6021 |
result.append(c); |
| 65 |
|
} else { |
| 66 |
1 |
result.append("_"); |
| 67 |
|
} |
| 68 |
|
} |
| 69 |
935 |
return result.toString(); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
public static String reverseJoin(String[] strings, String delimiter) { |
| 73 |
74 |
StringBuffer result = new StringBuffer(); |
| 74 |
243 |
for (int i=strings.length-1; i>=0; i--) { |
| 75 |
169 |
result.append(strings[i]); |
| 76 |
169 |
if (i>0) { |
| 77 |
95 |
result.append(delimiter); |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
74 |
return result.toString(); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
public static String join(String[] strings, String delimiter) { |
| 84 |
74 |
StringBuffer result = new StringBuffer(); |
| 85 |
171 |
for (int i=0; i<strings.length; i++) { |
| 86 |
97 |
if (i>0) { |
| 87 |
23 |
result.append(delimiter); |
| 88 |
|
} |
| 89 |
97 |
result.append(strings[i]); |
| 90 |
|
} |
| 91 |
74 |
return result.toString(); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
public static boolean isValidIdent(String name) { |
| 95 |
23 |
if (KEYWORDS.contains(name)) { |
| 96 |
0 |
return false; |
| 97 |
|
} |
| 98 |
23 |
if (!Character.isJavaIdentifierStart(name.charAt(0))) { |
| 99 |
0 |
return false; |
| 100 |
|
} |
| 101 |
47 |
for (int i=1; i<name.length(); i++) { |
| 102 |
24 |
if (!Character.isJavaIdentifierPart(name.charAt(0))) { |
| 103 |
0 |
return false; |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
23 |
return true; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
public static String attrAccess(XSDAttributeDeclaration attrDecl) { |
| 110 |
23 |
if (isValidIdent(attrDecl.getName())) { |
| 111 |
23 |
return "@" + attrDecl.getName(); |
| 112 |
|
} |
| 113 |
0 |
return "@[" + ActionScriptFactory.str(attrDecl.getName()) + "]"; |
| 114 |
|
} |
| 115 |
|
} |