1 //Copyright 2010 Microsoft Corporation
2 //
3 //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
4 //You may obtain a copy of the License at
5 //
6 //http://www.apache.org/licenses/LICENSE-2.0
7 //
8 //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
9 //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 //See the License for the specific language governing permissions and limitations under the License.
11 
12 
13 namespace System.Data.Services.Common
14 {
15     using System.Collections.Generic;
16     using System.Diagnostics;
17     using System.Linq;
18     using System.Xml;
19 
20     internal enum EpmSerializationKind
21     {
22         Attributes,
23 
24         Elements,
25 
26         All
27     }
28 
29     internal abstract class EpmContentSerializerBase
30     {
EpmContentSerializerBase(EpmTargetTree tree, bool isSyndication, object element, XmlWriter target)31         protected EpmContentSerializerBase(EpmTargetTree tree, bool isSyndication, object element, XmlWriter target)
32         {
33             this.Root = isSyndication ? tree.SyndicationRoot : tree.NonSyndicationRoot;
34             this.Element = element;
35             this.Target = target;
36             this.Success = false;
37         }
38 
39         protected EpmTargetPathSegment Root
40         {
41             get;
42             private set;
43         }
44 
45         protected object Element
46         {
47             get;
48             private set;
49         }
50 
51         protected XmlWriter Target
52         {
53             get;
54             private set;
55         }
56 
57         protected bool Success
58         {
59             get;
60             private set;
61         }
62 
Serialize()63         internal void Serialize()
64         {
65             foreach (EpmTargetPathSegment targetSegment in this.Root.SubSegments)
66             {
67 
68                 this.Serialize(targetSegment, EpmSerializationKind.All);
69             }
70 
71             this.Success = true;
72         }
73 
Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind)74         protected virtual void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind)
75         {
76             IEnumerable<EpmTargetPathSegment> segmentsToSerialize;
77             switch (kind)
78             {
79                 case EpmSerializationKind.Attributes:
80                     segmentsToSerialize = targetSegment.SubSegments.Where(s => s.IsAttribute == true);
81                     break;
82                 case EpmSerializationKind.Elements:
83                     segmentsToSerialize = targetSegment.SubSegments.Where(s => s.IsAttribute == false);
84                     break;
85                 default:
86                     Debug.Assert(kind == EpmSerializationKind.All, "Must serialize everything");
87                     segmentsToSerialize = targetSegment.SubSegments;
88                     break;
89             }
90 
91             foreach (EpmTargetPathSegment segment in segmentsToSerialize)
92             {
93                 this.Serialize(segment, kind);
94             }
95         }
96     }
97 }