001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.upgrade.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.upgrade.StagnantRowException;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * <a href="IdReplacer.java.html"><b><i>View Source</i></b></a>
033: *
034: * @author Brian Wing Shun Chan
035: *
036: */
037: public class IdReplacer {
038:
039: public static String replaceLongIds(String s, String begin,
040: ValueMapper valueMapper) throws Exception {
041:
042: if ((s == null) || (begin == null) || (valueMapper == null)
043: || (valueMapper.size() == 0)) {
044:
045: return s;
046: }
047:
048: char[] charArray = s.toCharArray();
049:
050: StringMaker sm = new StringMaker(s.length());
051:
052: int pos = 0;
053:
054: while (true) {
055: int x = s.indexOf(begin, pos);
056: int y = _getEndPos(charArray, x + begin.length());
057:
058: if ((x == -1) || (y == -1)) {
059: sm.append(s.substring(pos, s.length()));
060:
061: break;
062: } else {
063: sm.append(s.substring(pos, x + begin.length()));
064:
065: String oldString = s.substring(x + begin.length(), y);
066:
067: if (Validator.isNotNull(oldString)) {
068: Long oldValue = new Long(GetterUtil
069: .getLong(oldString));
070:
071: Long newValue = null;
072:
073: try {
074: newValue = (Long) valueMapper
075: .getNewValue(oldValue);
076: } catch (StagnantRowException sre) {
077: if (_log.isWarnEnabled()) {
078: _log.warn(sre);
079: }
080: }
081:
082: if (newValue == null) {
083: newValue = oldValue;
084: }
085:
086: sm.append(newValue);
087: }
088:
089: pos = y;
090: }
091: }
092:
093: return sm.toString();
094: }
095:
096: public String replaceLongIds(String s, String begin, String end,
097: ValueMapper valueMapper) throws Exception {
098:
099: if ((s == null) || (begin == null) || (end == null)
100: || (valueMapper == null) || (valueMapper.size() == 0)) {
101:
102: return s;
103: }
104:
105: StringMaker sm = new StringMaker(s.length());
106:
107: int pos = 0;
108:
109: while (true) {
110: int x = s.indexOf(begin, pos);
111: int y = s.indexOf(end, x + begin.length());
112:
113: if ((x == -1) || (y == -1)) {
114: sm.append(s.substring(pos, s.length()));
115:
116: break;
117: } else {
118: sm.append(s.substring(pos, x + begin.length()));
119:
120: Long oldValue = new Long(GetterUtil.getLong(s
121: .substring(x + begin.length(), y)));
122:
123: Long newValue = null;
124:
125: try {
126: newValue = (Long) valueMapper.getNewValue(oldValue);
127: } catch (StagnantRowException sre) {
128: if (_log.isWarnEnabled()) {
129: _log.warn(sre);
130: }
131: }
132:
133: if (newValue == null) {
134: newValue = oldValue;
135: }
136:
137: sm.append(newValue);
138:
139: pos = y;
140: }
141: }
142:
143: return sm.toString();
144: }
145:
146: private static int _getEndPos(char[] charArray, int pos) {
147: while (true) {
148: if (pos >= charArray.length) {
149: break;
150: }
151:
152: if (!Character.isDigit(charArray[pos])) {
153: break;
154: }
155:
156: pos++;
157: }
158:
159: return pos;
160: }
161:
162: private static Log _log = LogFactory.getLog(IdReplacer.class);
163:
164: }
|