示例#1
0
            private static string FormatValue(object v)
            {
                if (v == null)
                {
                    return("null");
                }
                if (v is bool)
                {
                    return((bool)v ? "true" : "false");
                }
                var s = v as string;

                if (s != null)
                {
                    return(Escaper.Escape(s, true));
                }
                var c = v as IConvertible;

                if (c != null)
                {
                    double d = c.ToDouble(null);
                    return(d.ToString(CultureInfo.InvariantCulture));
                }
                return(v.ToString());
            }
示例#2
0
        public static Status FromProperties(IDictionary <string, object> statusProperties)
        {
            object objectCode    = statusProperties[Code];
            object objectMessage = statusProperties[Message];
            object objectState   = statusProperties[State];

            if (objectCode == null && objectMessage == null && objectState == null)
            {
                return(null);
            }
            StatusBuilder statusBuilder = new StatusBuilder();

            if (objectCode != null)
            {
                statusBuilder.Code = Escaper.Escape(objectCode.ToString());
            }
            if (objectMessage != null)
            {
                statusBuilder.Message = objectMessage.ToString();
            }
            if (objectState != null)
            {
                statusBuilder.State = objectState.ToString();
            }
            return(statusBuilder.Build());
        }
示例#3
0
        internal static string ToString(object value)
        {
            if (value == null)
            {
                return("null");
            }

            var tc = System.Type.GetTypeCode(value.GetType());

            switch (tc)
            {
            case TypeCode.Object:
            {
                var c = value as IAbcConst;
                if (c != null)
                {
                    if (c.Kind == AbcConstKind.String)
                    {
                        string s = (string)c.Value;
                        if (s != null)
                        {
                            return(Escaper.Escape(s));
                        }
                        return("null");
                    }
                    return(c.ToString());
                }
                var arr = value as int[];
                if (arr != null)
                {
                    return(ToStringIntArray(arr));
                }
                return(value.ToString());
            }

            case TypeCode.Boolean:
                return((bool)value ? "true" : "false");

            case TypeCode.Char:
                return(Escaper.Escape(((char)value).ToString(), false));

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.DateTime:
                return(value.ToString());

            case TypeCode.String:
                return(Escaper.Escape((string)value));
            }
            return(string.Empty);
        }
