Пример #1
0
        public XmlQueryType CheckAverage(QilUnary node)
        {
            XmlQueryType xmlType = node.Child.XmlType;

            CheckNumericXS(node.Child);
            return(XmlQueryTypeFactory.PrimeProduct(xmlType, xmlType.MaybeEmpty ? XmlQueryCardinality.ZeroOrOne : XmlQueryCardinality.One));
        }
Пример #2
0
        /// <summary>
        /// Add "type" to the list of unique types that are used by this query.  Return the index of
        /// the unique type in the list.
        /// </summary>
        public int DeclareXmlType(XmlQueryType type)
        {
            _uniqueXmlTypes ??= new UniqueList <XmlQueryType>();

            XmlQueryTypeFactory.CheckSerializability(type);
            return(_uniqueXmlTypes.Add(type));
        }
Пример #3
0
        //-----------------------------------------------
        // sorting
        //-----------------------------------------------
        public XmlQueryType CheckSort(QilLoop node)
        {
            XmlQueryType varType = node.Variable.Binding.XmlType;

            CheckClassAndNodeType(node[0], typeof(QilIterator), QilNodeType.For);
            CheckClassAndNodeType(node[1], typeof(QilList), QilNodeType.SortKeyList);

            // Sort does not preserve DocOrderDistinct
            return(XmlQueryTypeFactory.PrimeProduct(varType, varType.Cardinality));
        }
Пример #4
0
        /// <summary>
        /// Add "type" to the list of unique types that are used by this query.  Return the index of
        /// the unique type in the list.
        /// </summary>
        public int DeclareXmlType(XmlQueryType type)
        {
            if (this.uniqueXmlTypes == null)
            {
                this.uniqueXmlTypes = new UniqueList <XmlQueryType>();
            }

            XmlQueryTypeFactory.CheckSerializability(type);
            return(this.uniqueXmlTypes.Add(type));
        }
Пример #5
0
        //-----------------------------------------------
        // loops
        //-----------------------------------------------
        public XmlQueryType CheckLoop(QilLoop node)
        {
            CheckClass(node[0], typeof(QilIterator));
            Check(node.Variable.NodeType == QilNodeType.For || node.Variable.NodeType == QilNodeType.Let, node, "Loop variable must be a For or Let iterator");

            XmlQueryType        bodyType     = node.Body.XmlType;
            XmlQueryCardinality variableCard = node.Variable.NodeType == QilNodeType.Let ? XmlQueryCardinality.One : node.Variable.Binding.XmlType.Cardinality;

            // Loops do not preserve DocOrderDistinct
            return(XmlQueryTypeFactory.PrimeProduct(bodyType, variableCard * bodyType.Cardinality));
        }
Пример #6
0
        private XmlQueryType DistinctType(XmlQueryType type)
        {
            if (type.Cardinality == XmlQueryCardinality.More)
            {
                return(XmlQueryTypeFactory.PrimeProduct(type, XmlQueryCardinality.OneOrMore));
            }

            if (type.Cardinality == XmlQueryCardinality.NotOne)
            {
                return(XmlQueryTypeFactory.PrimeProduct(type, XmlQueryCardinality.ZeroOrMore));
            }

            return(type);
        }
Пример #7
0
        //-----------------------------------------------
        // QilReplaceVisitor methods
        //-----------------------------------------------

        /// <summary>
        /// Once children have been replaced, the Xml type is recalculated.
        /// </summary>
        protected virtual void RecalculateType(QilNode node, XmlQueryType oldType)
        {
            XmlQueryType newType;

            newType = f.TypeChecker.Check(node);

            // Note the use of AtMost to account for cases when folding of Error nodes in the graph cause
            // cardinality to be recalculated.
            // For example, (Sequence (TextCtor (Error "error")) (Int32 1)) => (Sequence (Error "error") (Int32 1))
            // In this case, cardinality has gone from More to One
            Debug.Assert(newType.IsSubtypeOf(XmlQueryTypeFactory.AtMost(oldType, oldType.Cardinality)), "Replace shouldn't relax original type");

            node.XmlType = newType;
        }
