01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hssf.util;
19:
20: public class AreaReference {
21:
22: private CellReference[] cells;
23: private int dim;
24:
25: /** Create an area ref from a string representation
26: */
27: public AreaReference(String reference) {
28: String[] refs = seperateAreaRefs(reference);
29: dim = refs.length;
30: cells = new CellReference[dim];
31: for (int i = 0; i < dim; i++) {
32: cells[i] = new CellReference(refs[i]);
33: }
34: }
35:
36: //not sure if we need to be flexible here!
37: /** return the dimensions of this area
38: **/
39: public int getDim() {
40: return dim;
41: }
42:
43: /** return the cell references that define this area */
44: public CellReference[] getCells() {
45: return cells;
46: }
47:
48: public String toString() {
49: StringBuffer retval = new StringBuffer();
50: for (int i = 0; i < dim; i++) {
51: retval.append(':');
52: retval.append(cells[i].toString());
53: }
54: retval.deleteCharAt(0);
55: return retval.toString();
56: }
57:
58: /**
59: * seperates Area refs in two parts and returns them as seperate elements in a
60: * String array
61: */
62: private String[] seperateAreaRefs(String reference) {
63: String[] retval = null;
64:
65: int length = reference.length();
66:
67: int loc = reference.indexOf(':', 0);
68: if (loc == -1) {
69: retval = new String[1];
70: retval[0] = reference;
71: } else {
72: retval = new String[2];
73: int sheetStart = reference.indexOf("!");
74:
75: retval[0] = reference.substring(0, sheetStart + 1)
76: + reference.substring(sheetStart + 1, loc);
77: retval[1] = reference.substring(0, sheetStart + 1)
78: + reference.substring(loc + 1);
79: }
80: return retval;
81: }
82: }
|