Source Code Cross Referenced for ExcelPropertyTable.java in  » Testing » webtest » com » canoo » ant » table » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » webtest » com.canoo.ant.table 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.canoo.ant.table;
002:
003:        import java.io.File;
004:        import java.io.FileInputStream;
005:        import java.io.FileNotFoundException;
006:        import java.io.IOException;
007:        import java.util.ArrayList;
008:        import java.util.LinkedList;
009:        import java.util.List;
010:        import java.util.Properties;
011:
012:        import org.apache.log4j.Logger;
013:        import org.apache.poi.hssf.usermodel.HSSFCell;
014:        import org.apache.poi.hssf.usermodel.HSSFRow;
015:        import org.apache.poi.hssf.usermodel.HSSFSheet;
016:        import org.apache.poi.hssf.usermodel.HSSFWorkbook;
017:        import org.apache.poi.poifs.filesystem.POIFSFileSystem;
018:
019:        public class ExcelPropertyTable extends APropertyTable {
020:
021:            private static final Logger LOG = Logger
022:                    .getLogger(ExcelPropertyTable.class);
023:
024:            public ExcelPropertyTable() {
025:            }
026:
027:            protected boolean hasJoinTable() {
028:                final Object sheet;
029:                try {
030:                    sheet = getWorkbook().getSheet(KEY_JOIN);
031:                } catch (final IOException e) {
032:                    throw new RuntimeException("Failed to read container: >"
033:                            + getContainer() + "<", e);
034:                }
035:                return sheet != null;
036:            }
037:
038:            private HSSFWorkbook getWorkbook() throws IOException {
039:                final File file = getContainer();
040:                if (!file.exists()) {
041:                    throw new FileNotFoundException("File not found >"
042:                            + file.getAbsolutePath() + "< " + getContainer());
043:                } else if (!file.isFile() || !file.canRead()) {
044:                    throw new IllegalArgumentException(
045:                            "No a regular readable file: >"
046:                                    + file.getAbsolutePath() + "<");
047:                }
048:                final POIFSFileSystem excelFile = new POIFSFileSystem(
049:                        new FileInputStream(file));
050:                return new HSSFWorkbook(excelFile);
051:            }
052:
053:            protected List read(final String sheetName) throws IOException {
054:                final HSSFWorkbook workbook = getWorkbook();
055:                final HSSFSheet sheet;
056:                if (sheetName == null) {
057:                    sheet = workbook.getSheetAt(0); // no name specified, take the first sheet
058:                } else {
059:                    sheet = workbook.getSheet(sheetName);
060:                }
061:                if (null == sheet) {
062:                    String msg = "No sheet \"" + sheetName
063:                            + "\" found in file " + getContainer()
064:                            + ". Available sheets: ";
065:                    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
066:                        if (i != 0)
067:                            msg += ", ";
068:                        msg += workbook.getSheetName(i);
069:                    }
070:                    throw new IllegalArgumentException(msg);
071:                }
072:
073:                final int lastRowNum = sheet.getLastRowNum();
074:                final List header = new ArrayList();
075:                final HSSFRow headerRow = sheet.getRow(0);
076:                for (short i = 0; i < headerRow.getLastCellNum(); i++) {
077:                    final HSSFCell cell = headerRow.getCell(i);
078:                    if (cell != null)
079:                        header.add(stringValueOf(cell));
080:                    else
081:                        header.add(null);
082:                }
083:                final List result = new LinkedList();
084:                for (int rowNo = 1; rowNo <= lastRowNum; rowNo++) { // last Row is included
085:                    final HSSFRow row = sheet.getRow(rowNo);
086:                    if (row != null) // surprising, but row can be null
087:                    {
088:                        final Properties props = new Properties();
089:                        for (short i = 0; i < header.size(); i++) {
090:                            final String headerName = (String) header.get(i);
091:                            if (headerName != null) // handle empty cols
092:                            {
093:                                final HSSFCell cell = row.getCell(i);
094:                                final String value = stringValueOf(cell);
095:                                putValue(value, headerName, props);
096:                            }
097:                        }
098:                        result.add(props);
099:                    }
100:                }
101:
102:                return result;
103:            }
104:
105:            protected void putValue(String value, Object key, Properties props) {
106:                if (!EMPTY.equals(value)) { // do not add empty values to allow proper default handling
107:                    props.put(key, value);
108:                }
109:            }
110:
111:            private String stringValueOf(final HSSFCell cell) {
112:                if (null == cell) {
113:                    return EMPTY;
114:                }
115:                switch (cell.getCellType()) {
116:                case (HSSFCell.CELL_TYPE_STRING):
117:                    return cell.getRichStringCellValue().getString();
118:                case (HSSFCell.CELL_TYPE_NUMERIC):
119:                    return "" + cell.getNumericCellValue();
120:                case (HSSFCell.CELL_TYPE_BLANK):
121:                    return "";
122:                case (HSSFCell.CELL_TYPE_BOOLEAN):
123:                    return "" + cell.getBooleanCellValue();
124:                default:
125:                    LOG.warn("Cell Type not supported: " + cell.getCellType());
126:                    return EMPTY;
127:                }
128:            }
129:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.