Пример #8
0
        private XmlQueryType FindFilterType(QilIterator variable, QilNode body)
        {
            XmlQueryType leftType;
            QilBinary    binary;

            if (body.XmlType.TypeCode == XmlTypeCode.None)
            {
                return(XmlQueryTypeFactory.None);
            }

            switch (body.NodeType)
            {
            case QilNodeType.False:
                return(XmlQueryTypeFactory.Empty);

            case QilNodeType.IsType:
                // If testing the type of "variable", then filter type can be restricted
                if (Ref.Equals(((QilTargetType)body).Source, variable))
                {
                    return(XmlQueryTypeFactory.AtMost(((QilTargetType)body).TargetType, variable.Binding.XmlType.Cardinality));
                }
                break;

            case QilNodeType.And:
                // Both And conditions can be used to restrict filter's type
                leftType = FindFilterType(variable, ((QilBinary)body).Left);
                if (leftType != null)
                {
                    return(leftType);
                }

                return(FindFilterType(variable, ((QilBinary)body).Right));

            case QilNodeType.Eq:
                // Restrict cardinality if position($iterator) = $pos is found
                binary = (QilBinary)body;
                if (binary.Left.NodeType == QilNodeType.PositionOf)
                {
                    if (Ref.Equals(((QilUnary)binary.Left).Child, variable))
                    {
                        return(XmlQueryTypeFactory.AtMost(variable.Binding.XmlType, XmlQueryCardinality.ZeroOrOne));
                    }
                }
                break;
            }

            return(null);
        }
Пример #9
0
        public XmlQueryType CheckFilter(QilLoop node)
        {
            CheckClass(node[0], typeof(QilIterator));
            Check(node.Variable.NodeType == QilNodeType.For || node.Variable.NodeType == QilNodeType.Let, node, "Filter variable must be a For or Let iterator");
            CheckXmlType(node.Body, XmlQueryTypeFactory.BooleanX);

            // Attempt to restrict filter's type by checking condition
            XmlQueryType filterType = FindFilterType(node.Variable, node.Body);

            if (filterType != null)
            {
                return(filterType);
            }

            return(XmlQueryTypeFactory.AtMost(node.Variable.Binding.XmlType, node.Variable.Binding.XmlType.Cardinality));
        }
Пример #10
0
        /// <summary>
        /// Create an XmlQueryType that represents the type of "item".
        /// </summary>
        private XmlQueryType CreateXmlType(XPathItem item)
        {
            if (item.IsNode)
            {
                // Rtf
                RtfNavigator rtf = item as RtfNavigator;
                if (rtf != null)
                {
                    return(XmlQueryTypeFactory.Node);
                }

                // Node
                XPathNavigator nav = (XPathNavigator)item;
                switch (nav.NodeType)
                {
                case XPathNodeType.Root:
                case XPathNodeType.Element:
                    if (nav.XmlType == null)
                    {
                        return(XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), XmlSchemaComplexType.UntypedAnyType, false));
                    }

                    return(XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), nav.XmlType, nav.SchemaInfo.SchemaElement.IsNillable));

                case XPathNodeType.Attribute:
                    if (nav.XmlType == null)
                    {
                        return(XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), DatatypeImplementation.UntypedAtomicType, false));
                    }

                    return(XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.New(nav.LocalName, nav.NamespaceURI), nav.XmlType, false));
                }

                return(XmlQueryTypeFactory.Type(nav.NodeType, XmlQualifiedNameTest.Wildcard, XmlSchemaComplexType.AnyType, false));
            }

            // Atomic value
            return(XmlQueryTypeFactory.Type((XmlSchemaSimpleType)item.XmlType, true));
        }
        private XmlQueryType ParseType(string s)
        {
            if (s != null && s.Length > 0)
            {
                Match m = typeInfoRegex.Match(s);
                Debug.Assert(m.Success && m.Groups.Count == 4, "Malformed Type info");

                XmlQueryCardinality qc = new XmlQueryCardinality(m.Groups[1].Value);
                bool strict            = bool.Parse(m.Groups[3].Value);

                string[]       codes = m.Groups[2].Value.Split('|');
                XmlQueryType[] types = new XmlQueryType[codes.Length];

                for (int i = 0; i < codes.Length; i++)
                {
                    types[i] = XmlQueryTypeFactory.Type((XmlTypeCode)Enum.Parse(typeof(XmlTypeCode), codes[i]), strict);
                }

                return(XmlQueryTypeFactory.Product(XmlQueryTypeFactory.Choice(types), qc));
            }
            return(null);
        }
