Пример #1
0
        protected T ReadOptional <T>(string elementName, string elementNamespace, ReadElement <T> readFunction, XmlReader reader)
        {
            if (elementName == null)
            {
                throw new ArgumentNullException(nameof(elementName));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (readFunction == null)
            {
                throw new ArgumentNullException(nameof(readFunction));
            }

            if (!reader.IsStartElement(elementName, elementNamespace))
            {
                return(default(T));
            }
            else
            {
                T result = readFunction.Invoke(reader);
                return(result);
            }
        }
Пример #2
0
        protected T ReadRequired <T>(string elementName, string elementNamespace, ReadElement <T> readFunction, XmlReader reader)
        {
            if (elementName == null)
            {
                throw new ArgumentNullException(nameof(elementName));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (readFunction == null)
            {
                throw new ArgumentNullException(nameof(readFunction));
            }

            if (!reader.IsStartElement(XacmlConstants.ElementNames.ActionAttributeDesignator, this.Version.NamespacePolicy))
            {
                T result = readFunction.Invoke(reader);
                return(result);
            }
            else
            {
                throw new XmlException(elementName + " is required");
            }
        }
 private static void AppendReadInformationIfHas(ReadElement readElement, StringBuilder reading)
 {
     if (readElement.Information != null && !String.IsNullOrWhiteSpace(readElement.Information))
     {
         reading.Append(" (");
         reading.Append(readElement.Information);
         reading.Append(")");
     }
 }
Пример #4
0
        protected internal override void Initialize(ContentTypeReaderManager manager)
        {
            base.Initialize(manager);

            Type baseType = TargetType.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                baseTypeReader = manager.GetTypeReader(baseType);
            }

            constructor = TargetType.GetDefaultConstructor();

            const BindingFlags attrs = (
                BindingFlags.NonPublic |
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly
                );

            /* Sometimes, overridden properties of abstract classes can show up even with
             * BindingFlags.DeclaredOnly is passed to GetProperties. Make sure that
             * all properties in this list are defined in this class by comparing
             * its get method with that of its base class. If they're the same
             * Then it's an overridden property.
             */
            PropertyInfo[] properties = TargetType.GetProperties(attrs);
            FieldInfo[]    fields     = TargetType.GetFields(attrs);
            readers = new List <ReadElement>(fields.Length + properties.Length);

            // Gather the properties.
            foreach (PropertyInfo property in properties)
            {
                MethodInfo pm = property.GetGetMethod(true);
                if (pm == null || pm != pm.GetBaseDefinition())
                {
                    continue;
                }

                ReadElement read = GetElementReader(manager, property);
                if (read != null)
                {
                    readers.Add(read);
                }
            }

            // Gather the fields.
            foreach (FieldInfo field in fields)
            {
                ReadElement read = GetElementReader(manager, field);
                if (read != null)
                {
                    readers.Add(read);
                }
            }
        }
Пример #5
0
        protected internal override void Initialize(ContentTypeReaderManager manager)
        {
            base.Initialize(manager);

            Type baseType = TargetType.BaseType;

            if (baseType != null && baseType != typeof(object))
            {
                baseTypeReader = manager.GetTypeReader(baseType);
            }

            constructor = TargetType.GetDefaultConstructor();

            PropertyInfo[] properties = TargetType.GetAllProperties();
            FieldInfo[]    fields     = TargetType.GetAllFields();
            readers = new List <ReadElement>(fields.Length + properties.Length);

            // Gather the properties.
            foreach (PropertyInfo property in properties)
            {
                ReadElement read = GetElementReader(manager, property);
                if (read != null)
                {
                    readers.Add(read);
                }
            }

            // Gather the fields.
            foreach (FieldInfo field in fields)
            {
                ReadElement read = GetElementReader(manager, field);
                if (read != null)
                {
                    readers.Add(read);
                }
            }
        }
        private void GetRestrictReading(ReadElement readElement, StringBuilder restrictReading)
        {
            restrictReading.Append(readElement.Word);
            restrictReading.Append(" (");
            int j = 0;

            while (true)
            {
                restrictReading.Append(readElement.Retricts[j]);
                j++;
                if (j < readElement.Retricts.Length)
                {
                    restrictReading.Append("; ");
                }
                else
                {
                    break;
                }
            }
            restrictReading.Append(")");

            AppendReadInformationIfHas(readElement, restrictReading);
            restrictReading.Append("\n");
        }
Пример #7
0
 internal protected abstract bool TryCreate(MetaElementType elementType, out ReadElement element);
Пример #8
0
        protected void ReadListAbstract <T1, T2>(ICollection <T1> list, string elementName, string elementNamespace, ReadElement <T2> readFunction, XmlReader reader, bool isRequired = false)
            where T1 : T2
        {
            if (elementName == null)
            {
                throw new ArgumentNullException(nameof(elementName));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (readFunction == null)
            {
                throw new ArgumentNullException(nameof(readFunction));
            }

            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            if (isRequired && !reader.IsStartElement(elementName, elementNamespace))
            {
                throw new XmlException("A least 1 " + elementName + " is required");
            }

            while (reader.IsStartElement(elementName, elementNamespace))
            {
                T1 elem = this.ReadOptionalAbstract <T1, T2>(elementName, elementNamespace, readFunction, reader);
                list.Add(elem);
            }
        }
Пример #9
0
        protected T1 ReadOptionalAbstract <T1, T2>(string elementName, string elementNamespace, ReadElement <T2> readFunction, XmlReader reader)
            where T1 : T2
        {
            if (elementName == null)
            {
                throw new ArgumentNullException(nameof(elementName));
            }

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (readFunction == null)
            {
                throw new ArgumentNullException(nameof(readFunction));
            }

            if (!reader.IsStartElement(elementName, elementNamespace))
            {
                return((T1)default(T2));
            }
            else
            {
                T1 result = (T1)readFunction.Invoke(reader);
                return(result);
            }
        }
Пример #10
0
        void HandleReadData(StanzaIQ Request)
        {
            XElement ReadDataElement = Request.Payload.Elements().First();

            try
            {
                List <ResourceAccess> LimitedAccess = GetAccessRights(Request.From, LWTSD.GetAccessToken(ReadDataElement));

                int       maxpoints   = XmlConvert.ToInt32(ReadDataElement.Attribute("maxpoints").Value);
                int       startindex  = XmlConvert.ToInt32(ReadDataElement.Attribute("startindex").Value);
                SortOrder OrderByTime = SortOrder.None;
                if (ReadDataElement.Attribute("orderedbytime") != null)
                {
                    OrderByTime = SortOrderMethods.LoadFromString(ReadDataElement.Attribute("orderedbytime").Value);
                }

                StanzaIQ Response = new StanzaIQ(StanzaIQ.StanzaIQType.Result);
                Response.ID = Request.ID;
                Response.To = Request.From;

                XElement DataElement = new XElement(LWTSD.Namespace + "data");

                List <Resource> MatchingResources = new List <Resource>();

                foreach (XElement ReadElement in ReadDataElement.Elements(LWTSD.Namespace + "read"))
                {
                    if (ReadElement.Attribute("maxpoints") != null)
                    {
                        if (XmlConvert.ToInt32(ReadElement.Attribute("maxpoints").Value) < 1)
                        {
                            throw new InvalidOperationException("Maxpoints < 0 in read");
                        }
                    }
                    if (ReadElement.Attribute("startindex") != null)
                    {
                        int localstartindex = XmlConvert.ToInt32(ReadElement.Attribute("startindex").Value);
                        if (localstartindex < 1)
                        {
                            throw new InvalidOperationException("startindex < 0 in read");
                        }
                        if (localstartindex > 1)
                        {
                            continue; // We only have one point / resource
                        }
                    }

                    ResourcePath Path = ReadElement.Attribute("resource-path").Value;

                    if (!ResourceAccess.AllowsRead(LimitedAccess, Path))
                    {
                        throw new AccessViolation();
                    }

                    if (!Resources.ContainsKey(Path))
                    {
                        // Todo: Explicit exception to set proper error code
                        throw new Exception("Path does not exist: " + Path);
                    }

                    Resource Res = Resources[Path];
                    if (!Res.SupportsRead)
                    {
                        throw new InvalidOperationException("Resource does not support read: " + Path);
                    }

                    MatchingResources.Add(Res);
                }

                lock (Schema)
                {
                    int Index          = 0;
                    int ReturnedPoints = 0;

                    foreach (Resource Res in MatchingResources)
                    {
                        if (Index < startindex)
                        {
                            Index++;
                            continue;
                        }
                        if ((Index - startindex) >= maxpoints)
                        {
                            Index++;
                            break;
                        }

                        Index++;
                        ReturnedPoints++;

                        XElement ResourceElement = new XElement(LWTSD.Namespace + "resource");
                        ResourceElement.SetAttributeValue("path", Res.Path);
                        ResourceElement.SetAttributeValue("returnedpoints", "1");
                        ResourceElement.SetAttributeValue("totalpoints", "1");
                        XElement PointElement = Res.GetPoint();
                        ResourceElement.Add(PointElement);

                        DataElement.Add(ResourceElement);
                    }

                    DataElement.SetAttributeValue("schemaversion", Schema.GetVersion());
                    DataElement.SetAttributeValue("returnedpoints", XmlConvert.ToString(ReturnedPoints));
                    DataElement.SetAttributeValue("totalpoints", XmlConvert.ToString(MatchingResources.Count));
                }

                foreach (XElement ReSubscribeElement in ReadDataElement.Elements(LWTSD.Namespace + "re-subscribe"))
                {
                    string localsid = ReSubscribeElement.Attribute("subscriptionid").Value;
                    string sid      = GetSafeSubscriptionID(Request.From, localsid);

                    if (!Subscribers.ContainsKey(sid))
                    {
                        throw new InvalidOperationException("Subscription ID does not exist: " + localsid);
                    }

                    Subscriber Subscription = Subscribers[sid];
                    Subscription.LastResubscribed = DateTime.UtcNow;
                }

                Response.Payload.Add(DataElement);
                Uplink.SendStanza(Response);
            }
            catch (System.Exception ex)
            {
                StanzaIQ ErrorResponse = new StanzaIQ(StanzaIQ.StanzaIQType.Error);
                ErrorResponse.ID = Request.ID;
                ErrorResponse.To = Request.From;
                XElement ErroReasonElement = new XElement(LWTSD.Namespace + "errorreason");
                ErroReasonElement.SetAttributeValue("reason", ErrorReason.InvalidData);
                ErroReasonElement.Value = ex.ToString();
                ErrorResponse.Payload.Add(ErroReasonElement);
                Uplink.SendStanza(ErrorResponse);
            }
        }
Пример #11
0
        // TODO: add comments (ez)
        // <! this is a comment !>

        /// <summary>
        /// </summary>
        private static Node FromCGML_0_4_0(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(null);
            }

            Node root = null;

            ReadElement elementType = ReadElement.None;
            bool        reading     = false;
            ReadScope   scope       = ReadScope.Default;

            Node          thisNode = null;
            StringBuilder element  = new StringBuilder();
            char          lastChar = (char)0;

            Func <string> commitElement = new Func <string>(() =>
            {
                string s = element.ToString();
                element.Clear();
                return(s);
            });

            Action commitNodeKey = new Action(() =>
            {
                Node newNode = new Node(commitElement());

                if (root == null)
                {
                    root = newNode;
                }
                else
                {
                    thisNode.Push(newNode);
                }

                thisNode = newNode;
            });

            Action commitAttributeKey = new Action(() =>
            {
                thisNode.Attributes.Set(new Attribute(commitElement()));
                elementType = ReadElement.None;
            });

            Action commitValue = new Action(() =>
            {
                switch (scope)
                {
                case ReadScope.Node:
                    {
                        thisNode.Value = commitElement();

                        break;
                    }

                case ReadScope.Attribute:
                    {
                        thisNode.Attributes[thisNode.Attributes.Count - 1].Value = commitElement();

                        break;
                    }
                }
                elementType = ReadElement.None;
            });

            for (int i = 0; i < str.Length; i++)
            {
                if (i < str.Length)
                {
                    char c        = str[i];
                    char nextChar = i < str.Length - 1 ? str[i + 1] : (char)0;

                    if (reading)
                    {
                        switch (c)
                        {
                        case CGML.STRING_BEGIN_END:
                        {
                            if (lastChar == '\\')
                            {
                                break;
                            }

                            commitValue();
                            reading = false;

                            break;
                        }

                        default:
                        {
                            element.Append(c);
                            break;
                        }
                        }
                    }
                    else
                    {
                        switch (c)
                        {
                        // Ignore newlines
                        case '\n':
                            break;

                        // Ignore carriage return
                        case '\r':
                            break;

                        // Ignore tabs
                        case '\t':
                            break;

                        case CGML.NODE_BEGIN:
                        {
                            scope       = ReadScope.Node;
                            elementType = ReadElement.Key;

                            break;
                        }

                        case CGML.NODE_END:
                        {
                            scope       = ReadScope.Default;
                            elementType = ReadElement.None;

                            break;
                        }

                        case CGML.STRING_BEGIN_END:
                        {
                            elementType = ReadElement.Value;
                            reading     = true;

                            break;
                        }

                        case CGML.EQUAL_OPERATOR:
                        {
                            if (elementType == ReadElement.Key && scope == ReadScope.Attribute)
                            {
                                commitAttributeKey();
                                break;
                            }

                            break;
                        }

                        case ' ':
                        {
                            scope       = ReadScope.Attribute;
                            elementType = ReadElement.Key;

                            break;
                        }

                        case CGML.NODE_ENDMARK:
                        {
                            thisNode = thisNode.Parent;

                            break;
                        }

                        default:
                        {
                            if (elementType != ReadElement.None)
                            {
                                element.Append(c);
                            }

                            break;
                        }
                        }
                    }

                    if (scope == ReadScope.Node && elementType == ReadElement.Key)
                    {
                        if (nextChar == CGML.NODE_END || nextChar == CGML.EQUAL_OPERATOR || nextChar == ' ')
                        {
                            commitNodeKey();
                        }
                    }

                    lastChar = c;
                }
            }

            return(root);
        }