示例#4
0
 static void AppendValue(StringBuilder sb, object value)
 {
     if (value != null)
     {
         string str = value as string;
         if (str != null)
         {
             sb.Append(Escaper.Escape(str));
         }
         else if (value is bool)
         {
             sb.Append((bool)value ? "true" : "false");
         }
         else
         {
             var offsets = value as int[];
             if (offsets != null)
             {
                 for (int i = 0; i < offsets.Length; ++i)
                 {
                     if (i > 0)
                     {
                         sb.Append(", ");
                     }
                     sb.Append(offsets[i]);
                 }
             }
             else
             {
                 sb.Append(value);
             }
         }
     }
 }
        private XmlNode SerializeToXmlElement(SettingsProperty setting, SettingsPropertyValue value)
        {
            XmlDocument doc      = new XmlDocument();
            XmlElement  valueXml = doc.CreateElement(nameof(value));

            string serializedValue = value.SerializedValue as string;

#pragma warning disable CS0618 // Type or member is obsolete
            if (serializedValue == null && setting.SerializeAs == SettingsSerializeAs.Binary)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                // SettingsPropertyValue returns a byte[] in the binary serialization case. We need to
                // encode this - we use base64 since SettingsPropertyValue understands it and we won't have
                // to special case while deserializing.

                byte[] buffer = value.SerializedValue as byte[];
                if (buffer != null)
                {
                    serializedValue = Convert.ToBase64String(buffer);
                }
            }

            if (serializedValue == null)
            {
                serializedValue = string.Empty;
            }

            // We need to escape string serialized values
            if (setting.SerializeAs == SettingsSerializeAs.String)
            {
                serializedValue = Escaper.Escape(serializedValue);
            }

            valueXml.InnerXml = serializedValue;

            // Hack to remove the XmlDeclaration that the XmlSerializer adds.
            XmlNode unwanted = null;
            foreach (XmlNode child in valueXml.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.XmlDeclaration)
                {
                    unwanted = child;
                    break;
                }
            }
            if (unwanted != null)
            {
                valueXml.RemoveChild(unwanted);
            }

            return(valueXml);
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="current"></param>
        public override XElement WriteToXml(XElement current)
        {
            current       = current ?? new XElement("value", new XAttribute(JsonTypeAttributeName, Type));
            current.Value = "[REPLACE]";
            var value   = Escaper.Escape(ToString(false), EscapingType.XmlAttribute);
            var result  = current.ToString().Replace("[REPLACE]", value);
            var resulte = XElement.Parse(result);

            if (null != current.Parent)
            {
                current.ReplaceWith(resulte);
            }
            return(resulte);
        }
        public static Entry FromProperties(IDictionary <string, object> entryProperties)
        {
            const bool     isRequired   = true;
            IList <string> path         = getPath(entryProperties, isRequired);
            long           timestamp    = getTimestamp(entryProperties, isRequired);
            EntryBuilder   entryBuilder = new EntryBuilder(path, timestamp);

            object objectValue = entryProperties[Value];

            if (objectValue != null)
            {
                try
                {
                    double value = Convert.ToDouble(objectValue.ToString());
                    entryBuilder.Value = value;
                }
                catch (System.FormatException nfe)
                {
                    throw new NeotysAPIException(NeotysAPIException.ErrorType.NL_API_INVALID_ARGUMENT, "Invalid entry value: " + objectValue, nfe);
                }
            }

            object objectUrl = entryProperties[Url];

            if (objectUrl != null)
            {
                entryBuilder.Url = objectUrl.ToString();
            }
            object objectUnit = entryProperties[Unit];

            if (objectUnit != null)
            {
                entryBuilder.Unit = Escaper.Escape(objectUnit.ToString());
            }
            object objectStatus = entryProperties[Statuses.ElementName];

            if (objectStatus != null)
            {
                if (!(objectStatus is IDictionary <string, object>))
                {
                    throw new NeotysAPIException(NeotysAPIException.ErrorType.NL_API_INVALID_ARGUMENT, "Invalid entry status: " + objectStatus);
                }
                Status status = Statuses.FromProperties((IDictionary <string, object>)objectStatus);
                if (status != null)
                {
                    entryBuilder.Status = status;
                }
            }
            return(entryBuilder.Build());
        }
        public static IList <string> PathStringToList(string pathString, char separator)
        {
            if (System.String.IsNullOrEmpty(pathString))
            {
                throw new System.ArgumentException("Path is null or empty.");
            }
            IList <string> pathList = new List <string>();

            foreach (String pathElement in new List <string>(pathString.Split(new char[] { separator })))
            {
                pathList.Add(Escaper.Escape(pathElement));
            }
            return(pathList);
        }
        private XmlNode SerializeToXmlElement(SettingsProperty setting, SettingsPropertyValue value)
        {
            XmlDocument doc      = new XmlDocument();
            XmlElement  valueXml = doc.CreateElement("value");

            string serializedValue = value.SerializedValue as string;

            if (serializedValue == null && setting.SerializeAs == SettingsSerializeAs.Binary)
            {
                // SettingsPropertyValue returns a byte[] in the binary serialization case. We need to
                // encode this - we use base64 since SettingsPropertyValue understands it and we won't have
                // to special case while deserializing.
                byte[] buf = value.SerializedValue as byte[];
                if (buf != null)
                {
                    serializedValue = Convert.ToBase64String(buf);
                }
            }

            if (serializedValue == null)
            {
                serializedValue = String.Empty;
            }

            // We need to escape string serialized values
            if (setting.SerializeAs == SettingsSerializeAs.String)
            {
                serializedValue = Escaper.Escape(serializedValue);
            }

            valueXml.InnerXml = serializedValue;

            XmlNode unwanted = null;

            foreach (XmlNode child in valueXml.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.XmlDeclaration)
                {
                    unwanted = child;
                    break;
                }
            }
            if (unwanted != null)
            {
                valueXml.RemoveChild(unwanted);
            }

            return(valueXml);
        }
