Пример #1
0
        private static LLSDArray ParseXmlArray(XmlTextReader reader)
        {
            if (reader.NodeType != XmlNodeType.Element || reader.LocalName != "array")
            {
                throw new LLSDException("Expected <array>");
            }

            LLSDArray array = new LLSDArray();

            if (reader.IsEmptyElement)
            {
                reader.Read();
                return(array);
            }

            if (reader.Read())
            {
                while (true)
                {
                    SkipWhitespace(reader);

                    if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "array")
                    {
                        reader.Read();
                        break;
                    }

                    array.Add(ParseXmlElement(reader));
                }
            }

            return(array);
        }
Пример #2
0
        private static LLSD DeserializeNotationArray(StringReader reader)
        {
            int       character;
            LLSDArray llsdArray = new LLSDArray();

            while (((character = PeekAndSkipWhitespace(reader)) > 0) &&
                   ((char)character != arrayEndNotationMarker))
            {
                llsdArray.Add(DeserializeNotationElement(reader));

                character = ReadAndSkipWhitespace(reader);
                if (character < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of array discovered.");
                }
                else if ((char)character == kommaNotationDelimiter)
                {
                    continue;
                }
                else if ((char)character == arrayEndNotationMarker)
                {
                    break;
                }
            }
            if (character < 0)
            {
                throw new LLSDException("Notation LLSD parsing: Unexpected end of array discovered.");
            }

            return((LLSD)llsdArray);
        }
Пример #3
0
        private static void SerializeBinaryArray(MemoryStream stream, LLSDArray llsdArray)
        {
            stream.WriteByte(arrayBeginBinaryMarker);
            byte[] binaryNumElementsHostEnd = HostToNetworkIntBytes(llsdArray.Count);
            stream.Write(binaryNumElementsHostEnd, 0, int32Length);

            foreach (LLSD llsd in llsdArray)
            {
                SerializeBinaryElement(stream, llsd);
            }
            stream.WriteByte(arrayEndBinaryMarker);
        }
Пример #4
0
        private static void SerializeNotationArray(StringWriter writer, LLSDArray llsdArray)
        {
            writer.Write(arrayBeginNotationMarker);
            int lastIndex = llsdArray.Count - 1;

            for (int idx = 0; idx <= lastIndex; idx++)
            {
                SerializeNotationElement(writer, llsdArray[idx]);
                if (idx < lastIndex)
                {
                    writer.Write(kommaNotationDelimiter);
                }
            }
            writer.Write(arrayEndNotationMarker);
        }
Пример #5
0
        private static LLSD ParseBinaryArray(MemoryStream stream)
        {
            int       numElements = NetworkToHostInt(ConsumeBytes(stream, int32Length));
            int       crrElement  = 0;
            LLSDArray llsdArray   = new LLSDArray();

            while (crrElement < numElements)
            {
                llsdArray.Add(ParseBinaryElement(stream));
                crrElement++;
            }

            if (!FindByte(stream, arrayEndBinaryMarker))
            {
                throw new LLSDException("Binary LLSD parsing: Missing end marker in array.");
            }

            return((LLSD)llsdArray);
        }
