Clover.NET coverage report - Coverage for s2container.net

Coverage timestamp: 2006年5月30日 11:21:29

File Stats: LOC: 175   Methods: 6
NCLOC: 131 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Extension.DataSets\Impl\XlsReader.cs 84.6% 96.4% 100.0% 93.1%
coverage coverage
1   #region Copyright
2   /*
3   * Copyright 2005 the Seasar Foundation and the Others.
4   *
5   * Licensed under the Apache License, Version 2.0 (the "License");
6   * you may not use this file except in compliance with the License.
7   * You may obtain a copy of the License at
8   *
9   * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14   * either express or implied. See the License for the specific language
15   * governing permissions and limitations under the License.
16   */
17   #endregion
18  
19   using System.Data;
20   using System.Data.Common;
21   using System.Data.OleDb;
22   using System.IO;
23   using System.Text.RegularExpressions;
24  
25   namespace Seasar.Extension.DataSets.Impl
26   {
27   public class XlsReader : IDataReader
28   {
29   private const int DEFAULT_BUFFER_SIZE = 1024 * 4;
30  
31   private Regex illigalColumnNamePattern = new Regex("F[0-9]+$", RegexOptions.Compiled);
32  
33   private Regex workSheetPrefixPattern = new Regex(@"^#[0-9]+\s+.+$", RegexOptions.Compiled);
34  
35   private DataSet dataSet_;
36  
37 3 public XlsReader(string path)
38   {
39 3 string fullPath = Path.GetFullPath(path);
40 3 if (!File.Exists(fullPath))
41   {
42 0 throw new FileNotFoundException("xls file not found.", fullPath);
43   }
44  
45 3 dataSet_ = CreateDataSet(fullPath);
46   }
47  
48 9 public XlsReader(Stream stream)
49   {
50 9 string tempPath = Path.GetTempFileName();
51 9 using (Stream fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
52   {
53 9 byte[] b = new byte[DEFAULT_BUFFER_SIZE];
54 9 int n = 0;
55 53 while (0 < (n = stream.Read(b, 0, b.Length)))
56   {
57 44 fs.Write(b, 0, n);
58   }
59   }
60  
61 9 dataSet_ = CreateDataSet(tempPath);
62  
63 9 if (File.Exists(tempPath))
64   {
65 9 File.Delete(tempPath);
66   }
67   }
68  
69   #region IDataReader メンバ
70  
71 12 public DataSet Read()
72   {
73 12 return dataSet_;
74   }
75  
76   #endregion
77  
78 12 private DataSet CreateDataSet(string path)
79   {
80 12 string connectonString = string.Format(
81   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"",
82   path
83   );
84  
85 12 DataSet ds = new DataSet();
86 12 using (OleDbConnection con = new OleDbConnection(connectonString))
87   {
88 12 if (con.State != ConnectionState.Open)
89   {
90 12 con.Open();
91   }
92  
93 12 DataTable tableList = con.GetOleDbSchemaTable(
94   OleDbSchemaGuid.Tables_Info,
95   new object[] {null, null, null, "TABLE"}
96   );
97  
98 12 foreach (DataRow row in tableList.Rows)
99   {
100 41 string tableName = (string) row["TABLE_NAME"];
101 41 if (!tableName.EndsWith("$") && !tableName.EndsWith("$'"))
102   {
103 2 continue;
104   }
105  
106 39 string sql = string.Format("select * from [{0}]", tableName);
107 39 using (OleDbCommand cmd = new OleDbCommand(sql, con))
108   {
109 39 DbDataAdapter adapter = new OleDbDataAdapter(cmd);
110 39 adapter.AcceptChangesDuringFill = false;
111 39 DataTable table = new DataTable(tableName);
112 39 adapter.Fill(table);
113 39 SetupTable(table);
114 39 ds.Tables.Add(table);
115   //adapter.Fill(ds, tableName.Replace("$", string.Empty));
116   }
117   }
118   }
119  
120 12 return ds;
121   }
122  
123 39 private void SetupTable(DataTable table)
124   {
125 39 string tableName = table.TableName.Trim();
126 39 if (tableName.EndsWith("$"))
127   {
128 31 tableName = tableName.Remove(tableName.Length - 1, 1);
129   }
130 39 if (tableName.EndsWith("$'"))
131   {
132 8 tableName = tableName.Substring(1, tableName.Length - 3);
133   }
134 39 if (workSheetPrefixPattern.IsMatch(tableName))
135   {
136 8 tableName = tableName.Split(new char[] {' ',' '})[1];
137   }
138 39 table.TableName = tableName;
139  
140 39 int rowCount = table.Rows.Count;
141 39 if (rowCount > 0)
142   {
143 31 SetupColumns(table, table.Columns);
144   }
145   }
146  
147 31 private void SetupColumns(DataTable table, DataColumnCollection columns)
148   {
149 180 for (int i = 0; i < columns.Count; i++)
150   {
151 149 DataColumn column = columns[i];
152 149 string columnName = column.ColumnName.Trim();
153  
154 149 bool isRemove = false;
155  
156 149 if (string.Empty == columnName)
157   {
158 0 isRemove = true;
159   }
160  
161 149 if (illigalColumnNamePattern.IsMatch(columnName))
162   {
163 32 isRemove = true;
164   }
165  
166 149 if (isRemove)
167   {
168 32 columns.Remove(column);
169 32 i--;
170   }
171   }
172   }
173   }
174   }
175