XMLUtil { @Pure @SuppressWarnings({"checkstyle:magicnumber", "checkstyle:npathcomplexity"}) public static int parseColor(String xmlColor) throws ColorFormatException { if (xmlColor == null || "".equals(xmlColor)) { return 0; } if (xmlColor.startsWith("#")) { try { final String str = xmlColor.substring(1); if (str.length() == 6) { return 0xFF000000 | decodeHexInteger(str.toString()); } return decodeHexInteger(str.toString()); } catch (NumberFormatException e) { throw new ColorFormatException(xmlColor); } } Matcher matcher = HTML_RGB_PATTERN.matcher(xmlColor); if (matcher.find()) { final int red = decodeDecInteger(matcher.group(1)); final int green = decodeDecInteger(matcher.group(2)); final int blue = decodeDecInteger(matcher.group(3)); return encodeRgbaColor(red, green, blue, 0xFF); } matcher = HTML_RGBA_PATTERN.matcher(xmlColor); if (matcher.find()) { final int red = decodeDecInteger(matcher.group(1)); final int green = decodeDecInteger(matcher.group(2)); final int blue = decodeDecInteger(matcher.group(3)); final int alpha = decodeDecInteger(matcher.group(4)); return encodeRgbaColor(red, green, blue, alpha); } matcher = HTML_HSL_PATTERN.matcher(xmlColor); if (matcher.find()) { final double hue = decodeFactor(matcher.group(1)); final double saturation = decodeFactor(matcher.group(2)); final double lightness = decodeFactor(matcher.group(3)); return encodeHslaColor(hue, saturation, lightness, 0xFF); } matcher = HTML_HSLA_PATTERN.matcher(xmlColor); if (matcher.find()) { final double hue = decodeFactor(matcher.group(1)); final double saturation = decodeFactor(matcher.group(2)); final double lightness = decodeFactor(matcher.group(3)); final int alpha = decodeDecInteger(matcher.group(4)); return encodeHslaColor(hue, saturation, lightness, alpha); } final Integer color = ColorNames.getColorFromName(xmlColor); if (color != null) { return color.intValue(); } throw new ColorFormatException(xmlColor); } private  XMLUtil(); @Pure static boolean getAttributeBoolean(Node document, boolean caseSensitive, String... path); @Pure static boolean getAttributeBoolean(Node document, String... path); @Pure static Boolean getAttributeBooleanWithDefault(Node document, boolean caseSensitive,
			Boolean defaultValue, String... path); @Pure static Boolean getAttributeBooleanWithDefault(Node document, Boolean defaultValue, String... path); @Pure static Class<?> getAttributeClass(Node document, boolean caseSensitive, String... path); @Pure static Class<?> getAttributeClass(Node document, String... path); @Pure static Class<?> getAttributeClassWithDefault(Node document, boolean caseSensitive,
			Class<?> defaultValue, String... path); @Pure static Class<?> getAttributeClassWithDefault(Node document, Class<?> defaultValue, String... path); @Pure static int getAttributeColor(Node document, boolean caseSensitive, String... path); @Pure static int getAttributeColor(Node document, String... path); @Pure static Integer getAttributeColorWithDefault(Node document, boolean caseSensitive, Integer defaultValue,
			String... path); @Pure static Integer getAttributeColorWithDefault(Node document, Integer defaultValue, String... path); @Pure static Date getAttributeDate(Node document, boolean caseSensitive, String... path); @Pure static Date getAttributeDate(Node document, String... path); @Pure static Date getAttributeDateWithDefault(Node document, boolean caseSensitive, Date defaultValue, String... path); @Pure static Date getAttributeDateWithDefault(Node document, Date defaultValue, String... path); @Pure static double getAttributeDouble(Node document, boolean caseSensitive, String... path); @Pure static double getAttributeDouble(Node document, String... path); @Pure static Double getAttributeDoubleWithDefault(Node document, boolean caseSensitive, Double defaultValue,
			String... path); @Pure static Double getAttributeDoubleWithDefault(Node document, Double defaultValue, String... path); @Pure static T getAttributeEnum(Node document, Class<T> type, boolean caseSensitive, String... path); @Pure static T getAttributeEnum(Node document, Class<T> type, String... path); @Pure static T getAttributeEnumWithDefault(Node document, Class<T> type,
			boolean caseSensitive, T defaultValue, String... path); @Pure static T getAttributeEnumWithDefault(Node document, Class<T> type,
			T defaultValue, String... path); @Pure static float getAttributeFloat(Node document, boolean caseSensitive, String... path); @Pure static float getAttributeFloat(Node document, String... path); @Pure static Float getAttributeFloatWithDefault(Node document, boolean caseSensitive, Float defaultValue, String... path); @Pure static Float getAttributeFloatWithDefault(Node document, Float defaultValue, String... path); @Pure static int getAttributeInt(Node document, boolean caseSensitive, String... path); @Pure static int getAttributeInt(Node document, String... path); @Pure static Integer getAttributeIntWithDefault(Node document, boolean caseSensitive, Integer defaultValue, String... path); @Pure static Integer getAttributeIntWithDefault(Node document, Integer defaultValue, String... path); @Pure static long getAttributeLong(Node document, boolean caseSensitive, String... path); @Pure static long getAttributeLong(Node document, String... path); @Pure static Long getAttributeLongWithDefault(Node document, boolean caseSensitive, Long defaultValue, String... path); @Pure static Long getAttributeLongWithDefault(Node document, Long defaultValue, String... path); @Pure static URL getAttributeURL(Node document, boolean caseSensitive, String... path); @Pure static URL getAttributeURL(Node document, String... path); @Pure static URL getAttributeURLWithDefault(Node document, boolean caseSensitive, URL defaultValue, String... path); @Pure static URL getAttributeURLWithDefault(Node document, URL defaultValue, String... path); @Pure static UUID getAttributeUUID(Node document, boolean caseSensitive, String... path); @Pure static UUID getAttributeUUID(Node document, String... path); @Pure static List<UUID> getAttributeUUIDs(Node document, boolean caseSensitive, String... path); @Pure static List<UUID> getAttributeUUIDs(Node document, String... path); @Pure static UUID getAttributeUUIDWithDefault(Node document, boolean caseSensitive, UUID defaultValue, String... path); @Pure static UUID getAttributeUUIDWithDefault(Node document, UUID defaultValue, String... path); @Pure static String getAttributeValue(Node document, boolean casesSensitive, String... path); @Pure static String getAttributeValue(Node document, String... path); @Pure static String getAttributeValueWithDefault(Node document, boolean caseSensitive, String defaultValue,
			String... path); @Pure static String getAttributeValueWithDefault(Node document, String defaultValue, String... path); @Pure static T getChild(Node parent, Class<T> type); @Pure static Document getDocumentFor(Node node); @Pure static Element getElementFromPath(Node document, boolean caseSensitive, String... path); @Pure static Element getElementFromPath(Node document, String... path); @Pure static Element getElementMatching(Node document, XMLConstraint constraint, boolean caseSensitive, String... path); @Pure static Element getElementMatching(Node document, XMLConstraint constraint, String... path); @Pure static List<Element> getElementsFromPath(Node document, boolean caseSensitive, String... path); @Pure static List<Element> getElementsFromPath(Node document, String... path); @Pure static Node getNodeFromPath(Node document, boolean caseSensitive, String... path); @Pure static Node getNodeFromPath(Node document, String... path); @Pure static List<Node> getNodesFromPath(Node document, boolean caseSensitive, String... path); @Pure static List<Node> getNodesFromPath(Node document, String... path); @Pure static String getText(Node document, String... path); @Pure static Iterator<Node> iterate(Node parent, String nodeName); @Pure @SuppressWarnings({"checkstyle:magicnumber", "checkstyle:npathcomplexity"}) static int parseColor(String xmlColor); @Pure static Date parseDate(String xmlDate); @Pure static Object parseObject(String xmlSerializedObject); @Pure static byte[] parseString(String text); @Pure static Document parseXML(String xmlString); static Document readXML(File file); static Document readXML(InputStream stream); static Document readXML(Reader reader); static Document readXML(String file); static Document readXML(URL file); static DocumentFragment readXMLFragment(File file); static DocumentFragment readXMLFragment(File file, boolean skipRoot); static DocumentFragment readXMLFragment(InputStream stream); static DocumentFragment readXMLFragment(InputStream stream, boolean skipRoot); static DocumentFragment readXMLFragment(Reader reader); static DocumentFragment readXMLFragment(Reader reader, boolean skipRoot); static DocumentFragment readXMLFragment(String file); static DocumentFragment readXMLFragment(String file, boolean skipRoot); static DocumentFragment readXMLFragment(URL file); static DocumentFragment readXMLFragment(URL file, boolean skipRoot); static boolean setAttributeEnum(Node document, Class<T> type,
			boolean caseSensitive, T value, String... path); static boolean setAttributeEnum(Node document, Class<T> type, T value, String... path); @Pure @SuppressWarnings("checkstyle:magicnumber") static String toColor(int rgba); @Pure static String toColor(int red, int green, int blue, int alpha); @Pure static String toString(byte[] array); @Pure static String toString(Date date); @Pure static String toString(Node node); @Pure static String toString(Serializable object); static void writeXML(Document xmldocument, File file); static void writeXML(Document xmldocument, OutputStream stream); static void writeXML(Document xmldocument, String file); static void writeXML(Document xmldocument, Writer writer); static void writeXML(DocumentFragment fragment, File file); static void writeXML(DocumentFragment fragment, OutputStream stream); static void writeXML(DocumentFragment fragment, String file); static void writeXML(DocumentFragment fragment, Writer writer); static void writeXML(Node node, OutputStream stream); static void writeXML(Node node, Writer writer); static void writeResources(Element node, XMLResources resources, XMLBuilder builder); @SuppressWarnings({"checkstyle:nestedifdepth"}) static int readResources(Element node, XMLResources resources); @Pure static URL readResourceURL(Element node, XMLResources resources, String... path); static final String NODE_RESOURCE; static final String NODE_RESOURCES; static final String DATE_FORMAT; static final String ATTR_ID; static final String ATTR_NAME; static final String ATTR_COLOR; static final String ATTR_URL; static final String ATTR_MIMETYPE; static final String ATTR_FILE; }
