001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
026: import org.apache.poi.hssf.usermodel.HSSFDataFormat;
027: import org.apache.poi.hssf.usermodel.HSSFFont;
028: import org.apache.poi.hssf.usermodel.HSSFSheet;
029: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
030: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
031:
032: /**
033: * internal representation of a Workbook
034: *
035: * @author Marc Johnson (marc_johnson27591@hotmail.com)
036: * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
037: * @version CVS $Id: Workbook.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039:
040: // package scope
041: class Workbook {
042: private HSSFWorkbook _workbook;
043: private int _sheet_index;
044: private final static int REPEAT_CAPACITY = 91;
045: private Map _repeat;
046:
047: /**
048: * Constructor Workbook
049: */
050: Workbook() {
051: _workbook = new HSSFWorkbook();
052: _sheet_index = 0;
053: _repeat = new HashMap(REPEAT_CAPACITY);
054: }
055:
056: /**
057: * Method createDataFormat
058: * @return newly created DataFormat
059: */
060: HSSFDataFormat createDataFormat() {
061: return _workbook.createDataFormat();
062: }
063:
064: /**
065: * check if the format exists
066: * @param format and the value
067: * @return the format index
068: */
069: Object getValidate(String format, short value) {
070: if (_repeat.containsKey(format) == false) {
071: _repeat.put(format, new Short(value));
072: }
073: return _repeat.get(format);
074: }
075:
076: /**
077: * Method getNextName
078: * @return next name for a new sheet
079: */
080: String getNextName() {
081: return "Sheet" + _sheet_index++;
082: }
083:
084: /**
085: * Method createSheet
086: * @param name name of the sheet
087: * @return newly created sheet
088: */
089: HSSFSheet createSheet(final String name) {
090: return _workbook.createSheet(name);
091: }
092:
093: /**
094: * Method getPhysicalIndex
095: * @param name name of the sheet
096: * @return the sheet's physical index
097: */
098: int getPhysicalIndex(final String name) {
099: return _workbook.getSheetIndex(name);
100: }
101:
102: /**
103: * Method renameSheet
104: * @param index the sheet's physical index
105: * @param name the new name for the sheet
106: */
107: void renameSheet(final int index, final String name) {
108: _workbook.setSheetName(index, name,
109: HSSFWorkbook.ENCODING_UTF_16);
110: }
111:
112: /**
113: * create a cell style in the underlying HSSF model and return the
114: * reference this should match reasonably close to what is in the
115: * StyleRegion element in the gnumeric ss.
116: */
117: HSSFCellStyle createStyle() {
118: HSSFCellStyle style = _workbook.createCellStyle();
119: return style;
120: }
121:
122: /**
123: * create a font in the underlying HSSF model and return the reference
124: */
125: HSSFFont createFont() {
126: HSSFFont font = _workbook.createFont();
127: return font;
128: }
129:
130: HSSFWorkbook getWorkbook() {
131: return _workbook;
132: }
133:
134: /**
135: * write self to a filesystem
136: * @param filesystem the filesystem to be written to
137: * @exception IOException
138: */
139: void write(final POIFSFileSystem filesystem) throws IOException {
140: filesystem.createDocument(new ByteArrayInputStream(_workbook
141: .getBytes()), "Workbook");
142: }
143: } // end package scope class Workbook
|