示例#10
0
        public static Status NewStatus(string code, string message, string state)
        {
            StatusBuilder statusBuilder = new StatusBuilder();

            if (!System.String.IsNullOrEmpty(code))
            {
                statusBuilder.Code = Escaper.Escape(code);
            }
            if (!System.String.IsNullOrEmpty(message))
            {
                statusBuilder.Message = Escaper.Escape(message);
            }
            if (!System.String.IsNullOrEmpty(state))
            {
                statusBuilder.State = state;
            }
            return(statusBuilder.Build());
        }
示例#11
0
        /// <summary>
        /// The status is set to {@code Status.State.FAIL} if {@code exception} is not null. The message is set to the message
        /// from the exception if there is one. </summary>
        /// <param name="code"> </param>
        /// <param name="exception">
        /// @return </param>
        public static Status NewStatus(string code, Exception exception)
        {
            StatusBuilder statusBuilder = new StatusBuilder();

            if (!System.String.IsNullOrEmpty(code))
            {
                statusBuilder.Code = Escaper.Escape(code);
            }
            if (exception != null)
            {
                statusBuilder.State   = Status.State.Fail;
                statusBuilder.Message = exception.Message;
            }
            else
            {
                statusBuilder.State = Status.State.Pass;
            }
            return(statusBuilder.Build());
        }
        private XmlNode SerializeToXmlElement(SettingsProperty setting, SettingsPropertyValue value)
        {
            XmlElement element         = new XmlDocument().CreateElement("value");
            string     serializedValue = value.SerializedValue as string;

            if ((serializedValue == null) && (setting.SerializeAs == SettingsSerializeAs.Binary))
            {
                byte[] inArray = value.SerializedValue as byte[];
                if (inArray != null)
                {
                    serializedValue = Convert.ToBase64String(inArray);
                }
            }

            if (serializedValue == null)
            {
                serializedValue = string.Empty;
            }

            if (setting.SerializeAs == SettingsSerializeAs.String)
            {
                serializedValue = Escaper.Escape(serializedValue);
            }

            element.InnerXml = serializedValue;
            XmlNode oldChild = null;

            foreach (XmlNode node in element.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.XmlDeclaration)
                {
                    oldChild = node;
                    break;
                }
            }

            if (oldChild != null)
            {
                element.RemoveChild(oldChild);
            }

            return(element);
        }
        public static Context FromProperties(IDictionary <string, object> contextProperties)
        {
            ContextBuilder contextBuilder = new ContextBuilder();
            object         objectHardware = contextProperties[Hardware];

            if (objectHardware != null)
            {
                contextBuilder.Hardware = Escaper.Escape(objectHardware.ToString());
            }
            object objectOs = contextProperties[Os];

            if (objectOs != null)
            {
                contextBuilder.Os = Escaper.Escape(objectOs.ToString());
            }
            object objectSoftware = contextProperties[Software];

            if (objectSoftware != null)
            {
                contextBuilder.Software = Escaper.Escape(objectSoftware.ToString());
            }
            object objectLocation = contextProperties[Location];

            if (objectLocation != null)
            {
                contextBuilder.Location = Escaper.Escape(objectLocation.ToString());
            }
            object objectScript = contextProperties[Script];

            if (objectScript != null)
            {
                contextBuilder.Script = Escaper.Escape(objectScript.ToString());
            }
            object objectInstanceId = contextProperties[InstanceId];

            if (objectInstanceId != null)
            {
                contextBuilder.InstanceId = Escaper.Escape(objectInstanceId.ToString());
            }
            return(contextBuilder.build());
        }
 private static string Escape(string @string)
 {
     return(Escaper.Escape(@string));
 }