001package com.hfg.xml.msofficexml.xlsx.spreadsheetml; 002 003import java.util.List; 004 005import com.hfg.util.collection.CollectionUtil; 006import com.hfg.xml.XMLTag; 007import com.hfg.xml.msofficexml.xlsx.CellRange; 008import com.hfg.xml.msofficexml.xlsx.CellRef; 009 010 011//------------------------------------------------------------------------------ 012/** 013 Represents an Office Open XML sheet data (<ssml:sheetData>) tag. 014 015 @author J. Alex Taylor, hairyfatguy.com 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class SsmlSheetData extends SsmlXMLTag 039{ 040 private SsmlWorksheet mParentWorksheet; 041 042 //########################################################################### 043 // CONSTRUCTORS 044 //########################################################################### 045 046 //--------------------------------------------------------------------------- 047 public SsmlSheetData(SsmlWorksheet inParentWorksheet) 048 { 049 super(SsmlXML.SHEET_DATA, inParentWorksheet.getParentDoc()); 050 mParentWorksheet = inParentWorksheet; 051 } 052 053 //--------------------------------------------------------------------------- 054 public SsmlSheetData(SsmlWorksheet inParentWorksheet, XMLTag inXMLTag) 055 { 056 this(inParentWorksheet); 057 058 inXMLTag.verifyTagName(SsmlXML.SHEET_DATA); 059 060 // Copy subtags over to this tag 061 if (CollectionUtil.hasValues(inXMLTag.getSubtags())) 062 { 063 for (XMLTag subtag : (List<XMLTag>) (Object) inXMLTag.getSubtags()) 064 { 065 if (subtag.getTagName().equals(SsmlXML.ROW.getLocalName())) 066 { 067 addRow(new SsmlRow(inParentWorksheet, subtag)); 068 } 069 else 070 { 071 addSubtag(subtag); 072 } 073 } 074 } 075 } 076 077 //########################################################################### 078 // PUBLIC METHODS 079 //########################################################################### 080 081 //--------------------------------------------------------------------------- 082 public void addCell(SsmlCell inValue) 083 { 084 CellRef cellRef = inValue.getRef(); 085 SsmlRow row = getRowByRowIndex(cellRef.getRowIndex()); 086 row.addCell(inValue); 087 } 088 089 //--------------------------------------------------------------------------- 090 public SsmlCell getCell(CellRef inCellRef) 091 { 092 SsmlCell requestedCell = null; 093 094 SsmlRow row = getRowByRowIndex(inCellRef.getRowIndex()); 095 if (row != null) 096 { 097 List<SsmlCell> cells = row.getCells(); 098 if (CollectionUtil.hasValues(cells)) 099 { 100 for (SsmlCell cell : cells) 101 { 102 if (cell.getRef().equals(inCellRef)) 103 { 104 requestedCell = cell; 105 break; 106 } 107 } 108 } 109 } 110 else 111 { 112 row = addRow(inCellRef.getRowIndex()); 113 } 114 115 if (null == requestedCell) 116 { 117 requestedCell = new SsmlCell(mParentWorksheet).setRef(inCellRef); 118 row.addCell(requestedCell); 119 } 120 121 return requestedCell; 122 } 123 124 //--------------------------------------------------------------------------- 125 public SsmlRow addRow() 126 { 127 return addRow(findMaxRowIndex() + 1); 128 } 129 130 //--------------------------------------------------------------------------- 131 public SsmlRow addRow(int inRowIndex) 132 { 133 SsmlRow row = null; 134 135 List<SsmlRow> rows = getRows(); 136 if (CollectionUtil.hasValues(rows)) 137 { 138 for (int i = 0; i < rows.size(); i++) 139 { 140 SsmlRow existingRow = rows.get(i); 141 if (existingRow.getRowIndex() == inRowIndex) 142 { 143 row = existingRow; 144 break; 145 } 146 else if (existingRow.getRowIndex() > inRowIndex) 147 { 148 // This row has a higher index than the one we are adding. 149 // Insert the new row just before this one. 150 row = new SsmlRow(mParentWorksheet).setRowIndex(inRowIndex); 151 addSubtag(i, row); 152 break; 153 } 154 } 155 } 156 157 if (null == row) 158 { 159 row = new SsmlRow(mParentWorksheet).setRowIndex(inRowIndex); 160 addSubtag(row); 161 } 162 163 return row; 164 } 165 166 //--------------------------------------------------------------------------- 167 public SsmlRow getRowByRowIndex(int inRowIndex) 168 { 169 SsmlRow row = null; 170 List<SsmlRow> rows = getRows(); 171 if (CollectionUtil.hasValues(rows)) 172 { 173 for (int i = 0; i < rows.size(); i++) 174 { 175 SsmlRow existingRow = rows.get(i); 176 if (existingRow.getRowIndex() == inRowIndex) 177 { 178 row = existingRow; 179 break; 180 } 181 else if (existingRow.getRowIndex() > inRowIndex) 182 { 183 break; 184 } 185 } 186 } 187 188 return row; 189 } 190 191 //--------------------------------------------------------------------------- 192 public List<SsmlRow> getRows() 193 { 194 return (List<SsmlRow>) (Object) getSubtagsByName(SsmlXML.ROW); 195 } 196 197 //--------------------------------------------------------------------------- 198 public CellRange getCellRange() 199 { 200 CellRange cellRange = null; 201 List<XMLTag> rowTags = getSubtagsByName(SsmlXML.ROW); 202 203 int beginCol = Integer.MAX_VALUE; 204 int beginRow = Integer.MAX_VALUE; 205 int endCol = -1; 206 int endRow = -1; 207 208 if (CollectionUtil.hasValues(rowTags)) 209 { 210 for (SsmlRow row : getRows()) 211 { 212 CellRef minCellRef = row.getMinCellRef(); 213 if (minCellRef.getColIndex() < beginCol) 214 { 215 beginCol = minCellRef.getColIndex(); 216 } 217 218 if (minCellRef.getRowIndex() < beginRow) 219 { 220 beginRow = minCellRef.getRowIndex(); 221 } 222 223 CellRef maxCellRef = row.getMaxCellRef(); 224 if (maxCellRef.getColIndex() > endCol) 225 { 226 endCol = maxCellRef.getColIndex(); 227 } 228 229 if (maxCellRef.getRowIndex() > endRow) 230 { 231 endRow = maxCellRef.getRowIndex(); 232 } 233 } 234 235 cellRange = new CellRange(new CellRef().setColIndex(beginCol).setRowIndex(beginRow), 236 new CellRef().setColIndex(endCol).setRowIndex(endRow)); 237 238 } 239 240 return cellRange; 241 } 242 243 //########################################################################### 244 // PRIVATE METHODS 245 //########################################################################### 246 247 //--------------------------------------------------------------------------- 248 private void addRow(SsmlRow inRow) 249 { 250 addSubtag(inRow); 251 } 252 253 //--------------------------------------------------------------------------- 254 // Return the index value on the last row attached to this sheet. 255 // (The rows should always be guaranteed to be in the proper order.) 256 private int findMaxRowIndex() 257 { 258 int maxRowIndex = 0; 259 List<SsmlRow> rows = getRows(); 260 if (CollectionUtil.hasValues(rows)) 261 { 262 maxRowIndex = rows.get(rows.size() - 1).getRowIndex(); 263 } 264 265 return maxRowIndex; 266 } 267 268}