1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Util.java,v 1.15 2004/02/24 03:55:48 zongaro Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
  20. import java.util.StringTokenizer;
  21. import com.sun.org.apache.bcel.internal.generic.Type;
  22. import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  23. import com.sun.org.apache.xml.internal.utils.XMLChar;
  24. /**
  25. * @author Jacek Ambroziak
  26. * @author Santiago Pericas-Geertsen
  27. */
  28. public final class Util {
  29. private static char filesep;
  30. static {
  31. String temp = System.getProperty("file.separator", "/");
  32. filesep = temp.charAt(0);
  33. }
  34. public static String noExtName(String name) {
  35. final int index = name.lastIndexOf('.');
  36. return name.substring(0, index >= 0 ? index : name.length());
  37. }
  38. /**
  39. * Search for both slashes in order to support URLs and
  40. * files.
  41. */
  42. public static String baseName(String name) {
  43. int index = name.lastIndexOf('\\');
  44. if (index < 0) {
  45. index = name.lastIndexOf('/');
  46. }
  47. if (index >= 0)
  48. return name.substring(index + 1);
  49. else {
  50. int lastColonIndex = name.lastIndexOf(':');
  51. if (lastColonIndex > 0)
  52. return name.substring(lastColonIndex + 1);
  53. else
  54. return name;
  55. }
  56. }
  57. /**
  58. * Search for both slashes in order to support URLs and
  59. * files.
  60. */
  61. public static String pathName(String name) {
  62. int index = name.lastIndexOf('/');
  63. if (index < 0) {
  64. index = name.lastIndexOf('\\');
  65. }
  66. return name.substring(0, index + 1);
  67. }
  68. /**
  69. * Replace all illegal Java chars by '_'.
  70. */
  71. public static String toJavaName(String name) {
  72. if (name.length() > 0) {
  73. final StringBuffer result = new StringBuffer();
  74. char ch = name.charAt(0);
  75. result.append(Character.isJavaIdentifierStart(ch) ? ch : '_');
  76. final int n = name.length();
  77. for (int i = 1; i < n; i++) {
  78. ch = name.charAt(i);
  79. result.append(Character.isJavaIdentifierPart(ch) ? ch : '_');
  80. }
  81. return result.toString();
  82. }
  83. return name;
  84. }
  85. public static Type getJCRefType(String signature) {
  86. return Type.getType(signature);
  87. }
  88. public static String internalName(String cname) {
  89. return cname.replace('.', filesep);
  90. }
  91. public static void println(String s) {
  92. System.out.println(s);
  93. }
  94. public static void println(char ch) {
  95. System.out.println(ch);
  96. }
  97. public static void TRACE1() {
  98. System.out.println("TRACE1");
  99. }
  100. public static void TRACE2() {
  101. System.out.println("TRACE2");
  102. }
  103. public static void TRACE3() {
  104. System.out.println("TRACE3");
  105. }
  106. /**
  107. * Replace a certain character in a string with a new substring.
  108. */
  109. public static String replace(String base, char ch, String str) {
  110. return (base.indexOf(ch) < 0) ? base :
  111. replace(base, String.valueOf(ch), new String[] { str });
  112. }
  113. public static String replace(String base, String delim, String[] str) {
  114. final int len = base.length();
  115. final StringBuffer result = new StringBuffer();
  116. for (int i = 0; i < len; i++) {
  117. final char ch = base.charAt(i);
  118. final int k = delim.indexOf(ch);
  119. if (k >= 0) {
  120. result.append(str[k]);
  121. }
  122. else {
  123. result.append(ch);
  124. }
  125. }
  126. return result.toString();
  127. }
  128. /**
  129. * Replace occurances of '.', '-', '/' and ':'
  130. */
  131. public static String escape(String input) {
  132. return replace(input, ".-/:",
  133. new String[] { "$dot$", "$dash$", "$slash$", "$colon$" });
  134. }
  135. public static String getLocalName(String qname) {
  136. final int index = qname.lastIndexOf(":");
  137. return (index > 0) ? qname.substring(index + 1) : qname;
  138. }
  139. public static String getPrefix(String qname) {
  140. final int index = qname.lastIndexOf(":");
  141. return (index > 0) ? qname.substring(0, index) :
  142. Constants.EMPTYSTRING;
  143. }
  144. /**
  145. * Checks if the string is a literal (i.e. not an AVT) or not.
  146. */
  147. public static boolean isLiteral(String str) {
  148. final int length = str.length();
  149. for (int i = 0; i < length - 1; i++) {
  150. if (str.charAt(i) == '{' && str.charAt(i + 1) != '{') {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. /**
  157. * Checks if the string is valid list of qnames
  158. */
  159. public static boolean isValidQNames(String str) {
  160. if ((str != null) && (!str.equals(Constants.EMPTYSTRING))) {
  161. final StringTokenizer tokens = new StringTokenizer(str);
  162. while (tokens.hasMoreTokens()) {
  163. if (!XMLChar.isValidQName(tokens.nextToken())) {
  164. return false;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. }