FetchNextPage(BigInteger skipCount)
{
return fetchPageDelegate(MaxNumItems, skipCount);
}
///
/// A page.
///
public class Page
{
///
/// Constructor.
///
/// list of items
/// total number of items, if known
/// a flag whether there are more items, if known
public Page(IList
items, BigInteger? totalNumItems, bool? hasMoreItems)
{
Items = items;
TotalNumItems = totalNumItems;
HasMoreItems = hasMoreItems;
}
///
/// Gets the items of the page.
///
public IList
Items { get; private set; }
///
/// Gets the total number of items, if known.
///
public BigInteger? TotalNumItems { get; private set; }
///
/// Gets whether there are more items or not, if known.
///
public bool? HasMoreItems { get; private set; }
}
}
///
/// CMIS Collection Enumerable.
///
internal class CollectionEnumerable : AbstractEnumerable
{
///
/// Constructor.
///
/// the delegate that fetches a page
public CollectionEnumerable(PageFetcher pageFetcher) :
this(0, pageFetcher) { }
///
/// Constructor.
///
/// the position
/// the delegate that fetches a page
public CollectionEnumerable(BigInteger position, PageFetcher pageFetcher) :
base(position, pageFetcher) { }
///
protected override AbstractEnumerator CreateEnumerator()
{
return new CollectionEnumerator(SkipCount, PageFetcher);
}
}
///
/// Enumerator for iterating over all items in a CMIS Collection.
///
internal class CollectionEnumerator : AbstractEnumerator
{
///
/// Constructor.
///
/// the skip count
/// the delegate that fetches a page
public CollectionEnumerator(BigInteger skipCount, PageFetcher pageFetcher) :
base(skipCount, pageFetcher) { }
///
/// Move to the next items.
///
/// true if there is a next item, false otherwise
public override bool MoveNext()
{
PageFetcher.Page page = GetCurrentPage();
if (page == null)
{
return false;
}
IList items = page.Items;
if (items == null || items.Count == 0)
{
return false;
}
if (SkipOffset == items.Count)
{
if (!HasMoreItems)
{
return false;
}
page = IncrementPage();
items = page == null ? null : page.Items;
}
if (items == null || items.Count == 0 || SkipOffset == items.Count)
{
return false;
}
current = items[IncrementSkipOffset()];
return true;
}
}
///
/// Enumerable for a CMIS Collection Page.
///
internal class CollectionPageEnumerable : AbstractEnumerable
{
///
/// Constructor.
///
/// the delegate that fetches a page
public CollectionPageEnumerable(PageFetcher pageFetcher) :
this(0, pageFetcher) { }
///
/// Constructor.
///
/// the position
/// the delegate that fetches a page
public CollectionPageEnumerable(BigInteger position, PageFetcher pageFetcher) :
base(position, pageFetcher) { }
///
protected override AbstractEnumerator CreateEnumerator()
{
return new CollectionPageEnumerator(SkipCount, PageFetcher);
}
}
///
/// Enumerator for iterating over a page of items in a CMIS Collection.
///
internal class CollectionPageEnumerator : AbstractEnumerator
{
///
/// Constructor.
///
/// the skip count
/// the delegate that fetches a page
public CollectionPageEnumerator(BigInteger skipCount, PageFetcher pageFetcher) :
base(skipCount, pageFetcher) { }
///
/// Move to the next items.
///
/// true if there is a next item, false otherwise
public override bool MoveNext()
{
PageFetcher.Page page = GetCurrentPage();
if (page == null)
{
return false;
}
IList items = page.Items;
if (items == null || items.Count == 0 || SkipOffset == items.Count)
{
return false;
}
current = items[IncrementSkipOffset()];
return true;
}
}
///
/// Content Stream helpers.
///
public class ContentStreamUtils
{
/// Octet Stream MIME type.
private const string OctetStream = "application/octet-stream";
private ContentStreamUtils()
{
}
///
/// Creates a content stream object.
///
/// the filename
/// the length
/// the MIME type
/// the stream
/// the content stream
public static IContentStream CreateContentStream(string filename, BigInteger? length, string mimetype, Stream stream)
{
return new ContentStream()
{
FileName = CheckFilename(filename),
Length = length,
MimeType = CheckMimeType(mimetype),
Stream = stream
};
}
// --- byte arrays ---
///
/// Creates a content stream object from a byte array.
///
/// the filename
/// the byte array
/// the MIME type
/// the content stream
public static IContentStream CreateByteArrayContentStream(string filename, byte[] contentBytes, string mimetype)
{
if (contentBytes == null)
{
return CreateContentStream(filename, null, mimetype, null);
}
return CreateByteArrayContentStream(filename, contentBytes, 0, contentBytes.Length, mimetype);
}
///
/// Creates a content stream object from a byte array.
///
/// the filename
/// the byte array
/// the begin of the stream in the byte array
/// the length of the stream
/// the MIME type
/// the content stream
public static IContentStream CreateByteArrayContentStream(string filename, byte[] contentBytes, int index, int count, string mimetype)
{
if (contentBytes == null)
{
return CreateContentStream(filename, null, mimetype, null);
}
if (index < 0 || index > contentBytes.Length)
{
throw new ArgumentOutOfRangeException("index");
}
else if (count < 0 || (index + count) > contentBytes.Length || (index + count) < 0)
{
throw new ArgumentOutOfRangeException("count");
}
return CreateContentStream(filename, count, mimetype, new MemoryStream(contentBytes, index, count));
}
// --- strings ---
///
/// Creates a content stream object from a string.
///
/// the filename
/// the content
/// the content stream
public static IContentStream CreateTextContentStream(string filename, string content)
{
return CreateTextContentStream(filename, content, "text/plain; charset=UTF-8");
}
///
/// Creates a content stream object from a string.
///
/// the filename
/// the content
/// the MIME type
/// the content stream
public static IContentStream CreateTextContentStream(string filename, string content, string mimetype)
{
byte[] contentBytes = Encoding.UTF8.GetBytes(content);
return CreateByteArrayContentStream(filename, contentBytes, CheckMimeType(mimetype));
}
// ---
private static string CheckFilename(string filename)
{
if (filename == null || filename.Length == 0)
{
return "content";
}
return filename;
}
private static string CheckMimeType(string mimetype)
{
if (mimetype == null)
{
return OctetStream;
}
string result = mimetype.Trim();
if (result.Length < 3)
{
return OctetStream;
}
return result;
}
}
internal class StringListBuilder
{
private string seperator;
private bool first;
public StringBuilder StringBuilder { get; private set; }
public StringListBuilder() : this(",", new StringBuilder()) { }
public StringListBuilder(StringBuilder stringBuilder) : this(",", stringBuilder) { }
public StringListBuilder(string seperator) : this(seperator, new StringBuilder()) { }
public StringListBuilder(string seperator, StringBuilder stringBuilder)
{
this.seperator = seperator;
StringBuilder = stringBuilder;
first = true;
}
public void Add(string s)
{
if (!first)
{
StringBuilder.Append(seperator);
}
else
{
first = false;
}
StringBuilder.Append(s);
}
override public string ToString()
{
return StringBuilder.ToString();
}
}
}