001package com.hfg.xml.msoffice2003.spreadsheetml;
002
003import com.hfg.util.collection.CollectionUtil;
004import com.hfg.util.StringUtil;
005import com.hfg.xml.XMLTag;
006
007import java.util.List;
008import java.util.Map;
009import java.util.HashMap;
010
011//------------------------------------------------------------------------------
012/**
013 Excel workbook.
014
015 @author J. Alex Taylor, hairyfatguy.com
016 */
017//------------------------------------------------------------------------------
018// com.hfg 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// TODO: Add a validate() method to check the workbook structure for problems like duplicate worksheet names.
038
039public class ExcelWorkbook extends XMLTag
040{
041   private XMLTag mStylesTag = new XMLTag(SpreadsheetML.STYLES);
042
043   private Map<String, ExcelStyle> mStyleMap = new HashMap<String, ExcelStyle>();
044
045
046   //---------------------------------------------------------------------------
047   public ExcelWorkbook()
048   {
049      super(SpreadsheetML.WORKBOOK);
050      addSubtag(mStylesTag);
051   }
052
053   //---------------------------------------------------------------------------
054   public ExcelWorksheet addWorksheet()
055   {
056      ExcelWorksheet worksheet = new ExcelWorksheet();
057      addWorksheet(worksheet);
058
059      return worksheet;
060   }
061
062   //---------------------------------------------------------------------------
063   public ExcelWorksheet addWorksheet(String inName)
064   {
065      ExcelWorksheet worksheet = new ExcelWorksheet(inName);
066      addWorksheet(worksheet);
067
068      return worksheet;
069   }
070
071   //---------------------------------------------------------------------------
072   public void addWorksheet(ExcelWorksheet inWorksheet)
073   {
074      checkWorksheetNameForUniqueness(inWorksheet.getName());
075      addSubtag(inWorksheet);
076   }
077
078   //---------------------------------------------------------------------------
079   public List<ExcelWorksheet> getWorksheets()
080   {
081      return (List<ExcelWorksheet>) (Object) getSubtagsByName(SpreadsheetML.WORKSHEET.getLocalName());
082   }
083
084   //---------------------------------------------------------------------------
085   public ExcelWorkbook addStyle(ExcelStyle inStyle)
086   {
087      mStylesTag.addSubtag(inStyle);
088      mStyleMap.put(inStyle.getId(), inStyle);
089      return this;
090   }
091
092   //---------------------------------------------------------------------------
093   public ExcelStyle getStyle(String inStyleId)
094   {
095      return mStyleMap.get(inStyleId);
096   }
097
098   //---------------------------------------------------------------------------
099   private void checkWorksheetNameForUniqueness(String inName)
100   {
101      List<ExcelWorksheet> sheets = getWorksheets();
102      if (CollectionUtil.hasValues(sheets))
103      {
104         for (ExcelWorksheet sheet : sheets)
105         {
106            if (inName.equals(sheet.getName()))
107            {
108               throw new ExcelFormatException("The worksheet name " + StringUtil.singleQuote(inName) + " is not unique!");
109            }
110         }
111      }
112   }
113}