This project has retired. For details please refer to its
Attic page.
Util xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.jcr.util;
2021import java.util.Calendar;
22import java.util.GregorianCalendar;
2324/**25 * Miscellaneous utility functions26 */27publicfinalclassUtil {
28privateUtil() {}
2930/**31 * Convert from <code>Calendar</code> to a <code>GregorianCalendar</code>.32 * 33 * @param date34 * @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 */37publicstatic GregorianCalendar toCalendar(Calendar date) {
38if (date instanceof GregorianCalendar) {
39return (GregorianCalendar) date;
40 }
41else {
42 GregorianCalendar calendar = new GregorianCalendar();
43 calendar.setTimeZone(date.getTimeZone());
44 calendar.setTimeInMillis(date.getTimeInMillis());
45return calendar;
46 }
47 }
4849/**50 * Replace every occurrence of <code>target</code> in <code>string</code> with51 * <code>replacement</code>.52 *53 * @param string string to do replacement on54 * @param target string to search for55 * @param replacement string to replace with56 * @return string with replacing done57 */58publicstatic String replace(String string, String target, String replacement) {
59if ("".equals(target)) {
60thrownew IllegalArgumentException("target string must not be empty");
61 }
62if ("".equals(replacement) || target.equals(replacement)) {
63return string;
64 }
6566 StringBuilder result = new StringBuilder();
67int d = target.length();
68int k = 0;
69int j;
70do {
71 j = string.indexOf(target, k);
72if (j < 0) {
73 result.append(string.substring(k));
74 }
75else {
76 result.append(string.substring(k, j)).append(replacement);
77 }
7879 k = j + d;
80 } while (j >= 0);
8182return result.toString();
83 }
8485/**86 * Escapes a JCR path such that it can be used in a XPath query87 * @param path88 * @return escaped path89 */90publicstatic String escape(String path) {
91return replace(path, " ", "_x0020_"); // fixme do more thorough escaping of path92 }
9394 }