001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003import java.io.File;
004import java.util.List;
005
006import com.hfg.util.collection.CollectionUtil;
007import com.hfg.util.collection.OrderedMap;
008import com.hfg.xml.XMLNode;
009import com.hfg.xml.XMLTag;
010import com.hfg.xml.msofficexml.OfficeOpenXmlException;
011import com.hfg.xml.msofficexml.OfficeXML;
012import com.hfg.xml.msofficexml.docx.RelationshipXML;
013import com.hfg.xml.msofficexml.xlsx.Xlsx;
014import com.hfg.xml.msofficexml.xlsx.part.WorkbookPart;
015import com.hfg.xml.msofficexml.xlsx.part.WorksheetPart;
016
017
018//------------------------------------------------------------------------------
019/**
020 Represents an Office Open XML workbook (<s:workbook>) tag.
021
022 @author J. Alex Taylor, hairyfatguy.com
023 */
024//------------------------------------------------------------------------------
025// com.hfg XML/HTML Coding Library
026//
027// This library is free software; you can redistribute it and/or
028// modify it under the terms of the GNU Lesser General Public
029// License as published by the Free Software Foundation; either
030// version 2.1 of the License, or (at your option) any later version.
031//
032// This library is distributed in the hope that it will be useful,
033// but WITHOUT ANY WARRANTY; without even the implied warranty of
034// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
035// Lesser General Public License for more details.
036//
037// You should have received a copy of the GNU Lesser General Public
038// License along with this library; if not, write to the Free Software
039// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
040//
041// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
042// jataylor@hairyfatguy.com
043//------------------------------------------------------------------------------
044
045public class SsmlWorkbook extends SsmlXMLTag
046{
047   private XMLTag mSheetsTag;
048   private XMLTag mDefinedNamesTag;
049   private SsmlWorkbookProperties mWorkbookProperties;
050
051   private int    mSheetIndex = 1;
052
053   //##########################################################################
054   // CONSTRUCTORS
055   //##########################################################################
056
057   //---------------------------------------------------------------------------
058   public SsmlWorkbook(Xlsx inXlsx)
059   {
060      super(SsmlXML.WORKBOOK, inXlsx);
061      init();
062   }
063
064   //---------------------------------------------------------------------------
065   public SsmlWorkbook(Xlsx inXlsx, XMLTag inWorkbookTag)
066   {
067      super(SsmlXML.WORKBOOK, inXlsx);
068
069      try
070      {
071         XMLTag propertiesTag = inWorkbookTag.getOptionalSubtagByName(SsmlXML.WORKBOOK_PROPS);
072         if (propertiesTag != null)
073         {
074            mWorkbookProperties = new SsmlWorkbookProperties(getParentDoc(), propertiesTag);
075         }
076
077         // TODO: Parse other pieces
078      }
079      catch (Exception e)
080      {
081         throw new OfficeOpenXmlException("Problem parsing workbook!", e);
082      }
083   }
084
085   //---------------------------------------------------------------------------
086   private void init()
087   {
088      getProperties();
089   }
090
091   //##########################################################################
092   // PUBLIC METHODS
093   //##########################################################################
094
095   //---------------------------------------------------------------------------
096   public SsmlWorkbookProperties getProperties()
097   {
098      if (null == mWorkbookProperties)
099      {
100         mWorkbookProperties = new SsmlWorkbookProperties(getParentDoc());
101         addSubtag(mWorkbookProperties);
102      }
103
104      return mWorkbookProperties;
105   }
106
107   //---------------------------------------------------------------------------
108   public void addSheet(String inSheetName, String inRelationshipId)
109   {
110      XMLTag sheetTag = new XMLTag(SsmlXML.SHEET);
111      sheetTag.setAttribute(SsmlXML.NAME_ATT, inSheetName);
112      sheetTag.setAttribute(SsmlXML.SHEET_ID_ATT, mSheetIndex++);
113      sheetTag.setAttribute(RelationshipXML.ID_ATT, inRelationshipId);
114      getSheetsTag().addSubtag(sheetTag);
115   }
116
117   //---------------------------------------------------------------------------
118   public void removeSheet(String inSheetName)
119   {
120      List<XMLTag> sheetTags = mSheetsTag.getSubtagsByName(SsmlXML.SHEET);
121      if (CollectionUtil.hasValues(sheetTags))
122      {
123         for (XMLTag tag : sheetTags)
124         {
125            if (inSheetName.equals(tag.getAttribute(SsmlXML.NAME_ATT)))
126            {
127               mSheetsTag.removeSubtag(tag);
128               break;
129            }
130         }
131      }
132   }
133
134   //---------------------------------------------------------------------------
135   public void setDefinedName(String inName, SsmlWorksheet inWorksheet, String inValue)
136   {
137      removeDefinedName(inName, inWorksheet);
138
139      XMLNode definedNameTag = new XMLTag(SsmlXML.DEFINED_NAME)
140         .setAttribute(SsmlXML.NAME_ATT, inName)
141         .setAttribute(SsmlXML.LOCAL_SHEET_ID_ATT, inWorksheet.getParentWorksheetPart().getSheetIndex())
142         .setContent(inValue);
143
144      getDefinedNamesTag().addSubtag(definedNameTag);
145   }
146
147   //---------------------------------------------------------------------------
148   public void removeDefinedName(String inName, SsmlWorksheet inWorksheet)
149   {
150      List<XMLTag> tags = getDefinedNamesTag().getSubtagsByAttribute(SsmlXML.NAME_ATT, inName);
151      if (CollectionUtil.hasValues(tags))
152      {
153         if (inWorksheet != null)
154         {
155            for (XMLTag tag : tags)
156            {
157               if (tag.hasAttribute(SsmlXML.LOCAL_SHEET_ID_ATT)
158                     && tag.getAttributeValue(SsmlXML.LOCAL_SHEET_ID_ATT).equals(inWorksheet.getParentWorksheetPart().getSheetIndex() + ""))
159               {
160                  removeSubtag(tag);
161                  break;
162               }
163            }
164         }
165         else
166         {
167            for (XMLTag tag : tags)
168            {
169               removeSubtag(tag);
170            }
171         }
172      }
173   }
174
175   //##########################################################################
176   // PRIVATE METHODS
177   //##########################################################################
178
179   //---------------------------------------------------------------------------
180   private XMLTag getSheetsTag()
181   {
182      if (null == mSheetsTag)
183      {
184         mSheetsTag = new XMLTag(SsmlXML.SHEETS);
185         addSubtag(mSheetsTag);
186      }
187
188      return mSheetsTag;
189   }
190
191   //---------------------------------------------------------------------------
192   private XMLTag getDefinedNamesTag()
193   {
194      if (null == mDefinedNamesTag)
195      {
196         mDefinedNamesTag = new XMLTag(SsmlXML.DEFINED_NAMES);
197         addSubtag(mDefinedNamesTag);
198      }
199
200      return mDefinedNamesTag;
201   }
202
203
204}