KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > elementprocessor > impl > poi > hssf > elements > Workbook


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
25 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
26 import org.apache.poi.hssf.usermodel.HSSFFont;
27 import org.apache.poi.hssf.usermodel.HSSFSheet;
28 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
29 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
30
31 /**
32  * internal representation of a Workbook
33  *
34  * @author Marc Johnson (marc_johnson27591@hotmail.com)
35  * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
36  * @version CVS $Id: Workbook.java 30932 2004-07-29 17:35:38Z vgritsenko $
37  */

38
39 // package scope
40
class Workbook {
41     private HSSFWorkbook _workbook;
42     private int _sheet_index;
43     private final static int REPEAT_CAPACITY = 91;
44     private Map JavaDoc _repeat;
45
46     /**
47      * Constructor Workbook
48      */

49     Workbook() {
50         _workbook = new HSSFWorkbook();
51         _sheet_index = 0;
52         _repeat = new HashMap JavaDoc(REPEAT_CAPACITY);
53     }
54     /**
55      * Method createDataFormat
56      * @return newly created DataFormat
57      */

58     HSSFDataFormat createDataFormat() {
59         return _workbook.createDataFormat();
60     }
61
62     /**
63      * check if the format exists
64      * @param format and the value
65      * @return the format index
66      */

67     Object JavaDoc getValidate(String JavaDoc format, short value) {
68         if (_repeat.containsKey(format) == false) {
69             _repeat.put(format, new Short JavaDoc(value));
70         }
71         return _repeat.get(format);
72     }
73
74     /**
75      * Method getNextName
76      * @return next name for a new sheet
77      */

78     String JavaDoc getNextName() {
79         return "Sheet" + _sheet_index++;
80     }
81
82     /**
83      * Method createSheet
84      * @param name name of the sheet
85      * @return newly created sheet
86      */

87     HSSFSheet createSheet(final String JavaDoc name) {
88         return _workbook.createSheet(name);
89     }
90
91     /**
92      * Method getPhysicalIndex
93      * @param name name of the sheet
94      * @return the sheet's physical index
95      */

96     int getPhysicalIndex(final String JavaDoc name) {
97         return _workbook.getSheetIndex(name);
98     }
99
100     /**
101      * Method renameSheet
102      * @param index the sheet's physical index
103      * @param name the new name for the sheet
104      */

105     void renameSheet(final int index, final String JavaDoc name) {
106         _workbook.setSheetName(index, name, HSSFWorkbook.ENCODING_UTF_16);
107     }
108
109     /**
110      * create a cell style in the underlying HSSF model and return the
111      * reference this should match reasonably close to what is in the
112      * StyleRegion element in the gnumeric ss.
113      */

114     HSSFCellStyle createStyle() {
115         HSSFCellStyle style = _workbook.createCellStyle();
116         return style;
117     }
118
119     /**
120      * create a font in the underlying HSSF model and return the reference
121      */

122     HSSFFont createFont() {
123         HSSFFont font = _workbook.createFont();
124         return font;
125     }
126
127     HSSFWorkbook getWorkbook() {
128         return _workbook;
129     }
130
131     /**
132      * write self to a filesystem
133      * @param filesystem the filesystem to be written to
134      * @exception IOException
135      */

136     void write(final POIFSFileSystem filesystem) throws IOException JavaDoc {
137         filesystem.createDocument(new ByteArrayInputStream JavaDoc(
138                 _workbook.getBytes()), "Workbook");
139     }
140 } // end package scope class Workbook
141
Popular Tags