Пример #6
0
        private static LLSD ParseNotationElement(string notationData, out int endPos)
        {
            if (notationData.Length == 0)
            {
                endPos = 0;
                return(null);
            }

            // Identify what type of object this is
            switch (notationData[0])
            {
            case '!':
                endPos = 1;
                return(new LLSD());

            case '1':
                endPos = 1;
                return(LLSD.FromBoolean(true));

            case '0':
                endPos = 1;
                return(LLSD.FromBoolean(false));

            case 'i':
            {
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromInteger(0));
                }

                int value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromInteger(value));
                }
                else
                {
                    return(LLSD.FromInteger(0));
                }
            }

            case 'r':
            {
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromReal(0d));
                }

                double value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromReal(value));
                }
                else
                {
                    return(LLSD.FromReal(0d));
                }
            }

            case 'u':
            {
                if (notationData.Length < 17)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromUUID(LLUUID.Zero));
                }

                LLUUID value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromUUID(value));
                }
                else
                {
                    return(LLSD.FromUUID(LLUUID.Zero));
                }
            }

            case 'b':
                throw new NotImplementedException("Notation binary type is unimplemented");

            case 's':
            case '"':
            case '\'':
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromString(String.Empty));
                }

                endPos = FindEnd(notationData, 1);
                return(LLSD.FromString(notationData.Substring(1, endPos - 1).Trim(new char[] { '"', '\'' })));

            case 'l':
                throw new NotImplementedException("Notation URI type is unimplemented");

            case 'd':
                throw new NotImplementedException("Notation date type is unimplemented");

            case '[':
            {
                if (notationData.IndexOf(']') == -1)
                {
                    throw new LLSDException("Invalid notation array");
                }

                int       pos   = 0;
                LLSDArray array = new LLSDArray();

                while (notationData[pos] != ']')
                {
                    ++pos;

                    // Advance past comma if need be
                    if (notationData[pos] == ',')
                    {
                        ++pos;
                    }

                    // Allow a single whitespace character
                    if (pos < notationData.Length && notationData[pos] == ' ')
                    {
                        ++pos;
                    }

                    int end;
                    array.Add(ParseNotationElement(notationData.Substring(pos), out end));
                    pos += end;
                }

                endPos = pos + 1;
                return(array);
            }

            case '{':
            {
                if (notationData.IndexOf('}') == -1)
                {
                    throw new LLSDException("Invalid notation map");
                }

                int     pos       = 0;
                LLSDMap hashtable = new LLSDMap();

                while (notationData[pos] != '}')
                {
                    ++pos;

                    // Advance past comma if need be
                    if (notationData[pos] == ',')
                    {
                        ++pos;
                    }

                    // Allow a single whitespace character
                    if (pos < notationData.Length && notationData[pos] == ' ')
                    {
                        ++pos;
                    }

                    if (notationData[pos] != '\'')
                    {
                        throw new LLSDException("Expected a map key");
                    }

                    int endquote = notationData.IndexOf('\'', pos + 1);
                    if (endquote == -1 || (endquote + 1) >= notationData.Length || notationData[endquote + 1] != ':')
                    {
                        throw new LLSDException("Invalid map format");
                    }

                    string key = notationData.Substring(pos, endquote - pos);
                    key  = key.Trim(new char[] { '"', '\'' });    //key.Replace("'", String.Empty);
                    pos += (endquote - pos) + 2;

                    int end;
                    hashtable[key] = ParseNotationElement(notationData.Substring(pos), out end);
                    pos           += end;
                }

                endPos = pos + 1;
                return(hashtable);
            }

            default:
                throw new LLSDException("Unknown notation value type");
            }
        }
Пример #7
0
        public static void SerializeXmlElement(XmlTextWriter writer, LLSD data)
        {
            switch (data.Type)
            {
            case LLSDType.Unknown:
                writer.WriteStartElement(String.Empty, "undef", String.Empty);
                writer.WriteEndElement();
                break;

            case LLSDType.Boolean:
                writer.WriteStartElement(String.Empty, "boolean", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Integer:
                writer.WriteStartElement(String.Empty, "integer", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Real:
                writer.WriteStartElement(String.Empty, "real", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.String:
                writer.WriteStartElement(String.Empty, "string", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.UUID:
                writer.WriteStartElement(String.Empty, "uuid", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Date:
                writer.WriteStartElement(String.Empty, "date", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.URI:
                writer.WriteStartElement(String.Empty, "uri", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Binary:
                writer.WriteStartElement(String.Empty, "binary", String.Empty);
                writer.WriteStartAttribute(String.Empty, "encoding", String.Empty);
                writer.WriteString("base64");
                writer.WriteEndAttribute();
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Map:
                LLSDMap map = (LLSDMap)data;
                writer.WriteStartElement(String.Empty, "map", String.Empty);
                foreach (KeyValuePair <string, LLSD> kvp in map)
                {
                    writer.WriteStartElement(String.Empty, "key", String.Empty);
                    writer.WriteString(kvp.Key);
                    writer.WriteEndElement();

                    SerializeXmlElement(writer, kvp.Value);
                }
                writer.WriteEndElement();
                break;

            case LLSDType.Array:
                LLSDArray array = (LLSDArray)data;
                writer.WriteStartElement(String.Empty, "array", String.Empty);
                for (int i = 0; i < array.Count; i++)
                {
                    SerializeXmlElement(writer, array[i]);
                }
                writer.WriteEndElement();
                break;
            }
        }
Пример #8
0
        private static void SerializeNotationArrayFormatted(StringWriter writer, string intend, LLSDArray llsdArray)
        {
            writer.Write(Helpers.NewLine);
            writer.Write(intend);
            writer.Write(arrayBeginNotationMarker);
            int lastIndex = llsdArray.Count - 1;

            for (int idx = 0; idx <= lastIndex; idx++)
            {
                if (llsdArray[idx].Type != LLSDType.Array && llsdArray[idx].Type != LLSDType.Map)
                {
                    writer.Write(Helpers.NewLine);
                }
                writer.Write(intend + baseIntend);
                SerializeNotationElementFormatted(writer, intend, llsdArray[idx]);
                if (idx < lastIndex)
                {
                    writer.Write(kommaNotationDelimiter);
                }
            }
            writer.Write(Helpers.NewLine);
            writer.Write(intend);
            writer.Write(arrayEndNotationMarker);
        }