com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlSheetData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
import java.util.List;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.xlsx.CellRange;
import com.hfg.xml.msofficexml.xlsx.CellRef;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML sheet data (<ssml:sheetData>) tag.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class SsmlSheetData extends SsmlXMLTag
{
private SsmlWorksheet mParentWorksheet;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public SsmlSheetData(SsmlWorksheet inParentWorksheet)
{
super(SsmlXML.SHEET_DATA, inParentWorksheet.getParentDoc());
mParentWorksheet = inParentWorksheet;
}
//---------------------------------------------------------------------------
public SsmlSheetData(SsmlWorksheet inParentWorksheet, XMLTag inXMLTag)
{
this(inParentWorksheet);
inXMLTag.verifyTagName(SsmlXML.SHEET_DATA);
// Copy subtags over to this tag
if (CollectionUtil.hasValues(inXMLTag.getSubtags()))
{
for (XMLTag subtag : (List) (Object) inXMLTag.getSubtags())
{
if (subtag.getTagName().equals(SsmlXML.ROW.getLocalName()))
{
addRow(new SsmlRow(inParentWorksheet, subtag));
}
else
{
addSubtag(subtag);
}
}
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public void addCell(SsmlCell inValue)
{
CellRef cellRef = inValue.getRef();
SsmlRow row = getRowByRowIndex(cellRef.getRowIndex());
row.addCell(inValue);
}
//---------------------------------------------------------------------------
public SsmlCell getCell(CellRef inCellRef)
{
SsmlCell requestedCell = null;
SsmlRow row = getRowByRowIndex(inCellRef.getRowIndex());
if (row != null)
{
List cells = row.getCells();
if (CollectionUtil.hasValues(cells))
{
for (SsmlCell cell : cells)
{
if (cell.getRef().equals(inCellRef))
{
requestedCell = cell;
break;
}
}
}
}
else
{
row = addRow(inCellRef.getRowIndex());
}
if (null == requestedCell)
{
requestedCell = new SsmlCell(mParentWorksheet).setRef(inCellRef);
row.addCell(requestedCell);
}
return requestedCell;
}
//---------------------------------------------------------------------------
public SsmlRow addRow()
{
return addRow(findMaxRowIndex() + 1);
}
//---------------------------------------------------------------------------
public SsmlRow addRow(int inRowIndex)
{
SsmlRow row = null;
List rows = getRows();
if (CollectionUtil.hasValues(rows))
{
for (int i = 0; i < rows.size(); i++)
{
SsmlRow existingRow = rows.get(i);
if (existingRow.getRowIndex() == inRowIndex)
{
row = existingRow;
break;
}
else if (existingRow.getRowIndex() > inRowIndex)
{
// This row has a higher index than the one we are adding.
// Insert the new row just before this one.
row = new SsmlRow(mParentWorksheet).setRowIndex(inRowIndex);
addSubtag(i, row);
break;
}
}
}
if (null == row)
{
row = new SsmlRow(mParentWorksheet).setRowIndex(inRowIndex);
addSubtag(row);
}
return row;
}
//---------------------------------------------------------------------------
public SsmlRow getRowByRowIndex(int inRowIndex)
{
SsmlRow row = null;
List rows = getRows();
if (CollectionUtil.hasValues(rows))
{
for (int i = 0; i < rows.size(); i++)
{
SsmlRow existingRow = rows.get(i);
if (existingRow.getRowIndex() == inRowIndex)
{
row = existingRow;
break;
}
else if (existingRow.getRowIndex() > inRowIndex)
{
break;
}
}
}
return row;
}
//---------------------------------------------------------------------------
public List getRows()
{
return (List) (Object) getSubtagsByName(SsmlXML.ROW);
}
//---------------------------------------------------------------------------
public CellRange getCellRange()
{
CellRange cellRange = null;
List rowTags = getSubtagsByName(SsmlXML.ROW);
int beginCol = Integer.MAX_VALUE;
int beginRow = Integer.MAX_VALUE;
int endCol = -1;
int endRow = -1;
if (CollectionUtil.hasValues(rowTags))
{
for (SsmlRow row : getRows())
{
CellRef minCellRef = row.getMinCellRef();
if (minCellRef.getColIndex() < beginCol)
{
beginCol = minCellRef.getColIndex();
}
if (minCellRef.getRowIndex() < beginRow)
{
beginRow = minCellRef.getRowIndex();
}
CellRef maxCellRef = row.getMaxCellRef();
if (maxCellRef.getColIndex() > endCol)
{
endCol = maxCellRef.getColIndex();
}
if (maxCellRef.getRowIndex() > endRow)
{
endRow = maxCellRef.getRowIndex();
}
}
cellRange = new CellRange(new CellRef().setColIndex(beginCol).setRowIndex(beginRow),
new CellRef().setColIndex(endCol).setRowIndex(endRow));
}
return cellRange;
}
//---------------------------------------------------------------------------
public int getMaxRowIndex()
{
return findMaxRowIndex();
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private void addRow(SsmlRow inRow)
{
addSubtag(inRow);
}
//---------------------------------------------------------------------------
// Return the index value on the last row attached to this sheet.
// (The rows should always be guaranteed to be in the proper order.)
private int findMaxRowIndex()
{
int maxRowIndex = 0;
List rows = getRows();
if (CollectionUtil.hasValues(rows))
{
maxRowIndex = rows.get(rows.size() - 1).getRowIndex();
}
return maxRowIndex;
}
}