Пример #12
0
        /// <summary>
        /// Deserialize XmlQueryStaticData object from a byte array.
        /// </summary>
        public XmlQueryStaticData(byte[] data, Type[] ebTypes)
        {
            MemoryStream       dataStream = new MemoryStream(data, /*writable:*/ false);
            XmlQueryDataReader dataReader = new XmlQueryDataReader(dataStream);
            int length;

            // Read a format version
            int formatVersion = dataReader.ReadInt32Encoded();

            // Changes in the major part of version are not supported
            if ((formatVersion & ~0xff) > CurrentFormatVersion)
            {
                throw new NotSupportedException();
            }

            // XmlWriterSettings defaultWriterSettings;
            defaultWriterSettings = new XmlWriterSettings(dataReader);

            // IList<WhitespaceRule> whitespaceRules;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.whitespaceRules = new WhitespaceRule[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.whitespaceRules[idx] = new WhitespaceRule(dataReader);
                }
            }

            // string[] names;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.names = new string[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.names[idx] = dataReader.ReadString();
                }
            }

            // StringPair[][] prefixMappingsList;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.prefixMappingsList = new StringPair[length][];
                for (int idx = 0; idx < length; idx++)
                {
                    int length2 = dataReader.ReadInt32();
                    this.prefixMappingsList[idx] = new StringPair[length2];
                    for (int idx2 = 0; idx2 < length2; idx2++)
                    {
                        this.prefixMappingsList[idx][idx2] = new StringPair(dataReader.ReadString(), dataReader.ReadString());
                    }
                }
            }

            // Int32Pair[] filters;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.filters = new Int32Pair[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.filters[idx] = new Int32Pair(dataReader.ReadInt32Encoded(), dataReader.ReadInt32Encoded());
                }
            }

            // XmlQueryType[] types;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.types = new XmlQueryType[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.types[idx] = XmlQueryTypeFactory.Deserialize(dataReader);
                }
            }

            // XmlCollation[] collations;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.collations = new XmlCollation[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.collations[idx] = new XmlCollation(dataReader);
                }
            }

            // string[] globalNames;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.globalNames = new string[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.globalNames[idx] = dataReader.ReadString();
                }
            }

            // EarlyBoundInfo[] earlyBound;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                this.earlyBound = new EarlyBoundInfo[length];
                for (int idx = 0; idx < length; idx++)
                {
                    this.earlyBound[idx] = new EarlyBoundInfo(dataReader.ReadString(), ebTypes[idx]);
                }
            }

            Debug.Assert(formatVersion != CurrentFormatVersion || dataReader.Read() == -1, "Extra data at the end of the stream");
            dataReader.Close();
        }
