com.hfg.xml.msofficexml.OfficeOpenXmlDocument 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;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedSet;
import com.hfg.util.io.FileBytes;
import com.hfg.xml.msofficexml.part.*;
//------------------------------------------------------------------------------
/**
Abstract base class for Office Open XML format documents.
@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 abstract class OfficeOpenXmlDocument
{
private Set mParts = new OrderedSet<>(20);
private List mMediaParts;
private List mThemeParts;
private List mVbaParts;
private ContentTypesPart mContentTypesPart;
private PackageRelationshipPart mPackageRelationshipPart;
private DocumentPropertiesPart mDocumentPropertiesPart;
private String mName;
public static Level sSQLLoggingLevel = Level.FINE;
private final static Logger LOGGER = Logger.getLogger(OfficeOpenXmlDocument.class.getPackage().getName());
//---------------------------------------------------------------------------
protected OfficeOpenXmlDocument()
{
// Setup essential relationships
mPackageRelationshipPart = new PackageRelationshipPart(this);
mPackageRelationshipPart.addRelationship(RelationshipType.CORE_PROPERTIES, OfficeXML.CORE_PROPERTIES_FILE);
mContentTypesPart = new ContentTypesPart(this);
mDocumentPropertiesPart = new DocumentPropertiesPart(this);
mContentTypesPart.addOverride(mDocumentPropertiesPart, OfficeOpenXMLContentType.CORE_PROPERTIES);
}
//---------------------------------------------------------------------------
public void addPart(OfficeXMLPart inPart)
{
if (inPart instanceof MediaPart)
{
addMediaPart((MediaPart) inPart);
}
else
{
mParts.add(inPart);
if (inPart instanceof ThemePart)
{
if (null == mThemeParts)
{
mThemeParts = new ArrayList<>(5);
}
mThemeParts.add((ThemePart) inPart);
((ThemePart) inPart).setThemeIndex(mThemeParts.size());
}
}
}
//---------------------------------------------------------------------------
public void addMediaPart(MediaPart inValue)
{
if (null == mMediaParts)
{
mMediaParts = new ArrayList<>(10);
}
mMediaParts.add(inValue);
}
//---------------------------------------------------------------------------
public void addVBA_Part(VbaPart inValue)
{
if (null == mVbaParts)
{
mVbaParts = new ArrayList<>(2);
}
mVbaParts.add(inValue);
}
//---------------------------------------------------------------------------
public ContentTypesPart getContentTypesPart()
{
return mContentTypesPart;
}
//---------------------------------------------------------------------------
public PackageRelationshipPart getPackageRelationshipPart()
{
return mPackageRelationshipPart;
}
//---------------------------------------------------------------------------
public DocumentPropertiesPart getDocumentPropertiesPart()
{
return mDocumentPropertiesPart;
}
//---------------------------------------------------------------------------
public String name()
{
return mName;
}
//---------------------------------------------------------------------------
public OfficeOpenXmlDocument setName(String inValue)
{
mName = inValue;
return this;
}
//---------------------------------------------------------------------------
public FileBytes toFileBytes()
throws IOException
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
write(stream);
FileBytes fileBytes = new FileBytes(name());
fileBytes.setData(stream.toByteArray());
return fileBytes;
}
//---------------------------------------------------------------------------
public void write(File inFile)
throws IOException
{
FileOutputStream fileStream = null;
try
{
fileStream = new FileOutputStream(inFile);
write(fileStream);
}
finally
{
if (fileStream != null)
{
fileStream.close();
}
}
}
//---------------------------------------------------------------------------
public void write(OutputStream inStream)
throws IOException
{
if (LOGGER.isLoggable(sSQLLoggingLevel))
{
LOGGER.log(sSQLLoggingLevel, "Writing OfficeOpenXML document " + StringUtil.singleQuote(name()) + "\n" + OfficeOpenXmlXsd.getInstance().getConfigSummary());
}
ZipOutputStream zipStream = new ZipOutputStream(inStream);
for (OfficeXMLPart part : mParts)
{
// Be sure we don't have any lingering character entities.
part.replaceCharacterEntities();
ZipEntry zipEntry = new ZipEntry(part.getFile().getPath());
zipStream.putNextEntry(zipEntry);
part.toXML(zipStream);
zipStream.closeEntry();
}
if (CollectionUtil.hasValues(mMediaParts))
{
for (MediaPart mediaPart : mMediaParts)
{
ZipEntry zipEntry = new ZipEntry(mediaPart.getFile().getPath());
zipStream.putNextEntry(zipEntry);
mediaPart.write(zipStream);
zipStream.closeEntry();
}
}
if (CollectionUtil.hasValues(mVbaParts))
{
for (VbaPart vbaPart : mVbaParts)
{
ZipEntry zipEntry = new ZipEntry(vbaPart.getFile().getPath());
zipStream.putNextEntry(zipEntry);
vbaPart.write(zipStream);
zipStream.closeEntry();
}
}
zipStream.finish();
}
}