001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.drapgen.generate.xslt;
017:
018: import java.util.regex.Matcher;
019: import java.util.regex.Pattern;
020:
021: import org.directwebremoting.drapgen.generate.gi.GiProject;
022:
023: /**
024: * Functions to make up for the inadequacies of XSTL
025: * @author Joe Walker [joe at getahead dot ltd dot uk]
026: */
027: public class ExtensionFunctions {
028: /**
029: * Trim the string by removing whitespace from the start and end of the
030: * string.
031: * @see java.lang.String#trim()
032: * @param source The string to trim
033: * @return The trimmed version of the input string
034: */
035: public static String trim(String source) {
036: return source.trim();
037: }
038:
039: /**
040: * Lookup the class in the class cache to detect if has been marked as
041: * having a children.
042: * @param className The name of the class to check for children
043: * @return true if the named class is a superclass
044: */
045: public static boolean isSuperClass(String className) {
046: return registry.getClassByName(className).isSuperClass();
047: }
048:
049: /**
050: * Convert a jsx3 classname into a Java classname
051: * @param original The name of the jsx3 class
052: * @return The name of a comparable Java class
053: */
054: public static String normalizeClassname(String original) {
055: for (String[] swap : nativeSwaps) {
056: Pattern pattern = Pattern.compile(swap[SEARCH],
057: Pattern.UNIX_LINES);
058: Matcher matcher = pattern.matcher(original);
059: if (matcher.find()) {
060: return matcher.replaceAll(swap[REPLACE]);
061: }
062: }
063:
064: return original;
065: }
066:
067: /**
068: * Convert a jsx3 classname into a Java classname
069: * @param original The name of the jsx3 class
070: * @return The name of a comparable Java class
071: */
072: public static String normalizeNonNativeClassname(String original) {
073: for (String[] swap : nonNativeSwaps) {
074: Pattern pattern = Pattern.compile(swap[SEARCH],
075: Pattern.UNIX_LINES);
076: Matcher matcher = pattern.matcher(original);
077: if (matcher.find()) {
078: return matcher.replaceAll(swap[REPLACE]);
079: }
080: }
081:
082: return original;
083: }
084:
085: /**
086: * @param registry the generate to set
087: */
088: public static void setJsClassloader(GiProject registry) {
089: ExtensionFunctions.registry = registry;
090: }
091:
092: private static GiProject registry;
093:
094: private static final int SEARCH = 0;
095: private static final int REPLACE = 1;
096: private static final String[][] nativeSwaps = new String[][] {
097: { "^$", "Object" },
098: { "^Number$", "int" },
099: { "^jsx3\\.lang\\.Class$", "Class" },
100: { "^Array$", "Object[]" },
101: { "^Function$", "org.directwebremoting.proxy.CodeBlock" },
102: { "^jsx3\\.app\\.Properties$", "java.util.Properties" },
103: { "^jsx3\\.lang\\.Exception$", "Exception" },
104: { "^jsx3\\.Boolean$", "Boolean" },
105: { "^HTMLElement$", "String" },
106: { "^HTMLDocument$", "String" },
107: { "^VectorStroke$", "String" },
108: { "^jsx3\\.lang\\.IllegalArgumentException$",
109: "IllegalArgumentException" },
110: { "^jsx3\\.(.*)$", "org.directwebremoting.proxy.jsx3.$1" }, };
111: private static final String[][] nonNativeSwaps = new String[][] {
112: { "^$", "Object" },
113: { "^float$", "Float" },
114: { "^int$", "Integer" },
115: { "^boolean$", "Boolean" },
116: { "^Number$", "Integer" },
117: { "^jsx3\\.lang\\.Class$", "Class" },
118: { "^Array$", "Object[]" },
119: { "^Function$", "org.directwebremoting.proxy.CodeBlock" },
120: { "^jsx3\\.app\\.Properties$", "java.util.Properties" },
121: { "^jsx3\\.lang\\.Exception$", "Exception" },
122: { "^jsx3\\.Boolean$", "Boolean" },
123: { "^HTMLElement$", "String" },
124: { "^HTMLDocument$", "String" },
125: { "^VectorStroke$", "String" },
126: { "^jsx3\\.lang\\.IllegalArgumentException$",
127: "IllegalArgumentException" },
128: { "^jsx3\\.(.*)$", "org.directwebremoting.proxy.jsx3.$1" }, };
129: }
|