Esempio n. 1
0
        public static Boolean TryParseI18NString(this JObject JObject, String PropertyKey, out I18NString I18NString)
        {
            if (PropertyKey.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(PropertyKey), "The given property key must not be null or empty!");
            }

            I18NString = I18NString.Empty;
            var jobject = JObject[PropertyKey] as JObject;

            if (jobject == null)
            {
                throw new ArgumentException("The value of the given JSON property '" + PropertyKey + "' is not a JSON object!", nameof(JObject));
            }

            try
            {
                foreach (var jproperty in JObject)
                {
                    I18NString.Add((Languages)Enum.Parse(typeof(Languages), jproperty.Key),
                                   jproperty.Value.Value <String>());
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 2
0
        public static Boolean TryParseI18NString(HTTPRequest HTTPRequest, JObject DescriptionJSON, out I18NString I18N, out HTTPResponse Response)
        {
            if (DescriptionJSON == null)
            {
                I18N = null;

                Response = new HTTPResponseBuilder(HTTPRequest)
                {
                    HTTPStatusCode = HTTPStatusCode.BadRequest,
                    ContentType    = HTTPContentType.JSON_UTF8,
                    Content        = new JObject(new JProperty("description", "Invalid roaming network description!")).ToUTF8Bytes()
                }.AsImmutable();

                return(false);
            }

            Languages Language;
            JValue    Text;

            I18N = I18NString.Empty;

            foreach (var Description in DescriptionJSON)
            {
                if (!Enum.TryParse(Description.Key, out Language))
                {
                    I18N = null;

                    Response = new HTTPResponseBuilder(HTTPRequest)
                    {
                        HTTPStatusCode = HTTPStatusCode.BadRequest,
                        ContentType    = HTTPContentType.JSON_UTF8,
                        Content        = new JObject(new JProperty("description", "Unknown or invalid language definition '" + Description.Key + "'!")).ToUTF8Bytes()
                    }.AsImmutable();

                    return(false);
                }

                Text = Description.Value as JValue;

                if (Text == null)
                {
                    I18N = null;

                    Response = new HTTPResponseBuilder(HTTPRequest)
                    {
                        HTTPStatusCode = HTTPStatusCode.BadRequest,
                        ContentType    = HTTPContentType.JSON_UTF8,
                        Content        = new JObject(new JProperty("description", "Invalid description text!")).ToUTF8Bytes()
                    }.AsImmutable();

                    return(false);
                }

                I18N.Add(Language, Text.Value <String>());
            }

            Response = null;

            return(true);
        }
Esempio n. 3
0
        public static Boolean TryParseI18NString(this JObject JObject, out I18NString I18NString)
        {
            I18NString = I18NString.Empty;

            try
            {
                foreach (var jproperty in JObject)
                {
                    I18NString.Add((Languages)Enum.Parse(typeof(Languages), jproperty.Key),
                                   jproperty.Value.Value <String>());
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /// <summary>
        /// Try to parse the given XML representation of an OICP mobile authorization startes request.
        /// </summary>
        /// <param name="Request">An PullMobileAuthorizationStart request.</param>
        /// <param name="MobileAuthorizationStartXML">The XML to parse.</param>
        /// <param name="MobileAuthorizationStart">The parsed MobileAuthorizationStart request.</param>
        /// <param name="CustomMobileAuthorizationStartParser">A delegate to customize the deserialization of MobileAuthorizationStart responses.</param>
        /// <param name="CustomAddressParser">A delegate to parse custom Address XML elements.</param>
        /// <param name="CustomStatusCodeParser">A delegate to parse custom StatusCode XML elements.</param>
        /// <param name="OnException">An optional delegate called whenever an exception occured.</param>
        public static Boolean TryParse(MobileAuthorizeStartRequest Request,
                                       XElement MobileAuthorizationStartXML,
                                       out MobileAuthorizationStart MobileAuthorizationStart,
                                       CustomXMLParserDelegate <MobileAuthorizationStart> CustomMobileAuthorizationStartParser = null,
                                       CustomXMLParserDelegate <Address> CustomAddressParser       = null,
                                       CustomXMLParserDelegate <StatusCode> CustomStatusCodeParser = null,
                                       OnExceptionDelegate OnException = null)
        {
            try
            {
                if (MobileAuthorizationStartXML.Name != OICPNS.MobileAuthorization + "eRoamingMobileAuthorizationStart")
                {
                    MobileAuthorizationStart = null;
                    return(false);
                }

                var ChargingStationName = new I18NString();
                MobileAuthorizationStartXML.IfValueIsNotNullOrEmpty(OICPNS.MobileAuthorization + "ChargingStationName",
                                                                    v => ChargingStationName.Add(Languages.de, v));
                MobileAuthorizationStartXML.IfValueIsNotNullOrEmpty(OICPNS.MobileAuthorization + "EnChargingStationName",
                                                                    v => ChargingStationName.Add(Languages.en, v));

                var AdditionalInfo = new I18NString();
                MobileAuthorizationStartXML.IfValueIsNotNullOrEmpty(OICPNS.MobileAuthorization + "AdditionalInfo",
                                                                    v => AdditionalInfo.Add(Languages.de, v));
                MobileAuthorizationStartXML.IfValueIsNotNullOrEmpty(OICPNS.MobileAuthorization + "EnAdditionalInfo",
                                                                    v => AdditionalInfo.Add(Languages.en, v));


                MobileAuthorizationStart = new MobileAuthorizationStart(

                    Request,

                    (AuthorizationStatusTypes)Enum.Parse(typeof(AuthorizationStatusTypes),

                                                         MobileAuthorizationStartXML.ElementValueOrFail(OICPNS.MobileAuthorization + "AuthorizationStatus")),

                    MobileAuthorizationStartXML.MapElementOrFail(OICPNS.MobileAuthorization + "GeoCoordinates",
                                                                 XML_IO.ParseGeoCoordinatesXML),

                    ChargingStationName,

                    MobileAuthorizationStartXML.MapElement(OICPNS.MobileAuthorization + "Address",
                                                           (xml, e) => Address.Parse(xml,
                                                                                     CustomAddressParser,
                                                                                     e),
                                                           OnException),

                    MobileAuthorizationStartXML.MapValueOrNullable(OICPNS.MobileAuthorization + "SessionID",
                                                                   Session_Id.Parse),

                    MobileAuthorizationStartXML.MapElement(OICPNS.MobileAuthorization + "StatusCode",
                                                           (xml, e) => OICPv2_2.StatusCode.Parse(xml,
                                                                                                 CustomStatusCodeParser,
                                                                                                 e),
                                                           OnException),

                    MobileAuthorizationStartXML.MapValueOrDefault(OICPNS.MobileAuthorization + "TermsOfUse",
                                                                  s => new I18NString(Languages.unknown, s)),

                    AdditionalInfo);


                if (CustomMobileAuthorizationStartParser != null)
                {
                    MobileAuthorizationStart = CustomMobileAuthorizationStartParser(MobileAuthorizationStartXML,
                                                                                    MobileAuthorizationStart);
                }

                return(true);
            }
            catch (Exception e)
            {
                OnException?.Invoke(DateTime.UtcNow, MobileAuthorizationStartXML, e);

                MobileAuthorizationStart = null;
                return(false);
            }
        }
Esempio n. 5
0
        public static Boolean TryParseI18NString(HTTPRequest HTTPRequest, JObject DescriptionJSON, out I18NString I18N, out HTTPResponse Response)
        {

            if (DescriptionJSON == null)
            {

                I18N     = null;

                Response = new HTTPResponseBuilder(HTTPRequest) {
                               HTTPStatusCode  = HTTPStatusCode.BadRequest,
                               ContentType     = HTTPContentType.JSON_UTF8,
                               Content         = new JObject(new JProperty("description", "Invalid roaming network description!")).ToUTF8Bytes()
                           }.AsImmutable();

                return false;

            }

            Languages  Language;
            JValue     Text;
            I18N      = I18NString.Empty;

            foreach (var Description in DescriptionJSON)
            {

                if (!Enum.TryParse(Description.Key, out Language))
                {

                    I18N = null;

                    Response = new HTTPResponseBuilder(HTTPRequest) {
                                   HTTPStatusCode  = HTTPStatusCode.BadRequest,
                                   ContentType     = HTTPContentType.JSON_UTF8,
                                   Content         = new JObject(new JProperty("description", "Unknown or invalid language definition '" + Description.Key + "'!")).ToUTF8Bytes()
                               }.AsImmutable();

                    return false;

                }

                Text = Description.Value as JValue;

                if (Text == null)
                {

                    I18N = null;

                    Response = new HTTPResponseBuilder(HTTPRequest) {
                                   HTTPStatusCode  = HTTPStatusCode.BadRequest,
                                   ContentType     = HTTPContentType.JSON_UTF8,
                                   Content         = new JObject(new JProperty("description", "Invalid description text!")).ToUTF8Bytes()
                               }.AsImmutable();

                    return false;

                }

                I18N.Add(Language, Text.Value<String>());

            }

            Response = null;

            return true;

        }