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