internal void Broadcast(PropertyDictionary request)
 {
     foreach (ChatroomServerProtocol instance in _clients)
     {
         instance.SendMessage(request);
     }
 }
        public RectangularDocumentFrame(DocumentDeserializationContext context, PropertyDictionary frameDictionary)
            : base(context, frameDictionary)
        {
            if (!frameDictionary.HasDictionaryFor("clipBounds"))
            {
                throw new DocumentReadingException("A label frame is missing the required \"clipBounds\" field.");
            }

            if (!frameDictionary.HasDictionaryFor("framedObject"))
            {
                throw new DocumentReadingException("A label frame is missing the required \"framedObject\" field.");
            }

            _clipBounds = PropertyBuilders.ToRectangle(frameDictionary.DictionaryFor("clipBounds"));

            PropertyDictionary framedObjectDictionary = frameDictionary.DictionaryFor("framedObject");

            string framedObjectType = framedObjectDictionary.StringFor("type", "missing");

            switch (framedObjectType)
            {
                case "image":
                    _framedObject = new DocumentImage(context, framedObjectDictionary);
                    break;

                case "fill":
                    _framedObject = new DocumentFill(context, framedObjectDictionary);
                    break;

                default:
                    throw new DocumentReadingException(string.Format("The document object type \"{0}\" is not supported in this version.",
                        framedObjectType));
            }
        }
        public static FormattingPropertyDictionaryWrapper Wrap(PropertyDictionary dictionary)
        {
            FormattingPropertyDictionaryWrapper wrapper = new FormattingPropertyDictionaryWrapper();
            wrapper._dictionary = dictionary;

            return wrapper;
        }
Example #4
0
 public DocumentFill(DocumentDeserializationContext context, PropertyDictionary framedObjectDictionary)
 {
     if (framedObjectDictionary.HasDictionaryFor("color"))
     {
         _color = PropertyBuilders.ToColor(framedObjectDictionary.DictionaryFor("color"));
     }
 }
Example #5
0
        private static object ParseDictionary(Lexer lexer)
        {
            PropertyDictionary dictionary = new PropertyDictionary();

            while (true)
            {
                Token token = lexer.Next();

                if (token.Kind == TokenKind.DictionaryClose) break;

                object key = ParseObject(lexer, token);

                Token equals = lexer.Next();
                if (equals.Kind != TokenKind.Equals) ThrowBecauseOfAnUnexpectedToken(lexer, equals);

                object value = ParseObject(lexer, Token.None);

                dictionary.SetValueFor(key, value);

                Token terminator = lexer.Next();

                if (terminator.Kind == TokenKind.DictionaryClose) break;
                if (terminator.Kind == TokenKind.Separator) continue;

                ThrowBecauseOfAnUnexpectedToken(lexer, terminator);
            }

            return dictionary;
        }
Example #6
0
        public DocumentFrame(DocumentDeserializationContext context, PropertyDictionary frameProperties)
        {
            if (!frameProperties.HasDictionaryFor("offsetInDocument"))
            {
                throw new DocumentReadingException("A frame is missing the required \"offsetInDocument\" field.");
            }

            _offsetInDocument = PropertyBuilders.ToPoint(frameProperties.DictionaryFor("offsetInDocument"));
        }
Example #7
0
        public CsStructure(Structure structure, PropertyDictionary options)
        {
            _structure = structure;
            _options = options;

            if (_options.HasStringFor("surrogate-for") && !_options.HasValueFor("surrogate-for-is-reference-type"))
            {
                throw new LanguageException("Any use of \"surrogate-for\" must be accompanied with \"surrogate-for-is-reference-type\".");
            }
        }
        public LabelDocumentFrame(DocumentDeserializationContext context, PropertyDictionary frameProperties)
            : base(context, frameProperties)
        {
            if (!frameProperties.HasDictionaryFor("callOutOffsetInDocument"))
            {
                throw new DocumentReadingException("A label frame is missing the required \"callOutOffsetInDocument\" field.");
            }

            _callOutOffsetInDocument = PropertyBuilders.ToSize(frameProperties.DictionaryFor("callOutOffsetInDocument"));
            _label = frameProperties.StringFor("label", "");
            _caption = frameProperties.StringFor("caption", "");
        }
