001package com.hfg.xml.msoffice2003.spreadsheetml;
002
003import java.util.regex.Pattern;
004
005import com.hfg.util.StringUtil;
006import com.hfg.xml.XMLTag;
007
008//------------------------------------------------------------------------------
009/**
010 MSOffice 2003 Excel worksheet.
011
012 @author J. Alex Taylor, hairyfatguy.com
013 */
014//------------------------------------------------------------------------------
015// com.hfg Library
016//
017// This library is free software; you can redistribute it and/or
018// modify it under the terms of the GNU Lesser General Public
019// License as published by the Free Software Foundation; either
020// version 2.1 of the License, or (at your option) any later version.
021//
022// This library is distributed in the hope that it will be useful,
023// but WITHOUT ANY WARRANTY; without even the implied warranty of
024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
025// Lesser General Public License for more details.
026//
027// You should have received a copy of the GNU Lesser General Public
028// License along with this library; if not, write to the Free Software
029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030//
031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
032// jataylor@hairyfatguy.com
033//------------------------------------------------------------------------------
034// TODO:
035//   - Make sure the worksheet options are the last subtag.
036
037public class ExcelWorksheet extends XMLTag
038{
039
040   public static enum Pane
041   {
042      LOWER_RIGHT(0),
043      UPPER_RIGHT(1),
044      LOWER_LEFT(2),
045      UPPER_LEFT(3);
046
047      private int mValue;
048
049      //------------------------------------------------------------------------
050      private Pane(int inValue)
051      {
052         mValue = inValue;
053      }
054
055      //------------------------------------------------------------------------
056      public int value()
057      {
058         return mValue;
059      }
060   }
061
062   private String mDefaultName = "Sheet" + sID++;
063
064   private static int sID = 1;
065
066   //##########################################################################
067   // CONSTRUCTORS
068   //##########################################################################
069
070   //---------------------------------------------------------------------------
071   public ExcelWorksheet()
072   {
073      super(SpreadsheetML.WORKSHEET);
074      setName(mDefaultName);
075   }
076
077   //---------------------------------------------------------------------------
078   public ExcelWorksheet(String inName)
079   {
080      this();
081      setName(inName);
082   }
083
084   //##########################################################################
085   // PUBLIC METHODS
086   //##########################################################################
087
088   //---------------------------------------------------------------------------
089   public ExcelWorksheet setDefaultColumnWidth(int inValue)
090   {
091      setAttribute(SpreadsheetML.DEFAULT_COLUMN_WIDTH_ATT, inValue);
092      return this;
093   }
094
095   //---------------------------------------------------------------------------
096   public ExcelWorksheet setDefaultRowHeight(int inValue)
097   {
098      setAttribute(SpreadsheetML.DEFAULT_ROW_HEIGHT_ATT, inValue);
099      return this;
100   }
101
102   //---------------------------------------------------------------------------
103   public ExcelWorksheet setName(String inName)
104   {
105      String name = "";
106      if (StringUtil.isSet(inName))
107      {
108         name = inName.trim();
109         if (name.length() > SpreadsheetML.WORKSHEET_NAME_MAX_LENGTH)
110         {
111             throw new ExcelFormatException("The worksheet name " + StringUtil.singleQuote(name)
112                                            + " exceeds the maximum sheet name length of " + SpreadsheetML.WORKSHEET_NAME_MAX_LENGTH + "!");
113         }
114
115         name = purifyWorksheetName(name);
116      }
117
118
119      setAttribute(SpreadsheetML.NAME_ATT, name);
120      return this;
121   }
122
123   //---------------------------------------------------------------------------
124   public String getName()
125   {
126      return getAttributeValue(SpreadsheetML.NAME_ATT.getLocalName());
127   }
128
129   //---------------------------------------------------------------------------
130   /**
131    Specifies whether the user can make changes to this Worksheet.
132    */
133   public ExcelWorksheet setProtected(boolean inName)
134   {
135      setAttribute(SpreadsheetML.PROTECTED_ATT, inName);
136      return this;
137   }
138
139   //---------------------------------------------------------------------------
140   public ExcelTable addTable()
141   {
142      ExcelTable table = new ExcelTable();
143      addSubtag(table);
144
145      return table;
146   }
147
148   //---------------------------------------------------------------------------
149   public ExcelWorksheetOptions addWorksheetOptions()
150   {
151      ExcelWorksheetOptions options = new ExcelWorksheetOptions();
152      addSubtag(options);
153
154      return options;
155   }
156
157   //##########################################################################
158   // PRIVATE METHODS
159   //##########################################################################
160
161   private static final Pattern WORKSHEET_NAME_PURIFICATION_PATTERN = Pattern.compile("[\u0000\u0003\\:\\\\\\*\\?\\/\\[\\]]");
162
163   //---------------------------------------------------------------------------
164   /**
165    The name of the Excel worksheet must be unique in the workbook and cannot
166    begin or end with a single quote (') or contain any of the following characters:
167
168    0x0000
169    0x0003
170    colon (:)
171    backslash (\)
172    asterisk (*)
173    question mark (?)
174    forward slash (/)
175    opening square bracket ([)
176    closing square bracket (])
177
178    */
179   private String purifyWorksheetName(String inName)
180   {
181      return WORKSHEET_NAME_PURIFICATION_PATTERN.matcher(inName).replaceAll("_");
182   }
183
184}