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

com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlWorkbook Maven / Gradle / Ivy

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

import java.io.File;
import java.util.List;

import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedMap;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXmlException;
import com.hfg.xml.msofficexml.OfficeXML;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
import com.hfg.xml.msofficexml.xlsx.Xlsx;
import com.hfg.xml.msofficexml.xlsx.part.WorkbookPart;
import com.hfg.xml.msofficexml.xlsx.part.WorksheetPart;


//------------------------------------------------------------------------------
/**
 Represents an Office Open XML workbook (<s:workbook>) tag.

 @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 class SsmlWorkbook extends SsmlXMLTag
{
   private XMLTag mSheetsTag;
   private XMLTag mDefinedNamesTag;
   private SsmlWorkbookProperties mWorkbookProperties;

   private int    mSheetIndex = 1;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //---------------------------------------------------------------------------
   public SsmlWorkbook(Xlsx inXlsx)
   {
      super(SsmlXML.WORKBOOK, inXlsx);
      init();
   }

   //---------------------------------------------------------------------------
   public SsmlWorkbook(Xlsx inXlsx, XMLTag inWorkbookTag)
   {
      super(SsmlXML.WORKBOOK, inXlsx);

      try
      {
         XMLTag propertiesTag = inWorkbookTag.getOptionalSubtagByName(SsmlXML.WORKBOOK_PROPS);
         if (propertiesTag != null)
         {
            mWorkbookProperties = new SsmlWorkbookProperties(getParentDoc(), propertiesTag);
         }

         // TODO: Parse other pieces
      }
      catch (Exception e)
      {
         throw new OfficeOpenXmlException("Problem parsing workbook!", e);
      }
   }

   //---------------------------------------------------------------------------
   private void init()
   {
      getProperties();
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //---------------------------------------------------------------------------
   public SsmlWorkbookProperties getProperties()
   {
      if (null == mWorkbookProperties)
      {
         mWorkbookProperties = new SsmlWorkbookProperties(getParentDoc());
         addSubtag(mWorkbookProperties);
      }

      return mWorkbookProperties;
   }

   //---------------------------------------------------------------------------
   public void addSheet(String inSheetName, String inRelationshipId)
   {
      XMLTag sheetTag = new XMLTag(SsmlXML.SHEET);
      sheetTag.setAttribute(SsmlXML.NAME_ATT, inSheetName);
      sheetTag.setAttribute(SsmlXML.SHEET_ID_ATT, mSheetIndex++);
      sheetTag.setAttribute(RelationshipXML.ID_ATT, inRelationshipId);
      getSheetsTag().addSubtag(sheetTag);
   }

   //---------------------------------------------------------------------------
   public void removeSheet(String inSheetName)
   {
      List sheetTags = mSheetsTag.getSubtagsByName(SsmlXML.SHEET);
      if (CollectionUtil.hasValues(sheetTags))
      {
         for (XMLTag tag : sheetTags)
         {
            if (inSheetName.equals(tag.getAttribute(SsmlXML.NAME_ATT)))
            {
               mSheetsTag.removeSubtag(tag);
               break;
            }
         }
      }
   }

   //---------------------------------------------------------------------------
   public void setDefinedName(String inName, SsmlWorksheet inWorksheet, String inValue)
   {
      removeDefinedName(inName, inWorksheet);

      XMLNode definedNameTag = new XMLTag(SsmlXML.DEFINED_NAME)
         .setAttribute(SsmlXML.NAME_ATT, inName)
         .setAttribute(SsmlXML.LOCAL_SHEET_ID_ATT, inWorksheet.getParentWorksheetPart().getSheetIndex())
         .setContent(inValue);

      getDefinedNamesTag().addSubtag(definedNameTag);
   }

   //---------------------------------------------------------------------------
   public void removeDefinedName(String inName, SsmlWorksheet inWorksheet)
   {
      List tags = getDefinedNamesTag().getSubtagsByAttribute(SsmlXML.NAME_ATT, inName);
      if (CollectionUtil.hasValues(tags))
      {
         if (inWorksheet != null)
         {
            for (XMLTag tag : tags)
            {
               if (tag.hasAttribute(SsmlXML.LOCAL_SHEET_ID_ATT)
                     && tag.getAttributeValue(SsmlXML.LOCAL_SHEET_ID_ATT).equals(inWorksheet.getParentWorksheetPart().getSheetIndex() + ""))
               {
                  removeSubtag(tag);
                  break;
               }
            }
         }
         else
         {
            for (XMLTag tag : tags)
            {
               removeSubtag(tag);
            }
         }
      }
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################

   //---------------------------------------------------------------------------
   private XMLTag getSheetsTag()
   {
      if (null == mSheetsTag)
      {
         mSheetsTag = new XMLTag(SsmlXML.SHEETS);
         addSubtag(mSheetsTag);
      }

      return mSheetsTag;
   }

   //---------------------------------------------------------------------------
   private XMLTag getDefinedNamesTag()
   {
      if (null == mDefinedNamesTag)
      {
         mDefinedNamesTag = new XMLTag(SsmlXML.DEFINED_NAMES);
         addSubtag(mDefinedNamesTag);
      }

      return mDefinedNamesTag;
   }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy