1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.dataportal.elements;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.stream.Collectors;
|
16
|
|
17
|
import org.openqa.selenium.By;
|
18
|
import org.openqa.selenium.NoSuchElementException;
|
19
|
import org.openqa.selenium.WebElement;
|
20
|
|
21
|
import com.thoughtworks.selenium.SeleniumException;
|
22
|
|
23
|
import eu.etaxonomy.dataportal.ElementUtils;
|
24
|
|
25
|
/**
|
26
|
* @author a.kohlbecker
|
27
|
* @since Aug 13, 2020
|
28
|
*/
|
29
|
public class DescriptionList extends BaseElement {
|
30
|
|
31
|
private Map<String, List<DescriptionElement>> descriptionGroups = new HashMap<>();
|
32
|
|
33
|
/**
|
34
|
* @param element A WebElement for a DescriptionList ('{@code<dl>}') DOM object.
|
35
|
*/
|
36
|
public DescriptionList(WebElement element) {
|
37
|
super(element);
|
38
|
if(!element.getTagName().equals("dl")) {
|
39
|
throw new SeleniumException("Expecting DescriptionList (<dl>) as paramter element, but was " + ElementUtils.webElementTagToMarkup(element));
|
40
|
}
|
41
|
List<DescriptionElement> descriptionElements = null;
|
42
|
for(WebElement we : element.findElements(By.xpath("./child::*"))){
|
43
|
if(we.getTagName().equals("dt")) {
|
44
|
descriptionElements = new ArrayList<>();
|
45
|
descriptionGroups.put(we.getText(), descriptionElements);
|
46
|
}
|
47
|
if(we.getTagName().equals("dd")) {
|
48
|
if(descriptionElements == null) {
|
49
|
// oops, this is a dd element without preceding dt
|
50
|
descriptionElements = new ArrayList<>();
|
51
|
descriptionGroups.put("MISSING DT ELEMENT BEFORE DD", descriptionElements);
|
52
|
}
|
53
|
descriptionElements.add(new DescriptionElement(we));
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
*
|
60
|
* @return the descriptionGroups
|
61
|
*/
|
62
|
public Map<String, List<DescriptionElement>> getDescriptionGroups() {
|
63
|
return descriptionGroups;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
*
|
68
|
* @param descriptionGroupsKey The text of the {@code <dt>} element preceding
|
69
|
* one or more {@code <dd>} elements. Text of multiple {@code <dd>} will be joined
|
70
|
* using the new line ('{@code \n}') character.
|
71
|
*
|
72
|
* @return the DescriptionElementTexts joined with '{@code \n}' or null
|
73
|
* if the key is not present in the map.
|
74
|
*/
|
75
|
public String joinedDescriptionElementText(String descriptionGroupsKey) {
|
76
|
String joinedText = null;
|
77
|
if(descriptionGroups.containsKey((descriptionGroupsKey))) {
|
78
|
joinedText = descriptionGroups.get(descriptionGroupsKey)
|
79
|
.stream()
|
80
|
.map(de -> de.getDescriptionElementText())
|
81
|
.collect(Collectors.joining("\n"));
|
82
|
}
|
83
|
return joinedText;
|
84
|
}
|
85
|
|
86
|
public static class DescriptionElement {
|
87
|
|
88
|
private DescriptionList subList = null;
|
89
|
private BaseElement descriptionElementContent = null;
|
90
|
private String descriptionElementText = null;
|
91
|
|
92
|
DescriptionElement(WebElement descriptionElement){
|
93
|
try {
|
94
|
subList = new DescriptionList(descriptionElement.findElement(By.tagName("dl")));
|
95
|
descriptionElementContent = subList;
|
96
|
} catch (NoSuchElementException e1) {
|
97
|
try {
|
98
|
descriptionElementContent = new BaseElement(descriptionElement.findElement(By.xpath("./child::*")));
|
99
|
} catch (NoSuchElementException e2) {
|
100
|
descriptionElementText = descriptionElement.getText();
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* @return the subList
|
107
|
*/
|
108
|
public DescriptionList getSubList() {
|
109
|
return subList;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* @return the descriptionElementContent
|
114
|
*/
|
115
|
public BaseElement getDescriptionElementContent() {
|
116
|
return descriptionElementContent;
|
117
|
}
|
118
|
|
119
|
public String getDescriptionElementText() {
|
120
|
if(descriptionElementContent != null) {
|
121
|
return descriptionElementContent.getText();
|
122
|
} else {
|
123
|
return descriptionElementText;
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
|
128
|
}
|