Search in sources :

Example 16 with Attribute

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);
    }
}
Also used : Attribute(org.dom4j.Attribute) Iterator(java.util.Iterator)

Example 17 with 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;
}
Also used : Attribute(org.dom4j.Attribute) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Attribute

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);
    }
}
Also used : Attribute(org.dom4j.Attribute) AttributeProcess(com.amalto.workbench.utils.callback.AttributeProcess) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Test(org.junit.Test)

Example 19 with Attribute

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);
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document) Test(org.junit.Test)

Example 20 with Attribute

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);
}
Also used : Comment(org.dom4j.Comment) Entity(org.dom4j.Entity) NodeProcess(com.amalto.workbench.utils.callback.NodeProcess) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Text(org.dom4j.Text) CDATA(org.dom4j.CDATA) Test(org.junit.Test)

Aggregations

Attribute (org.dom4j.Attribute)114 Element (org.dom4j.Element)88 Document (org.dom4j.Document)30 ArrayList (java.util.ArrayList)28 List (java.util.List)26 Iterator (java.util.Iterator)23 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 File (java.io.File)11 SAXReader (org.dom4j.io.SAXReader)11 IOException (java.io.IOException)10 Map (java.util.Map)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 QTIObject (org.olat.ims.qti.editor.beecom.objects.QTIObject)6 Namespace (org.dom4j.Namespace)5 QName (org.dom4j.QName)5 PropertyString (org.pentaho.commons.util.repository.type.PropertyString)5 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 OutcomesProcessing (org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3