Class CssUtils


  • public class CssUtils
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String _0_BOTTOM_1  
      private static java.lang.String _0_LEFT_1  
      private static java.lang.String _0_RIGHT_1  
      private static java.lang.String _0_TOP_1  
      private static java.util.Set<java.lang.String> backgroundPositions  
      private static java.lang.String BORDER2  
      private static java.util.Set<java.lang.String> borderstyle  
      private static java.util.Set<java.lang.String> borderwidth  
      private static java.lang.String COLOR  
      static int DEFAULT_FONT_SIZE_PT
      Default font size if none is set.
      private static CssUtils instance  
      private static java.lang.String STYLE  
      private static java.lang.String WIDTH  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CssUtils()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      float calculateMarginTop​(float value, MarginMemory configuration)
      Calculates the margin top or spacingBefore based on the given value and the last margin bottom.
      float calculateMarginTop​(java.lang.String value, float largestFont, MarginMemory configuration)
      Calculates the margin top or spacingBefore based on the given value and the last margin bottom.
      float checkMetricStyle​(Tag t, java.lang.String style)
      Use only if value of style is a metric value (isMetricValue(String)) or a numeric value in pixels (isNumericValue(String)).
      Checks if the style is present in the css of the tag, then parses it to pt.
      java.lang.Float checkMetricStyle​(java.util.Map<java.lang.String,​java.lang.String> css, java.lang.String style)
      Use only if value of style is a metric value (isMetricValue(String)) or a numeric value in pixels (isNumericValue(String)).
      Checks if the style is present in the css of the tag, then parses it to pt.
      int determinePositionBetweenValueAndUnit​(java.lang.String string)
      Method used in preparation of splitting a string containing a numeric value with a metric unit (e.g.
      java.lang.String extractUrl​(java.lang.String url)
      Parses url("file.jpg") to file.jpg.
      static CssUtils getInstance()  
      float getLeftAndRightMargin​(Tag t, float pageWidth)
      Returns the sum of the left and right margin of a tag.
      boolean isMetricValue​(java.lang.String value)
      Checks whether a string contains an allowed metric unit in HTML/CSS; px, in, cm, mm, pc or pt.
      boolean isNumericValue​(java.lang.String value)
      Checks whether a string matches a numeric value (e.g.
      boolean isRelativeValue​(java.lang.String value)
      Checks whether a string contains an allowed value relative to previously set value.
      java.util.Map<java.lang.String,​java.lang.String> parseBorder​(java.lang.String border)  
      java.util.Map<java.lang.String,​java.lang.String> parseBorder​(java.lang.String border, java.lang.String borderKey)  
      java.util.Map<java.lang.String,​java.lang.String> parseBoxValues​(java.lang.String box, java.lang.String pre, java.lang.String post)
      Returns the top, bottom, left, right version for the given box.
      java.util.Map<java.lang.String,​java.lang.String> parseBoxValues​(java.lang.String box, java.lang.String pre, java.lang.String post, java.lang.String preKey)  
      float parsePxInCmMmPcToPt​(java.lang.String length)
      Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g.
      float parsePxInCmMmPcToPt​(java.lang.String length, java.lang.String defaultMetric)
      Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g.
      float parseRelativeValue​(java.lang.String relativeValue, float baseValue)
      Parses an relative value based on the base value that was given, in the metric unit of the base value.
      float parseValueToPt​(java.lang.String value, float baseValue)
      Convenience method for parsing a value to pt if a value can contain:
      a numeric value in pixels (e.g.
      java.util.Map<java.lang.String,​java.lang.String> processBackground​(java.lang.String background)
      Preparation before implementing the background style in iText.
      java.util.Map<java.lang.String,​java.lang.String> processFont​(java.lang.String font)
      Preparation before implementing the font style in iText.
      java.util.Map<java.lang.String,​java.lang.String> processListStyle​(java.lang.String listStyle)
      Preparation before implementing the list style in iText.
      java.lang.String[] splitComplexCssStyle​(java.lang.String s)  
      java.lang.String stripDoubleSpacesAndTrim​(java.lang.String str)
      Trims and Strips double spaces from the given string.
      java.lang.String stripDoubleSpacesTrimAndToLowerCase​(java.lang.String str)  
      java.lang.String trimAndRemoveQuoutes​(java.lang.String s)
      Trims a string and removes surrounding " or '.
      float validateTextHeight​(java.util.Map<java.lang.String,​java.lang.String> css, float textHeight)
      Validates a given textHeight based on the content of a tag against the css styles "min-height" and "max-height" of the tag if present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • instance

        private static CssUtils instance
      • DEFAULT_FONT_SIZE_PT

        public static final int DEFAULT_FONT_SIZE_PT
        Default font size if none is set.
        See Also:
        Constant Field Values
      • borderwidth

        private static final java.util.Set<java.lang.String> borderwidth
      • borderstyle

        private static final java.util.Set<java.lang.String> borderstyle
      • backgroundPositions

        private static final java.util.Set<java.lang.String> backgroundPositions
    • Constructor Detail

      • CssUtils

        private CssUtils()
    • Method Detail

      • getInstance

        public static CssUtils getInstance()
        Returns:
        Singleton instance of CssUtils.
      • parseBoxValues

        public java.util.Map<java.lang.String,​java.lang.String> parseBoxValues​(java.lang.String box,
                                                                                     java.lang.String pre,
                                                                                     java.lang.String post)
        Returns the top, bottom, left, right version for the given box. the keys will be the pre value concatenated with either top, bottom, right or left and the post value. Note: Does not work when double spaces are in the boxes value. (Tip: Use stripDoubleSpacesAndTrim(String))
        Parameters:
        box - the value to parse
        pre - the pre key part
        post - the post key part
        Returns:
        a map with the parsed properties
      • parseBoxValues

        public java.util.Map<java.lang.String,​java.lang.String> parseBoxValues​(java.lang.String box,
                                                                                     java.lang.String pre,
                                                                                     java.lang.String post,
                                                                                     java.lang.String preKey)
      • parseBorder

        public java.util.Map<java.lang.String,​java.lang.String> parseBorder​(java.lang.String border)
        Parameters:
        border - the border property
        Returns:
        a map of the border property parsed to each property (width, style, color).
      • parseBorder

        public java.util.Map<java.lang.String,​java.lang.String> parseBorder​(java.lang.String border,
                                                                                  java.lang.String borderKey)
      • stripDoubleSpacesAndTrim

        public java.lang.String stripDoubleSpacesAndTrim​(java.lang.String str)
        Trims and Strips double spaces from the given string.
        Parameters:
        str - the string to strip
        Returns:
        the string without double spaces
      • stripDoubleSpacesTrimAndToLowerCase

        public java.lang.String stripDoubleSpacesTrimAndToLowerCase​(java.lang.String str)
      • processBackground

        public java.util.Map<java.lang.String,​java.lang.String> processBackground​(java.lang.String background)
        Preparation before implementing the background style in iText. Splits the given background style and its attributes into background-color, background-image, background-repeat, background-attachment, background-position and css styles.
        Parameters:
        background - the string containing the font style value.
        Returns:
        a map with the values of font parsed into each css property.
      • processListStyle

        public java.util.Map<java.lang.String,​java.lang.String> processListStyle​(java.lang.String listStyle)
        Preparation before implementing the list style in iText. Splits the given list style and its attributes into list-style-type, list-style-position and list-style-image.
        Parameters:
        listStyle - the string containing the list style value.
        Returns:
        a map with the values of the parsed list style into each css property.
      • processFont

        public java.util.Map<java.lang.String,​java.lang.String> processFont​(java.lang.String font)
        Preparation before implementing the font style in iText. Splits the given font style and its attributes into font-size, line-height, font-weight, font-style, font-variant and font-family css styles.
        Parameters:
        font - the string containing the font style value.
        Returns:
        a map with the values of the parsed font into each css property.
      • checkMetricStyle

        public float checkMetricStyle​(Tag t,
                                      java.lang.String style)
        Use only if value of style is a metric value (isMetricValue(String)) or a numeric value in pixels (isNumericValue(String)).
        Checks if the style is present in the css of the tag, then parses it to pt. and returns the parsed value.
        Parameters:
        t - the tag which needs to be checked.
        style - the style which needs to be checked.
        Returns:
        float the parsed value of the style or 0f if the value was invalid.
      • checkMetricStyle

        public java.lang.Float checkMetricStyle​(java.util.Map<java.lang.String,​java.lang.String> css,
                                                java.lang.String style)
        Use only if value of style is a metric value (isMetricValue(String)) or a numeric value in pixels (isNumericValue(String)).
        Checks if the style is present in the css of the tag, then parses it to pt. and returns the parsed value.
        Parameters:
        css - the map of css styles which needs to be checked.
        style - the style which needs to be checked.
        Returns:
        float the parsed value of the style or 0f if the value was invalid.
      • isMetricValue

        public boolean isMetricValue​(java.lang.String value)
        Checks whether a string contains an allowed metric unit in HTML/CSS; px, in, cm, mm, pc or pt.
        Parameters:
        value - the string that needs to be checked.
        Returns:
        boolean true if value contains an allowed metric value.
      • isRelativeValue

        public boolean isRelativeValue​(java.lang.String value)
        Checks whether a string contains an allowed value relative to previously set value.
        Parameters:
        value - the string that needs to be checked.
        Returns:
        boolean true if value contains an allowed metric value.
      • isNumericValue

        public boolean isNumericValue​(java.lang.String value)
        Checks whether a string matches a numeric value (e.g. 123, 1.23, .123). All these metric values are allowed in HTML/CSS.
        Parameters:
        value - the string that needs to be checked.
        Returns:
        boolean true if value contains an allowed metric value.
      • parseValueToPt

        public float parseValueToPt​(java.lang.String value,
                                    float baseValue)
        Convenience method for parsing a value to pt if a value can contain:
        • a numeric value in pixels (e.g. 123, 1.23, .123),
        • a value with a metric unit (px, in, cm, mm, pc or pt) attached to it,
        • or a value with a relative value (%, em, ex).
        Note: baseValue must be in pt.

        Parameters:
        value - the string containing the value to be parsed.
        baseValue - float needed for the calculation of the relative value.
        Returns:
        parsedValue float containing the parsed value in pt.
      • parseRelativeValue

        public float parseRelativeValue​(java.lang.String relativeValue,
                                        float baseValue)
        Parses an relative value based on the base value that was given, in the metric unit of the base value.
        (e.g. margin=10% should be based on the page width, so if an A4 is used, the margin = 0.10*595.0 = 59.5f)
        Parameters:
        relativeValue - in %, em or ex.
        baseValue - the value the returned float is based on.
        Returns:
        the parsed float in the metric unit of the base value.
      • parsePxInCmMmPcToPt

        public float parsePxInCmMmPcToPt​(java.lang.String length,
                                         java.lang.String defaultMetric)
        Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g. 123, 1.23, .123) to pt.
        A numeric value (without px, pt, etc in the given length string) is considered to be in the default metric that was given.
        Parameters:
        length - the string containing the length.
        defaultMetric - the string containing the metric if it is possible that the length string does not contain one. If null the length is considered to be in px as is default in HTML/CSS.
        Returns:
        parsed value
      • parsePxInCmMmPcToPt

        public float parsePxInCmMmPcToPt​(java.lang.String length)
        Parses a length with an allowed metric unit (px, pt, in, cm, mm, pc, em or ex) or numeric value (e.g. 123, 1.23, .123) to pt.
        A numeric value is considered to be in px as is default in HTML/CSS.
        Parameters:
        length - the string containing the length.
        Returns:
        float the parsed length in pt.
      • determinePositionBetweenValueAndUnit

        public int determinePositionBetweenValueAndUnit​(java.lang.String string)
        Method used in preparation of splitting a string containing a numeric value with a metric unit (e.g. 18px, 9pt, 6cm, etc).

        Determines the position between digits and affiliated characters ('+','-','0-9' and '.') and all other characters.
        e.g. string "16px" will return 2, string "0.5em" will return 3 and string '-8.5mm' will return 4.
        Parameters:
        string - containing a numeric value with a metric unit
        Returns:
        int position between the numeric value and unit or 0 if string is null or string started with a non-numeric value.
      • getLeftAndRightMargin

        public float getLeftAndRightMargin​(Tag t,
                                           float pageWidth)
        Returns the sum of the left and right margin of a tag.
        Parameters:
        t - the tag of which the total horizontal margin is needed.
        pageWidth - the page width
        Returns:
        float the total horizontal margin.
      • extractUrl

        public java.lang.String extractUrl​(java.lang.String url)
        Parses url("file.jpg") to file.jpg.
        Parameters:
        url - the url attribute to parse
        Returns:
        the parsed url. Or original url if not wrappend in url()
      • validateTextHeight

        public float validateTextHeight​(java.util.Map<java.lang.String,​java.lang.String> css,
                                        float textHeight)
        Validates a given textHeight based on the content of a tag against the css styles "min-height" and "max-height" of the tag if present.
        Parameters:
        css - the styles of a tag
        textHeight - the current textHeight based on the content of a tag
        Returns:
        the final text height of an element.
      • calculateMarginTop

        public float calculateMarginTop​(java.lang.String value,
                                        float largestFont,
                                        MarginMemory configuration)
        Calculates the margin top or spacingBefore based on the given value and the last margin bottom.

        In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned
        Parameters:
        value - the margin-top value of the given tag.
        largestFont - used if a relative value was given to calculate margin.
        configuration - XmlWorkerConfig containing the last margin bottom.
        Returns:
        an offset
      • calculateMarginTop

        public float calculateMarginTop​(float value,
                                        MarginMemory configuration)
        Calculates the margin top or spacingBefore based on the given value and the last margin bottom.

        In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned
        Parameters:
        value - float containing the margin-top value.
        configuration - XmlWorkerConfig containing the last margin bottom.
        Returns:
        an offset
      • trimAndRemoveQuoutes

        public java.lang.String trimAndRemoveQuoutes​(java.lang.String s)
        Trims a string and removes surrounding " or '.
        Parameters:
        s - the string
        Returns:
        trimmed and unquoted string
      • splitComplexCssStyle

        public java.lang.String[] splitComplexCssStyle​(java.lang.String s)