2007-12-18 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Data / System.Data / XmlTableWriter.cs
1 // 
2 // System.Data/XmlTableWriter.cs
3 //
4 // Author:
5 //   Patrick Earl <mono@patearl.net>
6 //
7 // Copyright (c) 2006, Patrick Earl
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27
28 #if NET_2_0
29 using System;
30 using System.Collections.Generic;
31 using System.Data;
32 using System.Xml;
33
34 internal class XmlTableWriter {
35         // This method is modelled after the DataSet's WriteXml functionality.
36         internal static void WriteTables(XmlWriter writer,
37                                  XmlWriteMode mode,
38                                  List<DataTable> tables,
39                                  List<DataRelation> relations,
40                                  string mainDataTable,
41                                  string dataSetName)
42         {
43                 if (mode == XmlWriteMode.DiffGram) {
44                         foreach (DataTable table in tables)
45                                 table.SetRowsID();
46                         DataSet.WriteDiffGramElement(writer);
47                 }
48
49                 bool shouldOutputContent = (mode != XmlWriteMode.DiffGram);
50                 for (int n = 0; n < tables.Count && !shouldOutputContent; n++)
51                         shouldOutputContent = tables[n].Rows.Count > 0;
52
53                 if (shouldOutputContent) {
54                         // We assume that tables[0] is the main table being written.
55                         // We happen to know that the code above us does things that way.
56                         DataSet.WriteStartElement(writer, mode, tables[0].Namespace, tables[0].Prefix, XmlHelper.Encode(dataSetName));
57
58                         if (mode == XmlWriteMode.WriteSchema) {
59                                 DataTable [] _tables = new DataTable[tables.Count];
60                                 tables.CopyTo(_tables);
61                                 DataRelation[] _relations = new DataRelation[relations.Count];
62                                 relations.CopyTo(_relations);
63                                 new XmlSchemaWriter(writer,
64                                         _tables,
65                                         _relations,
66                                         mainDataTable,
67                                         dataSetName
68                                 ).WriteSchema();
69                         }
70
71                         WriteTableList (writer, mode, tables, DataRowVersion.Default);
72
73                         writer.WriteEndElement();
74                 }
75
76                 if (mode == XmlWriteMode.DiffGram) {
77                         List<DataTable> changedTables = new List<DataTable>();
78                         foreach (DataTable table in tables) {
79                                 DataTable changed = table.GetChanges(DataRowState.Modified | DataRowState.Deleted);
80                                 if (changed != null && changed.Rows.Count > 0) {
81                                         changedTables.Add(changed);
82                                 }
83                         }
84                         if (changedTables.Count > 0) {
85                                 DataSet.WriteStartElement(writer, XmlWriteMode.DiffGram, XmlConstants.DiffgrNamespace, XmlConstants.DiffgrPrefix, "before");
86                                 WriteTableList (writer, mode, changedTables, DataRowVersion.Original);
87                                 writer.WriteEndElement();
88                         }
89                 
90                         writer.WriteEndElement(); // diffgr:diffgram
91                 }
92
93                 writer.Flush();
94         }
95
96         internal static void WriteTableList(XmlWriter writer, XmlWriteMode mode, List<DataTable> tables, DataRowVersion version)
97         {
98                 foreach (DataTable table in tables)
99                         DataSet.WriteTable(writer, table, mode, version);
100         }
101 }
102 #endif