private void DoReadData(NSData data, NSString typeName) { switch (typeName.description()) { // Note that this does not mean that the file is utf8, instead it means that the // file is our default document type which means we need to deduce the encoding. case "Plain Text, UTF8 Encoded": case "HTML": Boss boss = ObjectModel.Create("TextEditorPlugin"); var encoding = boss.Get<ITextEncoding>(); NSString str = encoding.Decode(data, out m_encoding); m_text = NSMutableAttributedString.Alloc().initWithString(str).To<NSMutableAttributedString>(); if (m_encoding == Enums.NSMacOSRomanStringEncoding) DoEncodingWarning(); // If an html file is being edited in Continuum then ensure that it is saved // as plain text. (To save a document as html the user needs to use save as // and explicitly select html). setFileType(NSString.Create("Plain Text, UTF8 Encoded")); break; // These types are based on the file's extension so we can (more or less) trust them. case "Rich Text Format (RTF)": m_text = DoReadWrapped(data, Externs.NSRTFTextDocumentType); break; case "Word 97 Format (doc)": m_text = DoReadWrapped(data, Externs.NSDocFormatTextDocumentType); break; case "Word 2007 Format (docx)": m_text = DoReadWrapped(data, Externs.NSOfficeOpenXMLTextDocumentType); break; case "Open Document Text (odt)": m_text = DoReadWrapped(data, Externs.NSOpenDocumentTextDocumentType); break; // Open as Binary case "binary": m_text = NSMutableAttributedString.Create(data.bytes().ToText()); m_binary = true; m_encoding = Enums.NSUTF8StringEncoding; break; default: Contract.Assert(false, "bad typeName: " + typeName.description()); break; } }
public bool readFromData_ofType_error(NSData data, NSString typeName, IntPtr outError) { bool read = false; try { using (MemoryStream stream = new MemoryStream(data.bytes())) { var doc = new XmlDocument(); doc.Load(stream); DoParseXml(doc); m_engine = new ComputeEngine(m_settings); Marshal.WriteIntPtr(outError, IntPtr.Zero); read = true; NSNotificationCenter.defaultCenter().postNotificationName_object( StateChanged, DocChange.Create(this, ChangeType.All)); } } catch (Exception e) { Console.Error.WriteLine("{0}", e); NSMutableDictionary userInfo = NSMutableDictionary.Create(); userInfo.setObject_forKey(NSString.Create("Couldn't read the document."), Externs.NSLocalizedDescriptionKey); userInfo.setObject_forKey(NSString.Create(e.Message), Externs.NSLocalizedFailureReasonErrorKey); NSObject error = NSError.errorWithDomain_code_userInfo(Externs.Cocoa3Domain, 1, userInfo); Marshal.WriteIntPtr(outError, error); } return read; }