XmlHelper { public static boolean readBooleanAttribute(XMLStreamReader reader, String attr, boolean defaultValue) { String a = reader.getAttributeValue(null, attr); if (a == null || a.isEmpty()) { return defaultValue; } return Boolean.parseBoolean(a); } 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 readBooleanAttribute() 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); assertFalse(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_REPEATABLE, false)); assertFalse(XmlHelper.readBooleanAttribute(reader, "", false)); while (reader.hasNext()) { reader.next(); if (!reader.isStartElement() || !XmlHelper.KEY.equals(reader.getLocalName())) { continue; } if (32 == XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_CODES, -1)) { assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_REPEATABLE, false)); assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_MOVABLE, false)); } if (-1 == XmlHelper.readIntAttribute(reader, XmlHelper.ATTR_CODES, 0)) { assertTrue(XmlHelper.readBooleanAttribute(reader, XmlHelper.ATTR_STICKY, false)); } } }