Пример #1
0
        /**
         * <summary>Serialize an object.</summary>
         * <param name="name">The name of the object.</param>
         * <param name="obj">The object to serialize.</param>
         * <returns>The serialized ObjectTag</returns>
         */
        public static ObjectTag Serialize(string name, object obj)
        {
            ObjectTag objectTag = new ObjectTag(name);
            Type      type      = obj.GetType();

            objectTag.AddTag(new StringTag("ODS_TAG", type.FullName));
            BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            foreach (FieldInfo field in type.GetFields(flags))
            {
                if (field.GetCustomAttribute(typeof(ODSSerializeable)) == null)
                {
                    continue;
                }
                objectTag.AddTag(Wrap(field.Name, field.GetValue(obj)));
            }

            if (objectTag.GetValue().Count < 2)
            {
                throw new Exception("Cannot serialize object: No serializable fields detected!");
            }
            return(objectTag);
        }
Пример #2
0
        /**
         * <summary>
         * This method can append, delete, and set tags.
         * <para>A note on keys when appending: <code>ObjectOne.ObjectTwo.tagName</code> When appending data <c>tagName</c> will not be the actual tag name.
         * The tag name written to the file is the name of the specified tag in the value parameter. Any parent objects that do not exist will be created. For example:
         * <code>ObjectOne.ObjectTwo.NewObject.tagName</code> If in the example above <c>NewObject</c> does not exist, than the object will be created with the value tag inside
         * of it. Please see the wiki for a more detailed explanation on this.</para>
         * <para>When value is null, the specified key is deleted. <c>The key MUST exist or an {@link ODSException} will be thrown.</c></para>
         * </summary>
         *
         * <param name="key">
         * The key of the tag to append, delete, or set.
         * <para>When appending the key does not need to exist. ObjectTags that don't exist will be created automatically.</para>
         * <para>When the key is set to "" (An empty string) than it is assumed you want to append to the parent file.</para>
         * <para>Valid Tags:</para>
         *  <code>
         *  <para>ObjectOne.tagToDelete</para>
         *  <para>ObjectOne.NewObject.tagToAppend</para>
         *  <para>ObjectOne.tagToSet</para>
         *  </code>
         * </param>
         *
         * <param name="value">The tag to append or replace the key with. <para>If this parameter is null than the key will be deleted.</para></param>
         *
         */
        public void Set(string key, ITag value)
        {
            if (value == null)
            {
                bool output = Delete(key);
                if (!output)
                {
                    throw new ODSException("The key " + key + " does not exist!");
                }
                return;
            }
            if (key == "")
            {
                Save(new List <ITag>()
                {
                    value
                });
                return;
            }
            byte[] uncompressed = memStream.ToArray();

            KeyScout counter = InternalUtils.ScoutObjectData(uncompressed, key, new KeyScout());

            if (counter.GetEnd() == null)
            {
                if (counter.GetChildren().Count < 1)
                {
                    Append(value);
                    return;
                }
                string existingKey = "";
                foreach (KeyScoutChild child in counter.GetChildren())
                {
                    if (existingKey.Length != 0)
                    {
                        existingKey += ".";
                    }
                    existingKey += child.GetName();
                }
                string newKey = key.Replace(existingKey + ".", "");
                ITag   currentData;
                if (newKey.Split('.').Length > 1)
                {
                    ObjectTag     output = null;
                    ObjectTag     curTag = null;
                    List <string> keys   = new List <string>(newKey.Split('.'));
                    int           i      = 0;
                    foreach (string s in keys)
                    {
                        if (i == 0)
                        {
                            output = new ObjectTag(s);
                            curTag = output;
                        }
                        else if (i == keys.Count - 1)
                        {
                            curTag.AddTag(value);
                        }
                        else
                        {
                            ObjectTag tag = new ObjectTag(s);
                            curTag.AddTag(tag);
                            curTag = tag;
                        }
                        i++;
                    }
                    currentData = output;
                }
                else
                {
                    currentData = value;
                }
                // Actually replace the data and write it to the file.
                MemoryStream    tempStream = new MemoryStream();
                BigBinaryWriter bbw        = new BigBinaryWriter(tempStream);
                currentData.WriteData(bbw);
                bbw.Close();
                byte[] outputArray = InternalUtils.SetSubObjectData(uncompressed, counter, tempStream.ToArray());

                memStream.SetLength(0);
                memStream.Position = 0;
                memStream.Write(outputArray, 0, outputArray.Length);
            }
            else
            {
                MemoryStream    tempStream = new MemoryStream();
                BigBinaryWriter bbw        = new BigBinaryWriter(tempStream);
                value.WriteData(bbw);
                bbw.Close();

                byte[] replaceReturn = InternalUtils.ReplaceSubObjectData(uncompressed, counter, tempStream.ToArray());

                memStream.SetLength(0);
                memStream.Position = 0;
                memStream.Write(replaceReturn, 0, replaceReturn.Length);
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            ObjectDataStructure ods = new ObjectDataStructure(new FileInfo(Directory.GetCurrentDirectory() + @"\test3.ods"), new GZIPCompression());

            // Register a custom tag.
            ODSUtil.RegisterCustomTag(new CustomTag("", ""));

            Console.WriteLine(Directory.GetCurrentDirectory() + "\\test3.ods");

            List <ITag> tags = new List <ITag>();

            tags.Add(new StringTag("ExampleKey", "This is an example string!"));
            tags.Add(new IntTag("ExampleInt", 754));

            ObjectTag car = new ObjectTag("Car");

            car.AddTag(new StringTag("type", "Jeep"));
            car.AddTag(new IntTag("gas", 30));
            List <IntTag> coordsList = new List <IntTag>()
            {
                new IntTag("", 10), new IntTag("", 5), new IntTag("", 10)
            };

            car.AddTag(new ListTag <IntTag>("coords", coordsList));

            ObjectTag owner = new ObjectTag("Owner");

            owner.AddTag(new StringTag("firstName", "Jeff"));
            owner.AddTag(new StringTag("lastName", "Bob"));
            owner.AddTag(new IntTag("Age", 30));
            car.AddTag(owner);

            tags.Add(car);

            tags.Add(new CustomTag("Test", "Test"));

            CompressedObjectTag compressedObjectTag = new CompressedObjectTag("TestCompressedObject");

            compressedObjectTag.AddTag(ODSUtil.Wrap("TestObject", "This is a test!"));
            compressedObjectTag.AddTag(ODSUtil.Wrap("Number", 15));
            compressedObjectTag.AddTag(ODSUtil.Wrap("Decimal", 34.5));
            tags.Add(compressedObjectTag);


            ods.Save(tags);

            ods.Append(new StringTag("Test", "test"));

            ods.GetAll();

            // Loading Example

            StringTag tag = (StringTag)ods.Get("ExampleKey");

            Console.WriteLine("The value of the ExampleKey is: " + tag.GetValue());

            ObjectTag myCar     = (ObjectTag)ods.Get("Car");
            StringTag myCarType = (StringTag)myCar.GetTag("type");

            Console.WriteLine("The car is a " + myCarType.GetValue());

            Console.WriteLine("First Name:");
            StringTag ownerFirstName = (StringTag)ods.Get("Car.Owner.firstName");

            Console.WriteLine("Last Name:");
            StringTag ownerLastName = (StringTag)ods.Get("Car.Owner.lastName");

            Console.WriteLine("The owner of the car is " + ODSUtil.UnWrap(ownerFirstName) + " " + ODSUtil.UnWrap(ownerLastName));
            Console.WriteLine(ods.Find("Car.Owner.firstName"));

            Console.WriteLine(((CustomTag)ods.Get("Test")).GetValue());

            ods.Set("Car.Owner.firstName", new StringTag("firstName", "Example"));
            ods.ReplaceData("Car.Owner.Age", new IntTag("Age", 3));
            Console.WriteLine(ODSUtil.UnWrap((IntTag)ods.Get("Car.Owner.Age")));

            CompressedObjectTag compressedObject = (CompressedObjectTag)ods.Get("TestCompressedObject");

            IntTag numberTag = (IntTag)compressedObject.GetTag("Number");

            Console.WriteLine("Test Compression Number: " + numberTag.GetValue());

            ods.SaveToFile(new FileInfo(@"new_file.ods"), new GZIPCompression());
            //ods.Set("Car.Owner.Age", new IntTag("Age", 3));
        }