All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.OfficeOpenXmlDocument Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml;

import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedSet;
import com.hfg.util.io.FileBytes;
import com.hfg.xml.msofficexml.part.*;

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.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;

   private String mName;

   //---------------------------------------------------------------------------
   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 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
   {
      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();
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy