CssUtils { public static float parseAbsoluteLength(String length, String defaultMetric) { int pos = determinePositionBetweenValueAndUnit(length); if (pos == 0) { if (length == null) { length = "null"; } throw new StyledXMLParserException(MessageFormatUtil.format(StyledXMLParserException.NAN, length)); } double f = Double.parseDouble(length.substring(0, pos)); String unit = length.substring(pos); if (unit.startsWith(CommonCssConstants.PT) || unit.equals("") && defaultMetric.equals(CommonCssConstants.PT)) { return (float) f; } if (unit.startsWith(CommonCssConstants.IN) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.IN))) { return (float) (f * 72); } else if (unit.startsWith(CommonCssConstants.CM) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.CM))) { return (float) ((f / 2.54) * 72); } else if (unit.startsWith(CommonCssConstants.Q) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.Q))) { return (float) ((f / 2.54) * 72 / 40); } else if (unit.startsWith(CommonCssConstants.MM) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.MM))) { return (float) ((f / 25.4) * 72); } else if (unit.startsWith(CommonCssConstants.PC) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.PC))) { return (float) (f * 12); } else if (unit.startsWith(CommonCssConstants.PX) || (unit.equals("") && defaultMetric .equals(CommonCssConstants.PX))) { return (float) (f * 0.75); } logger.error(MessageFormatUtil.format(LogMessageConstant.UNKNOWN_ABSOLUTE_METRIC_LENGTH_PARSED, unit.equals("") ? defaultMetric : unit)); return (float) f; } private  CssUtils(); static List<String> splitStringWithComma(final String value); static BlendMode parseBlendMode(String cssValue); static List<List<String>> extractShorthandProperties(String str); static String normalizeCssProperty(String str); static String removeDoubleSpacesAndTrim(String str); static Integer parseInteger(String str); static Float parseFloat(String str); static Double parseDouble(String str); static float parseAngle(String angle, String defaultMetric); static float parseAngle(String angle); static int[] parseAspectRatio(String str); static float parseAbsoluteLength(String length, String defaultMetric); static float parseAbsoluteLength(String length); static float parseRelativeValue(final String relativeValue, final float baseValue); static UnitValue parseLengthValueToPt(final String value, final float emValue, final float remValue); static boolean isValidNumericValue(final String value); static float parseAbsoluteFontSize(String fontSizeValue, String defaultMetric); static float parseAbsoluteFontSize(String fontSizeValue); static float parseRelativeFontSize(final String relativeFontSizeValue, final float baseValue); static UnitValue[] parseSpecificCornerBorderRadius(String specificBorderRadius, final float emValue, final float remValue); static float parseResolution(String resolutionStr); static int determinePositionBetweenValueAndUnit(String string); static boolean isMetricValue(final String value); static boolean isAngleValue(final String value); static boolean isRelativeValue(final String value); static boolean isFontRelativeValue(final String value); static boolean isPercentageValue(final String value); static boolean isRemValue(final String value); static boolean isEmValue(final String value); static boolean isExValue(final String value); static boolean isNumericValue(final String value); static String extractUrl(final String url); static boolean isBase64Data(String data); static int findNextUnescapedChar(String source, char ch, int startIndex); static boolean isColorProperty(String value); static boolean compareFloats(double d1, double d2); static boolean compareFloats(float f1, float f2); static float[] parseRgbaColor(String colorValue); static Range parseUnicodeRange(String unicodeRange); static float convertPtsToPx(float pts); static double convertPtsToPx(double pts); static float convertPxToPts(float px); static double convertPxToPts(double px); static boolean isStyleSheetLink(IElementNode headChildElement); static boolean isInitialOrInheritOrUnset(String value);  }
@Test public void parseAbsoluteLengthFromNAN() { junitExpectedException.expect(StyledXMLParserException.class); junitExpectedException.expectMessage(MessageFormatUtil.format(StyledXMLParserException.NAN, "Definitely not a number")); String value = "Definitely not a number"; CssUtils.parseAbsoluteLength(value); }
@Test public void parseAbsoluteLengthFromNull() { junitExpectedException.expect(StyledXMLParserException.class); junitExpectedException.expectMessage(MessageFormatUtil.format(StyledXMLParserException.NAN, "null")); String value = null; CssUtils.parseAbsoluteLength(value); }
@Test public void parseAbsoluteLengthFrom10px() { String value = "10px"; float actual = CssUtils.parseAbsoluteLength(value, CommonCssConstants.PX); float expected = 7.5f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseAbsoluteLengthFrom10cm() { String value = "10cm"; float actual = CssUtils.parseAbsoluteLength(value, CommonCssConstants.CM); float expected = 283.46457f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseAbsoluteLengthFrom10in() { String value = "10in"; float actual = CssUtils.parseAbsoluteLength(value, CommonCssConstants.IN); float expected = 720.0f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseAbsoluteLengthFrom10pt() { String value = "10pt"; float actual = CssUtils.parseAbsoluteLength(value, CommonCssConstants.PT); float expected = 10.0f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseAboluteLengthExponential01() { String value = "1e2pt"; float actual = CssUtils.parseAbsoluteLength(value); float expected = 1e2f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseAboluteLengthExponential02() { String value = "1e2px"; float actual = CssUtils.parseAbsoluteLength(value); float expected = 1e2f * 0.75f; Assert.assertEquals(expected, actual, 0); }
@Test @LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.UNKNOWN_ABSOLUTE_METRIC_LENGTH_PARSED, count = 1)}) public void parseAbsoluteLengthFromUnknownType() { String value = "10pateekes"; float actual = CssUtils.parseAbsoluteLength(value, "pateekes"); float expected = 10.0f; Assert.assertEquals(expected, actual, 0); }
@Test public void parseLength() { Assert.assertEquals(9, CssUtils.parseAbsoluteLength("12"), 0); Assert.assertEquals(576, CssUtils.parseAbsoluteLength("8inch"), 0); Assert.assertEquals(576, CssUtils.parseAbsoluteLength("8", CommonCssConstants.IN), 0); }
@Test public void parseAbsoluteLengthTest() { Assert.assertEquals(75, CssUtils.parseAbsoluteLength("100", CommonCssConstants.PX), EPS); Assert.assertEquals(75, CssUtils.parseAbsoluteLength("100px"), EPS); }
@Test public void parseAbsoluteLength12cmTest() { Assert.assertEquals(340.15747f, CssUtils.parseAbsoluteLength("12cm"), 0f); }
@Test public void parseAbsoluteLength12qTest() { Assert.assertEquals(8.503937f, CssUtils.parseAbsoluteLength("12q"), 0f); }