Example #9
0
        public DocumentImage(DocumentDeserializationContext context, PropertyDictionary framedObjectDictionary)
        {
            int imageLinkIndex = framedObjectDictionary.IntegerFor("imageLinkIndex").Value;

            if (!context.ImageLinksByKey.ContainsKey(imageLinkIndex))
            {
                throw new DocumentReadingException(string.Format(
                    "The image link index \"{0}\" was not found in the image link manager dictionary.",
                    imageLinkIndex));
            }

            _imageLink = context.ImageLinksByKey[imageLinkIndex];
        }
        public static Color ToColor(PropertyDictionary dictionary)
        {
            if (!dictionary.HasIntegerFor("red", "green", "blue"))
            {
                throw new PropertyListException("A color property dictionary is missing one or " +
                    "both of the required fields (red, green or blue).");
            }

            return Color.FromArgb(
                (byte)dictionary.IntegerFor("alpha", 255),
                (byte)dictionary.IntegerFor("red").Value,
                (byte)dictionary.IntegerFor("green").Value,
                (byte)dictionary.IntegerFor("blue").Value
                );
        }
 internal void SendMessage(PropertyDictionary message)
 {
     try
     {
         SendTerm(Atom.From(message.PersistToString()));
     }
     catch(InvalidOperationException e)
     {
         //should we through a less generic exception to make this easier to detect?
         if (e.Message == "An attempt was made to send data through a closed socket.")
         {
             _parent.GracefullyHandleLostServer();
         }
         else throw;
     }
 }
        internal ImageLinkManager(DocumentDeserializationContext context, PropertyDictionary managerDictionary)
        {
            PropertyDictionary imageLinks = managerDictionary.DictionaryFor("imageLinks");

            if (imageLinks == null) throw new InvalidOperationException("The image manager dictionary is missing the \"imageLinks\" key.");

            foreach (object key in imageLinks.Keys)
            {
                if (!(key is int)) throw new InvalidOperationException("The image links dictionary contains a non-integer key, which is not permitted.");

                int linkIndex = (int)key;

                PropertyDictionary imageDictionary = imageLinks.DictionaryFor(key);

                ImageLink link = new ImageLink(context, imageDictionary);

                _referenceCountByLink[link] = 0;
                context.ImageLinksByKey[linkIndex] = link;
            }
        }
