001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: Util.java 1225577 2011-12-29 15:54:35Z mrglavas $
020     */
021    
022    package org.apache.xalan.xsltc.compiler.util;
023    
024    import java.util.StringTokenizer;
025    
026    import org.apache.bcel.generic.Type;
027    import org.apache.xalan.xsltc.compiler.Constants;
028    import org.apache.xml.utils.XML11Char;
029    
030    /**
031     * @author Jacek Ambroziak
032     * @author Santiago Pericas-Geertsen
033     */
034    public final class Util {
035        private static char filesep;
036    
037        static {
038            String temp = System.getProperty("file.separator", "/");
039            filesep = temp.charAt(0);
040        }
041    
042        public static String noExtName(String name) {
043            final int index = name.lastIndexOf('.');
044            return name.substring(0, index >= 0 ? index : name.length());
045        }
046    
047        /**
048         * Search for both slashes in order to support URLs and 
049         * files.
050         */
051        public static String baseName(String name) {
052            int index = name.lastIndexOf('\\');
053            if (index < 0) {
054                index = name.lastIndexOf('/');
055            }
056            
057            if (index >= 0)
058                return name.substring(index + 1);
059            else {
060                int lastColonIndex = name.lastIndexOf(':');
061                if (lastColonIndex > 0)
062                    return name.substring(lastColonIndex + 1);
063                else
064                    return name;
065            }
066        }
067    
068        /**
069         * Search for both slashes in order to support URLs and 
070         * files.
071         */
072        public static String pathName(String name) {
073            int index = name.lastIndexOf('/');
074            if (index < 0) {
075                index = name.lastIndexOf('\\');
076            }
077            return name.substring(0, index + 1);
078        }
079    
080        /**
081         * Replace all illegal Java chars by '_'.
082         */
083        public static String toJavaName(String name) {
084            if (name.length() > 0) {
085                final StringBuffer result = new StringBuffer();
086    
087                char ch = name.charAt(0);
088                result.append(Character.isJavaIdentifierStart(ch) ? ch : '_');
089    
090                final int n = name.length();
091                for (int i = 1; i < n; i++) {
092                    ch = name.charAt(i);
093                    result.append(Character.isJavaIdentifierPart(ch)  ? ch : '_');
094                }
095                return result.toString();
096            }
097            return name;
098        }
099    
100        public static Type getJCRefType(String signature) {
101            return Type.getType(signature);
102        }
103    
104        public static String internalName(String cname) {
105            return cname.replace('.', filesep);
106        }
107    
108        public static void println(String s) {
109            System.out.println(s);
110        }
111    
112        public static void println(char ch) {
113            System.out.println(ch);
114        }
115    
116        public static void TRACE1() {
117            System.out.println("TRACE1");
118        }
119    
120        public static void TRACE2() {
121            System.out.println("TRACE2");
122        }
123    
124        public static void TRACE3() {
125            System.out.println("TRACE3");
126        }
127    
128        /**
129         * Replace a certain character in a string with a new substring.
130         */
131        public static String replace(String base, char ch, String str) {
132            return (base.indexOf(ch) < 0) ? base : 
133                replace(base, String.valueOf(ch), new String[] { str });
134        }
135    
136        public static String replace(String base, String delim, String[] str) {
137            final int len = base.length();
138            final StringBuffer result = new StringBuffer();
139    
140            for (int i = 0; i < len; i++) {
141                final char ch = base.charAt(i);
142                final int k = delim.indexOf(ch);
143    
144                if (k >= 0) {
145                    result.append(str[k]);
146                }
147                else {
148                    result.append(ch);
149                }
150            }
151            return result.toString();
152        }
153    
154        /**
155         * Replace occurances of '.', '-', '/' and ':'
156         */
157        public static String escape(String input) {
158            return replace(input, ".-/:", 
159                new String[] { "$dot$", "$dash$", "$slash$", "$colon$" });
160        }
161    
162        public static String getLocalName(String qname) {
163            final int index = qname.lastIndexOf(':');
164            return (index > 0) ? qname.substring(index + 1) : qname;
165        }
166    
167        public static String getPrefix(String qname) {
168            final int index = qname.lastIndexOf(':');
169            return (index > 0) ? qname.substring(0, index) : 
170                Constants.EMPTYSTRING;
171        } 
172              
173        /**
174         * Checks if the string is a literal (i.e. not an AVT) or not.
175         */
176        public static boolean isLiteral(String str) {
177            final int length = str.length();
178            for (int i = 0; i < length - 1; i++) {
179                if (str.charAt(i) == '{' && str.charAt(i + 1) != '{') {
180                    return false;
181                }
182            }
183            return true;
184        }
185        
186        /**
187         * Checks if the string is valid list of qnames
188         */
189        public static boolean isValidQNames(String str) {
190            if ((str != null) && (!str.equals(Constants.EMPTYSTRING))) {
191                final StringTokenizer tokens = new StringTokenizer(str);
192                while (tokens.hasMoreTokens()) {
193                    if (!XML11Char.isXML11ValidQName(tokens.nextToken())) {
194                        return false;
195                    }
196                }
197            }
198            return true;
199        }    
200               
201    }
202