com.hfg.xml.msofficexml.xlsx.part.SharedStringsPart 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.List;
import com.hfg.util.collection.BiHashMap;
import com.hfg.util.collection.BiMap;
import com.hfg.xml.XMLDoc;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLUtil;
import com.hfg.xml.msofficexml.part.OfficeXMLPart;
import com.hfg.xml.msofficexml.xlsx.SsmlRelationshipType;
import com.hfg.xml.msofficexml.xlsx.Xlsx;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlTextRun;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
//------------------------------------------------------------------------------
/**
Office Open XML format Excel shared string part.
@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 SharedStringsPart extends OfficeXMLPart
{
private XMLTag mSharedStringsTableTag;
private BiMap mStringIndexMap = new BiHashMap<>();
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SharedStringsPart(Xlsx inXlsx)
{
super(inXlsx);
setFile(SsmlXML.SHARED_STRINGS_FILE);
// Register the content type
inXlsx.getContentTypesPart().addOverride(this, SsmlContentType.SPREADSHEET_SHARED_STRINGS);
// Register the relationship
inXlsx.getWorkbookRelationshipPart().addRelationship(SsmlRelationshipType.SHARED_STRINGS, this);
mSharedStringsTableTag = new SsmlSharedStringsTable(inXlsx);
setRootNode(mSharedStringsTableTag);
}
//---------------------------------------------------------------------------
public SharedStringsPart(Xlsx inXlsx, BufferedInputStream inStream)
{
this(inXlsx);
XMLDoc doc = new XMLDoc(inStream);
mSharedStringsTableTag = (XMLTag) doc.getRootNode();
int index = 0;
for (XMLNode stringItemTag : mSharedStringsTableTag.getSubtagsByName(SsmlXML.STRING_ITEM))
{
// Strip out the content // TODO: Return complex content (text runs) in their native state
StringBuilder buffer = new StringBuilder();
for (XMLNode subtag : (List) (Object) stringItemTag.getSubtags())
{
if (subtag.getTagName().equals(SsmlXML.TEXT.getLocalName()))
{
buffer.append(subtag.getContent());
}
else if (subtag.getTagName().equals(SsmlXML.TEXT_RUN.getLocalName()))
{
for (XMLNode textTag : subtag.getSubtagsByName(SsmlXML.TEXT))
{
buffer.append(textTag.getContent());
}
}
}
// Using unescapeEntities() instead of getUnescapedContent() because we were observing additional escaping like "
mStringIndexMap.put(XMLUtil.unescapeEntities(buffer.toString()), index++);
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
public int defineString(SsmlTextRun inValue)
{
Integer index = mStringIndexMap.get(inValue.toXML());
if (null == index)
{
// It's a new string value
index = mStringIndexMap.size();
mStringIndexMap.put(inValue.toXML(), index);
XMLTag stringItemTag = new XMLTag(SsmlXML.STRING_ITEM);
stringItemTag.addSubtag(inValue);
mSharedStringsTableTag.addSubtag(stringItemTag);
mSharedStringsTableTag.setAttribute(SsmlXML.COUNT_ATT, mStringIndexMap.size());
mSharedStringsTableTag.setAttribute(SsmlXML.UNIQUE_COUNT_ATT, mStringIndexMap.size());
}
return index;
}
//---------------------------------------------------------------------------
public int defineString(List inValue)
{
StringBuilder xml = new StringBuilder();
for (SsmlTextRun run : inValue)
{
xml.append(run.toXML());
}
Integer index = mStringIndexMap.get(xml.toString());
if (null == index)
{
// It's a new string value
index = mStringIndexMap.size();
mStringIndexMap.put(xml.toString(), index);
XMLTag stringItemTag = new XMLTag(SsmlXML.STRING_ITEM);
stringItemTag.addSubtags(inValue);
mSharedStringsTableTag.addSubtag(stringItemTag);
mSharedStringsTableTag.setAttribute(SsmlXML.COUNT_ATT, mStringIndexMap.size());
mSharedStringsTableTag.setAttribute(SsmlXML.UNIQUE_COUNT_ATT, mStringIndexMap.size());
}
return index;
}
//---------------------------------------------------------------------------
public int defineString(String inValue)
{
Integer index = mStringIndexMap.get(inValue);
if (null == index)
{
// It's a new string value
index = mStringIndexMap.size();
mStringIndexMap.put(inValue, index);
XMLTag textTag = new XMLTag(SsmlXML.TEXT);
textTag.setContent(inValue);
XMLTag stringItemTag = new XMLTag(SsmlXML.STRING_ITEM);
stringItemTag.addSubtag(textTag);
mSharedStringsTableTag.addSubtag(stringItemTag);
mSharedStringsTableTag.setAttribute(SsmlXML.COUNT_ATT, mStringIndexMap.size());
mSharedStringsTableTag.setAttribute(SsmlXML.UNIQUE_COUNT_ATT, mStringIndexMap.size());
}
return index;
}
//---------------------------------------------------------------------------
public String getString(int inIndex)
{
return mStringIndexMap.getKey(inIndex);
}
private class SsmlSharedStringsTable extends SsmlXMLTag
{
//---------------------------------------------------------------------------
public SsmlSharedStringsTable(Xlsx inXlsx)
{
super(SsmlXML.SHARED_STRING_TABLE, inXlsx);
}
}
}