コード例 #1
0
        public string GetText(ObjectId id, TextSettings settings)
        {
            // read the file json of the text generator file
            string filePath = settings.PathTextGeneratorProvider + settings.Culture.Name + ".json";
            string generatedText;

            if (File.Exists(filePath))
            {
                var parsedObject = JSONPref.ParseJSON(System.IO.File.ReadAllText(filePath));

                // get only the translator sections in JSON
                string translator = parsedObject[CommonVariables.KEY_JSON_TRANSLATOR_FILE_TRANSLATOR].ToString();

                // deserialize the JSON section
                var obj = JsonConvert.DeserializeObject <IDictionary <string, object> >(translator);
                generatedText = (obj.ContainsKey(id.Id))? (string)obj[id.Id]:"[WARNING] The key " + id.Id + " does not exist in the translator file " + settings.Culture.Name + ".json";
            }
            else
            {
                throw new FileNotFoundException("The translator file " + filePath + " does not exist. It is required when we use the text generation from a FILE. Basically it is a translator <key, value>");
            }

            // return the text of the given id
            return(generatedText);
        }
コード例 #2
0
        /**
         * Create the ITemplate
         *
         * @param id of the template
         */
        private ITemplate CreateTemplate(object KeyTemplate)
        {
            // read and parse the JSON file
            var parsedObject = JSONPref.ParseJSON(System.IO.File.ReadAllText(File));

            // each template has information about their regions
            // we store template's regions and region's products in memory
            StoreRegionsAndProducts((string)parsedObject[CommonVariables.KEY_JSON_TEMPLATES_REGIONS]);



            // from here we known that the JSON exists and it is well format

            // get only the template sections in JSON
            string templatesConfJson = parsedObject[CommonVariables.KEY_JSON_TEMPLATES_CONF].ToString(Formatting.None);

            // deserialize the JSON section
            var obj = JsonConvert.DeserializeObject <IDictionary <string, object>[]>(templatesConfJson);

            // retrieve the template attributes
            IDictionary <string, object> templateAttributes = obj.Where(i => i[CommonVariables.KEY_JSON_TEMPLATES_NAME].Equals(KeyTemplate)).First();

            // create the ITemplate as a Sequence
            Sequence sequence = new(templateAttributes, TextGeneratorProvider);

            // create the list of regions
            List <ITemplate> listRegions = sequence.Regions;

            CreateListOfRegions(parsedObject, KeyTemplate, ref listRegions);

            // create the list of "TemplatesAudienceTargets"
            sequence.Audience = CreateListOfAudienceTargets(parsedObject, KeyTemplate);

            return(sequence);
        }
コード例 #3
0
        /**
         * Store the regions of the templates
         *
         * @param full path of the JSON file that contains the regions
         */
        private void StoreRegionsAndProducts(string fileRegions)
        {
            string strInput = fileRegions;
            string jsonOutput;

            // if it is a path, we read it
            if (fileRegions.Contains(".json"))
            {
                strInput = System.IO.File.ReadAllText(fileRegions);
            }

            // parse the content of the JSON file
            var parsedRegions = JSONPref.ParseJSON(strInput);


            // get the information about the regions
            jsonOutput          = parsedRegions.GetValue(CommonVariables.KEY_JSON_TEMPLATES_REGIONS).ToString(Formatting.None);
            listTemplateRegions = JsonConvert.DeserializeObject <IDictionary <string, object>[]>(jsonOutput);

            // get the information about the products inside the regions
            jsonOutput            = parsedRegions.GetValue(CommonVariables.KEY_JSON_REGIONS_GROUP_PRODUCTS_IN_REGIONS).ToString(Formatting.None);
            listRegionsInProducts = JsonConvert.DeserializeObject <List <TypeProductsInRegions> >(jsonOutput);
        }
コード例 #4
0
        /**
         * With each Template (Catalog) Constructor there exists a tuple <idTemplate, TemplateFactory>
         *
         * Where idTemplate is the template identificator
         *
         * TemplateFactory has:
         *
         *  - Template identificator
         *  - Path where we can find the template
         *  - And the template in its interface ITemplate (create only when it is required)
         *
         * @param full path of the file or template
         */
        private void StoreIdTemplateAssociatedWithFactory(string pathFile)
        {
            // read the file
            string strInput = File.ReadAllText(pathFile);

            // parse the file and check whether it is a JSON or not
            var parsedObject = JSONPref.ParseJSON(strInput);

            // get the list of templates
            var listTemplates = parsedObject.GetValue(CommonVariables.KEY_JSON_TEMPLATES_CONF);

            // for each template
            foreach (JObject template in listTemplates)
            {
                // if the NameTemplate does not exist in the catalog
                string keyTemplate = (string)template.GetValue(CommonVariables.KEY_JSON_TEMPLATES_NAME);
                if (!CatalogTemplate.ContainsKey(keyTemplate))
                {
                    // we store the idTemplate and the factory
                    CatalogTemplate.Add(keyTemplate, new PreferenceTemplateFactory(keyTemplate, pathFile, Settings.TextGeneratorProvider));
                }
            }
        }