1
/* Copyright (c) 2006-2008 Google Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
* Oct 13 2008 Joe Feser joseph.feser@gmail.com
17
* Converted ArrayLists and other .NET 1.1 collections to use Generics
18
* Combined IExtensionElement and IExtensionElementFactory interfaces
24
using System.Collections;
27
using Google.GData.Client;
29
namespace Google.GData.Extensions {
32
/// GData schema extension describing a nested entry link.
34
public class EntryLink : IExtensionElementFactory
37
/// <summary>holds the href property of the EntryLink element</summary>
39
/// <summary>holds the readOnlySet property of the EntryLink element</summary>
40
private bool readOnly;
41
/// <summary>holds the AtomEntry property of the EntryLink element</summary>
42
private AtomEntry entry;
43
/// <summary>holds the rel attribute of the EntyrLink element</summary>
46
private bool readOnlySet;
62
get { return this.readOnly; }
63
set { this.readOnly = value; this.readOnlySet = true; }
67
/// Nested entry (optional).
69
public AtomEntry Entry
72
set { entry = value; }
75
//////////////////////////////////////////////////////////////////////
76
/// <summary>accessor method public string Rel</summary>
77
/// <returns> </returns>
78
//////////////////////////////////////////////////////////////////////
81
get {return this.rel;}
82
set {this.rel = value;}
84
/////////////////////////////////////////////////////////////////////////////
87
#region EntryLink Parser
88
//////////////////////////////////////////////////////////////////////
89
/// <summary>parses an xml node to create an EntryLink object</summary>
90
/// <param name="node">entrylink node</param>
91
/// <param name="parser">AtomFeedParser to use</param>
92
/// <returns> the created EntryLink object</returns>
93
//////////////////////////////////////////////////////////////////////
94
public static EntryLink ParseEntryLink(XmlNode node, AtomFeedParser parser)
97
EntryLink link = null;
98
Tracing.Assert(node != null, "node should not be null");
101
throw new ArgumentNullException("node");
105
object localname = node.LocalName;
106
if (localname.Equals(GDataParserNameTable.XmlEntryLinkElement))
108
link = new EntryLink();
109
if (node.Attributes != null)
111
if (node.Attributes[GDataParserNameTable.XmlAttributeHref] != null)
113
link.Href = node.Attributes[GDataParserNameTable.XmlAttributeHref].Value;
116
if (node.Attributes[GDataParserNameTable.XmlAttributeReadOnly] != null)
118
link.ReadOnly = node.Attributes[GDataParserNameTable.XmlAttributeReadOnly].Value.Equals(Utilities.XSDTrue);
121
if (node.Attributes[GDataParserNameTable.XmlAttributeRel] != null)
123
link.Rel = node.Attributes[GDataParserNameTable.XmlAttributeRel].Value;
128
if (node.HasChildNodes)
130
XmlNode entryChild = node.FirstChild;
132
while (entryChild != null && entryChild is XmlElement)
134
if (entryChild.LocalName == AtomParserNameTable.XmlAtomEntryElement &&
135
entryChild.NamespaceURI == BaseNameTable.NSAtom)
138
if (link.Entry == null)
140
XmlReader reader = new XmlNodeReader(entryChild);
141
// move the reader to the first node
143
AtomFeedParser p = new AtomFeedParser();
144
p.NewAtomEntry += new FeedParserEventHandler(link.OnParsedNewEntry);
145
p.ParseEntry(reader);
149
throw new ArgumentException("Only one entry is allowed inside the g:entryLink");
152
entryChild = entryChild.NextSibling;
162
//////////////////////////////////////////////////////////////////////
163
/// <summary>Event chaining. We catch this from the AtomFeedParser
164
/// we want to set this to our property, and do not add the entry to the collection
166
/// <param name="sender"> the object which send the event</param>
167
/// <param name="e">FeedParserEventArguments, holds the feed entry</param>
168
/// <returns> </returns>
169
//////////////////////////////////////////////////////////////////////
170
internal void OnParsedNewEntry(object sender, FeedParserEventArgs e)
172
// by default, if our event chain is not hooked, add it to the collection
173
Tracing.TraceCall("received new item notification");
174
Tracing.Assert(e != null, "e should not be null");
177
throw new ArgumentNullException("e");
180
if (e.CreatingEntry == false)
184
// add it to the collection
185
Tracing.TraceMsg("\t new EventEntry found");
186
this.Entry = e.Entry;
187
e.DiscardEntry = true;
191
/////////////////////////////////////////////////////////////////////////////
197
#region overloaded for persistence
199
//////////////////////////////////////////////////////////////////////
200
/// <summary>Returns the constant representing this XML element.</summary>
201
//////////////////////////////////////////////////////////////////////
202
public static string XmlName
204
get { return GDataParserNameTable.XmlEntryLinkElement; }
208
/// Used to save the EntryLink instance into the passed in xmlwriter
210
/// <param name="writer">the XmlWriter to write into</param>
211
public void Save(XmlWriter writer)
213
if (Utilities.IsPersistable(this.Href) ||
217
writer.WriteStartElement(XmlPrefix, XmlName, XmlNameSpace);
218
if (Utilities.IsPersistable(this.Href))
220
writer.WriteAttributeString(GDataParserNameTable.XmlAttributeHref, this.Href);
222
if (Utilities.IsPersistable(this.Rel))
224
writer.WriteAttributeString(GDataParserNameTable.XmlAttributeRel, this.Rel);
226
if (this.readOnlySet)
228
writer.WriteAttributeString(GDataParserNameTable.XmlAttributeReadOnly,
229
Utilities.ConvertBooleanToXSDString(this.ReadOnly));
234
entry.SaveToXml(writer);
236
writer.WriteEndElement();
241
#region IExtensionElementFactory Members
245
/// returns the xml local name for this element
247
string IExtensionElementFactory.XmlName
256
/// returns the xml namespace for this element
258
public string XmlNameSpace
262
return BaseNameTable.gNamespace;
267
/// returns the xml prefix to be used for this element
269
public string XmlPrefix
273
return BaseNameTable.gDataPrefix;
278
/// factory method to create an instance of a batchinterrupt during parsing
280
/// <param name="node">the xmlnode that is going to be parsed</param>
281
/// <param name="parser">the feedparser that is used right now</param>
282
/// <returns></returns>
283
public IExtensionElementFactory CreateInstance(XmlNode node, AtomFeedParser parser)
285
return EntryLink.ParseEntryLink(node, parser);