Ejemplo n.º 1
0
        /// <summary>
        /// Resolves all the styles in the specified <see cref="Feature"/>.
        /// </summary>
        /// <param name="feature">
        /// The <c>Feature</c> to search for styles.
        /// </param>
        /// <param name="file">
        /// The <see cref="KmlFile"/> the feature belongs to.
        /// </param>
        /// <param name="state">
        /// The <see cref="StyleState"/> of the styles to look for.
        /// </param>
        /// <param name="resolver">
        /// Used to resolve external files referenced by the styles.
        /// </param>
        /// <param name="cache">IKMLCache object to use previously loaded external files</param>
        /// <returns>
        /// A new <see cref="Style"/> that has been resolved.
        /// </returns>
        /// <exception cref="ArgumentNullException">feature/file is null.</exception>
        public static Style CreateResolvedStyle(Feature feature, KmlFile file, StyleState state, IFileResolver resolver, IKmlCache cache = null)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature");
            }

            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var instance = new StyleResolver(file.StyleMap);
            instance.fileResolver = resolver;
            instance.state = state;
            instance.cache = cache;
            instance.Merge(feature.StyleUrl);

            foreach (StyleSelector selector in feature.Styles)
            {
                instance.Merge(selector);
            }

            return instance.style;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Changes inlined styles to shared styles in the closest Document parent.
        /// </summary>
        /// <typeparam name="T">
        /// A class deriving from <see cref="Element"/>.
        /// </typeparam>
        /// <param name="element">The element instance.</param>
        /// <returns>
        /// A new element with the inlined styles changed to shared styles.
        /// </returns>
        public static T SplitStyles<T>(T element)
            where T : Element
        {
            var instance = new StyleResolver(new Dictionary<string, StyleSelector>());

            // Can't modify the Children collection while we're iterating so
            // create a temporary list of styles to add
            var sharedStyles = new List<Tuple<Document, StyleSelector>>();

            T clone = element.Clone();
            var children = clone.Flatten().ToList(); // Adding a style will add to the children.
            foreach (Element e in children)
            {
                var tuple = instance.Split(e);
                if (tuple != null)
                {
                    sharedStyles.Add(tuple);
                }
            }

            // Finished iterating the flattened hierarchy (which incudes
            // Children) so we can now add the shared styles
            foreach (var style in sharedStyles)
            {
                style.Item1.AddStyle(style.Item2);
            }

            return clone;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Inlines the shared Style of the features in the specified element.
        /// </summary>
        /// <typeparam name="T">
        /// A class deriving from <see cref="Element"/>.
        /// </typeparam>
        /// <param name="element">The element instance.</param>
        /// <returns>A new element with the shared styles inlined.</returns>
        public static T InlineStyles <T>(T element) where T : Element
        {
            var instance = new StyleResolver(new Dictionary <string, StyleSelector>());

            // Don't modify the original but create a copy instead
            T clone = element.Clone();

            foreach (var e in clone.Flatten())
            {
                instance.InlineElement(e);
            }
            return(clone);
        }
Ejemplo n.º 4
0
        /// <summary>Resolves all the styles in the specified Feature.</summary>
        /// <param name="feature">The Feature to search for Styles.</param>
        /// <param name="file">The KmlFile the feature belongs to.</param>
        /// <param name="state">The StyleState of the styles to look for.</param>
        /// <returns>A new Style that has been resolved.</returns>
        /// <exception cref="ArgumentNullException">feature/file is null.</exception>
        public static Style CreateResolvedStyle(Feature feature, KmlFile file, StyleState state)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature");
            }
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var instance = new StyleResolver(file.StyleMap);

            instance._state = state;
            instance.Merge(feature.StyleUrl, feature.StyleSelector);
            return(instance._style);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a balloon text for the specified feature, calling
        /// <see cref="ParseEntityFields"/> to gather information.
        /// </summary>
        /// <param name="feature">The feature to create a balloon text for.</param>
        /// <returns>
        /// The expanded version of the feature's balloon text, if specified;
        /// otherwise an automatically generated HTML summary of the feature.
        /// </returns>
        /// <exception cref="ArgumentNullException">feature is null.</exception>
        /// <remarks>
        /// By default this method will not resolve any external styles.
        /// </remarks>
        public string CreateBalloonText(Feature feature)
        {
            Check.IsNotNull(feature, nameof(feature));
            this.ParseEntityFields(feature);

            Style style = StyleResolver.CreateResolvedStyle(feature, this.file, StyleState.Normal);

            if ((style.Balloon != null) && (style.Balloon.Text != null))
            {
                return(this.ExpandEntities(style.Balloon.Text));
            }

            // If the Balloon doesn't exist or doesn't have any text then we'll
            // make an HTML default table.
            var html = new StringBuilder();

            if (feature.Name != null)
            {
                html.Append("<h3>");
                html.Append(feature.Name);
                html.Append("</h3><br/><br/>");
            }

            if ((feature.Description != null) && (feature.Description.Text != null))
            {
                html.Append(this.ExpandEntities(feature.Description.Text));
            }

            // Now a table of the extended data
            if (this.markup.Count != 0)
            {
                html.AppendLine("\n<table border=\"1\">");
                foreach (Tuple <string, string> data in this.markup)
                {
                    html.Append("<tr><td>");
                    html.Append(data.Item1);
                    html.Append("</td><td>");
                    html.Append(data.Item2);
                    html.AppendLine("</tr>");
                }
            }

            return(html.ToString());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Resolves all the styles in the specified <see cref="Feature"/>.
        /// </summary>
        /// <param name="feature">
        /// The <c>Feature</c> to search for styles.
        /// </param>
        /// <param name="file">
        /// The <see cref="KmlFile"/> the feature belongs to.
        /// </param>
        /// <param name="state">
        /// The <see cref="StyleState"/> of the styles to look for.
        /// </param>
        /// <param name="resolver">
        /// Used to resolve external files referenced by the styles.
        /// </param>
        /// <returns>
        /// A new <see cref="Style"/> that has been resolved.
        /// </returns>
        /// <exception cref="ArgumentNullException">feature/file is null.</exception>
        public static Style CreateResolvedStyle(Feature feature, KmlFile file, StyleState state, IFileResolver resolver)
        {
            Check.IsNotNull(feature, nameof(feature));
            Check.IsNotNull(file, nameof(file));

            var instance = new StyleResolver(file.StyleMap)
            {
                fileResolver = resolver,
                state        = state,
            };

            instance.Merge(feature.StyleUrl);

            foreach (StyleSelector selector in feature.Styles)
            {
                instance.Merge(selector);
            }

            return(instance.style);
        }