1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.upgrade.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.upgrade.StagnantRowException;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  /**
25   * <a href="IdReplacer.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class IdReplacer {
30  
31      public static String replaceLongIds(
32              String s, String begin, ValueMapper valueMapper)
33          throws Exception {
34  
35          if ((s == null) || (begin == null) ||
36              (valueMapper == null) || (valueMapper.size() == 0)) {
37  
38              return s;
39          }
40  
41          char[] charArray = s.toCharArray();
42  
43          StringBundler sb = new StringBundler();
44  
45          int pos = 0;
46  
47          while (true) {
48              int x = s.indexOf(begin, pos);
49              int y = _getEndPos(charArray, x + begin.length());
50  
51              if ((x == -1) || (y == -1)) {
52                  sb.append(s.substring(pos, s.length()));
53  
54                  break;
55              }
56              else {
57                  sb.append(s.substring(pos, x + begin.length()));
58  
59                  String oldString = s.substring(x + begin.length(), y);
60  
61                  if (Validator.isNotNull(oldString)) {
62                      Long oldValue = new Long(GetterUtil.getLong(oldString));
63  
64                      Long newValue = null;
65  
66                      try {
67                          newValue = (Long)valueMapper.getNewValue(oldValue);
68                      }
69                      catch (StagnantRowException sre) {
70                          if (_log.isWarnEnabled()) {
71                              _log.warn(sre);
72                          }
73                      }
74  
75                      if (newValue == null) {
76                          newValue = oldValue;
77                      }
78  
79                      sb.append(newValue);
80                  }
81  
82                  pos = y;
83              }
84          }
85  
86          return sb.toString();
87      }
88  
89      public String replaceLongIds(
90              String s, String begin, String end, ValueMapper valueMapper)
91          throws Exception {
92  
93          if ((s == null) || (begin == null) || (end == null) ||
94              (valueMapper == null) || (valueMapper.size() == 0)) {
95  
96              return s;
97          }
98  
99          StringBundler sb = new StringBundler();
100 
101         int pos = 0;
102 
103         while (true) {
104             int x = s.indexOf(begin, pos);
105             int y = s.indexOf(end, x + begin.length());
106 
107             if ((x == -1) || (y == -1)) {
108                 sb.append(s.substring(pos, s.length()));
109 
110                 break;
111             }
112             else {
113                 sb.append(s.substring(pos, x + begin.length()));
114 
115                 Long oldValue = new Long(GetterUtil.getLong(
116                     s.substring(x + begin.length(), y)));
117 
118                 Long newValue = null;
119 
120                 try {
121                     newValue = (Long)valueMapper.getNewValue(oldValue);
122                 }
123                 catch (StagnantRowException sre) {
124                     if (_log.isWarnEnabled()) {
125                         _log.warn(sre);
126                     }
127                 }
128 
129                 if (newValue == null) {
130                     newValue = oldValue;
131                 }
132 
133                 sb.append(newValue);
134 
135                 pos = y;
136             }
137         }
138 
139         return sb.toString();
140     }
141 
142     private static int _getEndPos(char[] charArray, int pos) {
143         while (true) {
144             if (pos >= charArray.length) {
145                 break;
146             }
147 
148             if (!Character.isDigit(charArray[pos])) {
149                 break;
150             }
151 
152             pos++;
153         }
154 
155         return pos;
156     }
157 
158     private static Log _log = LogFactoryUtil.getLog(IdReplacer.class);
159 
160 }