This project has retired. For details please refer to its Attic page.
Util 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.jcr.util;
20  
21  import java.util.Calendar;
22  import java.util.GregorianCalendar;
23  
24  /**
25   * Miscellaneous utility functions
26   */
27  public final class Util {
28      private Util() {}
29  
30      /**
31       * Convert from <code>Calendar</code> to a <code>GregorianCalendar</code>.
32       * 
33       * @param date
34       * @return  <code>date</code> if it is an instance of <code>GregorianCalendar</code>.
35       *   Otherwise a new <code>GregorianCalendar</code> instance for <code>date</code>.
36       */
37      public static GregorianCalendar toCalendar(Calendar date) {
38          if (date instanceof GregorianCalendar) {
39              return (GregorianCalendar) date;
40          }
41          else {
42              GregorianCalendar calendar = new GregorianCalendar();
43              calendar.setTimeZone(date.getTimeZone());
44              calendar.setTimeInMillis(date.getTimeInMillis());
45              return calendar;
46          }
47      }
48  
49      /**
50       * Replace every occurrence of <code>target</code> in <code>string</code> with
51       * <code>replacement</code>.
52       *
53       * @param string  string to do replacement on
54       * @param target  string to search for
55       * @param replacement  string to replace with
56       * @return  string with replacing done
57       */
58      public static String replace(String string, String target, String replacement) {
59          if ("".equals(target)) {
60              throw new IllegalArgumentException("target string must not be empty");
61          }
62          if ("".equals(replacement) || target.equals(replacement)) {
63              return string;
64          }
65  
66          StringBuilder result = new StringBuilder();
67          int d = target.length();
68          int k = 0;
69          int j;
70          do {
71              j = string.indexOf(target, k);
72              if (j < 0) {
73                  result.append(string.substring(k));
74              }
75              else {
76                  result.append(string.substring(k, j)).append(replacement);
77              }
78  
79              k = j + d;
80          } while (j >= 0);
81  
82          return result.toString();
83      }
84  
85      /**
86       * Escapes a JCR path such that it can be used in a XPath query
87       * @param path
88       * @return  escaped path
89       */
90      public static String escape(String path) {
91          return replace(path, " ", "_x0020_"); // fixme do more thorough escaping of path
92      }
93  
94  }