#region Disclaimer / License
// Copyright (C) 2013, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// Original code from SharpDevelop 3.2.1 licensed under the same terms (LGPL 2.1)
// Copyright 2002-2010 by
//
// AlphaSierraPapa, Christoph Wille
// Vordernberger Strasse 27/8
// A-8700 Leoben
// Austria
//
// email: office@alphasierrapapa.com
// court of jurisdiction: Landesgericht Leoben
//
// 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 Disclaimer / License
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using System;
using System.Collections.Generic;
using System.Text;
namespace Maestro.Editors.Generic.XmlEditor.AutoCompletion
{
///
/// A collection that stores objects.
///
[Serializable]
internal class XmlSchemaCompletionDataCollection : System.Collections.CollectionBase
{
///
/// Initializes a new instance of .
///
public XmlSchemaCompletionDataCollection()
{
}
///
/// Initializes a new instance of based on another .
///
///
/// A from which the contents are copied
///
public XmlSchemaCompletionDataCollection(XmlSchemaCompletionDataCollection val)
{
this.AddRange(val);
}
///
/// Initializes a new instance of containing any array of objects.
///
///
/// A array of objects with which to intialize the collection
///
public XmlSchemaCompletionDataCollection(XmlSchemaCompletionData[] val)
{
this.AddRange(val);
}
///
/// Represents the entry at the specified index of the .
///
/// The zero-based index of the entry to locate in the collection.
/// The entry at the specified index of the collection.
/// is outside the valid range of indexes for the collection.
public XmlSchemaCompletionData this[int index]
{
get
{
return ((XmlSchemaCompletionData)(List[index]));
}
set
{
List[index] = value;
}
}
public ICompletionData[] GetNamespaceCompletionData()
{
List completionItems = new List();
foreach (XmlSchemaCompletionData schema in this)
{
XmlCompletionData completionData = new XmlCompletionData(schema.NamespaceUri, XmlCompletionData.DataType.NamespaceUri);
completionItems.Add(completionData);
}
return completionItems.ToArray();
}
///
/// Represents the entry with the specified namespace URI.
///
/// The schema's namespace URI.
/// The entry with the specified namespace URI.
public XmlSchemaCompletionData this[string namespaceUri]
{
get
{
return GetItem(namespaceUri);
}
}
///
/// Adds a with the specified value to the
/// .
///
/// The to add.
/// The index at which the new element was inserted.
public int Add(XmlSchemaCompletionData val)
{
return List.Add(val);
}
///
/// Copies the elements of an array to the end of the .
///
///
/// An array of type containing the objects to add to the collection.
///
///
public void AddRange(XmlSchemaCompletionData[] val)
{
for (int i = 0; i < val.Length; i++)
{
this.Add(val[i]);
}
}
///
/// Adds the contents of another to the end of the collection.
///
///
/// A containing the objects to add to the collection.
///
///
public void AddRange(XmlSchemaCompletionDataCollection val)
{
for (int i = 0; i < val.Count; i++)
{
this.Add(val[i]);
}
}
///
/// Gets a value indicating whether the
/// contains the specified .
///
/// The to locate.
///
/// if the is contained in the collection;
/// otherwise, .
///
///
public bool Contains(XmlSchemaCompletionData val)
{
return List.Contains(val);
}
///
/// Copies the values to a one-dimensional instance at the
/// specified index.
///
/// The one-dimensional that is the destination of the values copied from .
/// The index in where copying begins.
///
/// is multidimensional.
/// -or-
/// The number of elements in the is greater than
/// the available space between and the end of
/// .
///
/// is .
/// is less than 's lowbound.
///
public void CopyTo(XmlSchemaCompletionData[] array, int index)
{
List.CopyTo(array, index);
}
///
/// Returns the index of a in
/// the .
///
/// The to locate.
///
/// The index of the of in the
/// , if found; otherwise, -1.
///
///
public int IndexOf(XmlSchemaCompletionData val)
{
return List.IndexOf(val);
}
///
/// Inserts a into the at the specified index.
///
/// The zero-based index where should be inserted.
/// The to insert.
///
public void Insert(int index, XmlSchemaCompletionData val)
{
List.Insert(index, val);
}
///
/// Returns an enumerator that can iterate through the .
///
public new XmlSchemaCompletionDataEnumerator GetEnumerator()
{
return new XmlSchemaCompletionDataEnumerator(this);
}
///
/// Removes a specific from the .
///
/// The to remove from the .
/// is not found in the Collection.
public void Remove(XmlSchemaCompletionData val)
{
List.Remove(val);
}
///
/// Gets the schema completion data with the same filename.
///
/// if no matching schema found.
public XmlSchemaCompletionData GetSchemaFromFileName(string fileName)
{
foreach (XmlSchemaCompletionData schema in this)
{
if (IsEqualFileName(schema.FileName, fileName))
{
return schema;
}
}
return null;
}
private static string NormalizePath(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return fileName;
int i;
bool isWeb = false;
for (i = 0; i < fileName.Length; i++)
{
if (fileName[i] == '/' || fileName[i] == '\\')
break;
if (fileName[i] == ':')
{
if (i > 1)
isWeb = true;
break;
}
}
char outputSeparator = isWeb ? '/' : System.IO.Path.DirectorySeparatorChar;
StringBuilder result = new StringBuilder();
if (isWeb == false && fileName.StartsWith(@"\\") || fileName.StartsWith("//"))
{
i = 2;
result.Append(outputSeparator);
}
else
{
i = 0;
}
int segmentStartPos = i;
for (; i <= fileName.Length; i++)
{
if (i == fileName.Length || fileName[i] == '/' || fileName[i] == '\\')
{
int segmentLength = i - segmentStartPos;
switch (segmentLength)
{
case 0:
// ignore empty segment (if not in web mode)
// On unix, don't ignore empty segment if i==0
if (isWeb || (i == 0 && Environment.OSVersion.Platform == PlatformID.Unix))
{
result.Append(outputSeparator);
}
break;
case 1:
// ignore /./ segment, but append other one-letter segments
if (fileName[segmentStartPos] != '.')
{
if (result.Length > 0) result.Append(outputSeparator);
result.Append(fileName[segmentStartPos]);
}
break;
case 2:
if (fileName[segmentStartPos] == '.' && fileName[segmentStartPos + 1] == '.')
{
// remove previous segment
int j;
for (j = result.Length - 1; j >= 0 && result[j] != outputSeparator; j--)
{
}
if (j > 0)
{
result.Length = j;
}
break;
}
else
{
// append normal segment
goto default;
}
default:
if (result.Length > 0) result.Append(outputSeparator);
result.Append(fileName, segmentStartPos, segmentLength);
break;
}
segmentStartPos = i + 1; // remember start position for next segment
}
}
if (isWeb == false)
{
if (result.Length > 0 && result[result.Length - 1] == outputSeparator)
{
result.Length -= 1;
}
if (result.Length == 2 && result[1] == ':')
{
result.Append(outputSeparator);
}
}
return result.ToString();
}
private bool IsEqualFileName(string fileName1, string fileName2)
{
return string.Equals(NormalizePath(fileName1),
NormalizePath(fileName2),
StringComparison.OrdinalIgnoreCase);
}
///
/// Enumerator that can iterate through a XmlSchemaCompletionDataCollection.
///
///
///
public class XmlSchemaCompletionDataEnumerator : System.Collections.IEnumerator
{
private readonly System.Collections.IEnumerator baseEnumerator;
private System.Collections.IEnumerable temp;
///
/// Initializes a new instance of .
///
public XmlSchemaCompletionDataEnumerator(XmlSchemaCompletionDataCollection mappings)
{
this.temp = ((System.Collections.IEnumerable)(mappings));
this.baseEnumerator = temp.GetEnumerator();
}
///
/// Gets the current in the .
///
public XmlSchemaCompletionData Current
{
get
{
return ((XmlSchemaCompletionData)(baseEnumerator.Current));
}
}
object System.Collections.IEnumerator.Current
{
get
{
return baseEnumerator.Current;
}
}
///
/// Advances the enumerator to the next of the .
///
public bool MoveNext()
{
return baseEnumerator.MoveNext();
}
///
/// Sets the enumerator to its initial position, which is before the first element in the .
///
public void Reset()
{
baseEnumerator.Reset();
}
}
private XmlSchemaCompletionData GetItem(string namespaceUri)
{
XmlSchemaCompletionData matchedItem = null;
foreach (XmlSchemaCompletionData item in this)
{
if (item.NamespaceUri == namespaceUri)
{
matchedItem = item;
break;
}
}
return matchedItem;
}
}
}