Пример #13
0
        /// <summary>
        /// Serialize XmlQueryStaticData object into a byte array.
        /// </summary>
        public void GetObjectData(out byte[] data, out Type[] ebTypes)
        {
            MemoryStream       dataStream = new MemoryStream(4096);
            XmlQueryDataWriter dataWriter = new XmlQueryDataWriter(dataStream);

            // First put the format version
            dataWriter.Write7BitEncodedInt(CurrentFormatVersion);

            // XmlWriterSettings defaultWriterSettings;
            _defaultWriterSettings.GetObjectData(dataWriter);

            // IList<WhitespaceRule> whitespaceRules;
            if (_whitespaceRules == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_whitespaceRules.Count);
                foreach (WhitespaceRule rule in _whitespaceRules)
                {
                    rule.GetObjectData(dataWriter);
                }
            }

            // string[] names;
            if (_names == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_names.Length);
                foreach (string name in _names)
                {
                    dataWriter.Write(name);
                }
            }

            // StringPair[][] prefixMappingsList;
            if (_prefixMappingsList == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_prefixMappingsList.Length);
                foreach (StringPair[] mappings in _prefixMappingsList)
                {
                    dataWriter.Write(mappings.Length);
                    foreach (StringPair mapping in mappings)
                    {
                        dataWriter.Write(mapping.Left);
                        dataWriter.Write(mapping.Right);
                    }
                }
            }

            // Int32Pair[] filters;
            if (_filters == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_filters.Length);
                foreach (Int32Pair filter in _filters)
                {
                    dataWriter.Write7BitEncodedInt(filter.Left);
                    dataWriter.Write7BitEncodedInt(filter.Right);
                }
            }

            // XmlQueryType[] types;
            if (_types == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_types.Length);
                foreach (XmlQueryType type in _types)
                {
                    XmlQueryTypeFactory.Serialize(dataWriter, type);
                }
            }

            // XmlCollation[] collations;
            if (_collations == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_collations.Length);
                foreach (XmlCollation collation in _collations)
                {
                    collation.GetObjectData(dataWriter);
                }
            }

            // string[] globalNames;
            if (_globalNames == null)
            {
                dataWriter.Write(0);
            }
            else
            {
                dataWriter.Write(_globalNames.Length);
                foreach (string name in _globalNames)
                {
                    dataWriter.Write(name);
                }
            }

            // EarlyBoundInfo[] earlyBound;
            if (_earlyBound == null)
            {
                dataWriter.Write(0);
                ebTypes = null;
            }
            else
            {
                dataWriter.Write(_earlyBound.Length);
                ebTypes = new Type[_earlyBound.Length];
                int idx = 0;
                foreach (EarlyBoundInfo info in _earlyBound)
                {
                    dataWriter.Write(info.NamespaceUri);
                    ebTypes[idx++] = info.EarlyBoundType;
                }
            }

            dataWriter.Dispose();
            data = dataStream.ToArray();
        }
Пример #14
0
 public XmlQueryType CheckNodeRange(QilBinary node)
 {
     CheckXmlType(node.Left, XmlQueryTypeFactory.NodeNotRtf);
     CheckXmlType(node.Right, XmlQueryTypeFactory.NodeNotRtf);
     return(XmlQueryTypeFactory.Choice(node.Left.XmlType, XmlQueryTypeFactory.ContentS, node.Right.XmlType));
 }
Пример #15
0
 public XmlQueryType CheckAncestorOrSelf(QilUnary node)
 {
     CheckXmlType(node.Child, XmlQueryTypeFactory.NodeNotRtf);
     return(XmlQueryTypeFactory.Choice(node.Child.XmlType, XmlQueryTypeFactory.DocumentOrElementS));
 }
Пример #16
0
 public XmlQueryType CheckDescendantOrSelf(QilUnary node)
 {
     CheckXmlType(node.Child, XmlQueryTypeFactory.NodeNotRtf);
     return(XmlQueryTypeFactory.Choice(node.Child.XmlType, XmlQueryTypeFactory.ContentS));
 }
