1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * @LastModified: Sep 2017 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 22 package com.sun.org.apache.xalan.internal.xsltc.compiler.util; 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.XML11Char; 27 import java.util.StringTokenizer; 28 import jdk.xml.internal.SecuritySupport; 29 30 /** 31 * @author Jacek Ambroziak 32 * @author Santiago Pericas-Geertsen 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 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 */ 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 */ 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 */ 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 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 }