private void copyAttributes(Element srcNode, Element destNode) { for (Object attrObj : srcNode.attributes()) { final Attribute attr = (Attribute) attrObj; destNode.addAttribute(attr.getQName(), attr.getValue()); } }
private static void addOrModifyAttribute(Element parent, String name, String value) { final Attribute attribute = parent.attribute( name ); if ( attribute == null ) { parent.addAttribute( name, value ); } else { attribute.setValue( value ); } }
public Route(Route route) { Element elementCopy = route.element.createCopy(); docFactory.createDocument().add(elementCopy); this.element = elementCopy; // Copy cached JIDs (for performance reasons) this.toJID = route.toJID; this.fromJID = route.fromJID; }
private void validateOutput(String output, boolean selected) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("country", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) rootElement.selectSingleNode("option[@value = 'UK']"); Attribute selectedAttr = e.attribute("selected"); if (selected) { assertTrue(selectedAttr != null && "selected".equals(selectedAttr.getValue())); } else { assertNull(selectedAttr); } }
@SuppressWarnings("rawtypes") private void assertHtmlOutput(String output) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); List nodes = document.getRootElement().selectNodes("/html/body/table/tr"); Element tr1 = (Element) nodes.get(0); assertRowElement(tr1, "1", "Whatsit", "12.99"); Element tr2 = (Element) nodes.get(1); assertRowElement(tr2, "2", "Thingy", "13.99"); Element tr3 = (Element) nodes.get(2); assertRowElement(tr3, "3", "Gizmo", "14.99"); Element tr4 = (Element) nodes.get(3); assertRowElement(tr4, "4", "Cranktoggle", "11.99"); }
public List<Element> getStanzaElements() { return document.getRootElement().elements(); }
public Element asXMLElement() { Element option = DocumentHelper.createElement(QName.get("option", "jabber:x:data")); if (getLabel() != null) { option.addAttribute("label", getLabel()); } if (getValue() != null) { option.addElement("value").addText(getValue()); } return option; } }
public String asXML() { return document.getRootElement().asXML(); }
private void assertRowElement(Element elem, String id, String name, String price) { Element idElem = (Element) elem.elements().get(0); Element nameElem = (Element) elem.elements().get(1); Element priceElem = (Element) elem.elements().get(2); assertEquals("ID incorrect.", id, idElem.getText()); assertEquals("Name incorrect.", name, nameElem.getText()); assertEquals("Price incorrect.", price, priceElem.getText()); }
protected static String createErrorBody(String type, String condition) { final Element body = DocumentHelper.createElement( QName.get( "body", "http://jabber.org/protocol/httpbind" ) ); body.addAttribute("type", type); body.addAttribute("condition", condition); return body.asXML(); }
@Override public Element getSubsriptionErrorDetail() { return DocumentHelper.createElement( QName.get("closed-node", "http://jabber.org/protocol/pubsub#errors")); }
@Test public void withSingleValueBooleanChecked() throws Exception { this.tag.setPath("jedi"); 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 checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("jedi", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); assertEquals("true", checkboxElement.attribute("value").getValue()); }
@Override public Element getSubsriptionErrorDetail() { return DocumentHelper.createElement( QName.get("not-in-roster-group", "http://jabber.org/protocol/pubsub#errors")); }
private void assertStringArray() throws JspException, DocumentException { int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("name", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) rootElement.selectSingleNode("option[text() = 'Rob']"); assertEquals("Rob node not selected", "selected", e.attribute("selected").getValue()); }
@Override public Element getSubsriptionErrorDetail() { return DocumentHelper.createElement(QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors")); }
@Test public void collectionOfPetsAsString() throws Exception { this.tag.setPath("pets"); this.tag.setValue("Spot"); 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 checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("pets", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); }
@Override public Element getSubsriptionErrorDetail() { return DocumentHelper.createElement(QName.get("presence-subscription-required", "http://jabber.org/protocol/pubsub#errors")); }
@Test public void collectionOfColoursSelected() throws Exception { this.tag.setPath("otherColours"); this.tag.setValue("RED"); 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 checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("otherColours", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); }
@Test public void collectionOfPetsAsStringNotSelected() throws Exception { this.tag.setPath("pets"); this.tag.setValue("Santa's Little Helper"); 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 checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("pets", checkboxElement.attribute("name").getValue()); assertNull(checkboxElement.attribute("checked")); }
@Test public void collectionOfColoursNotSelected() throws Exception { this.tag.setPath("otherColours"); this.tag.setValue("PURPLE"); 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 checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("otherColours", checkboxElement.attribute("name").getValue()); assertNull(checkboxElement.attribute("checked")); }