Mercurial > hg > ucis.core
comparison Pml/PmlBuilder.cs @ 0:3ab940a0c7a0
Initial commit
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 11 Sep 2012 16:28:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ab940a0c7a0 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Text; | |
4 | |
5 namespace UCIS.Pml { | |
6 public class PmlBuilder { | |
7 private IPmlWriter pWriter; | |
8 private Stack<PmlElement> pStack = new Stack<PmlElement>(); | |
9 | |
10 public PmlBuilder(IPmlWriter Writer) { | |
11 pWriter = Writer; | |
12 } | |
13 public PmlBuilder() { | |
14 pWriter = null; | |
15 } | |
16 | |
17 public IPmlWriter BaseWriter { | |
18 get { return pWriter; } | |
19 set { pWriter = value; } | |
20 } | |
21 | |
22 private PmlElement AddChildElement(PmlElement Element, bool AddToStack) { | |
23 return AddChildElement(Element, AddToStack, null); | |
24 } | |
25 private PmlElement AddChildElement(PmlElement Element, bool AddToStack, string ChildName) { | |
26 PmlElement Parent; | |
27 if (pStack.Count > 0) { | |
28 Parent = pStack.Peek(); | |
29 if (Parent is PmlDictionary) { | |
30 if (ChildName == null) throw new ArgumentNullException("ChildName", "Dictionary items need a Name"); | |
31 ((PmlDictionary)Parent).Add(ChildName, Element); | |
32 } else if (Parent is PmlCollection) { | |
33 if (ChildName != null) throw new ArgumentOutOfRangeException("ChildName", "Can not add named element to a Collection"); | |
34 ((PmlCollection)Parent).Add(Element); | |
35 } else { | |
36 throw new InvalidOperationException("Invalid Element type in stack: " + Parent.Type.ToString()); | |
37 } | |
38 } else { | |
39 if (ChildName != null) { | |
40 throw new ArgumentOutOfRangeException("ChildName", "Can not create named element without container (Dictionary)"); | |
41 } else if (!AddToStack) { | |
42 pWriter.WriteMessage(Element); | |
43 } | |
44 } | |
45 if (AddToStack) pStack.Push(Element); | |
46 return Element; | |
47 } | |
48 | |
49 public PmlElement EndElement() { | |
50 if (pStack.Count > 0) { | |
51 PmlElement Element = pStack.Pop(); | |
52 if (pStack.Count == 0) { | |
53 if (pWriter != null) pWriter.WriteMessage(Element); | |
54 } | |
55 return Element; | |
56 } else { | |
57 return null; | |
58 } | |
59 } | |
60 public PmlElement GetMessage() { | |
61 if (pStack.Count == 1) { | |
62 PmlElement Element = pStack.Pop(); | |
63 return Element; | |
64 } else if (pStack.Count == 0) { | |
65 throw new InvalidOperationException("No stacked element. The top most element should not be ended. All elements, except Dictionary and Collection, are sent automatically."); | |
66 } else { | |
67 throw new InvalidOperationException("All elements, except for the top most element, should be ended."); | |
68 } | |
69 } | |
70 | |
71 public PmlElement SendMessage() { | |
72 PmlElement Element; | |
73 if (pWriter == null) throw new NullReferenceException("Writer is not set"); | |
74 Element = GetMessage(); | |
75 pWriter.WriteMessage(Element); | |
76 return Element; | |
77 } | |
78 | |
79 public PmlDictionary Dictionary() { | |
80 return (PmlDictionary)AddChildElement(new PmlDictionary(), true); | |
81 } | |
82 public PmlCollection Collection() { | |
83 return (PmlCollection)AddChildElement(new PmlCollection(), true); | |
84 } | |
85 public PmlString String(string Value) { | |
86 return (PmlString)AddChildElement(new PmlString(Value), false); | |
87 } | |
88 public PmlBinary Binary(byte[] Value) { | |
89 return (PmlBinary)AddChildElement(new PmlBinary(Value), false); | |
90 } | |
91 public PmlInteger Integer(UInt64 Value) { | |
92 return (PmlInteger)AddChildElement(new PmlInteger(Value), false); | |
93 } | |
94 public PmlInteger Integer(Int64 Value) { | |
95 return (PmlInteger)AddChildElement(new PmlInteger(Value), false); | |
96 } | |
97 public PmlNull Null() { | |
98 return (PmlNull)AddChildElement(new PmlNull(), false); | |
99 } | |
100 public PmlElement Element(PmlElement Child) { | |
101 return AddChildElement(Child, false); | |
102 } | |
103 | |
104 public PmlDictionary Dictionary(string Name) { | |
105 return (PmlDictionary)AddChildElement(new PmlDictionary(), true, Name); | |
106 } | |
107 public PmlCollection Collection(string Name) { | |
108 return (PmlCollection)AddChildElement(new PmlCollection(), true, Name); | |
109 } | |
110 public PmlString String(string Name, string Value) { | |
111 return (PmlString)AddChildElement(new PmlString(Value), false, Name); | |
112 } | |
113 public PmlBinary Binary(string Name, byte[] Value) { | |
114 return (PmlBinary)AddChildElement(new PmlBinary(Value), false, Name); | |
115 } | |
116 public PmlInteger Integer(string Name, UInt64 Value) { | |
117 return (PmlInteger)AddChildElement(new PmlInteger(Value), false, Name); | |
118 } | |
119 public PmlInteger Integer(string Name, Int64 Value) { | |
120 return (PmlInteger)AddChildElement(new PmlInteger(Value), false, Name); | |
121 } | |
122 public PmlNull Null(string Name) { | |
123 return (PmlNull)AddChildElement(new PmlNull(), false, Name); | |
124 } | |
125 public PmlElement Element(string Name, PmlElement Child) { | |
126 return AddChildElement(Child, false, Name); | |
127 } | |
128 } | |
129 } |