XmlHelper { public static Optional<String> readAttribute(XMLStreamReader reader, String attr) { return Optional.ofNullable(reader.getAttributeValue(null, attr)); } private  XmlHelper(); static Optional<String> readAttribute(XMLStreamReader reader, String attr); static Optional<Integer> readIntAttribute(XMLStreamReader reader, String attr); static Optional<Double> readDoubleAttribute(XMLStreamReader reader, String attr); static int readIntAttribute(XMLStreamReader reader, String attr, int defaultValue); static double readDoubleAttribute(XMLStreamReader reader, String attr, double defaultValue); static boolean readBooleanAttribute(XMLStreamReader reader, String attr,
      boolean defaultValue); static int parseInt(String i); static void close(XMLStreamReader reader); static final String KEYBOARD; static final String ROW; static final String KEY; static final String ATTR_KEY_WIDTH; static final String ATTR_KEY_HEIGHT; static final String ATTR_H_GAP; static final String ATTR_V_GAP; static final String ATTR_ROW_EDGE_FLAGS; static final String ATTR_KEY_EDGE_FLAGS; static final String ATTR_KEY_LABEL; static final String ATTR_KEY_LABEL_STYLE; static final String ATTR_KEY_ICON_STYLE; static final String ATTR_KEY_OUTPUT_TEXT; static final String ATTR_CODES; static final String ATTR_MOVABLE; static final String ATTR_REPEATABLE; static final String ATTR_STICKY; static final String FLAG_RIGHT; static final String FLAG_LEFT; }
@Test public void readAttribute() throws XMLStreamException, FactoryConfigurationError, IOException { URL url = XmlHelperTest.class.getResource("/xml/default/kb-layout.xml"); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(url.openStream()); reader.next(); reader.require(XMLStreamConstants.START_ELEMENT, null, XmlHelper.KEYBOARD); assertEquals("40", XmlHelper.readAttribute(reader, XmlHelper.ATTR_KEY_WIDTH).orElse(null)); assertEquals("30", XmlHelper.readAttribute(reader, XmlHelper.ATTR_KEY_HEIGHT).orElse(null)); assertEquals("0", XmlHelper.readAttribute(reader, XmlHelper.ATTR_H_GAP).orElse(null)); assertEquals("0", XmlHelper.readAttribute(reader, XmlHelper.ATTR_V_GAP).orElse(null)); assertNull(XmlHelper.readAttribute(reader, "verticalGapX").orElse(null)); assertNull(XmlHelper.readAttribute(reader, "").orElse(null)); assertNull(XmlHelper.readAttribute(reader, null).orElse(null)); }