1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
22 
23 import com.sun.org.apache.bcel.internal.generic.Type;
24 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
25 import com.sun.org.apache.xml.internal.utils.XML11Char;
26 import java.util.StringTokenizer;
27 import jdk.xml.internal.SecuritySupport;
28 
29 /**
30  * @author Jacek Ambroziak
31  * @author Santiago Pericas-Geertsen
32  * @LastModified: Sep 2017
33  */
34 public final class Util {
35     private static char filesep;
36 
37     static {
38         String temp = SecuritySupport.getSystemProperty("file.separator", "/");
39         filesep = temp.charAt(0);
40     }
41 
noExtName(String name)42     public static String noExtName(String name) {
43         final int index = name.lastIndexOf('.');
44         return name.substring(0, index >= 0 ? index : name.length());
45     }
46 
47     /**
48      * Search for both slashes in order to support URLs and
49      * files.
50      */
baseName(String name)51     public static String baseName(String name) {
52         int index = name.lastIndexOf('\\');
53         if (index < 0) {
54             index = name.lastIndexOf('/');
55         }
56 
57         if (index >= 0)
58             return name.substring(index + 1);
59         else {
60             int lastColonIndex = name.lastIndexOf(':');
61             if (lastColonIndex > 0)
62                 return name.substring(lastColonIndex + 1);
63             else
64                 return name;
65         }
66     }
67 
68     /**
69      * Search for both slashes in order to support URLs and
70      * files.
71      */
pathName(String name)72     public static String pathName(String name) {
73         int index = name.lastIndexOf('/');
74         if (index < 0) {
75             index = name.lastIndexOf('\\');
76         }
77         return name.substring(0, index + 1);
78     }
79 
80     /**
81      * Replace all illegal Java chars by '_'.
82      */
toJavaName(String name)83     public static String toJavaName(String name) {
84         if (name.length() > 0) {
85             final StringBuffer result = new StringBuffer();
86 
87             char ch = name.charAt(0);
88             result.append(Character.isJavaIdentifierStart(ch) ? ch : '_');
89 
90             final int n = name.length();
91             for (int i = 1; i < n; i++) {
92                 ch = name.charAt(i);
93                 result.append(Character.isJavaIdentifierPart(ch)  ? ch : '_');
94             }
95             return result.toString();
96         }
97         return name;
98     }
99 
getJCRefType(String signature)100     public static Type getJCRefType(String signature) {
101         return Type.getType(signature);
102     }
103 
internalName(String cname)104     public static String internalName(String cname) {
105         return cname.replace('.', filesep);
106     }
107 
println(String s)108     public static void println(String s) {
109         System.out.println(s);
110     }
111 
println(char ch)112     public static void println(char ch) {
113         System.out.println(ch);
114     }
115 
TRACE1()116     public static void TRACE1() {
117         System.out.println("TRACE1");
118     }
119 
TRACE2()120     public static void TRACE2() {
121         System.out.println("TRACE2");
122     }
123 
TRACE3()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      */
replace(String base, char ch, String str)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 
replace(String base, String delim, String[] str)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      */
escape(String input)157     public static String escape(String input) {
158         return replace(input, ".-/:",
159             new String[] { "$dot$", "$dash$", "$slash$", "$colon$" });
160     }
161 
getLocalName(String qname)162     public static String getLocalName(String qname) {
163         final int index = qname.lastIndexOf(":");
164         return (index > 0) ? qname.substring(index + 1) : qname;
165     }
166 
getPrefix(String qname)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      */
isLiteral(String str)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      */
isValidQNames(String str)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