Search in sources :

Example 26 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method withMultiValueMapWithDelimiter.

@Test
public void withMultiValueMapWithDelimiter() throws Exception {
    String delimiter = " | ";
    this.tag.setDelimiter(delimiter);
    this.tag.setPath("stringArray");
    Map m = new LinkedHashMap();
    m.put("foo", "FOO");
    m.put("bar", "BAR");
    m.put("baz", "BAZ");
    this.tag.setItems(m);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("foo", checkboxElement1.attribute("value").getValue());
    assertEquals("FOO", spanElement1.getStringValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("bar", checkboxElement2.attribute("value").getValue());
    assertEquals(delimiter + "BAR", spanElement2.getStringValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("baz", checkboxElement3.attribute("value").getValue());
    assertEquals(delimiter + "BAZ", spanElement3.getStringValue());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 27 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method withMultiValueWithReverseEditor.

@Test
public void withMultiValueWithReverseEditor() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "FOO", "BAR", "BAZ" });
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    MyLowerCaseEditor editor = new MyLowerCaseEditor();
    bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, editor);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("FOO", checkboxElement1.attribute("value").getValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("BAR", checkboxElement2.attribute("value").getValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("BAZ", checkboxElement3.attribute("value").getValue());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.Test)

Example 28 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method collectionOfItemPets.

/**
	 * Test case where items toString() doesn't fit the item ID
	 */
@Test
public void collectionOfItemPets() throws Exception {
    this.tag.setPath("someSet");
    List allPets = new ArrayList();
    allPets.add(new ItemPet("PET1"));
    allPets.add(new ItemPet("PET2"));
    allPets.add(new ItemPet("PET3"));
    this.tag.setItems(allPets);
    this.tag.setItemValue("name");
    this.tag.setItemLabel("label");
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("someSet", checkboxElement1.attribute("name").getValue());
    assertNotNull("should be checked", checkboxElement1.attribute("checked"));
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("PET1", checkboxElement1.attribute("value").getValue());
    assertEquals("PET1", spanElement1.getStringValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("someSet", checkboxElement2.attribute("name").getValue());
    assertNotNull("should be checked", checkboxElement2.attribute("checked"));
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("PET2", checkboxElement2.attribute("value").getValue());
    assertEquals("PET2", spanElement2.getStringValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("someSet", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("PET3", checkboxElement3.attribute("value").getValue());
    assertEquals("PET3", spanElement3.getStringValue());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) Test(org.junit.Test)

Example 29 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method withMultiValueArrayAndDynamicAttributes.

@Test
public void withMultiValueArrayAndDynamicAttributes() throws Exception {
    String dynamicAttribute1 = "attr1";
    String dynamicAttribute2 = "attr2";
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "foo", "bar", "baz" });
    this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
    this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("foo", checkboxElement1.attribute("value").getValue());
    assertEquals("foo", spanElement1.getStringValue());
    assertEquals(dynamicAttribute1, checkboxElement1.attribute(dynamicAttribute1).getValue());
    assertEquals(dynamicAttribute2, checkboxElement1.attribute(dynamicAttribute2).getValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("bar", checkboxElement2.attribute("value").getValue());
    assertEquals("bar", spanElement2.getStringValue());
    assertEquals(dynamicAttribute1, checkboxElement2.attribute(dynamicAttribute1).getValue());
    assertEquals(dynamicAttribute2, checkboxElement2.attribute(dynamicAttribute2).getValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("baz", checkboxElement3.attribute("value").getValue());
    assertEquals("baz", spanElement3.getStringValue());
    assertEquals(dynamicAttribute1, checkboxElement3.attribute(dynamicAttribute1).getValue());
    assertEquals(dynamicAttribute2, checkboxElement3.attribute(dynamicAttribute2).getValue());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.Test)

Example 30 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method withMultiValueWithFormatter.

@Test
public void withMultiValueWithFormatter() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "   foo", "   bar", "   baz" });
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    FormattingConversionService cs = new FormattingConversionService();
    cs.addFormatterForFieldType(String.class, new Formatter<String>() {

        @Override
        public String print(String object, Locale locale) {
            return object;
        }

        @Override
        public String parse(String text, Locale locale) throws ParseException {
            return text.trim();
        }
    });
    bindingResult.initConversion(cs);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("   foo", checkboxElement1.attribute("value").getValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("   bar", checkboxElement2.attribute("value").getValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("   baz", checkboxElement3.attribute("value").getValue());
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ParseException(java.text.ParseException) Document(org.dom4j.Document) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.Test)

Aggregations

SAXReader (org.dom4j.io.SAXReader)172 Document (org.dom4j.Document)143 Element (org.dom4j.Element)128 StringReader (java.io.StringReader)98 Test (org.junit.Test)78 ArrayList (java.util.ArrayList)35 List (java.util.List)35 File (java.io.File)28 DocumentException (org.dom4j.DocumentException)21 IOException (java.io.IOException)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 Map (java.util.Map)10 Attribute (org.dom4j.Attribute)10 InputStream (java.io.InputStream)9 XMLWriter (org.dom4j.io.XMLWriter)9 OutputFormat (org.dom4j.io.OutputFormat)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6