Exemplo n.º 1
0
        void WriteElement(Element element)
        {
            if (TypeNames.ContainsValue(element.ClassName))
            {
                throw new CodecException(String.Format("Element {0} uses reserved type name \"{1}\".", element.ID, element.ClassName));
            }
            Writer.WriteTokens(element.ClassName);
            Writer.WriteLine("{");
            Writer.Indent++;
            Writer.WriteTokenLine("id", "elementid", element.ID.ToString());
            Writer.WriteTokenLine("name", "string", element.Name);

            foreach (var attr in element)
            {
                if (attr.Value == null)
                {
                    WriteAttribute(attr.Key, typeof(Element), null, false);
                }
                else
                {
                    WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
                }
            }

            Writer.Indent--;
            Writer.WriteLine("}");
        }
Exemplo n.º 2
0
        public void Encode(Datamodel dm, int encoding_version, Stream stream)
        {
            Writer          = new KV2Writer(stream);
            EncodingVersion = encoding_version;

            Writer.Write(String.Format(CodecUtilities.HeaderPattern, "keyvalues2", encoding_version, dm.Format, dm.FormatVersion));
            Writer.WriteLine();

            Users = new Dictionary <Element, int>();

            if (EncodingVersion >= 9 && dm.PrefixAttributes.Count > 0)
            {
                Writer.WriteTokens("$prefix_element$");
                Writer.WriteLine("{");
                Writer.Indent++;
                foreach (var attr in dm.PrefixAttributes)
                {
                    WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
                }
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            CountUsers(dm.Root);
            WriteElement(dm.Root);
            Writer.WriteLine();

            foreach (var pair in Users.Where(pair => pair.Value > 1))
            {
                if (pair.Key == dm.Root)
                {
                    continue;
                }
                Writer.WriteLine();
                WriteElement(pair.Key);
                Writer.WriteLine();
            }

            Writer.Flush();
        }
Exemplo n.º 3
0
        public void Encode(Datamodel dm, int encoding_version, Stream stream)
        {
            Writer = new KV2Writer(stream);
            EncodingVersion = encoding_version;

            Writer.Write(String.Format(CodecUtilities.HeaderPattern, "keyvalues2", encoding_version, dm.Format, dm.FormatVersion));
            Writer.WriteLine();

            Users = new Dictionary<Element, int>();

            if (EncodingVersion >= 9 && dm.PrefixAttributes.Count > 0)
            {
                Writer.WriteTokens("$prefix_element$");
                Writer.WriteLine("{");
                Writer.Indent++;
                foreach (var attr in dm.PrefixAttributes)
                    WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            CountUsers(dm.Root);
            WriteElement(dm.Root);
            Writer.WriteLine();

            foreach (var pair in Users.Where(pair => pair.Value > 1))
            {
                if (pair.Key == dm.Root)
                    continue;
                Writer.WriteLine();
                WriteElement(pair.Key);
                Writer.WriteLine();
            }

            Writer.Flush();
        }