Representing the graph context
Inheritance: IScalarUnmarshallingContext
Example #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="outputBuffer"></param>
        /// <param name="translationContext"></param>
        /// <param name="allFieldDescriptors"></param>
        private void SerializeFields(object obj, BinaryWriter outputBuffer, TranslationContext translationContext, IList<FieldDescriptor> allFieldDescriptors)
        {
            if (SimplTypesScope.graphSwitch == SimplTypesScope.GRAPH_SWITCH.ON)
            {
                if (translationContext.NeedsHashCode(obj))
                {
                    WriteSimplIdAttribute(obj, outputBuffer);
                }
            }

            foreach (FieldDescriptor childFd in allFieldDescriptors)
            {
                MemoryStream memoryStreamCollection = new MemoryStream();
                BinaryWriter collectionBuffer = new BinaryWriter(memoryStreamCollection);

                switch (childFd.FdType)
                {
                    case FieldTypes.Scalar:
                        WriteValue(obj, childFd, outputBuffer, translationContext);
                        break;
                    case FieldTypes.CompositeElement:
                        Object compositeObject = childFd.GetObject(obj);
                        FieldDescriptor compositeObjectFieldDescriptor = childFd.IsPolymorphic
                                                                             ? GetClassDescriptor(
                                                                                 compositeObject).PseudoFieldDescriptor
                                                                             : childFd;

                        WriteWrap(childFd, outputBuffer, memoryStreamCollection);
                        Serialize(compositeObject, compositeObjectFieldDescriptor, outputBuffer, translationContext);
                        WriteWrap(childFd, outputBuffer, memoryStreamCollection);
                        break;
                    case FieldTypes.CollectionScalar:
                    case FieldTypes.MapScalar:
                        Object scalarCollectionObject = childFd.GetObject(obj);
                        ICollection scalarCollection = XmlTools.GetCollection(scalarCollectionObject);
                        foreach (Object collectionObject in scalarCollection)
                        {
                            WriteScalarCollectionLeaf(collectionObject, childFd, collectionBuffer, translationContext);
                        }
                        WriteWrap(childFd, outputBuffer, memoryStreamCollection);
                        break;
                    case FieldTypes.CollectionElement:
                    case FieldTypes.MapElement:
                        Object compositeCollectionObject = childFd.GetObject(obj);
                        ICollection compositeCollection = XmlTools.GetCollection(compositeCollectionObject);
                        foreach (Object collectionComposite in compositeCollection)
                        {
                            FieldDescriptor collectionObjectFieldDescriptor = childFd.IsPolymorphic
                                                                                  ? GetClassDescriptor(
                                                                                      collectionComposite).
                                                                                        PseudoFieldDescriptor
                                                                                  : childFd;
                            Serialize(collectionComposite, collectionObjectFieldDescriptor, collectionBuffer,
                                      translationContext);
                        }
                        WriteWrap(childFd, outputBuffer, memoryStreamCollection);
                        break;
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="inputSimplTypesScope"></param>
 /// <param name="inputContext"></param>
 /// <param name="hookStrategy"></param>
 protected PullDeserializer(SimplTypesScope inputSimplTypesScope, TranslationContext inputContext,
     IDeserializationHookStrategy hookStrategy)
 {
     simplTypesScope = inputSimplTypesScope;
     translationContext = inputContext;
     deserializationHookStrategy = hookStrategy;
 }
        public void OnMetadataExtracted(object sender, JSCallbackEventArgs args)
        {
            if (args.Arguments.Length == 0)
            {
                Console.WriteLine("No value returned");
                return;
            }
            JSValue value = args.Arguments[0];

            String metadataJSON = value.ToString();
            //Console.WriteLine(metadataJSON);
            Console.WriteLine("Extraction time : " + DateTime.Now.Subtract(parseStart).TotalMilliseconds);

            Console.WriteLine("Done getting value. Serializing JSON string to ElementState.");
            TranslationContext context = new TranslationContext();
            context.SetUriContext(_puri);
            SimplTypesScope metadataTScope = SemanticsSessionScope.MetadataTranslationScope;
            Document myShinyNewMetadata = (Document)metadataTScope.Deserialize(metadataJSON, context,  new MetadataDeserializationHookStrategy(SemanticsSessionScope), StringFormat.Json);
            Console.WriteLine("Extraction time including page load and deserialization: " + DateTime.Now.Subtract(timeStart).TotalMilliseconds);
            _webView.LoadCompleted -= WebView_LoadCompleted;

            SemanticsSessionScope.GlobalDocumentCollection.AddDocument(myShinyNewMetadata, _puri);

            SemanticsSessionScope.WebBrowserPool.Release(_webView);
            _requestTimedOut.Stop();

            _closure.TaskCompletionSource.TrySetResult(myShinyNewMetadata);
        }
Example #4
0
        public override string Marshall(object instance, TranslationContext context = null)
        {
            Color color = instance is Color ? (Color) instance : new Color();
            var result = color.ToString();
            if (color.A == 255)
                result = result.Remove(1, 2);

            return result;
        }
Example #5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="textWriter"></param>
 /// <param name="translationContext"></param>
 private static void WriteSimplIdAttribute(object obj, TextWriter textWriter, TranslationContext translationContext)
 {
     textWriter.Write(' ');
     textWriter.Write(TranslationContext.SimplId);
     textWriter.Write('=');
     textWriter.Write('"');
     textWriter.Write(translationContext.GetSimplId(obj));
     textWriter.Write('"');
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="textWriter"></param>
        /// <param name="translationContext"></param>
        /// <param name="fd"></param>
        private void SerializeComposite(object obj, TextWriter textWriter, TranslationContext translationContext,
            FieldDescriptor fd)
        {
            Object compositeObject = fd.GetObject(obj);
            FieldDescriptor compositeAsScalarFd = GetClassDescriptor(compositeObject).ScalarValueFieldDescripotor;

            if (compositeAsScalarFd != null)
            {
                WriteBibtexAttribute(compositeObject, fd, textWriter, translationContext);
            }
        }
Example #7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="binaryWriter"></param>
 /// <param name="translationContext"></param>
 public override void Serialize(object obj, BinaryWriter binaryWriter, TranslationContext translationContext)
 {
     translationContext.ResolveGraph(obj);
     ClassDescriptor rootObjectClassDescriptor = ClassDescriptor.GetClassDescriptor(obj.GetType());
     try
     {
         Serialize(obj, rootObjectClassDescriptor.PseudoFieldDescriptor, binaryWriter, translationContext);
     }
     catch (Exception ex)
     {
         throw new SimplTranslationException("IO exception occured: ", ex);
     }
 }
Example #8
0
        public override string Marshall(object instance, TranslationContext context = null)
        {
            if (context != null && context.BaseUri != null)
            {
                var uri = instance as ParsedUri;
                if (uri != null)
                {
                    var relativeUri = context.BaseUri.MakeRelativeUri(uri);
                    return relativeUri.ToString();
                }
            }

            return ((ParsedUri) instance).ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="rootObjectFieldDescriptor"></param>
        /// <param name="textWriter"></param>
        /// <param name="translationContext"></param>
        private void Serialize(object obj, FieldDescriptor rootObjectFieldDescriptor, TextWriter textWriter,
            TranslationContext translationContext)
        {
            SerializationPreHook(obj, translationContext);

            WriteObjectStart(rootObjectFieldDescriptor, textWriter);

            IEnumerable<FieldDescriptor> allFieldDescriptors = GetClassDescriptor(obj).AllFieldDescriptors;

            SerializeFields(obj, textWriter, translationContext, allFieldDescriptors.ToList());

            WriteClose(textWriter);

            SerializationPostHook(obj, translationContext);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="pSimplTypesScope"></param>
 /// <param name="pTranslationContext"></param>
 /// <param name="pDeserializationHookStrategy"></param>
 /// <param name="format"></param>
 /// <returns></returns>
 public static PullDeserializer GetPullDeserializer(SimplTypesScope pSimplTypesScope, TranslationContext pTranslationContext, IDeserializationHookStrategy pDeserializationHookStrategy, Format format)
 {
     switch (format)
     {
         case Format.Xml:
             return new XmlPullDeserializer(pSimplTypesScope, pTranslationContext, pDeserializationHookStrategy);
         case Format.Json:
             return new JsonPullDeserializer(pSimplTypesScope, pTranslationContext, pDeserializationHookStrategy);
         case Format.Bibtex:
             throw new SimplTranslationException("bibtex serialization is not supported");
         case Format.Tlv:
             return new TlvPullDeserializer(pSimplTypesScope, pTranslationContext, pDeserializationHookStrategy);
         default:
             throw new SimplTranslationException(format + "format not supported");
     }
 }
Example #11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="textWriter"></param>
        /// <param name="translationContext"></param>
        public override void Serialize(object obj, TextWriter textWriter, TranslationContext translationContext)
        {
            translationContext.ResolveGraph(obj);
            ClassDescriptor rootObjectClassDescriptor = ClassDescriptor.GetClassDescriptor(obj.GetType());
            try
            {
                WriteStart(textWriter);

                Serialize(obj, rootObjectClassDescriptor.PseudoFieldDescriptor, textWriter, translationContext, true);

                WriteClose(textWriter);
            }
            catch (Exception ex)
            {
                throw new SimplTranslationException("IO exception occured: ", ex);
            }
        }
Example #12
0
        private static void SerializeScalar(object obj, FieldDescriptor fd, TextWriter textWriter, TranslationContext translationContext)
        {
            // check wether we need quotation marks to surround the value.
            bool needQuotationMarks = true;
            ScalarType st = fd.ScalarType;
            if (st != null)
            {
                needQuotationMarks = st.NeedsJsonQuotationWrap();
            }

            textWriter.Write('"');
            textWriter.Write(fd.TagName);
            textWriter.Write('"');
            textWriter.Write(':');
            if (needQuotationMarks)
                textWriter.Write('"');
            fd.AppendValue(textWriter, obj, translationContext, Format.Json);
            if (needQuotationMarks)
                textWriter.Write('"');
        }
Example #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="rootObjectFieldDescriptor"></param>
        /// <param name="binaryWriter"></param>
        /// <param name="translationContext"></param>
        private void Serialize(object obj, FieldDescriptor rootObjectFieldDescriptor, BinaryWriter binaryWriter,
            TranslationContext translationContext)
        {
            if (AlreadySerialized(obj, translationContext))
            {
                WriteSimplRef(obj, rootObjectFieldDescriptor, binaryWriter);
                return;
            }

            translationContext.MapObject(obj);

            SerializationPreHook(obj, translationContext);
            ClassDescriptor rootObjectClassDescriptor = GetClassDescriptor(obj);

            MemoryStream bufferMemoryStream = new MemoryStream();
            BinaryWriter outputBuffer = new BinaryWriter(bufferMemoryStream);

            IEnumerable<FieldDescriptor> allFieldDescriptors = GetClassDescriptor(obj).AllFieldDescriptors;

            SerializeFields(obj, outputBuffer, translationContext, allFieldDescriptors.ToList());
            WriteHeader(binaryWriter, bufferMemoryStream, rootObjectFieldDescriptor.TlvId);
            SerializationPostHook(obj, translationContext);
        }
Example #14
0
 public abstract override string Marshall(object instance, TranslationContext context = null);
Example #15
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fd"></param>
        /// <param name="outputBuffer"></param>
        /// <param name="translationContext"></param>
        private void WriteValue(object obj, FieldDescriptor fd, BinaryWriter outputBuffer, TranslationContext translationContext)
        {
            if (!fd.IsDefaultValueFromContext(obj))
            {
                outputBuffer.Write(fd.TlvId);

                StringBuilder value = new StringBuilder();
                StringWriter valueWriter = new StringWriter(value);

                fd.AppendValue(valueWriter, obj, translationContext, Format.Tlv);

                MemoryStream temp = new MemoryStream();
                BinaryWriter tempStream = new BinaryWriter(temp);
                tempStream.Write(value.ToString().ToCharArray());

                outputBuffer.Write((int)temp.Length);
                temp.WriteTo(outputBuffer.BaseStream);
            }
        }
Example #16
0
 public void SetFieldToScalar(object root, string value, TranslationContext translationContext)
 {
     if (ScalarType != null && !ScalarType.IsMarshallOnly)
     {
         ScalarType.SetField(root, field, value, dataFormat, translationContext);
     }
 }
Example #17
0
 public void AppendValue(TextWriter textWriter, object obj, TranslationContext translationContext, Format format)
 {
     ScalarType.AppendValue(textWriter, this, obj, format, translationContext);
 }
Example #18
0
 public void AppendCollectionScalarValue(TextWriter textWriter, object obj, TranslationContext translationContext,
                                         Format format)
 {
     ScalarType.AppendValue(obj, textWriter, !IsCdata, format);
 }
Example #19
0
        public void AddLeafNodeToCollection(object root, string value, TranslationContext translationContext)
        {
            if(String.IsNullOrEmpty(value))
            {
                return;
            }

            if(ScalarType != null)
            {
                Object typeConvertedValue = ScalarType.GetInstance(value, dataFormat, translationContext);

                if(typeConvertedValue != null)
                {
                    IList collection = (IList) AutomaticLazyGetCollectionOrMap(root);
                    collection.Add(typeConvertedValue);
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="stream"></param>
 /// <param name="translationContext"></param>
 public abstract void Serialize(Object obj, Stream stream,
     TranslationContext translationContext);
        public override void DeserializationPostHook(TranslationContext translationContext)
        {
            int typeCode = this.GetFieldType();
              if (typeCode == FieldTypes.CollectionScalar || this.inheritDone)
            return;

              String childCompositeName = ChildType ?? UNRESOLVED_NAME;
              MetaMetadataCollectionField thisField = this;
              var composite = new MetaMetadataCompositeField(childCompositeName, Kids);

              composite.TypeChangeListener += (newType) => this.childType = newType;
              composite.ExtendsChangeListener += (newExtends) => this.childExtends = newExtends;
              composite.TagChangeListener += (newTag) => this.childTag = newTag;

              composite.Parent = this;
              composite.Type = childType;
              composite.ExtendsAttribute = this.childExtends;
              kids.Clear();
              kids.Add(composite.Name, composite);
              composite.PromoteChildren = this.PromoteChildren;
        }
Example #22
0
 private void WriteCollectionScalar(object obj, FieldDescriptor fd, TextWriter textWriter, TranslationContext translationContext)
 {
     if (fd.ScalarType.NeedsJsonQuotationWrap())
         textWriter.Write('"');
     fd.AppendCollectionScalarValue(textWriter, obj, translationContext, Format.Json);
     if (fd.ScalarType.NeedsJsonQuotationWrap())
         textWriter.Write('"');
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="inputSimplTypesScope"></param>
 /// <param name="inputContext"></param>
 /// <param name="deserializationHookStrategy"></param>
 protected BinaryPullDeserializer(SimplTypesScope inputSimplTypesScope, TranslationContext inputContext,
     IDeserializationHookStrategy deserializationHookStrategy)
     : base(inputSimplTypesScope, inputContext, deserializationHookStrategy)
 {
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="textWriter"></param>
 /// <param name="translationContext"></param>
 public abstract void Serialize(object obj, BinaryWriter textWriter, TranslationContext translationContext);
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="stream"></param>
 /// <param name="translationContext"></param>
 public override void Serialize(object obj, Stream stream, TranslationContext translationContext)
 {
     Serialize(obj, new BinaryWriter(stream), translationContext);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="inputSimplTypesScope"></param>
 /// <param name="inputContext"></param>
 protected BinaryPullDeserializer(SimplTypesScope inputSimplTypesScope, TranslationContext inputContext)
     : base(inputSimplTypesScope, inputContext)
 {
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="translationContext"></param>
 protected void SerializationPreHook(Object obj, TranslationContext translationContext)
 {
     if (obj is ISimplSerializationPre)
     {
         ((ISimplSerializationPre) obj).SerializationPreHook(translationContext);
     }
 }
Example #28
0
 public override string Marshall(object instance, TranslationContext context = null)
 {
     return ((FileInfo)instance).Name;
 }
 protected Boolean AlreadySerialized(Object obj, TranslationContext translationContext)
 {
     return SimplTypesScope.graphSwitch == SimplTypesScope.GRAPH_SWITCH.ON
            && translationContext.AlreadyMarshalled(obj);
 }
Example #30
0
 public override string Marshall(object instance, TranslationContext context = null)
 {
     return (string) instance;
 }