001package com.hfg.xml.msofficexml.part;
002
003
004import java.io.File;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.OutputStream;
008
009import com.hfg.util.io.StreamUtil;
010
011//------------------------------------------------------------------------------
012/**
013 Represents an Office Open XML VBA part.
014
015 @author J. Alex Taylor, hairyfatguy.com
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037
038public abstract class VbaPart
039{
040   private File        mFile;
041   private InputStream mStream;
042
043   //###########################################################################
044   // CONSTRUCTORS
045   //###########################################################################
046
047   //---------------------------------------------------------------------------
048   public VbaPart(InputStream inStream, String inName)
049   {
050      if (null == inStream)
051      {
052         throw new RuntimeException("No InputStream specified!");
053      }
054
055      mFile = new File(getDir(), generateFilename(inName));
056
057      mStream = inStream;
058   }
059
060   //###########################################################################
061   // PUBLIC METHODS
062   //###########################################################################
063
064   //---------------------------------------------------------------------------
065   public File getFile()
066   {
067      return mFile;
068   }
069
070   //---------------------------------------------------------------------------
071   public void write(OutputStream inOutStream)
072      throws IOException
073   {
074      StreamUtil.writeToStream(mStream, inOutStream);
075   }
076
077
078   //---------------------------------------------------------------------------
079   protected abstract String getDir();
080
081   //---------------------------------------------------------------------------
082   private String generateFilename(String inOrigName)
083   {
084      String name = inOrigName.trim().replaceAll("\\s+", "_");
085
086      return name;
087   }
088}