Form extends AbstractHtml { public String getNameBasedJsObject( final Collection<String> onlyForTagNames) { final StringBuilder jsObjectBuilder = new StringBuilder("{"); final Set<String> appendedValues = new HashSet<>(); loopThroughAllNestedChildren(child -> { final String tagName = child.getTagName(); if (onlyForTagNames.contains(tagName)) { final AbstractAttribute nameAttr = child .getAttributeByName(AttributeNameConstants.NAME); final AbstractAttribute typeAttr = child .getAttributeByName(AttributeNameConstants.TYPE); if (nameAttr != null) { final String nameAttrValue = nameAttr.getAttributeValue(); if (!appendedValues.contains(nameAttrValue)) { if (typeAttr != null) { final String typeAttrValue = typeAttr .getAttributeValue(); if (Type.CHECKBOX.equals(typeAttrValue) || Type.RADIO.equals(typeAttrValue)) { jsObjectBuilder.append(nameAttrValue) .append(':').append(nameAttrValue) .append(".checked,"); appendedValues.add(nameAttrValue); } else { jsObjectBuilder.append(nameAttrValue) .append(':').append(nameAttrValue) .append(".value,"); appendedValues.add(nameAttrValue); } } else { jsObjectBuilder.append(nameAttrValue).append(':') .append(nameAttrValue).append(".value,"); appendedValues.add(nameAttrValue); } } } } return true; }, false, this); return jsObjectBuilder.replace(jsObjectBuilder.length() - 1, jsObjectBuilder.length(), "}").toString(); } Form(final AbstractHtml base,
            final AbstractAttribute... attributes); String getNameBasedJsObject(
            final Collection<String> onlyForTagNames); String getIdBasedJsObject(final Collection<String> onlyForTagNames); String getIdBasedJsObject(final String functionName,
            final Collection<String> onlyForTagNames); String getNameBasedJsObject(); String getIdBasedJsObject(); String getIdBasedJsObject(final String functionName); String getIdBasedJsObjectPlus(
            final Collection<String> additionalTagNames); String getIdBasedJsObjectPlus(final String functionName,
            final Collection<String> additionalTagNames);  }
@Test public void testGetNameBasedJsObjectSetOfString() { final Form form = new Form(null) { { new Input(this, new Name("name1")); new Div(this){{new Input(this, new Name("name2"));}}; new Input(this, new Name("name3")); new Div(this){{new Input(this, new Name("name4"));}}; } }; final String jsObjectForNames = form.getNameBasedJsObject( new HashSet<String>(Arrays.asList("input"))); Assert.assertEquals("{name1:name1.value,name3:name3.value,name4:name4.value,name2:name2.value}", jsObjectForNames); }
@Test public void testGetNameBasedJsObjectSetOfString2() { final Form form = new Form(null) { { new Input(this, new Name("name1")); new Div(this){{new Input(this, new Name("name2"));}}; new Input(this, new Name("name3")); new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; } }; final String jsObjectForNames = form.getNameBasedJsObject( new HashSet<String>(Arrays.asList("input"))); Assert.assertEquals("{name1:name1.value,name3:name3.value,name4:name4.value,name2:name2.value}", jsObjectForNames); }
@Test public void testGetNameBasedJsObjectSetOfString3() { final Form form = new Form(null) { { new Input(this, new Name("name1")); new Div(this, new Name("excluded")){{new Input(this, new Name("name2"));}}; new Input(this, new Name("name3")); new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; new TextArea(this, new Name("excluded")); } }; final String jsObjectForNames = form.getNameBasedJsObject( new HashSet<String>(Arrays.asList("input"))); Assert.assertEquals("{name1:name1.value,name3:name3.value,name4:name4.value,name2:name2.value}", jsObjectForNames); }
@Test public void testGetNameBasedJsObject() { final Form form = new Form(null) { { new Input(this, new Name("name1")); new Div(this, new Name("excluded")){{new Input(this, new Name("name2"));}}; new Input(this, new Name("name3")); new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name4"));}}; new Div(this){{new Input(this, new Name("name5"), new Type(Type.TEXT));}}; new Div(this){{new Input(this, new Name("name6"), new Type(Type.CHECKBOX));}}; new Div(this){{new Input(this, new Name("name7"), new Type(Type.RADIO));}}; new TextArea(this, new Name("included")); } }; final String jsObjectForNames = form.getNameBasedJsObject(); Assert.assertEquals("{name1:name1.value,name3:name3.value,included:included.value,name7:name7.checked,name6:name6.checked,name5:name5.value,name4:name4.value,name2:name2.value}", jsObjectForNames); }