Example #13
0
        public void WriteDictionary(PropertyDictionary dictionary)
        {
            AppendLine("{0}", "{");
            Indent();

            foreach (object key in dictionary.Keys)
            {
                object value = dictionary.ValueFor(key);

                if (value == null) continue;

                WriteObject(key);
                Append(" = ");
                WriteObject(value);
                AppendLine(";");
            }

            Dedent();
            Append("{0}", "}");
        }
        public void HandleRequest(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!request.HasValueFor(ChatroomKeys.MessageType) || request.StringFor(ChatroomKeys.MessageType) != Key)
            {
                protocol.SendMalformedRequestResponse();
                return;
            }

            object connected = null;

            lock (_lockObject)
            {
                connected = protocol.Parent.RequestLogin(protocol, request);
                request.SetValueFor(ChatroomKeys.Message, connected);
            }

            if (connected.ToString() == ChatroomKeys.LoginSuccess)
            {
                protocol.Parent.BroadcastServerMessage("{0} has joined.", protocol.Username);
                protocol.SendWelcomeMessage();
            }

            protocol.SendTerm(Atom.From(request.PersistToString()));
        }
        public void HandleRequest(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!request.HasValueFor(ChatroomKeys.MessageType) || !(request.StringFor(ChatroomKeys.MessageType) == Key))
            {
                protocol.SendMalformedRequestResponse();
                return;
            }

            lock (_lockObject)
            {
                string sender = protocol.Parent.ResolveSenderName(request);

                if (sender != null)
                {
                    request[ChatroomKeys.SenderName] = sender;

                    protocol.Parent.Broadcast(request);
                }
                else
                {
                    protocol.SendMalformedRequestResponse();
                }
            }
        }
        public Uri ResolveMasterReference(PropertyDictionary master, string manifestPath)
        {
            if (master == null || string.IsNullOrEmpty(master.StringFor("fileName")))
            {
                throw new AdornedReferenceResolutionException(string.Format(
                    "The manifest for reference \"{0}\" does not have a master section or the master " +
                    "section does not contain a \"fileName\" setting.",
                    manifestPath));
            }

            string imageFileName = Path.Combine(_referencesDirectory, master.StringFor("fileName"));

            if (!File.Exists(imageFileName))
            {
                throw new AdornedReferenceResolutionException(string.Format(
                    "A file (\"{0}\") referenced in the reference manifest \"{1}\" could not be found.",
                    imageFileName, manifestPath));
            }

            string newUrl = ResolveFormat(Path.GetFullPath(imageFileName), master);
            string newerUrl = ResolveFormat(newUrl.ToString(), master);
            string reallyNewUrl = ResolveFormat(newerUrl.ToString(), master);

            return new Uri(reallyNewUrl);
        }
        public Uri ResolveInline(string contentType, string content)
        {
            if (contentType != "latex")
            {
                throw new AdornedReferenceResolutionException(string.Format(
                    "The inline block content type \"{0}\" is unknown or not supported.", contentType));
            }

            string newFilePath = _temporaryFileManager.CreateAnonymousFilePath(".tex");

            using (StreamWriter writer = new StreamWriter(newFilePath))
            {
                writer.WriteLine(@"\batchmode");
                writer.WriteLine(@"\documentclass[fleqn]{article}");
                writer.WriteLine(@"\mathindent=0em");
                writer.WriteLine(@"\usepackage[a4paper,landscape]{geometry}");
                writer.WriteLine(@"\usepackage[fleqn]{amsmath}");
                writer.WriteLine(@"\usepackage[active,delayed,pdftex,tightpage]{preview}");
                writer.WriteLine(@"\setlength\PreviewBorder{5pt}");
                writer.WriteLine(@"\begin{document}");
                writer.WriteLine(@"\begin{preview}");

                writer.WriteLine(content.Trim());

                writer.WriteLine(@"\end{preview}");
                writer.WriteLine(@"\end{document}");

                writer.Close();
            }

            PropertyDictionary properties = new PropertyDictionary();
            properties.SetValueFor("resolution", 120);

            string newUrl = ResolveFormat(newFilePath, properties);
            string newerUrl = ResolveFormat(newUrl.ToString(), properties);

            return new Uri(newerUrl);
        }
Example #18
0
 public ImageLink(DocumentDeserializationContext context, PropertyDictionary dictionary)
 {
     _fileName = Path.Combine(context.AbsolutePath, dictionary.StringFor("fileName"));
     _physicalDimension = PropertyBuilders.ToSize(dictionary.DictionaryFor("physicalDimension"));
 }
        public static Point ToPoint(PropertyDictionary dictionary)
        {
            if (!dictionary.HasIntegerFor("x", "y"))
            {
                throw new PropertyListException("A point property dictionary is missing one or " +
                    "both of the required fields (x or y).");
            }

            return new Point(
                dictionary.IntegerFor("x").Value,
                dictionary.IntegerFor("y").Value
                );
        }
        internal object RequestLogin(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!CanAddClient) return ChatroomKeys.LoginFail_TooManyClients;
            if (!UsernameIsAvailable(request.StringFor(ChatroomKeys.SenderName))) return ChatroomKeys.LoginFail_UserNameInUse;

            protocol.Username = request.StringFor(ChatroomKeys.SenderName);
            protocol.Key = request.StringFor(ChatroomKeys.SenderId);

            _clients.AddClient(protocol);

            BroadcastLoggedInUsers();

            return ChatroomKeys.LoginSuccess;
        }
        internal string ResolveSenderName(PropertyDictionary request)
        {
            if (request.HasValueFor(ChatroomKeys.SenderId))
            {
                ChatroomServerProtocol protocol = _clients[request.StringFor(ChatroomKeys.SenderId)];

                return protocol == null ? null : protocol.Username;
            }

            return null;
        }
