001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.util;
018
019 /**
020 * Helper methods for working with Strings.
021 */
022 public final class StringHelper {
023
024 /**
025 * Constructor of utility class should be private.
026 */
027 private StringHelper() {
028 }
029
030 /**
031 * Ensures that <code>s</code> is friendly for a URL or file system.
032 *
033 * @param s String to be sanitized.
034 * @return sanitized version of <code>s</code>.
035 * @throws NullPointerException if <code>s</code> is <code>null</code>.
036 */
037 public static String sanitize(String s) {
038 return s
039 .replace(':', '-')
040 .replace('_', '-')
041 .replace('.', '-')
042 .replace('/', '-')
043 .replace('\\', '-');
044 }
045
046 /**
047 * Counts the number of times the given char is in the string
048 *
049 * @param s the string
050 * @param ch the char
051 * @return number of times char is located in the string
052 */
053 public static int countChar(String s, char ch) {
054 if (ObjectHelper.isEmpty(s)) {
055 return 0;
056 }
057
058 int matches = 0;
059 for (int i = 0; i < s.length(); i++) {
060 char c = s.charAt(i);
061 if (ch == c) {
062 matches++;
063 }
064 }
065
066 return matches;
067 }
068
069 public static String removeQuotes(String s) {
070 if (ObjectHelper.isEmpty(s)) {
071 return s;
072 }
073
074 s = s.replaceAll("'", "");
075 s = s.replaceAll("\"", "");
076 return s;
077 }
078
079 public static String removeLeadingAndEndingQuotes(String s) {
080 if (ObjectHelper.isEmpty(s)) {
081 return s;
082 }
083
084 if (s.startsWith("'") && s.endsWith("'")) {
085 return s.substring(1, s.length() - 1);
086 }
087 if (s.startsWith("\"") && s.endsWith("\"")) {
088 return s.substring(1, s.length() - 1);
089 }
090 return s;
091 }
092
093 public static boolean isQuoted(String s) {
094 if (ObjectHelper.isEmpty(s)) {
095 return false;
096 }
097
098 if (s.startsWith("'") && s.endsWith("'")) {
099 return true;
100 }
101 if (s.startsWith("\"") && s.endsWith("\"")) {
102 return true;
103 }
104
105 return false;
106 }
107
108 /**
109 * Encodes the text into safe XML by replacing < > and & with XML tokens
110 *
111 * @param text the text
112 * @return the encoded text
113 */
114 public static String xmlEncode(String text) {
115 if (text == null) {
116 return "";
117 }
118 // must replace amp first, so we dont replace < to amp later
119 return text.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<").replaceAll(">", ">");
120 }
121
122 /**
123 * Determines if the string has at least one letter in upper case
124 * @param text the text
125 * @return <tt>true</tt> if at least one letter is upper case, <tt>false</tt> otherwise
126 */
127 public static boolean hasUpperCase(String text) {
128 if (text == null) {
129 return false;
130 }
131
132 for (int i = 0; i < text.length(); i++) {
133 char ch = text.charAt(i);
134 if (Character.isUpperCase(ch)) {
135 return true;
136 }
137 }
138
139 return false;
140 }
141
142 /**
143 * Does the expression have the language start token?
144 *
145 * @param expression the expression
146 * @param language the name of the language, such as simple
147 * @return <tt>true</tt> if the expression contains the start token, <tt>false</tt> otherwise
148 */
149 public static boolean hasStartToken(String expression, String language) {
150 if (expression == null) {
151 return false;
152 }
153
154 if (expression.indexOf("${") >= 0) {
155 return true;
156 }
157
158 if (language != null && expression.indexOf("$" + language + "{") >= 0) {
159 return true;
160 }
161
162 return false;
163 }
164
165 }