class 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);
  }

}

class XmlHelperTest {

  @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));
      }
    }
  }

}