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 com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedSet;
import com.hfg.xml.msofficexml.part.*;
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.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
//------------------------------------------------------------------------------
/**
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 ContentTypesPart mContentTypesPart;
private PackageRelationshipPart mPackageRelationshipPart;
private DocumentPropertiesPart mDocumentPropertiesPart;
//---------------------------------------------------------------------------
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)
{
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 ContentTypesPart getContentTypesPart()
{
return mContentTypesPart;
}
//---------------------------------------------------------------------------
public PackageRelationshipPart getPackageRelationshipPart()
{
return mPackageRelationshipPart;
}
//---------------------------------------------------------------------------
public DocumentPropertiesPart getDocumentPropertiesPart()
{
return mDocumentPropertiesPart;
}
//---------------------------------------------------------------------------
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
{
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();
}
}
zipStream.finish();
}
}