1 16 17 package org.apache.poi.hssf.util; 18 19 20 21 30 31 public class RangeAddress { 32 final static int WRONG_POS = -1; 33 final static int MAX_HEIGHT = 66666; 34 final static char SO_FORMNAME_ENCLOSURE = '\''; 35 String m_sheetName; 36 String m_cellFrom; 37 String m_cellTo; 38 39 45 public RangeAddress (String _url) { 46 init (_url); 47 } 48 49 public RangeAddress (int _startCol, int _startRow, int _endCol, int _endRow) { 50 init (numTo26Sys (_startCol) + _startRow + ":" 51 + numTo26Sys (_endCol) + _endRow); 52 } 53 54 58 public String getAddress (){ 59 String result = ""; 60 if(m_sheetName != null) 61 result += m_sheetName+"!"; 62 if(m_cellFrom != null){ 63 result += m_cellFrom; 64 if(m_cellTo != null) 65 result += ":" + m_cellTo; 66 } 67 return result; 68 } 69 70 71 public String getSheetName (){ 72 return m_sheetName; 73 } 74 75 public String getRange (){ 76 String result = ""; 77 if(m_cellFrom != null){ 78 result += m_cellFrom; 79 if(m_cellTo != null) 80 result += ":" + m_cellTo; 81 } 82 return result; 83 } 84 85 public boolean isCellOk (String _cell){ 86 if (_cell != null){ 87 if ( (getYPosition (_cell) != WRONG_POS) && 88 (getXPosition (_cell) != WRONG_POS) ) 89 return true; 90 else 91 return false; 92 } else 93 return false; 94 } 95 96 public boolean isSheetNameOk (){ 97 return isSheetNameOk (m_sheetName); 98 } 99 100 private static boolean intern_isSheetNameOk (String _sheetName, boolean _canBeWaitSpace){ 101 for (int i = 0 ; i < _sheetName.length (); i++){ 102 char ch = _sheetName.charAt (i); 103 if (! (Character.isLetterOrDigit (ch) || (ch == '_')|| 104 _canBeWaitSpace&&(ch == ' '))){ 105 return false; 106 } 107 } 108 return true; 109 } 110 111 public static boolean isSheetNameOk (String _sheetName){ 112 boolean res = false; 113 if ( ( _sheetName != null) && !_sheetName.equals ("")){ 114 res = intern_isSheetNameOk (_sheetName,true); 115 }else 116 res = true; 117 return res; 118 } 119 120 121 public String getFromCell (){ 122 return m_cellFrom; 123 } 124 125 public String getToCell (){ 126 return m_cellTo; 127 } 128 129 public int getWidth (){ 130 if(m_cellFrom != null && m_cellTo != null){ 131 int toX = getXPosition (m_cellTo); 132 int fromX = getXPosition (m_cellFrom); 133 if ((toX == WRONG_POS) || (fromX == WRONG_POS)){ 134 return 0; 135 }else 136 return toX - fromX + 1; 137 } 138 return 0; 139 } 140 141 public int getHeight (){ 142 if(m_cellFrom != null && m_cellTo != null){ 143 int toY = getYPosition (m_cellTo); 144 int fromY = getYPosition (m_cellFrom); 145 if ((toY == WRONG_POS) || (fromY == WRONG_POS)){ 146 return 0; 147 }else 148 return toY - fromY + 1; 149 } 150 return 0; 151 } 152 153 public void setSize (int _width, int _height){ 154 if(m_cellFrom == null) 155 m_cellFrom = "a1"; 156 int tlX, tlY, rbX, rbY; 157 tlX = getXPosition (m_cellFrom); 158 tlY = getYPosition (m_cellFrom); 159 m_cellTo = numTo26Sys (tlX + _width - 1); 160 m_cellTo += String.valueOf (tlY + _height - 1); 161 } 162 163 public boolean hasSheetName (){ 164 if(m_sheetName == null) 165 return false; 166 return true; 167 } 168 169 public boolean hasRange (){ 170 if(m_cellFrom == null || m_cellTo == null) 171 return false; 172 return true; 173 } 174 175 public boolean hasCell (){ 176 if(m_cellFrom == null) 177 return false; 178 return true; 179 } 180 181 private void init (String _url){ 182 183 _url = removeString(_url, "$"); 184 _url = removeString(_url, "'"); 185 186 String [] urls = parseURL (_url); 187 m_sheetName = urls[0]; 188 m_cellFrom = urls[1]; 189 m_cellTo = urls[2]; 190 191 if (m_cellTo == null){ 193 m_cellTo = m_cellFrom; 194 } 195 196 m_cellTo = removeString(m_cellTo,"."); 198 199 200 } 201 202 private String [] parseURL (String _url){ 203 String [] result = new String [3]; 204 int index = _url.indexOf(':'); 205 if (index >= 0) { 206 String fromStr = _url.substring(0, index); 207 String toStr = _url.substring(index+1); 208 index = fromStr.indexOf('!'); 209 if (index >= 0) { 210 result[0] = fromStr.substring(0, index); 211 result[1] = fromStr.substring(index+1); 212 } else { 213 result[1] = fromStr; 214 } 215 index = toStr.indexOf('!'); 216 if (index >= 0) { 217 result[2] = toStr.substring(index+1); 218 } else { 219 result[2] = toStr; 220 } 221 } else { 222 index = _url.indexOf('!'); 223 if (index >= 0) { 224 result[0] = _url.substring(0, index); 225 result[1] = _url.substring(index+1); 226 } else { 227 result[1] = _url; 228 } 229 } 230 return result; 231 } 232 233 public int getYPosition (String _subrange){ 234 int result = WRONG_POS; 235 _subrange = _subrange.trim (); 236 if (_subrange.length () != 0){ 237 String digitstr = getDigitPart (_subrange); 238 try { 239 result = Integer.parseInt (digitstr); 240 if (result > MAX_HEIGHT){ 241 result = WRONG_POS; 242 } 243 } 244 catch (Exception ex) { 245 246 result = WRONG_POS; 247 } 248 } 249 return result; 250 } 251 252 private static boolean isLetter (String _str){ 253 boolean res = true; 254 if ( !_str.equals ("") ){ 255 for (int i = 0 ; i < _str.length (); i++){ 256 char ch = _str.charAt (i); 257 if (! Character.isLetter (ch)){ 258 res = false; 259 break; 260 } 261 } 262 }else 263 res = false; 264 return res; 265 } 266 267 public int getXPosition (String _subrange){ 268 int result = WRONG_POS; 269 String tmp = filter$ (_subrange); 270 tmp = this.getCharPart (_subrange); 271 if (isLetter (tmp) && ((tmp.length () == 2)|| (tmp.length () == 1) )){ 273 result = get26Sys (tmp); 274 } 275 return result; 276 } 277 278 public String getDigitPart (String _value){ 279 String result = ""; 280 int digitpos = getFirstDigitPosition (_value); 281 if(digitpos >= 0){ 282 result = _value.substring (digitpos); 283 } 284 return result; 285 } 286 287 public String getCharPart (String _value){ 288 String result = ""; 289 int digitpos = getFirstDigitPosition (_value); 290 if(digitpos >= 0){ 291 result = _value.substring (0, digitpos); 292 } 293 return result; 294 } 295 296 private String filter$ (String _range){ 297 String res = ""; 298 for (int i = 0 ; i < _range.length () ; i++){ 299 char ch = _range.charAt (i); 300 if ( ch != '$' ){ 301 res = res + ch; 302 } 303 } 304 return res; 305 } 306 307 private int getFirstDigitPosition (String _value){ 308 int result = WRONG_POS; 309 if(_value != null && _value.trim ().length () == 0){ 310 return result; 311 } 312 _value = _value.trim (); 313 int length = _value.length (); 314 for(int i = 0; i < length; i++){ 315 if(Character.isDigit (_value.charAt (i))){ 316 result = i; 317 break; 318 } 319 } 320 return result; 321 } 322 323 public int get26Sys (String _s){ 324 int sum = 0; 325 int multiplier = 1; 326 if (_s != "") { 327 for (int i = _s.length ()-1 ; i >= 0 ; i--){ 328 char ch = _s.charAt (i); 329 int val = Character.getNumericValue (ch) - Character.getNumericValue ('A')+1; 330 sum = sum + val * multiplier; 331 multiplier = multiplier * 26; 332 } 333 return sum; 334 } 335 return WRONG_POS; 336 } 337 338 public String numTo26Sys (int _num){ 339 int sum = 0; 340 int reminder; 341 String s =""; 342 do{ 343 _num --; 344 reminder = _num % 26; 345 int val = 65 + reminder; 346 _num = _num / 26; 347 s = (char)val + s; }while(_num > 0); 349 return s; 350 } 351 352 public String replaceString(String _source , String _oldPattern, 353 String _newPattern){ 354 StringBuffer res = new StringBuffer (_source); 355 int pos = -1; 356 357 while ((pos = res.toString().indexOf(_oldPattern, pos)) > -1){ 358 res.replace(pos, pos + _oldPattern.length(), _newPattern); 359 } 360 361 return res.toString(); 362 } 363 364 public String removeString(String _source, String _match){ 365 return replaceString(_source, _match, ""); 366 } 367 368 } 369 | Popular Tags |