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.graphics.units.GfxSize2D;
010import com.hfg.util.io.StreamUtil;
011import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
012import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
013
014//------------------------------------------------------------------------------
015/**
016 Represents an Office Open XML media part.
017
018 @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class MediaPart extends OfficeXMLPart
042{
043   private File        mFile;
044   private String      mDescription;
045   private String      mTitle;
046   private InputStream mStream;
047   private GfxSize2D   mDimensions;
048
049   //###########################################################################
050   // CONSTRUCTORS
051   //###########################################################################
052
053   //---------------------------------------------------------------------------
054   public MediaPart(OfficeOpenXmlDocument inParentDoc, InputStream inStream, String inName)
055   {
056      super(inParentDoc);
057
058      if (null == inStream)
059      {
060         throw new RuntimeException("No InputStream specified!");
061      }
062
063      mFile = new File(WmlXML.MEDIA_DIR, generateFilename(inName));
064
065      mStream = inStream;
066   }
067
068   //---------------------------------------------------------------------------
069   public MediaPart(OfficeOpenXmlDocument inParentDoc, InputStream inStream, String inName, GfxSize2D inDimensions)
070   {
071      this(inParentDoc, inStream, inName);
072      setDimensions(inDimensions);
073   }
074
075   //###########################################################################
076   // PUBLIC METHODS
077   //###########################################################################
078
079   //---------------------------------------------------------------------------
080   public File getFile()
081   {
082      return mFile;
083   }
084
085   //---------------------------------------------------------------------------
086   public MediaPart setDescription(String inValue)
087   {
088      mDescription = inValue;
089      return this;
090   }
091
092   //---------------------------------------------------------------------------
093   public String getDescription()
094   {
095      return mDescription;
096   }
097
098   //---------------------------------------------------------------------------
099   public MediaPart setTitle(String inValue)
100   {
101      mTitle = inValue;
102      return this;
103   }
104
105   //---------------------------------------------------------------------------
106   public String getTitle()
107   {
108      return mTitle;
109   }
110
111   //---------------------------------------------------------------------------
112   public MediaPart setDimensions(GfxSize2D inDimensions)
113   {
114      mDimensions = inDimensions;
115      return this;
116   }
117
118   //---------------------------------------------------------------------------
119   public GfxSize2D getDimensions()
120   {
121      return mDimensions;
122   }
123
124   //---------------------------------------------------------------------------
125   public void write(OutputStream inOutStream)
126      throws IOException
127   {
128      StreamUtil.writeToStream(mStream, inOutStream);
129   }
130
131
132   //---------------------------------------------------------------------------
133   private String generateFilename(String inOrigName)
134   {
135      String name = inOrigName.trim().replaceAll("\\s+", "_");
136
137      return name;
138   }
139}