示例#1
0
        /**
         * Performs substitutions on the pref. See field comments for details on what
         * is substituted.
         *
         * @param substituter
         * @return The substituted pref.
         */
        public UserPref substitute(Substitutions substituter)
        {
            UserPref pref = new UserPref(this);

            Substitutions.Type type = Substitutions.Type.MESSAGE;
            pref.displayName  = substituter.substituteString(type, displayName);
            pref.defaultValue = substituter.substituteString(type, defaultValue);
            if (enumValues.Count == 0)
            {
                pref.enumValues = new Dictionary <string, string>();
            }
            else
            {
                Dictionary <String, String> values
                    = new Dictionary <String, String>(enumValues.Count);
                foreach (var entry in enumValues)
                {
                    values.Add(entry.Key,
                               substituter.substituteString(type, entry.Value));
                }
                pref.enumValues = values;
            }
            if (orderedEnumValues.Count == 0)
            {
                pref.orderedEnumValues = new List <EnumValuePair>();
            }
            else
            {
                List <EnumValuePair> orderedValues
                    = new List <EnumValuePair>();
                foreach (EnumValuePair evp in orderedEnumValues)
                {
                    orderedValues.Add(new EnumValuePair(evp.getValue(),
                                                        substituter.substituteString(type, evp.getDisplayValue())));
                }
                pref.orderedEnumValues = orderedValues;
            }
            return(pref);
        }
示例#2
0
 /**
  * Produces a UserPref suitable for substitute()
  * @param userPref
  */
 private UserPref(UserPref userPref)
 {
     name     = userPref.name;
     dataType = userPref.dataType;
     required = userPref.required;
 }
示例#3
0
        /**
         * Creates a new Module from the given xml input.
         *
         * @param url
         * @param xml
         * @throws SpecParserException
         */
        public GadgetSpec(Uri url, String xml)
        {
            XmlElement doc;

            try
            {
                doc = XmlUtil.Parse(xml);
            }
            catch (XmlException e)
            {
                throw new SpecParserException("Malformed XML in file " + url.ToString(), e);
            }
            this.url = url;

            // This might not be good enough; should we take message bundle changes
            // into account?
            this.checksum = HashUtil.checksum(xml);

            XmlNodeList children = doc.ChildNodes;

            ModulePrefs     modulePrefs = null;
            List <UserPref> userPrefs   = new List <UserPref>();
            Dictionary <String, List <XmlElement> > views = new Dictionary <String, List <XmlElement> >();

            for (int i = 0, j = children.Count; i < j; ++i)
            {
                XmlNode child = children[i];
                if (!(child is XmlElement))
                {
                    continue;
                }
                XmlElement element = (XmlElement)child;
                switch (element.Name)
                {
                case "ModulePrefs":
                    if (modulePrefs == null)
                    {
                        modulePrefs = new ModulePrefs(element, url);
                    }
                    else
                    {
                        throw new SpecParserException("Only 1 ModulePrefs is allowed.");
                    }
                    break;

                case "UserPref":
                    UserPref pref = new UserPref(element);
                    userPrefs.Add(pref);
                    break;

                case "Content":
                    String viewNames = XmlUtil.getAttribute(element, "view", "default");
                    foreach (String _view in viewNames.Split(','))
                    {
                        String            view = _view.Trim();
                        List <XmlElement> viewElements;
                        if (!views.TryGetValue(view, out viewElements))
                        {
                            viewElements = new List <XmlElement>();
                            views.Add(view, viewElements);
                        }
                        viewElements.Add(element);
                    }
                    break;
                }
            }

            if (modulePrefs == null)
            {
                throw new SpecParserException("At least 1 ModulePrefs is required.");
            }

            this.modulePrefs = modulePrefs;

            if (views.Count == 0)
            {
                throw new SpecParserException("At least 1 Content is required.");
            }

            Dictionary <String, View> tmpViews = new Dictionary <String, View>();

            foreach (var view in views)
            {
                View v = new View(view.Key, view.Value, url);
                tmpViews.Add(v.getName(), v);
            }
            this.views = tmpViews;


            if (userPrefs.Count != 0)
            {
                this.userPrefs = userPrefs;
            }
            else
            {
                this.userPrefs = new List <UserPref>();
            }
        }