#region Disclaimer / License
// Copyright (C) 2009, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.IO;
using System.Xml;
using Topology.Geometries;
namespace OSGeo.MapGuide.MaestroAPI
{
///
/// Represents a set of results from a query
///
public class FeatureSetReader : IDisposable
{
private FeatureSetColumn[] m_columns;
private FeatureSetRow m_row;
private XmlTextReader m_reader;
private OSGeo.MapGuide.MgReader m_rd;
public FeatureSetReader(OSGeo.MapGuide.MgReader rd)
{
m_rd = rd;
m_columns = new FeatureSetColumn[rd.GetPropertyCount()];
for(int i = 0; i < m_columns.Length; i++)
m_columns[i] = new FeatureSetColumn(rd.GetPropertyName(i), rd.GetPropertyType(rd.GetPropertyName(i)));
}
//TODO: Make internal
public FeatureSetReader(Stream m_source)
{
m_reader = new XmlTextReader(m_source);
m_reader.WhitespaceHandling = WhitespaceHandling.Significant;
//First we extract the response layout
m_reader.Read();
if (m_reader.Name != "xml")
throw new Exception("Bad document");
m_reader.Read();
if (m_reader.Name != "FeatureSet" && m_reader.Name != "PropertySet")
throw new Exception("Bad document");
m_reader.Read();
if (m_reader.Name != "xs:schema" && m_reader.Name != "PropertyDefinitions")
throw new Exception("Bad document");
XmlDocument doc = new XmlDocument();
doc.LoadXml(m_reader.ReadOuterXml());
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
mgr.AddNamespace("gml", "http://www.opengis.net/gml");
mgr.AddNamespace("fdo", "http://fdo.osgeo.org/schemas");
//TODO: Assumes there is only one type returned... perhaps more can be returned....
XmlNodeList lst = doc.SelectNodes("xs:schema/xs:complexType/xs:complexContent/xs:extension/xs:sequence/xs:element", mgr);
if (lst.Count == 0)
lst = doc.SelectNodes("xs:schema/xs:complexType/xs:sequence/xs:element", mgr);
if (lst.Count == 0)
lst = doc.SelectNodes("PropertyDefinitions/PropertyDefinition");
m_columns = new FeatureSetColumn[lst.Count];
for(int i = 0;i= m_nulls.Length)
throw new InvalidOperationException("Index " + index.ToString() + ", was out of bounds");
else
return m_nulls[index];
}
public object this[int index]
{
get
{
if (index >= m_items.Length)
throw new InvalidOperationException("Index " + index.ToString() + ", was out of bounds");
else
{
if (m_lazyloadGeometry[index] && !m_nulls[index])
{
m_items[index] = this.Reader.Read((string)m_items[index]);
m_lazyloadGeometry[index] = false;
}
return m_items[index];
}
}
}
public int GetOrdinal(string name)
{
if (name == null)
throw new ArgumentNullException("name");
if (name == "")
throw new Exception("The name parameter must not be empty");
name = name.Trim();
for(int i = 0; i < m_parent.Columns.Length; i++)
if (m_parent.Columns[i].Name.Equals(name))
return i;
for(int i = 0; i < m_parent.Columns.Length; i++)
if (m_parent.Columns[i].Name.ToLower().Equals(name.ToLower()))
return i;
string[] t = new string[m_parent.Columns.Length];
for(int i = 0; i < m_parent.Columns.Length; i++)
t[i] = m_parent.Columns[i].Name;
throw new InvalidOperationException("Column name: " + name + ", was not found\nColumn names (" + m_parent.Columns.Length.ToString() + "): " + string.Join(", ", t));
}
public object this[string name]
{
get
{
return this[GetOrdinal(name)];
}
}
}
}