public StripDoubleSpacesAndTrim ( String str ) : String | ||
str | String | |
Результат | String |
/** * Also taking into account the CSS properties of any parent tag in the given tag. * * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#resolveStyles(com.itextpdf.tool.xml.Tag) */ virtual public void ResolveStyles(Tag t) { // get css for this tag from resolver IDictionary <String, String> tagCss = new Dictionary <String, String>(); IDictionary <String, String> listCss = null; if (null != cssFiles && cssFiles.HasFiles()) { tagCss = cssFiles.GetCSS(t); if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.P) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.TD)) { listCss = cssFiles.GetCSS(new Tag(HTML.Tag.UL)); } } // get css from style attr if (null != t.Attributes && t.Attributes.Count != 0) { if (t.Attributes.ContainsKey(HTML.Attribute.CELLPADDING)) { CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLPADDING], "cellpadding-", "")); } if (t.Attributes.ContainsKey(HTML.Attribute.CELLSPACING)) { CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLSPACING], "cellspacing-", "")); } String styleAtt; t.Attributes.TryGetValue(HTML.Attribute.STYLE, out styleAtt); if (!string.IsNullOrEmpty(styleAtt)) { Dictionary <String, String> tagAttrCss = new Dictionary <string, string>(); String[] styles = styleAtt.Split(';'); foreach (String s in styles) { String[] part = s.Split(splitColon, 2); if (part.Length == 2) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(part[0]); String value = utils.StripDoubleSpacesAndTrim(part[1]); SplitRules(tagAttrCss, key, value); } } foreach (KeyValuePair <String, String> e in tagAttrCss) { tagCss[e.Key] = e.Value; } } } // inherit css from parent tags, as defined in provided CssInheritanceRules or if property = inherit IDictionary <String, String> css = t.CSS; if (t.Name != null) { if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.I) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.CITE) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.EM) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.VAR) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.DFN) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.ADDRESS)) { tagCss[CSS.Property.FONT_STYLE] = CSS.Value.ITALIC; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.B) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.STRONG)) { tagCss[CSS.Property.FONT_WEIGHT] = CSS.Value.BOLD; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.U) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.INS)) { tagCss[CSS.Property.TEXT_DECORATION] = CSS.Value.UNDERLINE; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.S) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.STRIKE) || Util.EqualsIgnoreCase(t.Name, HTML.Tag.DEL)) { tagCss[CSS.Property.TEXT_DECORATION] = CSS.Value.LINE_THROUGH; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.BIG)) { tagCss[CSS.Property.FONT_SIZE] = CSS.Value.LARGER; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.SMALL)) { tagCss[CSS.Property.FONT_SIZE] = CSS.Value.SMALLER; } } if (listCss != null && listCss.ContainsKey(CSS.Property.LIST_STYLE_TYPE)) { css[CSS.Property.LIST_STYLE_TYPE] = listCss[CSS.Property.LIST_STYLE_TYPE]; } if (MustInherit(t.Name) && null != t.Parent && null != t.Parent.CSS) { if (null != this.inherit) { foreach (KeyValuePair <String, String> entry in t.Parent.CSS) { String key = entry.Key; if ((tagCss.ContainsKey(key) && CSS.Value.INHERIT.Equals(tagCss[key])) || CanInherite(t, key)) { if (key.Contains(CSS.Property.CELLPADDING) && (HTML.Tag.TD.Equals(t.Name) || HTML.Tag.TH.Equals(t.Name))) { String paddingKey = key.Replace(CSS.Property.CELLPADDING, CSS.Property.PADDING); //if (!tagCss.containsKey(paddingKey)) { tagCss[paddingKey] = entry.Value; //continue; //} } else { //splitRules(css, key, entry.getValue()); css[key] = entry.Value; } } } } else { foreach (KeyValuePair <string, string> entry in t.Parent.CSS) { css.Add(entry); } } } if (t.Name != null) { if (t.Name.Equals(HTML.Tag.FONT)) { String font_family; if (t.Attributes.TryGetValue(HTML.Attribute.FACE, out font_family)) { css[CSS.Property.FONT_FAMILY] = font_family; } String color; if (t.Attributes.TryGetValue(HTML.Attribute.COLOR, out color)) { css[CSS.Property.COLOR] = color; } String size; if (t.Attributes.TryGetValue(HTML.Attribute.SIZE, out size)) { if (size.Equals("1")) { css[CSS.Property.FONT_SIZE] = CSS.Value.XX_SMALL; } else if (size.Equals("2")) { css[CSS.Property.FONT_SIZE] = CSS.Value.X_SMALL; } else if (size.Equals("3")) { css[CSS.Property.FONT_SIZE] = CSS.Value.SMALL; } else if (size.Equals("4")) { css[CSS.Property.FONT_SIZE] = CSS.Value.MEDIUM; } else if (size.Equals("5")) { css[CSS.Property.FONT_SIZE] = CSS.Value.LARGE; } else if (size.Equals("6")) { css[CSS.Property.FONT_SIZE] = CSS.Value.X_LARGE; } else if (size.Equals("7")) { css[CSS.Property.FONT_SIZE] = CSS.Value.XX_LARGE; } } } else if (t.Name.Equals(HTML.Tag.A)) { css[CSS.Property.TEXT_DECORATION] = CSS.Value.UNDERLINE; css[CSS.Property.COLOR] = "blue"; } } // overwrite properties (if value != inherit) foreach (KeyValuePair <String, String> e in tagCss) { if (!Util.EqualsIgnoreCase(CSS.Value.INHERIT, e.Value)) { if (e.Key.Equals(CSS.Property.TEXT_DECORATION)) { String oldValue = null; css.TryGetValue(e.Key, out oldValue); css[e.Key] = MergeTextDecorationRules(oldValue, e.Value); } else { css[e.Key] = e.Value; } } } }
virtual public void PopulateOneCss(IDictionary <String, String> aggregatedProps, IDictionary <String, String> cssDeclaration) { IDictionary <String, String> css = new Dictionary <String, String>(); foreach (KeyValuePair <String, String> e in cssDeclaration) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value, CSS.Property.BORDER_TOP)); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_BOTTOM, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value, CSS.Property.BORDER_BOTTOM)); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_LEFT, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value, CSS.Property.BORDER_LEFT)); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_RIGHT, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value, CSS.Property.BORDER_RIGHT)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { IDictionary <String, String> margins = utils.ParseBoxValues(value, "margin-", ""); foreach (String marginKey in margins.Keys) { if (!css.ContainsKey(marginKey)) { css.Add(marginKey, margins[marginKey]); } } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { IDictionary <String, String> paddings = utils.ParseBoxValues(value, "padding-", ""); foreach (String paddingKey in paddings.Keys) { if (!css.ContainsKey(paddingKey)) { css.Add(paddingKey, paddings[paddingKey]); } } //CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else if (key.ToLowerInvariant().Contains(CSS.Property.BACKGROUND)) { IDictionary <String, String> backgroundStyles = utils.ProcessBackground(value); foreach (String backgroundKey in backgroundStyles.Keys) { if (!css.ContainsKey(backgroundKey)) { css.Add(backgroundKey, backgroundStyles[backgroundKey]); } } } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }
virtual public void CreateSelectors(Tag t, IDictionary <String, object> selectors) { IDictionary <String, object> tagNameSet = new Dictionary <String, object>(); foreach (String selector in selectors.Keys) { tagNameSet[t.Name + (selector.StartsWith(" ") ? "" : " ") + selector] = null; } IDictionary <String, object> tagClassSet = new Dictionary <String, object>(); String tagClasses; String[] classSplit = null; if (t.Attributes.TryGetValue(HTML.Attribute.CLASS, out tagClasses)) { classSplit = utils.StripDoubleSpacesAndTrim(tagClasses).Split(' '); foreach (String selector in selectors.Keys) { foreach (String tagClass in classSplit) { StringBuilder builder = new StringBuilder(); builder.Append('.').Append(tagClass); if (selector.StartsWith("#") || selector.StartsWith(".") || selector.StartsWith(" ")) { tagClassSet[builder + selector] = null; if (!selector.StartsWith(" ")) { tagClassSet[builder + " " + selector] = null; } foreach (String tagSelector in tagNameSet.Keys) { builder = new StringBuilder(tagSelector); builder.Append('.').Append(tagClass); tagClassSet[builder.ToString()] = null; } } else { builder.Append(" " + selector); tagClassSet[builder.ToString()] = null; } } } } String id = null; IDictionary <String, Object> tagIdSet = new Dictionary <string, object>(); if (t.Attributes.TryGetValue(HTML.Attribute.ID, out id)) { foreach (String selector in selectors.Keys) { StringBuilder builder = new StringBuilder(); builder.Append('#').Append(id); if (selector.StartsWith("#") || selector.StartsWith(".") || selector.StartsWith(" ")) { tagClassSet[builder + selector] = null; if (!selector.StartsWith(" ")) { tagClassSet[builder.ToString() + " " + selector] = null; } } else { builder.Append(" " + selector); tagClassSet[builder.ToString()] = null; } } } if (selectors.Count == 0) { tagNameSet[t.Name] = null; if (tagClasses != null) { foreach (String tagClass in classSplit) { StringBuilder builder = new StringBuilder(); builder.Append('.').Append(tagClass); tagClassSet[builder.ToString()] = null; builder = new StringBuilder(); builder.Append(" .").Append(tagClass); tagClassSet[builder.ToString()] = null; foreach (String tagSelector in tagNameSet.Keys) { builder = new StringBuilder(tagSelector); builder.Append('.').Append(tagClass); tagClassSet[builder.ToString()] = null; } } } if (id != null) { StringBuilder builder = new StringBuilder(); builder.Append('#').Append(id); tagIdSet[builder.ToString()] = null; builder = new StringBuilder(); builder.Append(" #").Append(id); tagIdSet[builder.ToString()] = null; } } CssUtils.MapPutAll(selectors, tagNameSet); CssUtils.MapPutAll(selectors, tagClassSet); CssUtils.MapPutAll(selectors, tagIdSet); if (t.Parent != null && selectors.Count < 30) { CreateSelectors(t.Parent, selectors); } }
/** * Also taking into account the CSS properties of any parent tag in the given tag. * * @see com.itextpdf.tool.xml.pipeline.css.CSSResolver#resolveStyles(com.itextpdf.tool.xml.Tag) */ public void ResolveStyles(Tag t) { // get css for this tag from resolver IDictionary <String, String> tagCss = new Dictionary <String, String>(); if (null != cssFiles && cssFiles.HasFiles()) { tagCss = cssFiles.GetCSS(t); } // get css from style attr if (null != t.Attributes && t.Attributes.Count != 0) { if (t.Attributes.ContainsKey(HTML.Attribute.CELLPADDING)) { CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLPADDING], "cellpadding-", "")); } if (t.Attributes.ContainsKey(HTML.Attribute.CELLSPACING)) { CssUtils.MapPutAll(tagCss, utils.ParseBoxValues(t.Attributes[HTML.Attribute.CELLSPACING], "cellspacing-", "")); } String styleAtt; t.Attributes.TryGetValue(HTML.Attribute.STYLE, out styleAtt); if (null != styleAtt && styleAtt.Length > 0) { String[] styles = styleAtt.Split(';'); foreach (String s in styles) { String[] part = s.Split(splitColon, 2); if (part.Length == 2) { String key = part[0].Trim(); String value = utils.StripDoubleSpacesAndTrim(part[1]); SplitRules(tagCss, key, value); } } } } // inherit css from parent tags, as defined in provided CssInheritanceRules or if property = inherit IDictionary <String, String> css = t.CSS; if (MustInherit(t.Name) && null != t.Parent && null != t.Parent.CSS) { if (null != this.inherit) { foreach (KeyValuePair <String, String> entry in t.Parent.CSS) { String key = entry.Key; if ((tagCss.ContainsKey(key) && CSS.Value.INHERIT.Equals(tagCss[key])) || CanInherite(t, key)) { //splitRules(css, key, entry.GetValue()); css[key] = entry.Value; } } } else { CssUtils.MapPutAll(css, t.Parent.CSS); } } // overwrite properties (if value != inherit) foreach (KeyValuePair <String, String> e in tagCss) { if (!Util.EqualsIgnoreCase(CSS.Value.INHERIT, e.Value)) { css[e.Key] = e.Value; } } }
public void PopulateOneCss(ICssFile cssFile, IDictionary <String, String> aggregatedProps, String selector) { IDictionary <String, String> t = cssFile.Get(selector); IDictionary <String, String> css = new Dictionary <String, String>(); foreach (KeyValuePair <String, String> e in t) { String key = utils.StripDoubleSpacesTrimAndToLowerCase(e.Key); String value = utils.StripDoubleSpacesAndTrim(e.Value); if (Util.EqualsIgnoreCase(CSS.Property.BORDER, key)) { CssUtils.MapPutAll(css, utils.ParseBorder(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "margin-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_WIDTH, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-width")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_STYLE, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-style")); } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_COLOR, key)) { CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "border-", "-color")); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING, key)) { IDictionary <String, String> paddings = utils.ParseBoxValues(value, "padding-", ""); foreach (String paddingKey in paddings.Keys) { if (!css.ContainsKey(paddingKey)) { css.Add(paddingKey, paddings[paddingKey]); } } //CssUtils.MapPutAll(css, utils.ParseBoxValues(value, "padding-", "")); } else if (Util.EqualsIgnoreCase(CSS.Property.FONT, key)) { CssUtils.MapPutAll(css, utils.ProcessFont(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.LIST_STYLE, key)) { CssUtils.MapPutAll(css, utils.ProcessListStyle(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.BACKGROUND, key)) { IDictionary <String, String> backgroundStyles = utils.ProcessBackground(value); foreach (String backgroundKey in backgroundStyles.Keys) { if (!css.ContainsKey(backgroundKey)) { css.Add(backgroundKey, backgroundStyles[backgroundKey]); } } } else { css[key] = value; } } CssUtils.MapPutAll(aggregatedProps, css); }