@Test public void parseColor_valid() throws Exception { assertHexEquals(0xFF0000FF, XMLUtil.parseColor("blue")); assertHexEquals(0xFFFF0000, XMLUtil.parseColor("red")); assertHexEquals(0xFFFF0022, XMLUtil.parseColor("rgb(255, 0, 34)")); assertHexEquals(0xFF00FF01, XMLUtil.parseColor("#00FF01")); assertHexEquals(0x3400FF01, XMLUtil.parseColor("#3400FF01")); assertHexEquals(0x04FF0022, XMLUtil.parseColor("rgba(255, 0, 34, 4)")); assertHexEquals(0xFF333333, XMLUtil.parseColor("hsl(0.7, 0, 0.2)")); assertHexEquals(0x04333333, XMLUtil.parseColor("hsla(0.7, 0, 0.2, 4)")); assertHexEquals(0xFF333333, XMLUtil.parseColor("hsl(70%, 0%, 0.2)")); assertHexEquals(0x04333333, XMLUtil.parseColor("hsla(70%, 0%, 0.2, 4)")); assertHexEquals(0xFF23194C, XMLUtil.parseColor("hsl(0.7, .5, 0.2)")); assertHexEquals(0x0423194C, XMLUtil.parseColor("hsla(0.7, .5, 0.2, 4)")); assertHexEquals(0xFF23194C, XMLUtil.parseColor("hsl(70%, 50%, 0.2)")); assertHexEquals(0x0423194C, XMLUtil.parseColor("hsla(70%, 50%, 0.2, 4)")); }
@Test public void parseColor_invalid01() throws Exception { assertThrows(ColorFormatException.class, () -> assertHexEquals(0, XMLUtil.parseColor("0x00FF01"))); }
@Test public void parseColor_invalid02() throws Exception { assertThrows(ColorFormatException.class, () -> assertHexEquals(0, XMLUtil.parseColor("0xFF00FF01"))); }
@Test public void parseColor_invalid03() throws Exception { assertThrows(ColorFormatException.class, () -> assertHexEquals(0, XMLUtil.parseColor("1"))); }