5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System
.Collections
.Generic
;
30 using System
.Collections
.ObjectModel
;
32 using System
.ServiceModel
;
33 using System
.ServiceModel
.Channels
;
36 namespace System
.ServiceModel
.Syndication
38 public class ServiceDocument
40 public static TServiceDocument Load
<TServiceDocument
> (XmlReader reader
)
41 where TServiceDocument
: ServiceDocument
, new()
43 var doc
= new TServiceDocument ();
44 new AtomPub10ServiceDocumentFormatter
<TServiceDocument
> (doc
).ReadFrom (reader
);
48 public static ServiceDocument
Load (XmlReader reader
)
50 return Load
<ServiceDocument
> (reader
);
54 public ServiceDocument ()
56 Workspaces
= new Collection
<Workspace
> ();
59 public ServiceDocument (IEnumerable
<Workspace
> workspaces
)
62 if (workspaces
!= null)
63 foreach (var w
in workspaces
)
67 ServiceDocumentFormatter formatter
;
68 SyndicationExtensions extensions
= new SyndicationExtensions ();
70 internal ServiceDocumentFormatter InternalFormatter
{
71 set { formatter = value; }
74 public Dictionary
<XmlQualifiedName
, string> AttributeExtensions
{
75 get { return extensions.Attributes; }
78 public Uri BaseUri { get; set; }
80 public SyndicationElementExtensionCollection ElementExtensions
{
81 get { return extensions.Elements; }
84 public string Language { get; set; }
86 public Collection
<Workspace
> Workspaces { get; private set; }
89 protected internal virtual Workspace
CreateWorkspace ()
91 return new Workspace ();
94 public ServiceDocumentFormatter
GetFormatter ()
96 if (formatter
== null)
97 formatter
= new AtomPub10ServiceDocumentFormatter (this);
101 public void Save (XmlWriter writer
)
103 GetFormatter ().WriteTo (writer
);
106 protected internal virtual bool TryParseAttribute (string name
, string ns
, string value, string version
)
108 if (version
!= "http://www.w3.org/2007/app")
112 case "http://www.w3.org/XML/1998/namespace":
115 BaseUri
= new Uri (value, UriKind
.RelativeOrAbsolute
);
126 protected internal virtual bool TryParseElement (XmlReader reader
, string version
)
129 throw new ArgumentNullException ("reader");
131 reader
.MoveToContent ();
133 if (reader
.LocalName
!= "service" || reader
.NamespaceURI
!= version
)
136 for (int i
= 0; i
< reader
.AttributeCount
; i
++) {
137 reader
.MoveToAttribute (i
);
138 if (!TryParseAttribute (reader
.LocalName
, reader
.NamespaceURI
, reader
.Value
, version
))
139 AttributeExtensions
.Add (new XmlQualifiedName (reader
.LocalName
, reader
.NamespaceURI
), reader
.Value
);
141 reader
.MoveToElement ();
143 if (reader
.IsEmptyElement
)
144 throw new XmlException ("AtomPP service element requires at least one workspace element");
146 reader
.ReadStartElement ();
148 for (reader
.MoveToContent (); reader
.NodeType
!= XmlNodeType
.EndElement
; reader
.MoveToContent ()) {
149 if (reader
.LocalName
== "workspace" && reader
.NamespaceURI
== version
) {
150 var ws
= CreateWorkspace ();
151 if (ws
.TryParseElement (reader
, version
)) {
156 ElementExtensions
.Add (new SyndicationElementExtension (reader
));
159 reader
.ReadEndElement ();
164 protected internal virtual void WriteAttributeExtensions (XmlWriter writer
, string version
)
166 extensions
.WriteAttributeExtensions (writer
, version
);
169 protected internal virtual void WriteElementExtensions (XmlWriter writer
, string version
)
171 extensions
.WriteElementExtensions (writer
, version
);