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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2008 Novell, Inc.
23 // Andreia Gaita (avidigal@novell.com)
27 using System.Runtime.InteropServices;
28 using System.Runtime.CompilerServices;
31 using Mono.Mozilla.DOM;
33 namespace Mono.Mozilla {
35 internal class DocumentEncoder : DOMObject {
37 nsIDocumentEncoder docEncoder = null;
39 public DocumentEncoder (WebBrowser control) : base (control) {
40 IntPtr docEncoderServicePtr = IntPtr.Zero;
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);
50 docEncoder = (nsIDocumentEncoder)Marshal.GetObjectForIUnknown (docEncoderServicePtr);
51 } catch (System.Exception) {
52 throw new Mono.WebBrowser.Exception (Mono.WebBrowser.Exception.ErrorCodes.DocumentEncoderService);
55 if (control.platform != control.enginePlatform)
56 this.docEncoder = nsDocumentEncoder.GetProxy (control, docEncoder);
59 #region IDisposable Members
61 protected override void Dispose (bool disposing)
65 this.docEncoder = null;
73 public string MimeType {
76 mimeType = "text/html";
84 DocumentEncoderFlags flags;
85 public DocumentEncoderFlags Flags {
87 set { flags = value; }
90 void Init (Document document, string mimeType, DocumentEncoderFlags flags)
92 UniString type = new UniString (mimeType);
95 docEncoder.init ((nsIDOMDocument)document.nodeNoProxy,
98 } catch (System.Exception ex) {
99 throw new Mono.WebBrowser.Exception (Mono.WebBrowser.Exception.ErrorCodes.DocumentEncoderService, ex);
103 public string EncodeToString (Document document) {
104 Init (document, MimeType, Flags);
105 docEncoder.encodeToString(storage);
106 return Base.StringGet (storage);
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);
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 + "\"";
121 str += ">" + content + "</" + tag + ">";
125 public io.Stream EncodeToStream (Document document) {
126 Init (document, MimeType, Flags);
127 Stream m = new Stream (new io.MemoryStream ());
128 docEncoder.encodeToStream (m);
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);
141 void nsIDocumentEncoder.setSelection ([MarshalAs (UnmanagedType.Interface)] nsISelection aSelection)
148 void nsIDocumentEncoder.setRange ([MarshalAs (UnmanagedType.Interface)] nsIDOMRange aRange)
155 void nsIDocumentEncoder.setNode ([MarshalAs (UnmanagedType.Interface)] nsIDOMNode aNode)
162 void nsIDocumentEncoder.setContainerNode ([MarshalAs (UnmanagedType.Interface)] nsIDOMNode aContainer)
169 void nsIDocumentEncoder.setCharset ( string aCharset)
176 void nsIDocumentEncoder.setWrapColumn ( uint aWrapColumn)
183 string nsIDocumentEncoder.getMimeType ()
188 void nsIDocumentEncoder.encodeToStream ([MarshalAs (UnmanagedType.Interface)] nsIOutputStream aStream)
195 string nsIDocumentEncoder.encodeToString ()
202 string nsIDocumentEncoder.encodeToStringWithContext (out string aContextString,
203 out string aInfoString)
210 void nsIDocumentEncoder.setNodeFixup ([MarshalAs (UnmanagedType.Interface)] nsIDocumentEncoderNodeFixup aFixup)