use of org.dom4j.Attribute in project tmdm-studio-se by Talend.
the class XmlUtil method iterateAttribute.
public static void iterateAttribute(Element element, AttributeProcess attributeProcess) throws DocumentException {
// iterate through attributes of element
for (Iterator i = element.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute) i.next();
// do something
attributeProcess.process(attribute);
}
}
use of org.dom4j.Attribute in project tmdm-studio-se by Talend.
the class XmlUtil method findLinks.
public static List findLinks(Document document) throws DocumentException {
List<String> urls = new ArrayList();
// $NON-NLS-1$
List list = document.selectNodes("//a/@href");
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Attribute attribute = (Attribute) iter.next();
String url = attribute.getValue();
urls.add(url);
}
return urls;
}
use of org.dom4j.Attribute in project tmdm-studio-se by Talend.
the class XmlUtilTest method testIterateAttribute.
/*
* iterateAttribute(Element element, AttributeProcess attributeProcess)
*/
@Test
public void testIterateAttribute() {
// $NON-NLS-1$
String tag = "name";
Element element = DocumentHelper.createElement(tag);
// $NON-NLS-1$ //$NON-NLS-2$
Attribute attr1 = DocumentHelper.createAttribute(element, "Attr1", "Value1");
// $NON-NLS-1$ //$NON-NLS-2$
Attribute attr2 = DocumentHelper.createAttribute(element, "Attr2", "Value2");
// $NON-NLS-1$ //$NON-NLS-2$
Attribute attr3 = DocumentHelper.createAttribute(element, "Attr3", "Value3");
element.add(attr1);
element.add(attr2);
element.add(attr3);
AttributeProcess mockAttributeProcess = mock(AttributeProcess.class);
try {
XmlUtil.iterateAttribute(element, mockAttributeProcess);
verify(mockAttributeProcess).process(attr1);
verify(mockAttributeProcess).process(attr2);
verify(mockAttributeProcess).process(attr3);
} catch (DocumentException e) {
log.error(e.getMessage(), e);
}
}
use of org.dom4j.Attribute in project tmdm-studio-se by Talend.
the class XmlUtilTest method testFindLink.
@Test
public void testFindLink() {
Document doc = DocumentHelper.createDocument();
// $NON-NLS-1$
Element rootElement = DocumentHelper.createElement("root");
doc.add(rootElement);
// $NON-NLS-1$
Element linkElement1 = DocumentHelper.createElement("a");
// $NON-NLS-1$
String url1 = "url1";
// $NON-NLS-1$
Attribute attr1 = DocumentHelper.createAttribute(rootElement, "href", url1);
linkElement1.add(attr1);
//
rootElement.add(linkElement1);
// $NON-NLS-1$
Element linkElement2 = DocumentHelper.createElement("a");
// $NON-NLS-1$
String url2 = "url2";
// $NON-NLS-1$
Attribute attr2 = DocumentHelper.createAttribute(rootElement, "href", url2);
linkElement2.add(attr2);
// $NON-NLS-1$
Element childElement = DocumentHelper.createElement("child");
rootElement.add(childElement);
childElement.add(linkElement2);
try {
List<?> links = XmlUtil.findLinks(doc);
assertTrue(links.contains(url1));
assertTrue(links.contains(url2));
} catch (DocumentException e) {
log.error(e.getMessage(), e);
}
}
use of org.dom4j.Attribute in project tmdm-studio-se by Talend.
the class XmlUtilTest method testTreeWalk.
/*
* treeWalk(Element element, NodeProcess nodeProcess)
*/
@Test
public void testTreeWalk() {
// $NON-NLS-1$
String tag = "name";
Element parent = DocumentHelper.createElement(tag);
// $NON-NLS-1$ //$NON-NLS-2$
Attribute attr1 = DocumentHelper.createAttribute(parent, "Attr", "Value");
// $NON-NLS-1$
CDATA cDATA1 = DocumentHelper.createCDATA("cdata");
// $NON-NLS-1$
Comment comment1 = DocumentHelper.createComment("comment");
// $NON-NLS-1$ //$NON-NLS-2$
Entity entity1 = DocumentHelper.createEntity("entity", "entityname");
// $NON-NLS-1$
Text text1 = DocumentHelper.createText("text");
parent.add(attr1);
parent.add(cDATA1);
parent.add(comment1);
parent.add(entity1);
parent.add(text1);
// $NON-NLS-1$
Element childElement = DocumentHelper.createElement("child");
parent.add(childElement);
// $NON-NLS-1$ //$NON-NLS-2$
Attribute attr2 = DocumentHelper.createAttribute(parent, "Attr", "Value");
// $NON-NLS-1$
CDATA cDATA2 = DocumentHelper.createCDATA("cdata");
// $NON-NLS-1$
Comment comment2 = DocumentHelper.createComment("comment");
// $NON-NLS-1$ //$NON-NLS-2$
Entity entity2 = DocumentHelper.createEntity("entity", "entityname");
// $NON-NLS-1$
Text text2 = DocumentHelper.createText("text");
childElement.add(attr2);
childElement.add(cDATA2);
childElement.add(comment2);
childElement.add(entity2);
childElement.add(text2);
NodeProcess mockNodeProcess = mock(NodeProcess.class);
XmlUtil.treeWalk(parent, mockNodeProcess);
verify(mockNodeProcess, times(0)).process(attr1);
verify(mockNodeProcess).process(cDATA1);
verify(mockNodeProcess).process(comment1);
verify(mockNodeProcess).process(entity1);
verify(mockNodeProcess).process(text1);
verify(mockNodeProcess, times(0)).process(attr2);
verify(mockNodeProcess).process(cDATA2);
verify(mockNodeProcess).process(comment2);
verify(mockNodeProcess).process(entity2);
verify(mockNodeProcess).process(text2);
}
Aggregations