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()
{
int rowIndex = 1;
List rows = getRows();
if (CollectionUtil.hasValues(rows))
{
rowIndex = rows.get(rows.size() - 1).getRowIndex() + 1;
}
// They might set the row index later but we want to be sure that it has a value.
SsmlRow row = new SsmlRow(mParentWorksheet).setRowIndex(rowIndex);
addSubtag(row);
return row;
}
//---------------------------------------------------------------------------
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 - 1, 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;
}
//---------------------------------------------------------------------------
private void addRow(SsmlRow inRow)
{
addSubtag(inRow);
}
}