KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > util > Util


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
20 package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import com.sun.org.apache.bcel.internal.generic.Type;
25 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
26 import com.sun.org.apache.xml.internal.utils.XMLChar;
27
28 /**
29  * @author Jacek Ambroziak
30  * @author Santiago Pericas-Geertsen
31  */

32 public final class Util {
33     private static char filesep;
34
35     static {
36     String JavaDoc temp = System.getProperty("file.separator", "/");
37     filesep = temp.charAt(0);
38     }
39
40     public static String JavaDoc noExtName(String JavaDoc name) {
41     final int index = name.lastIndexOf('.');
42     return name.substring(0, index >= 0 ? index : name.length());
43     }
44
45     /**
46      * Search for both slashes in order to support URLs and
47      * files.
48      */

49     public static String JavaDoc baseName(String JavaDoc name) {
50     int index = name.lastIndexOf('\\');
51     if (index < 0) {
52         index = name.lastIndexOf('/');
53     }
54     
55     if (index >= 0)
56         return name.substring(index + 1);
57     else {
58         int lastColonIndex = name.lastIndexOf(':');
59         if (lastColonIndex > 0)
60             return name.substring(lastColonIndex + 1);
61         else
62             return name;
63     }
64     }
65
66     /**
67      * Search for both slashes in order to support URLs and
68      * files.
69      */

70     public static String JavaDoc pathName(String JavaDoc name) {
71     int index = name.lastIndexOf('/');
72     if (index < 0) {
73         index = name.lastIndexOf('\\');
74     }
75     return name.substring(0, index + 1);
76     }
77
78     /**
79      * Replace all illegal Java chars by '_'.
80      */

81     public static String JavaDoc toJavaName(String JavaDoc name) {
82     if (name.length() > 0) {
83         final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
84
85         char ch = name.charAt(0);
86         result.append(Character.isJavaIdentifierStart(ch) ? ch : '_');
87
88         final int n = name.length();
89         for (int i = 1; i < n; i++) {
90         ch = name.charAt(i);
91         result.append(Character.isJavaIdentifierPart(ch) ? ch : '_');
92         }
93         return result.toString();
94     }
95     return name;
96     }
97
98     public static Type getJCRefType(String JavaDoc signature) {
99     return Type.getType(signature);
100     }
101
102     public static String JavaDoc internalName(String JavaDoc cname) {
103     return cname.replace('.', filesep);
104     }
105
106     public static void println(String JavaDoc s) {
107     System.out.println(s);
108     }
109
110     public static void println(char ch) {
111     System.out.println(ch);
112     }
113
114     public static void TRACE1() {
115     System.out.println("TRACE1");
116     }
117
118     public static void TRACE2() {
119     System.out.println("TRACE2");
120     }
121
122     public static void TRACE3() {
123     System.out.println("TRACE3");
124     }
125
126     /**
127      * Replace a certain character in a string with a new substring.
128      */

129     public static String JavaDoc replace(String JavaDoc base, char ch, String JavaDoc str) {
130     return (base.indexOf(ch) < 0) ? base :
131         replace(base, String.valueOf(ch), new String JavaDoc[] { str });
132     }
133
134     public static String JavaDoc replace(String JavaDoc base, String JavaDoc delim, String JavaDoc[] str) {
135     final int len = base.length();
136     final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
137
138     for (int i = 0; i < len; i++) {
139         final char ch = base.charAt(i);
140         final int k = delim.indexOf(ch);
141
142         if (k >= 0) {
143         result.append(str[k]);
144         }
145         else {
146         result.append(ch);
147         }
148     }
149     return result.toString();
150     }
151
152     /**
153      * Replace occurances of '.', '-', '/' and ':'
154      */

155     public static String JavaDoc escape(String JavaDoc input) {
156     return replace(input, ".-/:",
157         new String JavaDoc[] { "$dot$", "$dash$", "$slash$", "$colon$" });
158     }
159
160     public static String JavaDoc getLocalName(String JavaDoc qname) {
161     final int index = qname.lastIndexOf(":");
162     return (index > 0) ? qname.substring(index + 1) : qname;
163     }
164
165     public static String JavaDoc getPrefix(String JavaDoc qname) {
166     final int index = qname.lastIndexOf(":");
167     return (index > 0) ? qname.substring(0, index) :
168         Constants.EMPTYSTRING;
169     }
170           
171     /**
172      * Checks if the string is a literal (i.e. not an AVT) or not.
173      */

174     public static boolean isLiteral(String JavaDoc str) {
175         final int length = str.length();
176         for (int i = 0; i < length - 1; i++) {
177             if (str.charAt(i) == '{' && str.charAt(i + 1) != '{') {
178             return false;
179             }
180         }
181         return true;
182     }
183     
184     /**
185      * Checks if the string is valid list of qnames
186      */

187     public static boolean isValidQNames(String JavaDoc str) {
188         if ((str != null) && (!str.equals(Constants.EMPTYSTRING))) {
189             final StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(str);
190             while (tokens.hasMoreTokens()) {
191                 if (!XMLChar.isValidQName(tokens.nextToken())) {
192                     return false;
193                 }
194             }
195         }
196         return true;
197     }
198            
199 }
200
201
Popular Tags