Пример #17
0
 public XmlQueryType CheckDifference(QilBinary node)
 {
     CheckXmlType(node.Left, XmlQueryTypeFactory.NodeNotRtfS);
     CheckXmlType(node.Right, XmlQueryTypeFactory.NodeNotRtfS);
     return(XmlQueryTypeFactory.AtMost(node.Left.XmlType, node.Left.XmlType.Cardinality));
 }
Пример #18
0
 public XmlQueryType CheckUnion(QilBinary node)
 {
     CheckXmlType(node.Left, XmlQueryTypeFactory.NodeNotRtfS);
     CheckXmlType(node.Right, XmlQueryTypeFactory.NodeNotRtfS);
     return(DistinctType(XmlQueryTypeFactory.Sequence(node.Left.XmlType, node.Right.XmlType)));
 }
Пример #19
0
 //-----------------------------------------------
 // choice
 //-----------------------------------------------
 public XmlQueryType CheckConditional(QilTernary node)
 {
     CheckXmlType(node.Left, XmlQueryTypeFactory.BooleanX);
     return(XmlQueryTypeFactory.Choice(node.Center.XmlType, node.Right.XmlType));
 }
Пример #20
0
        public XmlQueryStaticData(byte[] data, Type[]?ebTypes)
        {
            MemoryStream       dataStream = new MemoryStream(data, writable: false);
            XmlQueryDataReader dataReader = new XmlQueryDataReader(dataStream);
            int length;

            // Read a format version
            int formatVersion = dataReader.Read7BitEncodedInt();

            // Changes in the major part of version are not supported
            if ((formatVersion & ~0xff) > CurrentFormatVersion)
            {
                throw new NotSupportedException();
            }

            // XmlWriterSettings defaultWriterSettings;
            _defaultWriterSettings = new XmlWriterSettings(dataReader);

            // IList<WhitespaceRule> whitespaceRules;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _whitespaceRules = new WhitespaceRule[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _whitespaceRules[idx] = new WhitespaceRule(dataReader);
                }
            }

            // string[] names;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _names = new string[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _names[idx] = dataReader.ReadString();
                }
            }

            // StringPair[][] prefixMappingsList;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _prefixMappingsList = new StringPair[length][];
                for (int idx = 0; idx < length; idx++)
                {
                    int length2 = dataReader.ReadInt32();
                    _prefixMappingsList[idx] = new StringPair[length2];
                    for (int idx2 = 0; idx2 < length2; idx2++)
                    {
                        _prefixMappingsList[idx][idx2] = new StringPair(dataReader.ReadString(), dataReader.ReadString());
                    }
                }
            }

            // Int32Pair[] filters;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _filters = new Int32Pair[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _filters[idx] = new Int32Pair(dataReader.Read7BitEncodedInt(), dataReader.Read7BitEncodedInt());
                }
            }

            // XmlQueryType[] types;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _types = new XmlQueryType[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _types[idx] = XmlQueryTypeFactory.Deserialize(dataReader);
                }
            }

            // XmlCollation[] collations;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _collations = new XmlCollation[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _collations[idx] = new XmlCollation(dataReader);
                }
            }

            // string[] globalNames;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _globalNames = new string[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _globalNames[idx] = dataReader.ReadString();
                }
            }

            // EarlyBoundInfo[] earlyBound;
            length = dataReader.ReadInt32();
            if (length != 0)
            {
                _earlyBound = new EarlyBoundInfo[length];
                for (int idx = 0; idx < length; idx++)
                {
                    _earlyBound[idx] = new EarlyBoundInfo(dataReader.ReadString(), ebTypes ![idx]);
Пример #21
0
 public XmlQueryType CheckXsltCopy(QilBinary node)
 {
     CheckXmlType(node.Left, XmlQueryTypeFactory.NodeNotRtf);
     CheckXmlType(node.Right, XmlQueryTypeFactory.NodeS);
     return(XmlQueryTypeFactory.Choice(node.Left.XmlType, node.Right.XmlType));
 }