Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 422 lines (357 sloc) 9.041 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
/* ====================================================================
Copyright (C) 2004-2008 fyiReporting Software, LLC
Copyright (C) 2011 Peter Gill <peter@majorsilence.com>

This file is part of the fyiReporting RDL project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


For additional information, email info@fyireporting.com or visit
the website www.fyiReporting.com.
*/
using System;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

namespace fyiReporting.Data
{
/// <summary>
/// Summary description for XmlDataReader.
/// </summary>
public class XmlDataReader : IDataReader
{
XmlConnection _xconn;
XmlCommand _xcmd;
System.Data.CommandBehavior _behavior;
// xpath
XPathDocument _xpd; // the document
XPathNavigator _xpn; // the navigator
XPathNodeIterator _xpni; // the main iterator
XmlNamespaceManager _nsmgr; // name space manager
ListDictionary _NameSpaces; // names spaces used in xml document

// column information
object[] _Data; // data values of the columns
ArrayList _Names; // names of the columns
ArrayList _Types; // types of the columns

public XmlDataReader(System.Data.CommandBehavior behavior, XmlConnection conn, XmlCommand cmd)
{
_xconn = conn;
_xcmd = cmd;
_behavior = behavior;

// create an iterator to the selected rows
_xpd = new XPathDocument (_xcmd.Url);
_xpn = _xpd.CreateNavigator();
_xpni = _xpn.Select(_xcmd.RowsXPath); // select the rows
_NameSpaces = new ListDictionary();
_Names = new ArrayList();
_Types = new ArrayList();

// Now determine the actual structure of the row depending on the command
if (_xcmd.ColumnsXPath != null)
ColumnsSpecifiedInit(); // xpaths to all columns specified
else
{
_xcmd.ColumnsXPath = new ArrayList(); // xpath of all columns will simply be the name

switch (_xcmd.Type)
{
case "both":
ColumnsAttributes();
ColumnsElements();
break;
case "attributes":
ColumnsAttributes();
break;
case "elements":
ColumnsElements();
break;
}
}
_Data = new object[_Names.Count]; // allocate enough room for data

if (_NameSpaces.Count > 0)
{
_nsmgr = new XmlNamespaceManager(new NameTable());
foreach (string nsprefix in _NameSpaces.Keys)
{
_nsmgr.AddNamespace(nsprefix, _NameSpaces[nsprefix] as string); // setup namespaces
}
}
else
_nsmgr = null;
}

void ColumnsAttributes()
{
//go to the first row to get info
XPathNodeIterator temp_xpni = _xpni.Clone(); // temporary iterator for determining columns
temp_xpni.MoveNext();

XPathNodeIterator ni = temp_xpni.Current.Select("@*"); // select for attributes
while (ni.MoveNext())
{
_Types.Add(ni.Current.Value.GetType());
AddName(ni.Current.Name);
_xcmd.ColumnsXPath.Add("@" + ni.Current.Name);
}
}

void ColumnsElements()
{
//go to the first row to get info
XPathNodeIterator temp_xpni = _xpni.Clone(); // temporary iterator for determining columns
temp_xpni.MoveNext();

XPathNodeIterator ni = temp_xpni.Current.Select("*");
while (ni.MoveNext())
{
_Types.Add(ni.Current.Value.GetType());
AddName(ni.Current.Name);
_xcmd.ColumnsXPath.Add(ni.Current.Name);
if (ni.Current.NamespaceURI != String.Empty &&
ni.Current.Prefix != String.Empty &&
_NameSpaces[ni.Current.Prefix] == null)
{
_NameSpaces.Add(ni.Current.Prefix, ni.Current.NamespaceURI);
}
}
}

void ColumnsSpecifiedInit()
{
XPathNodeIterator temp_xpni = _xpni.Clone(); // temporary iterator for determining columns

temp_xpni.MoveNext ();
foreach (string colxpath in _xcmd.ColumnsXPath )
{
XPathNodeIterator ni = temp_xpni.Current.Select(colxpath);
ni.MoveNext ();
if (ni.Count < 1)
{ // didn't get anything in the first row; this is ok because
// the element might not exist in the first row.
_Types.Add("".GetType()); // assume string
AddName(colxpath); // just use the name of the path
}
else
{
_Types.Add(ni.Current.Value.GetType());
AddName(ni.Current.Name);
}
}
}

// adds name to array; ensure name is unique in the table
void AddName(string name)
{
int ci=0;
string wname = name;
while (_Names.IndexOf(wname) >= 0)
{
wname = name + "_" + (++ci).ToString();
}
_Names.Add(wname);
}

#region IDataReader Members

public int RecordsAffected
{
get
{
return 0;
}
}

public bool IsClosed
{
get
{
return _xconn.IsOpen;
}
}

public bool NextResult()
{
return false;
}

public void Close()
{
_xpd = null;
_xpn = null;
_xpni = null;

_Data = null;
_Names = null;
_Types = null;
}

public bool Read()
{
if (!_xpni.MoveNext())
return false;

// obtain the data from each column
int ci=0;
foreach (string colxpath in _xcmd.ColumnsXPath )
{
XPathNodeIterator ni;
if (_nsmgr == null)
ni = _xpni.Current.Select(colxpath);
else
{
XPathExpression xp = _xpni.Current.Compile(colxpath);
xp.SetContext(_nsmgr);
ni = _xpni.Current.Select(xp);
}
ni.MoveNext ();
_Data[ci++] = ni.Count == 0? null: ni.Current.Value;
}

return true;
}

public int Depth
{
get
{
// TODO: Add XmlDataReader.Depth getter implementation
return 0;
}
}

public DataTable GetSchemaTable()
{
// TODO: Add XmlDataReader.GetSchemaTable implementation
return null;
}

#endregion

#region IDisposable Members

public void Dispose()
{
this.Close();
}

#endregion

#region IDataRecord Members

public int GetInt32(int i)
{
return Convert.ToInt32(_Data[i]);
}

public object this[string name]
{
get
{
int ci = this.GetOrdinal(name);
return _Data[ci];
}
}

object System.Data.IDataRecord.this[int i]
{
get
{
return _Data[i];
}
}

public object GetValue(int i)
{
return _Data[i];
}

public bool IsDBNull(int i)
{
return _Data[i] == null;
}

public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException("GetBytes not implemented.");
}

public byte GetByte(int i)
{
return Convert.ToByte(_Data[i]);
}

public Type GetFieldType(int i)
{
return this._Types[i] as Type;
}

public decimal GetDecimal(int i)
{
return Convert.ToDecimal(_Data[i]);
}

public int GetValues(object[] values)
{
int i;
for (i=0; i < values.Length; i++)
{
values[i] = i >= _Data.Length? System.DBNull.Value: _Data[i];
}

return Math.Min(values.Length, _Data.Length);
}

public string GetName(int i)
{
return _Names[i] as string;
}

public int FieldCount
{
get
{
return _Data.Length;
}
}

public long GetInt64(int i)
{
return Convert.ToInt64(_Data[i]);
}

public double GetDouble(int i)
{
return Convert.ToDouble(_Data[i]);
}

public bool GetBoolean(int i)
{
return Convert.ToBoolean(_Data[i]);
}

public Guid GetGuid(int i)
{
throw new NotImplementedException("GetGuid not implemented.");
}

public DateTime GetDateTime(int i)
{
return Convert.ToDateTime(_Data[i]);
}

public int GetOrdinal(string name)
{
int ci=0;
// do case sensitive lookup
foreach (string cname in _Names)
{
if (cname == name)
return ci;
ci++;
}

// do case insensitive lookup
ci=0;
name = name.ToLower();
foreach (string cname in _Names)
{
if (cname.ToLower() == name)
return ci;
ci++;
}

throw new ArgumentException(string.Format("Column '{0}' not known.", name));
}

public string GetDataTypeName(int i)
{
Type t = _Types[i] as Type;
return t.ToString();
}

public float GetFloat(int i)
{
return Convert.ToSingle(_Data[i]);
}

public IDataReader GetData(int i)
{
throw new NotImplementedException("GetData not implemented.");
}

public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException("GetChars not implemented.");
}

public string GetString(int i)
{
return Convert.ToString(_Data[i]);
}

public char GetChar(int i)
{
return Convert.ToChar(_Data[i]);
}

public short GetInt16(int i)
{
return Convert.ToInt16(_Data[i]);
}

#endregion
}
}
Something went wrong with that request. Please try again.