Ejemplo n.º 1
0
        private void ReadChunk(BinaryReader br, List <BaseChunk> chunks)
        {
            // no point parsing last 8 bytes as it's either padding or an empty chunk
            if (br.BaseStream.Length - br.BaseStream.Position <= 8)
            {
                return;
            }

            string tag = br.ReadString(4);

            if (TypeLookup.ContainsKey(tag))
            {
                br.BaseStream.Position -= 4;
                var chunk = (BaseChunk)Activator.CreateInstance(TypeLookup[tag], br, Version);

                if (Version == 0 && chunk is VERS vers)
                {
                    Version = vers.Version;
                }

                chunks.Add(chunk);
            }
            else
            {
                throw new Exception($"Unknown type {tag}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Registers custom type mapping
        /// </summary>
        /// <param name="tsType">The TypeScript type</param>
        /// <param name="type">The native type</param>
        /// <returns></returns>
        public Scripter WithTypeMapping(TsType tsType, Type type)
        {
            var typeSymbol = _compilation.GetTypeByMetadataName(type.FullName);

            if (typeSymbol != null)
            {
                if (TypeLookup.ContainsKey(typeSymbol))
                {
                    throw new ArgumentException("Mapping for " + type.Name + " is already defined.", nameof(type));
                }


                TypeLookup[typeSymbol] = tsType;
            }

            return(this);
        }
Ejemplo n.º 3
0
        internal void Parse(INode n, XsdtType x)
        {
            switch (n.NodeType)
            {
            case NodeType.Uri:
                x.NetType      = typeof(Uri);
                x.ValuePart    = n.ToString();
                x.XsdtTypeName = XsdtPrimitiveDataType.XsdtAnyUri;
                ConvertValueToNet(x);
                break;

            case NodeType.Literal:
                ILiteralNode lit = (ILiteralNode)n;

                x.XsdtTypeName = XsdtPrimitiveDataType.XsdtUnknown;
                x.ValuePart    = lit.Value;

                if (lit.DataType != null)
                {
                    if (lit.DataType.ToString().StartsWith(NamespaceMapper.XMLSCHEMA))
                    {
                        x.TypePart = lit.DataType.ToString().Substring(NamespaceMapper.XMLSCHEMA.Length);
                    }
                    else
                    {
                        x.TypePart = "string";
                    }
                }
                else
                {
                    if (TurtleSpecsHelper.IsValidPlainLiteral(lit.Value))
                    {
                        if (TurtleSpecsHelper.IsValidInteger(lit.Value))
                        {
                            x.TypePart = "integer";
                        }
                        else if (TurtleSpecsHelper.IsValidDecimal(lit.Value))
                        {
                            x.TypePart = "decimal";
                        }
                        else if (TurtleSpecsHelper.IsValidDouble(lit.Value))
                        {
                            x.TypePart = "double";
                        }
                        else
                        {
                            x.TypePart = "boolean";
                        }
                    }
                    else
                    {
                        x.TypePart = "string";
                    }
                }

                foreach (int i in Enum.GetValues(typeof(XsdtPrimitiveDataType)))
                {
                    var           result = (XsdtPrimitiveDataType)i;
                    XsdtAttribute attr   = GetXsdtAttrFor(result);
                    if (attr.TypeUri == x.TypePart)
                    {
                        x.XsdtTypeName = result;
                    }
                }

                if (TypeLookup.ContainsKey(x.XsdtTypeName))
                {
                    x.NetType = TypeLookup[x.XsdtTypeName];
                }
                else
                {
                    throw new LinqToRdfException("XsdtTypeConverter does not know how to convert the XML Schema Datatype " + x.TypePart);
                }

                ConvertValueToNet(x);
                break;

            default:
                throw new LinqToRdfException("XsdtTypeConverter can only convert URI and Literal Nodes");
            }
        }
Ejemplo n.º 4
0
 public static bool IsTypeKnown(StringId id)
 {
     return(m_attrTypes.ContainsKey(id) || m_userTypes.ContainsKey(id));
 }
Ejemplo n.º 5
0
        private static bool RegisterTypeToLookup(TypeLookup lookup, string name, int hash, DataType type)
        {
            if (name != null)
            {
                if (hash != -1)
                {
                    // add manual lookup
                    StringHasher.AddToLookup(hash, name);
                }
                else
                {
                    hash = StringHasher.GetHash(name);

                    // try adding this to the lookup
                    if (!StringHasher.CanResolveHash(hash))
                    {
                        Debug.WriteLine($"- Adding '{name}' to lookup");
                        StringHasher.AddToLookup(name);
                    }
                }

                if (!lookup.ContainsKey(hash))
                {
                    lookup.Add(hash, type);
                }

                var id       = name;
                var parentId = "";

                var splitIdx = name.LastIndexOf('.');

                if (splitIdx != -1)
                {
                    parentId = name.Substring(0, splitIdx);
                    id       = name.Substring(splitIdx + 1);

                    StringHasher.AddToLookup(parentId);
                    StringHasher.AddToLookup(id);
                }

                // auto-register accompanying "text_*" string
                if ((type == DataType.StringId) || (type == DataType.PathId))
                {
                    id = $"text_{id}";

                    if (!String.IsNullOrEmpty(parentId))
                    {
                        id = $"{parentId}.{id}";
                    }

                    RegisterTypeToLookup(lookup, id, -1, DataType.String);
                }
            }
            else
            {
                // empty attribute!?
                if (hash == -1)
                {
                    return(false);
                }

                //--var canResolve = StringHasher.CanResolveHash(hash);
                //--
                //--name = (canResolve)
                //--    ? StringHasher.ResolveHash(hash)
                //--    : $"_{hash:X8}";
                //--
                //--if (canResolve)
                //--{
                //--    if (IsTypeKnown(hash))
                //--    {
                //--        var knownType = GetType(hash);
                //--
                //--        //WriteUniqueHint($"<!-- Remove: --><Attribute Hash=\"{attrHash:X8}\" Type=\"{attrType.ToString()}\" /><!-- SameAs --><Attribute Name=\"{name}\" Type=\"{knownType.ToString()}\" />");
                //--    }
                //--    else
                //--    {
                //--        //WriteUniqueHint($"<!-- Rename: --><Attribute Hash=\"{attrHash:X8}\" Type=\"{attrType.ToString()}\" /><!-- EqualTo --><Attribute Name=\"{name}\" Type=\"{attrType.ToString()}\" />");
                //--    }
                //--}

                if (!lookup.ContainsKey(hash))
                {
                    lookup.Add(hash, type);
                }
            }

            return(true);
        }