KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > util > RangeAddress


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.util;
18
19
20
21 /**
22  * Title: Range Address <P>
23  * Description: provides connectivity utilities for ranges<P>
24  *
25  *
26  * REFERENCE: <P>
27  * @author IgOr KaTz && EuGeNe BuMaGiN (Tal Moshaiov) (VistaPortal LDT.)
28 @version 1.0
29  */

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 JavaDoc m_sheetName;
36   String JavaDoc m_cellFrom;
37   String JavaDoc m_cellTo;
38
39   /**
40    * Accepts an external reference from excel.
41    * <p>
42    * i.e. Sheet1!$A$4:$B$9
43    * @param _url
44    */

45   public RangeAddress (String JavaDoc _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   /**
55    *
56    * @return String <b>note: </b> All absolute references are removed
57    */

58   public String JavaDoc getAddress (){
59     String JavaDoc 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 JavaDoc getSheetName (){
72     return m_sheetName;
73   }
74   
75   public String JavaDoc getRange (){
76     String JavaDoc 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 JavaDoc _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 JavaDoc _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 JavaDoc _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 JavaDoc getFromCell (){
122     return m_cellFrom;
123   }
124   
125   public String JavaDoc 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 JavaDoc _url){
182
183     _url = removeString(_url, "$");
184     _url = removeString(_url, "'");
185     
186     String JavaDoc[] urls = parseURL (_url);
187     m_sheetName = urls[0];
188     m_cellFrom = urls[1];
189     m_cellTo = urls[2];
190
191     //What if range is one celled ?
192
if (m_cellTo == null){
193       m_cellTo = m_cellFrom;
194     }
195         
196     //Removing noneeds characters
197
m_cellTo = removeString(m_cellTo,".");
198     
199     
200   }
201   
202   private String JavaDoc[] parseURL (String JavaDoc _url){
203     String JavaDoc[] result = new String JavaDoc[3];
204     int index = _url.indexOf(':');
205     if (index >= 0) {
206       String JavaDoc fromStr = _url.substring(0, index);
207       String JavaDoc 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 JavaDoc _subrange){
234     int result = WRONG_POS;
235     _subrange = _subrange.trim ();
236     if (_subrange.length () != 0){
237       String JavaDoc digitstr = getDigitPart (_subrange);
238       try {
239         result = Integer.parseInt (digitstr);
240         if (result > MAX_HEIGHT){
241           result = WRONG_POS;
242         }
243       }
244       catch (Exception JavaDoc ex) {
245         
246         result = WRONG_POS;
247       }
248     }
249     return result;
250   }
251   
252   private static boolean isLetter (String JavaDoc _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 JavaDoc _subrange){
268     int result = WRONG_POS;
269     String JavaDoc tmp = filter$ (_subrange);
270     tmp = this.getCharPart (_subrange);
271     // we will process only 2 letters ranges
272
if (isLetter (tmp) && ((tmp.length () == 2)|| (tmp.length () == 1) )){
273       result = get26Sys (tmp);
274     }
275     return result;
276   }
277   
278   public String JavaDoc getDigitPart (String JavaDoc _value){
279     String JavaDoc result = "";
280     int digitpos = getFirstDigitPosition (_value);
281     if(digitpos >= 0){
282       result = _value.substring (digitpos);
283     }
284     return result;
285   }
286   
287   public String JavaDoc getCharPart (String JavaDoc _value){
288     String JavaDoc 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 JavaDoc filter$ (String JavaDoc _range){
297     String JavaDoc 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 JavaDoc _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 JavaDoc _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 JavaDoc numTo26Sys (int _num){
339     int sum = 0;
340     int reminder;
341     String JavaDoc s ="";
342     do{
343       _num --;
344       reminder = _num % 26;
345       int val = 65 + reminder;
346       _num = _num / 26;
347       s = (char)val + s; // reverce
348
}while(_num > 0);
349     return s;
350   }
351   
352     public String JavaDoc replaceString(String JavaDoc _source , String JavaDoc _oldPattern,
353     String JavaDoc _newPattern){
354         StringBuffer JavaDoc res = new StringBuffer JavaDoc(_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 JavaDoc removeString(String JavaDoc _source, String JavaDoc _match){
365         return replaceString(_source, _match, "");
366     }
367   
368 }
369
Popular Tags