0faccc527f95b86c4b0fc1e9e65b0601dbd60a6d
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / DOM / DocumentEncoder.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26 using System;
27 using System.Runtime.InteropServices;
28 using System.Runtime.CompilerServices;
29 using System.Text;
30 using io=System.IO;
31 using Mono.Mozilla.DOM;
32
33 namespace Mono.Mozilla {
34
35         internal class DocumentEncoder : DOMObject {
36
37                 nsIDocumentEncoder docEncoder = null;
38                 
39                 public DocumentEncoder (WebBrowser control) : base (control) {          
40                         IntPtr docEncoderServicePtr = IntPtr.Zero;
41
42                         this.control.ServiceManager.getServiceByContractID (
43                                                         "@mozilla.org/layout/documentEncoder;1?type=text/html",
44                                                         typeof (nsIDocumentEncoder).GUID,
45                                                         out docEncoderServicePtr);
46                         if (docEncoderServicePtr == IntPtr.Zero)
47                                 throw new Mono.WebBrowser.Exception (Mono.WebBrowser.Exception.ErrorCodes.DocumentEncoderService);
48
49                         try {
50                                 docEncoder = (nsIDocumentEncoder)Marshal.GetObjectForIUnknown (docEncoderServicePtr);
51                         } catch (System.Exception ex) {
52                                 throw new Mono.WebBrowser.Exception (Mono.WebBrowser.Exception.ErrorCodes.DocumentEncoderService);
53                         }
54
55                         if (control.platform != control.enginePlatform)
56                                 this.docEncoder = nsDocumentEncoder.GetProxy (control, docEncoder);
57                 }
58
59 #region IDisposable Members
60
61                 protected override void Dispose (bool disposing)
62                 {
63                         if (!disposed) {
64                                 if (disposing) {
65                                         this.docEncoder = null;
66                                 }
67                                 disposed = true;
68                         }
69                 }
70 #endregion
71                 
72                 string mimeType;
73                 public string MimeType {
74                         get {
75                                 if (mimeType == null)
76                                         mimeType = "text/html";
77                                 return mimeType;
78                         }
79                         set {
80                                 mimeType = value;
81                         }
82                 }
83                 
84                 DocumentEncoderFlags flags;
85                 public DocumentEncoderFlags Flags {
86                         get { return flags; }
87                         set { flags = value; }
88                 }
89                 
90                 void Init (Document document, string mimeType, DocumentEncoderFlags flags)
91                 {
92                         UniString type = new UniString (mimeType);
93                         
94                         try {
95                                 docEncoder.init ((nsIDOMDocument)document.nodeNoProxy, 
96                                              type.Handle, 
97                                                  (uint)flags);
98                         } catch (System.Exception ex) {
99                                 throw new Mono.WebBrowser.Exception (Mono.WebBrowser.Exception.ErrorCodes.DocumentEncoderService, ex);
100                         }
101                 }
102                 
103                 public string EncodeToString (Document document) {
104                         Init (document, MimeType, Flags);
105                         docEncoder.encodeToString(storage);
106                         return Base.StringGet (storage);
107                 }
108                 
109                 public string EncodeToString (HTMLElement element) {
110                         Init ((Document)element.Owner, MimeType, Flags);
111                         docEncoder.setNode (element.nodeNoProxy);
112                         docEncoder.encodeToString(storage);
113                         string content = Base.StringGet (storage);
114                         
115                         string tag = element.TagName;
116                         string str = "<" + tag;
117                         foreach (Mono.WebBrowser.DOM.IAttribute att in element.Attributes) {
118                                 str += " " + att.Name + "=\"" + att.Value + "\"";
119                         }
120                         
121                         str += ">" + content + "</" + tag + ">";
122                         return str;
123                 }
124
125                 public io.Stream EncodeToStream (Document document) {
126                         Init (document, MimeType, Flags);
127                         Stream m = new Stream (new io.MemoryStream ());
128                         docEncoder.encodeToStream (m);
129                         return m.BaseStream;
130                 }
131
132                 public io.Stream EncodeToStream (HTMLElement element) {
133                         Init ((Document)element.Owner, MimeType, Flags);
134                         docEncoder.setNode (element.nodeNoProxy);
135                         Stream m = new Stream (new io.MemoryStream ());
136                         docEncoder.encodeToStream (m);
137                         return m.BaseStream;
138                 }
139                 
140 /*
141                 void nsIDocumentEncoder.setSelection ([MarshalAs (UnmanagedType.Interface)]  nsISelection aSelection)
142                 {
143                         return ;
144                 }
145
146
147
148                 void nsIDocumentEncoder.setRange ([MarshalAs (UnmanagedType.Interface)]  nsIDOMRange aRange)
149                 {
150                         return ;
151                 }
152
153
154
155                 void nsIDocumentEncoder.setNode ([MarshalAs (UnmanagedType.Interface)]  nsIDOMNode aNode)
156                 {
157                         return ;
158                 }
159
160
161
162                 void nsIDocumentEncoder.setContainerNode ([MarshalAs (UnmanagedType.Interface)]  nsIDOMNode aContainer)
163                 {
164                         return ;
165                 }
166
167
168
169                 void nsIDocumentEncoder.setCharset ( string aCharset)
170                 {
171                         return ;
172                 }
173
174
175
176                 void nsIDocumentEncoder.setWrapColumn ( uint aWrapColumn)
177                 {
178                         return ;
179                 }
180
181
182
183                 string nsIDocumentEncoder.getMimeType ()
184                 {
185                         return null;
186                 }
187
188                 void nsIDocumentEncoder.encodeToStream ([MarshalAs (UnmanagedType.Interface)]  nsIOutputStream aStream)
189                 {
190                         return ;
191                 }
192
193
194
195                 string nsIDocumentEncoder.encodeToString ()
196                 {
197                         return ;
198                 }
199
200
201
202                 string nsIDocumentEncoder.encodeToStringWithContext (out string aContextString,
203                                 out string aInfoString)
204                 {
205                         return ;
206                 }
207
208
209
210                 void nsIDocumentEncoder.setNodeFixup ([MarshalAs (UnmanagedType.Interface)]  nsIDocumentEncoderNodeFixup aFixup)
211                 {
212                         return ;
213                 }
214 */
215
216         }
217 }