Example #22
0
        static DocumentFrame DeserializeFrame(DocumentDeserializationContext context, PropertyDictionary frameProperties)
        {
            string type = frameProperties.StringFor("type", "missing");

            switch (type)
            {
                case "label":
                    return new LabelDocumentFrame(context, frameProperties);

                case "rectangular":
                    return new RectangularDocumentFrame(context, frameProperties);

                default:
                    throw new DocumentReadingException(string.Format("The frame type \"{0}\" is not recognised by this version.",
                        type));
            }
        }
        public static Rectangle ToRectangle(PropertyDictionary dictionary)
        {
            if (!dictionary.HasIntegerFor("x", "y", "width", "height"))
            {
                throw new PropertyListException("A rectangle property dictionary is missing some or " +
                    "all of the required fields (x, y, width or height).");
            }

            return new Rectangle(
                dictionary.IntegerFor("x").Value,
                dictionary.IntegerFor("y").Value,
                dictionary.IntegerFor("width").Value,
                dictionary.IntegerFor("height").Value
                );
        }
        public Uri ResolvePasteUpReference(PropertyDictionary manifest, string manifestPath)
        {
            Document document = Document.Deserialize(manifest, Path.GetDirectoryName(manifestPath));

            PasteUpRenderer renderer = new PasteUpRenderer(document);

            string outputPath = _temporaryFileManager.CreateAnonymousFilePath(".png");

            using (Bitmap bitmap = renderer.Render())
            {
                bitmap.Save(outputPath);
            }

            return new Uri(outputPath);
        }
        public static Size ToSize(PropertyDictionary dictionary)
        {
            if (!dictionary.HasIntegerFor("width", "height"))
            {
                throw new PropertyListException("A size property dictionary is missing one or " +
                    "both of the required fields (width or height).");
            }

            return new Size(
                dictionary.IntegerFor("width").Value,
                dictionary.IntegerFor("height").Value
                );
        }
        public string ResolveFormat(string path, PropertyDictionary properties)
        {
            string extension = Path.GetExtension(path).ToLower();

            if (extension == ".png")
            {
                return path;
            }

            if (extension == ".jpg")
            {
                return path;
            }

            if (extension == ".dot")
            {
                string newFilePath = _temporaryFileManager.CreateAnonymousFilePath(".ps");

                string arguments = string.Format("\"{0}\" -Tps2 \"-o{1}\"",
                    path, newFilePath);

                RunUtility(_dotCommand, arguments);

                return newFilePath;
            }

            if (extension == ".ps")
            {
                string newFilePath = _temporaryFileManager.CreateAnonymousFilePath(".pdf");

                string arguments = string.Format(
                    "-dFirstPage=1 -dLastPage=1 -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dUseCropBox " +
                    "-sOutputFile=\"{1}\" \"{0}\"",
                    path, newFilePath);

                RunUtility(_ghostscriptCommand, arguments);

                return newFilePath;
            }

            if (extension == ".pdf" || extension == ".ai")
            {
                string newFilePath = _temporaryFileManager.CreateAnonymousFilePath(".png");

                int resolution = properties.IntegerFor("resolution", 72);

                string arguments = string.Format(
                    "-dFirstPage=1 -dLastPage=1 -sDEVICE=png16m -dBATCH -dNOPAUSE " +
                    "-dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dUseCropBox -r{2} " +
                    "-sOutputFile=\"{1}\" \"{0}\"",
                    path, newFilePath, resolution);

                RunUtility(_ghostscriptCommand, arguments);

                return newFilePath;
            }

            if (extension == ".tex")
            {
                string newFilePath = _temporaryFileManager.CreateAnonymousFilePath(".pdf", ".log", ".aux");

                string arguments = string.Format("-output-directory=\"{0}\" -job-name=\"{1}\" \"{2}\"",
                    Path.GetDirectoryName(newFilePath), Path.GetFileNameWithoutExtension(newFilePath), path);

                RunUtility(_texCommand, arguments);

                return newFilePath;
            }

            return null;
        }
        private PropertyDictionary GenerateImpliedManifest(string manifestPath)
        {
            // Search for image files to fake a manifest from; return in the loop if one is found:
            foreach (string extension in _imageFileExtensions)
            {
                Debug.Assert(extension.StartsWith("."));

                string candidateName = Path.ChangeExtension(manifestPath, extension);

                if (File.Exists(candidateName))
                {
                    PropertyDictionary fileDictionary = new PropertyDictionary();
                    PropertyDictionary masterDictionary = new PropertyDictionary();

                    fileDictionary.SetValueFor("master", masterDictionary);

                    masterDictionary.SetValueFor("fileName", Path.GetFileName(candidateName));

                    return fileDictionary;
                }
            }

            // Nothing was found; complain:
            throw new AdornedReferenceResolutionException(string.Format(
                "The manifest file expected to be at \"{0}\" could not be found, and no images with " +
                "the same name were found.",
                manifestPath));
        }
