KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > database > excel > xlFolderXls


1 /*(Header: NiLOSTEP / xlSQL)
2
3  Copyright (C) 2004 NiLOSTEP
4    NiLOSTEP Information Sciences
5    http://nilostep.com
6    nilo.de.roock@nilostep.com
7
8  This program is free software; you can redistribute it and/or modify it under
9  the terms of the GNU General Public License as published by the Free Software
10  Foundation; either version 2 of the License, or (at your option) any later
11  version.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  more details. You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */

20 package com.nilostep.xlsql.database.excel;
21
22 import com.nilostep.xlsql.database.*;
23
24 import java.io.*;
25
26 import jxl.*;
27
28 import jxl.write.*;
29
30
31 /**
32  * Extends xlFolder for Excel
33  *
34  * @author Jim Caprioli
35  */

36 public class xlFolderXls extends xlFolder implements FilenameFilter {
37     private static final String JavaDoc XLS = ".xls";
38
39     /**
40      * Creates a new xlFolderXls object.
41      *
42      * @param dir relative root dir of workbooks
43      *
44      * @throws xlException when this object cannot be instantiated
45      */

46     public xlFolderXls(File dir) throws xlException {
47         super(dir);
48     }
49
50     protected void readSubFolders(File dir) throws xlException {
51         directory = dir;
52
53         File[] f = dir.listFiles(this);
54
55         if (f == null) {
56             throw new xlException("xlSQL: invalid directory");
57         }
58
59         for (int i = 0; i < f.length; i++) {
60             try {
61                 Workbook workbook = Workbook.getWorkbook(f[i]);
62                 String JavaDoc name = f[i].getName()
63                                   .substring(0, f[i].getName().lastIndexOf('.'));
64                 xlSubFolder obj = new xlWorkbook(directory, name);
65                 subfolders.put(name.toUpperCase(), obj);
66             } catch (jxl.read.biff.BiffException jxle) {
67                 logger.info(f[i] + " contains no xls data");
68
69                 continue; // to next in for loop
70
} catch (IOException ioe) {
71                 logger.warning("io error while reading " + f[i]);
72
73                 continue; // to next in for loop
74
}
75         }
76     }
77
78     /**
79      * Create a schema in the Excel context: workbook
80      *
81      * @param workbook name
82      */

83     public void addSchema(String JavaDoc workbook) {
84         // key in Uppercase, don't touch case for name
85
String JavaDoc workbookU = workbook.toUpperCase();
86
87         if (subfolders.containsKey(workbookU)) {
88             xlSubFolder wb = (xlSubFolder) subfolders.get(workbookU);
89             wb.setDirty(UPDATE, true);
90         } else {
91             xlSubFolder obj = new xlWorkbook(directory, workbook, true);
92             subfolders.put(workbookU, obj);
93         }
94     }
95
96     /**
97      * Create a table in the Excel context: sheet in workbook
98      *
99      * @param workbook workbook name
100      * @param sheet sheet name
101      *
102      * @throws IllegalArgumentException when workbook does not exist
103      */

104     public void addTable(String JavaDoc workbook, String JavaDoc sheet) {
105         // key in Uppercase, don't touch case for name
106
String JavaDoc workbookU = workbook.toUpperCase();
107         String JavaDoc sheetU = sheet.toUpperCase();
108
109         if (subfolders.containsKey(workbookU)) {
110             xlSubFolder wb = (xlSubFolder) subfolders.get(workbookU);
111
112             if (wb.getFiles().containsKey(sheetU)) {
113                 xlFile sh = (xlFile) wb.getFiles().get(sheetU);
114                 sh.setIsChanged(UPDATE, true);
115             } else {
116                 xlFile obj = new xlSheet(directory, wb.getSubFolderName(),
117                                          sheet, true);
118                 wb.getFiles().put(sheetU, obj);
119                 wb.getValidFiles().put(sheetU, obj);
120             }
121         } else {
122             throw new IllegalArgumentException JavaDoc(NOARGS);
123         }
124     }
125
126     /**
127      * For type XLS: accept documents where extension is .xls (
128      * https://xlsql.dev.java.net/servlets/ProjectIssues issue 33 )
129      *
130      * @param dir DOCUMENT ME!
131      * @param name DOCUMENT ME!
132      *
133      * @return DOCUMENT ME!
134      */

135     public boolean accept(File dir, String JavaDoc name) {
136         String JavaDoc ext = "";
137
138         if (name.length() > 3) {
139             ext = name.substring(name.length() - 4, name.length());
140         }
141
142         return (ext.equalsIgnoreCase(XLS));
143     }
144 }
Popular Tags