Example #1
0
        public NSString Decode(NSData data, out uint encoding)
        {
            Contract.Requires(data != null, "data is null");

            NSString result = null;

            if (data.length() > 0)
            {
                int skipBytes = 0;
                encoding = DoGetEncoding(data, ref skipBytes);
                if (encoding != 0)
                {
                    if (skipBytes > 0)
                        data = data.subdataWithRange(new NSRange(skipBytes, (int) data.length() - skipBytes));
                    result = DoDecode(data, encoding);

                    // The first few bytes of most legacy documents will look like utf8 so
                    // if we couldn't decode it using utf8 we need to fall back onto Mac
                    // OS Roman.
                    if (NSObject.IsNullOrNil(result) && encoding == Enums.NSUTF8StringEncoding)
                    {
                        encoding = Enums.NSMacOSRomanStringEncoding;
                        result = DoDecode(data, encoding);
                    }
                }

                if (NSObject.IsNullOrNil(result))
                    throw new InvalidOperationException("Couldn't read the file as Unicode or Mac OS Roman.");	// should only happen if there are embedded control characters in the header
            }
            else
            {
                result = NSString.Empty;
                encoding = Enums.NSUTF8StringEncoding;
            }

            return result;
        }
Example #2
0
        public bool readFromData_ofType_error(NSData data, NSString typeName, IntPtr outError)
        {
            bool read = true;

            try
            {
                // Note that we have to use the path instead of data so that Cecil knows
                // where to look for the mdb file.
                m_assemblyPath = fileURL().path().description();

                m_assembly = AssemblyDefinition.ReadAssembly(m_assemblyPath);
                DoLoadSymbols(m_assembly);
                DoGetNamespaces(m_assembly);

                Marshal.WriteIntPtr(outError, IntPtr.Zero);
            }
            catch (Exception e)
            {
                NSMutableDictionary userInfo = NSMutableDictionary.Create();
                userInfo.setObject_forKey(NSString.Create("Couldn't read the assembly."), 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);

                Log.WriteLine(TraceLevel.Error, "App", "Couldn't read the assembly:");
                Log.WriteLine(TraceLevel.Error, "App", "{0}", e);

                read = false;
            }

            return read;
        }
Example #3
0
		private NSMutableAttributedString DoReadWrapped(NSData data, NSString type)
		{
			NSMutableAttributedString str;
			
			if (data.length() > 0)
			{
				NSDictionary options = NSDictionary.dictionaryWithObject_forKey(type, Externs.NSDocumentTypeDocumentAttribute);
				NSError error;
				str = NSMutableAttributedString.Alloc().initWithData_options_documentAttributes_error(data, options, IntPtr.Zero, out error).To<NSMutableAttributedString>();
				if (!NSObject.IsNullOrNil(error))
					error.Raise();
				
				str.autorelease();
			}
			else
				str = NSMutableAttributedString.Create();
			
			return str;
		}
Example #4
0
		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;
			}
		}
Example #5
0
		public bool readFromData_ofType_error(NSData data, NSString typeName, IntPtr outError)
		{
			bool read = false;
			
			try
			{
				Contract.Assert(NSObject.IsNullOrNil(m_text), "m_text is not null");
				
				if (DoShouldOpen(data.length()))
				{
					DoReadData(data, typeName);
					if (NSObject.IsNullOrNil(m_text))
						throw new InvalidOperationException("Couldn't decode the file.");
					
					string text = m_text.string_().description();
					DoSetEndian(text);
					DoCheckForControlChars(m_text.string_());
					
					if (m_controller != null)			// will be null for initial open, but non-null for revert
					{
						m_controller.RichText = m_text;
						m_text = null;
					}
					else
						m_text.retain();
						
					read = true;
					Marshal.WriteIntPtr(outError, IntPtr.Zero);
				}
				else
				{
					NSObject error = NSError.errorWithDomain_code_userInfo(Externs.Cocoa3Domain, Enums.NSUserCancelledError, null);
					Marshal.WriteIntPtr(outError, error);
				}
			}
			catch (Exception e)
			{
				Log.WriteLine(TraceLevel.Error, "App", "Couldn't open {0:D}", fileURL());
				Log.WriteLine(TraceLevel.Error, "App", "{0}", e);
				
				NSMutableDictionary userInfo = NSMutableDictionary.Create();
				userInfo.setObject_forKey(NSString.Create("Couldn't read the document data."), 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;
		}
Example #6
0
 public NSString Decode(NSData data)
 {
     uint encoding;
     return Decode(data, out encoding);
 }
Example #7
0
        private uint DoGetEncoding(NSData data, ref int skipBytes)
        {
            uint encoding = 0;
            const int HeaderBytes = 2*64;

            byte[] buffer;
            data.getBytes_length(out buffer, HeaderBytes);

            // Check for a BOM.
            if (buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF)
            {
                encoding = Enums.NSUTF32BigEndianStringEncoding;
                skipBytes = 4;
            }
            else if (buffer[0] == 0xFF && buffer[1] == 0xFE && buffer[2] == 0x00 && buffer[3] == 0x00)
            {
                encoding = Enums.NSUTF32LittleEndianStringEncoding;
                skipBytes = 4;
            }
            else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
            {
                encoding = Enums.NSUTF16BigEndianStringEncoding;
                skipBytes = 2;
            }
            else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
            {
                encoding = Enums.NSUTF16LittleEndianStringEncoding;
                skipBytes = 2;
            }

            // See if it looks like utf-32.
            if (encoding == 0)
            {
                if (DoLooksLikeUTF32(buffer, true, buffer.Length))
                    encoding = Enums.NSUTF32BigEndianStringEncoding;
                else if (DoLooksLikeUTF32(buffer, false, buffer.Length))
                    encoding = Enums.NSUTF32LittleEndianStringEncoding;
            }

            // See if it looks like utf-16.
            if (encoding == 0)
            {
                if (DoLooksLikeUTF16(buffer, true, buffer.Length))
                    encoding = Enums.NSUTF16BigEndianStringEncoding;
                else if (DoLooksLikeUTF16(buffer, false, buffer.Length))
                    encoding = Enums.NSUTF16LittleEndianStringEncoding;
            }

            // See if it could be utf-8.
            if (encoding == 0)
            {
                if (buffer.All(b => DoIsValidUTF8(b)))
                    encoding = Enums.NSUTF8StringEncoding;
            }

            // Fall back on Mac OS Roman.
            if (encoding == 0)
            {
                if (buffer.All(b => DoIsValidMacRoman(b)))
                    encoding = Enums.NSMacOSRomanStringEncoding;
            }

            return encoding;
        }
Example #8
0
        private NSString DoDecode(NSData data, uint encoding)
        {
            NSString result = NSString.Alloc().initWithData_encoding(data, encoding);

            if (NSObject.IsNullOrNil(result))
                result = null;
            else
                result.autorelease();

            return result;
        }
Example #9
0
 public bool readFromData_ofType_error(NSData data, NSString typeName, IntPtr outError)
 {
     return true;
 }
Example #10
0
    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;
    }