Example #28
0
        public static Document Deserialize(PropertyDictionary container, string absolutePath)
        {
            PropertyDictionary documentProperties = container.DictionaryFor("pasteUp");

            if (documentProperties == null) return null;

            if (!documentProperties.HasIntegerFor("majorVersion", "majorVersion"))
            {
                throw new DocumentReadingException("The document lacks the required major and minor version numbers.");
            }

            if (documentProperties.IntegerFor("majorVersion") > MajorVersion)
            {
                throw new DocumentReadingException("The document version is not supported by this reader.");
            }

            Document document = new Document();

            if (documentProperties.IntegerFor("majorVersion") == MajorVersion &&
                documentProperties.IntegerFor("minorVersion") > MinorVersion)
            {
                document._wasDownConverted = true;
            }

            DocumentDeserializationContext context = new DocumentDeserializationContext(
                documentProperties.IntegerFor("majorVersion").Value,
                documentProperties.IntegerFor("minorVersion").Value,
                absolutePath);

            PropertyDictionary imageLinkManagerProperties = documentProperties.DictionaryFor("imageLinkManager");

            if (imageLinkManagerProperties == null) throw new DocumentReadingException(
                "The image link manager dictionary is missing from the document.");

            document._imageLinkManager = new ImageLinkManager(context, imageLinkManagerProperties);

            PropertyArray framesArray = documentProperties.ArrayFor("frames");

            if (framesArray == null) throw new DocumentReadingException("The frames array is missing from the document.");

            for (int i = 0; i < framesArray.Count; i++)
            {
                PropertyDictionary frameProperties = framesArray.DictionaryAt(i);

                if (frameProperties == null) throw new DocumentReadingException(
                    "The document frame array contains an invalid non-dictionary object.");

                document._frames.Add(DeserializeFrame(context, frameProperties));
            }

            return document;
        }
 public void HandleRequest(ObviousCode.Interlace.ChatroomServer.Protocols.ChatroomServerProtocol protocol, PropertyDictionary request)
 {
     protocol.Parent.NotifyOnLogout(protocol);
 }
Example #30
0
 public MessageReceivedEventArgs(PropertyDictionary message)
 {
     _message = message;
 }