This project has retired. For details please refer to its Attic page.
StringUtil xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.support.query;
20  
21  
22  public class StringUtil {
23      
24      /**
25       * remove all escape sequences in a string and return unescaped string
26       * escape character is backslash \, so \\ --> \, \' --> ' additional
27       * escaped characters can be allowed in escapedChars
28       * @param literal
29       *      String to unescape
30       * @param escapedChars
31       *      set of allowed characters to be escaped with a backslash, if set to
32       *      null then ' (quote) and \ (backslash) are allowed to be escaped
33       * @return
34       *      unescaped literal or null if the literal is illegal
35       */
36      public static String unescape(String literal, String escapedChars) {
37          char c ='?';
38          int i=0;
39          StringBuffer sb = new StringBuffer();
40          
41          if (null == escapedChars) 
42              escapedChars = "\\'";
43          
44          if (null == literal)
45              return null;
46          
47          int len = literal.length();
48          
49          if (len == 1 && literal.charAt(0) == '\\')
50              return null;
51  
52          if (len > 1 && literal.charAt(len-2) != '\\' && literal.charAt(len-1) == '\\') 
53              return null;
54  
55          for (i=0; i<len; i++) {
56              c = literal.charAt(i);
57              if ( c == '\\') {
58                  char escChar = literal.charAt(i+1);
59                  boolean matched = false;
60                  for (int j = 0; j< escapedChars.length(); j++) {
61                      if (escChar == escapedChars.charAt(j)) {
62                          sb.append(escChar);
63                          ++i;
64                          matched = true;
65                          break;
66                      }
67                  }
68                  
69                  if (!matched)
70                      return null;
71                  
72              } else {
73                  sb.append(literal.charAt(i));
74              }
75          }
76          
77          return sb.toString();
78      }
79  
80  }