com.hfg.xml.msofficexml.xlsx.part.StylesPart 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.part;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;
import com.hfg.xml.XMLDoc;
import com.hfg.xml.XMLNode;
import com.hfg.util.StackTraceUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLUtil;
import com.hfg.xml.msofficexml.OfficeOpenXmlException;
import com.hfg.xml.msofficexml.xlsx.SsmlRelationshipType;
import com.hfg.xml.msofficexml.xlsx.Xlsx;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.*;
//------------------------------------------------------------------------------
/**
Styles part specific to OfficeOpenXML SpreadsheetML.
@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 StylesPart extends XlsxPart
{
private XMLTag mStylesheetTag;
private XMLTag mNumberFormatsTag;
private XMLTag mFontsTag;
private XMLTag mFillsTag;
private XMLTag mBordersTag;
private XMLTag mCellFormatsTag;
private XMLTag mStyleFormatsTag;
private XMLTag mCellStylesTag;
private XMLTag mDifferentialFormatsTag;
private SsmlFont mDefaultFont;
private SsmlFill mDefaultFill;
private SsmlBorder mDefaultBorder;
private SsmlStyleFormat mDefaultStyleFormat;
private SsmlCellFormat mDefaultCellFormat;
private SsmlCellStyle mNormalStyle;
private SsmlCellFormat mDefaultDateCellFormat;
private List mNumberFormats = new ArrayList<>();
private List mFonts = new ArrayList<>();
private List mFills = new ArrayList<>();
private List mBorders = new ArrayList<>();
private List mCellFormats = new ArrayList<>();
private List mStyleFormats = new ArrayList<>();
private List mCellStyles = new ArrayList<>();
private List mDifferentialFormats = new ArrayList<>();
//---------------------------------------------------------------------------
/**
This constructor should only be used by the Xlsx object.
* @param inXlsx the parent Excel document
*/
public StylesPart(Xlsx inXlsx)
{
super(inXlsx);
setFile(SsmlXML.STYLES_FILE);
if (! (StackTraceUtil.getCallingClassName().equals(getClass().getName())
|| StackTraceUtil.getCallingClassName().equals(Xlsx.class.getName())))
{
throw new OfficeOpenXmlException("An Xlsx object can have only one styles part! Use xlsx.getStylesPart() to access it.");
}
// Register the content type
inXlsx.getContentTypesPart().addOverride(this, SsmlContentType.SPREADSHEET_STYLES);
// Register the relationship
inXlsx.getWorkbookRelationshipPart().addRelationship(SsmlRelationshipType.STYLES, this);
mStylesheetTag = new SsmlStylesheet(inXlsx);
setRootNode(mStylesheetTag);
// setDefaults();
}
//---------------------------------------------------------------------------
public StylesPart(Xlsx inXlsx, BufferedInputStream inStream)
{
this(inXlsx);
XMLDoc doc = new XMLDoc(inStream);
mStylesheetTag = (XMLTag) doc.getRootNode();
mNumberFormatsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.NUM_FORMATS);
if (mNumberFormatsTag != null)
{
for (XMLTag subtag : (List) (Object) mNumberFormatsTag.getSubtagsByName(SsmlXML.NUM_FORMAT))
{
mNumberFormats.add(new SsmlNumberFormat(inXlsx, subtag));
}
}
mFontsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.FONTS);
if (mFontsTag != null)
{
for (XMLTag subtag : (List) (Object) mFontsTag.getSubtagsByName(SsmlXML.FONT))
{
mFonts.add(new SsmlFont(inXlsx, subtag));
}
}
// TODO
mFillsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.FILLS);
// TODO
mBordersTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.BORDERS);
mCellFormatsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.CELL_FORMATS);
if (mCellFormatsTag != null)
{
for (XMLTag subtag : (List) (Object) mCellFormatsTag.getSubtagsByName(SsmlXML.CELL_FORMAT))
{
mCellFormats.add(new SsmlCellFormat(inXlsx, subtag));
}
}
mStyleFormatsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.CELL_STYLE_FORMATS);
mCellStylesTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.CELL_STYLES);
mDifferentialFormatsTag = mStylesheetTag.getOptionalSubtagByName(SsmlXML.DIFFERENTIAL_FORMATS);
}
//---------------------------------------------------------------------------
public void setDefaults()
{
mDefaultFont = new SsmlFont(this).setName("Verdana").setSize(10);
mDefaultFill = new SsmlFill(this).setPatternType(SsmlPatternType.none);
// Add a second default fill value that sometimes seems to be required
new SsmlFill(this).setPatternType(SsmlPatternType.gray125);
mDefaultBorder = new SsmlBorder(this);
mDefaultStyleFormat = new SsmlStyleFormat(this)
.setFont(mDefaultFont)
.setFill(mDefaultFill)
.setBorder(mDefaultBorder);
mDefaultCellFormat = new SsmlCellFormat(this)
.setFont(mDefaultFont)
.setFill(mDefaultFill)
.setBorder(mDefaultBorder)
.setStyleFormat(mDefaultStyleFormat);
mNormalStyle = new SsmlCellStyle(this)
.setName("Normal")
.setBuiltinId(1)
.setStyleFormat(mDefaultStyleFormat);
}
//---------------------------------------------------------------------------
public SsmlFont getDefaultFont()
{
return mDefaultFont;
}
//---------------------------------------------------------------------------
public SsmlFill getDefaultFill()
{
return mDefaultFill;
}
//---------------------------------------------------------------------------
public SsmlBorder getDefaultBorder()
{
return mDefaultBorder;
}
//---------------------------------------------------------------------------
public SsmlStyleFormat getDefaultStyleFormat()
{
return mDefaultStyleFormat;
}
//---------------------------------------------------------------------------
public SsmlCellFormat getDefaultCellFormat()
{
return mDefaultCellFormat;
}
//---------------------------------------------------------------------------
public SsmlCellFormat getDefaultDateCellFormat()
{
if (null == mDefaultDateCellFormat)
{
mDefaultDateCellFormat = new SsmlCellFormat(this)
.setNumberFormat(SsmlNumberFormat.DATE_MM_DD_YY)
.setFont(mDefaultFont)
.setFill(mDefaultFill)
.setBorder(mDefaultBorder);
}
return mDefaultDateCellFormat;
}
//---------------------------------------------------------------------------
public List getNumberFormats()
{
return mNumberFormats;
}
//---------------------------------------------------------------------------
public int defineNumberFormat(SsmlNumberFormat inValue)
{
int index = mNumberFormats.size();
mNumberFormats.add(inValue);
if (null == mNumberFormatsTag)
{
mNumberFormatsTag = new XMLTag(SsmlXML.NUM_FORMATS);
mStylesheetTag.addSubtag(mNumberFormatsTag);
}
// Add it to a number formats tag
mNumberFormatsTag.addSubtag(inValue);
mNumberFormatsTag.setAttribute(SsmlXML.COUNT_ATT, mNumberFormats.size());
return index;
}
//---------------------------------------------------------------------------
public int defineFont(SsmlFont inValue)
{
int index = mFonts.size();
mFonts.add(inValue);
if (null == mFontsTag)
{
mFontsTag = new XMLTag(SsmlXML.FONTS);
mStylesheetTag.addSubtag(mFontsTag);
}
// Add it to a fonts tag
mFontsTag.addSubtag(inValue);
mFontsTag.setAttribute(SsmlXML.COUNT_ATT, mFonts.size());
return index;
}
//---------------------------------------------------------------------------
public List getFonts()
{
return mFonts;
}
//---------------------------------------------------------------------------
public int defineFill(SsmlFill inValue)
{
int index = mFills.size();
mFills.add(inValue);
if (null == mFillsTag)
{
mFillsTag = new XMLTag(SsmlXML.FILLS);
mStylesheetTag.addSubtag(mFillsTag);
}
// Add it to a fills tag
mFillsTag.addSubtag(inValue);
mFillsTag.setAttribute(SsmlXML.COUNT_ATT, mFills.size());
return index;
}
//---------------------------------------------------------------------------
public int defineBorder(SsmlBorder inValue)
{
int index = mBorders.size();
mBorders.add(inValue);
if (null == mBordersTag)
{
mBordersTag = new XMLTag(SsmlXML.BORDERS);
mStylesheetTag.addSubtag(mBordersTag);
}
// Add it to a borders tag
mBordersTag.addSubtag(inValue);
mBordersTag.setAttribute(SsmlXML.COUNT_ATT, mBorders.size());
return index;
}
//---------------------------------------------------------------------------
public int defineCellFormat(SsmlCellFormat inValue)
{
int index = mCellFormats.size();
mCellFormats.add(inValue);
if (null == mCellFormatsTag)
{
mCellFormatsTag = new XMLTag(SsmlXML.CELL_FORMATS);
mStylesheetTag.addSubtag(mCellFormatsTag);
}
// Add it to a cell formats (cellXfs) tag
mCellFormatsTag.addSubtag(inValue);
mCellFormatsTag.setAttribute(SsmlXML.COUNT_ATT, mCellFormats.size());
return index;
}
//---------------------------------------------------------------------------
public List getCellFormats()
{
return mCellFormats;
}
//---------------------------------------------------------------------------
public int defineDifferentialFormat(SsmlDifferentialFormat inValue)
{
int index = mDifferentialFormats.size();
mDifferentialFormats.add(inValue);
if (null == mDifferentialFormatsTag)
{
mDifferentialFormatsTag = new XMLTag(SsmlXML.DIFFERENTIAL_FORMATS);
mStylesheetTag.addSubtag(mDifferentialFormatsTag);
}
// Add it to a differential formats (dxfs) tag
mDifferentialFormatsTag.addSubtag(inValue);
mDifferentialFormatsTag.setAttribute(SsmlXML.COUNT_ATT, mDifferentialFormats.size());
return index;
}
//---------------------------------------------------------------------------
public List getDifferentialFormat()
{
return mDifferentialFormats;
}
//---------------------------------------------------------------------------
public int defineStyleFormat(SsmlStyleFormat inValue)
{
int index = mStyleFormats.size();
mStyleFormats.add(inValue);
if (null == mCellFormatsTag)
{
mStyleFormatsTag = new XMLTag(SsmlXML.CELL_STYLE_FORMATS);
mStylesheetTag.addSubtag(mStyleFormatsTag);
}
// Add it to a cell style formats (cellStyleXfs) tag
mStyleFormatsTag.addSubtag(inValue);
mStyleFormatsTag.setAttribute(SsmlXML.COUNT_ATT, mStyleFormats.size());
return index;
}
//---------------------------------------------------------------------------
public SsmlStyleFormat getStyleFormat(int inIndex)
{
SsmlStyleFormat requestedStyleFormat = null;
if (inIndex >=0
&& inIndex < mStyleFormats.size())
{
requestedStyleFormat = mStyleFormats.get(inIndex);
}
return requestedStyleFormat;
}
//---------------------------------------------------------------------------
public int defineCellStyle(SsmlCellStyle inValue)
{
int index = mCellStyles.size();
mCellStyles.add(inValue);
if (null == mCellStylesTag)
{
mCellStylesTag = new XMLTag(SsmlXML.CELL_STYLES);
mStylesheetTag.addSubtag(mCellStylesTag);
}
// Add it to a cell styles (cellStyles) tag
mCellStylesTag.addSubtag(inValue);
mCellStylesTag.setAttribute(SsmlXML.COUNT_ATT, mCellStyles.size());
return index;
}
private class SsmlStylesheet extends SsmlXMLTag
{
//---------------------------------------------------------------------------
public SsmlStylesheet(Xlsx inXlsx)
{
super(SsmlXML.STYLESHEET, inXlsx);
}
}
}