本文整理汇总了Java中com.gargoylesoftware.htmlunit.html.DomNode类的典型用法代码示例。如果您正苦于以下问题:Java DomNode类的具体用法?Java DomNode怎么用?Java DomNode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DomNode类属于com.gargoylesoftware.htmlunit.html包,在下文中一共展示了DomNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: showPageContent
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* Echo's the content of the current page to stdout. All empty lines will be
* removed and separator containing of dashed will be inserted before and
* after. This abuses the fact that all STiNE pages use a div element with
* the id pageContent.
*/
public void showPageContent() {
DomNode context = (DomNode) this.page.getElementById("pageContent");
if (context == null) {
context = this.page;
}
// This removes all the empty lines from the page's content and acquires
// the result.
String pageContent = Arrays.stream(context.asText().replaceAll("\\r\\n?", "\n").split("\n"))
.filter(line -> !line.matches("\\p{Z}*")).collect(Collectors.joining("\n"));
// This creates a 50 chars long sequence of dashes
String horzLine = new String(new char[50]).replace("\0", "-");
System.out.println(horzLine);
System.out.println(pageContent);
System.out.println(horzLine);
}
示例2: test
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public boolean test(Context context)
{
LoggerFactory.getLogger(MatchesPath.class).debug("Testing condition: " + this);
DomNode element = context.getXPathProcessor().getSingleElementOfType(Util.getCurrentPage(context), Util.replacePlaceholders(path, context), DomNode.class);
String elementValue = (element instanceof DomAttr) ? element.getNodeValue() : element.asText();
return elementValue.matches(regex);
}
示例3: getSingleElementOfType
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* Retrieves a single element from a {@link DomNode} by a given XPath expression.
* If exactly one object of the expected type is returned, this method returns the cast object.
* An exception is thrown if no object is found, more than one object is found or the object found is not of a subtype of {@code T}.
*
* @param node DomNode from which the element is retrieved.
* @param expression XPath expression.
* @param type Class of expected type of returned object.
* @param <T> Expected type of returned object.
* @return Retrieved object cast into type {@code T}.
*/
public <T> T getSingleElementOfType(DomNode node, String expression, Class<T> type)
{
List<?> elements = node.getByXPath(expression);
if(elements.size() == 0)
throw new RuntimeException("No element has been found for expression " + expression);
else if(elements.size() > 1)
throw new RuntimeException("Expecting exactly one element but " + elements.size() + " have been found for expression " + expression);
else if(!(type.isInstance(elements.get(0))))
throw new RuntimeException("Element for expression " + expression + " is not of type " + type.getName());
LoggerFactory.getLogger(XPathProcessor.class).debug("Found 1 element matching XPath expression " + expression);
return (T)elements.get(0);
}
示例4: getElementsOfType
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* Retrieves elements from a {@link DomNode} by a given XPath expression.
* If all objects are of a type compatible with {@code T}, this method returns a {@code List<T>}.
* An exception is thrown if at least one of the retrieved objects is not of a subtype of {@code T}.
*
* @param node DomNode from which the elements are retrieved.
* @param expression XPath expression.
* @param type Class of expected type of returned object.
* @param <T> Expected type of returned object.
* @return List of objects cast into type {@code T}.
*/
public <T> List<T> getElementsOfType(DomNode node, String expression, Class<T> type)
{
List<T> result = new ArrayList<>();
node.getByXPath(expression).forEach(element ->
{
if(!(type.isInstance(element)))
throw new RuntimeException("Elements for expression " + expression + " are not of type " + type.getName());
result.add((T)element);
});
LoggerFactory.getLogger(XPathProcessor.class).debug("Found " + result.size() + " " + (result.size() == 1 ? "element" : "elements") + " matching XPath expression " + expression);
return result;
}
示例5: execute
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context)
{
LoggerFactory.getLogger(AddToListPath.class).debug("Executing action: " + this);
List<DomNode> elements = context.getXPathProcessor().getElementsOfType(Util.getCurrentPage(context), Util.replacePlaceholders(path, context), DomNode.class);
elements.forEach(element -> context.addToList(listName, (element instanceof DomAttr) ? element.getNodeValue().trim() : element.asText().trim()));
}
示例6: execute
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context)
{
LoggerFactory.getLogger(SetVariablePath.class).debug("Executing action: " + this);
DomNode element = context.getXPathProcessor().getSingleElementOfType(Util.getCurrentPage(context), Util.replacePlaceholders(path, context), DomNode.class);
context.setVariable(variableName, (element instanceof DomAttr) ? element.getNodeValue() : element.asText());
}
示例7: execute
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context)
{
LoggerFactory.getLogger(AddToMap.class).debug("Executing action: " + this);
List<DomNode> keyElements = context.getXPathProcessor().getElementsOfType(Util.getCurrentPage(context), Util.replacePlaceholders(keyPath, context), DomNode.class);
List<DomNode> valueElements = context.getXPathProcessor().getElementsOfType(Util.getCurrentPage(context), Util.replacePlaceholders(valuePath, context), DomNode.class);
if(keyElements.size() != valueElements.size())
throw new RuntimeException("Number of keys and values does not match for key expression " + Util.replacePlaceholders(keyPath, context) + " and value expression " + Util.replacePlaceholders(valuePath, context));
for(int i = 0; i < keyElements.size(); i++)
context.addToMap(mapName, (keyElements.get(i) instanceof DomAttr) ? keyElements.get(i).getNodeValue() : keyElements.get(i).asText(), (valueElements.get(i) instanceof DomAttr) ? valueElements.get(i).getNodeValue() : valueElements.get(i).asText());
}
示例8: getAllDomNodes
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private Set<DomNode> getAllDomNodes(HtmlPage page) {
Set<DomNode> ret = new HashSet<>();
Iterable<DomNode> topLevelChildren = page.getBody().getChildren();
for (DomNode n : topLevelChildren) {
ret.addAll(recursiveGetChildren(n));
}
return ret;
}
示例9: recursiveGetChildren
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private Collection<? extends DomNode> recursiveGetChildren(DomNode n) {
Set<DomNode> ret = new HashSet<>();
if (n != null) {
ret.add(n);
Iterable<DomNode> children = n.getChildren();
for (DomNode c : children) {
ret.addAll(recursiveGetChildren(c));
}
}
return ret;
}
示例10: getEditedList
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private void getEditedList() throws FailingHttpStatusCodeException, MalformedURLException, IOException
{
List<String> articleListContributed=getEditedLinks();
WebClient browser=new WebClient(BrowserVersion.CHROME);
browser.getOptions().setJavaScriptEnabled(false);
browser.getOptions().setThrowExceptionOnFailingStatusCode(false);
browser.getOptions().setThrowExceptionOnScriptError(false);
String wikiURL="https://en.wikipedia.org/w/index.php?title=";
for(String s:articleListContributed)
{//*[@id="pagehistory"]/li[2]
String finalURL=wikiURL+s.replaceAll(" ", "_")+"&action=history";
//System.out.println(finalURL);
HtmlPage rawpage= browser.getPage(wikiURL+s.replaceAll(" ", "_")+"&action=history");
@SuppressWarnings("unchecked")
List<DomNode> totalEditsUsers =(List<DomNode>) rawpage.getByXPath("//span[@class='history-user']");
//System.out.println(getFirstEditIndex(totalEditsUsers));
@SuppressWarnings("unchecked")
List<DomNode> sizeArticles = (List<DomNode>) rawpage.getByXPath("//span[@class='history-size']");
int currentSize=getContentSize(sizeArticles,0);
int beforeEditSize=getContentSize(sizeArticles, getFirstEditIndex(totalEditsUsers)+1);
//System.out.println("Change in Size:"+(currentSize-beforeEditSize));
int changeInSize=(currentSize-beforeEditSize);
System.out.println(finalURL+"||"+changeInSize+"||"+getFirstEditIndex(totalEditsUsers));
}
}
示例11: getContentSize
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private static int getContentSize(List<DomNode> sizeArticles, int index)
{
int i=0;
String sizeInBytes=sizeArticles.get(index).getChildNodes().get(0).asText();
Pattern pattern = Pattern.compile("\\((.*?) bytes");
//System.out.println(sizeInBytes);
Matcher m=pattern.matcher(sizeInBytes);
if(m.find())
{
String bytes=m.group(1).replaceAll(",","");
i=Integer.parseInt(bytes);
}
return i;
}
示例12: getFirstEditIndex
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private static int getFirstEditIndex(List<DomNode> totalEditsUsers)
{
int i=0;
for(DomNode d:totalEditsUsers)
{
String collaboratorName=d.getChildNodes().get(0).asText();
if(collaboratorName.contentEquals(wikiUserName))
{
i=totalEditsUsers.indexOf(d);
}
}
return i;
}
示例13: test
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public void test() throws Exception {
String pageName = uniqueWikiPageName("HistoryTest");
HtmlPage page = editWikiPage(pageName, "Initial content", "", "", true);
page = editWikiPage(pageName, "Altered content", "", "s/Initial/Altered", false);
HtmlPage history = clickHistoryLink(page);
List<HtmlTableRow> historyRows = (List<HtmlTableRow>) history.getByXPath("//tr[td]");
assertEquals(3, historyRows.size());
HtmlTableRow altered = historyRows.get(1);
// First column is date/time.
verifyRow(altered, "s/Initial/Altered");
HtmlTableRow initial = historyRows.get(2);
verifyRow(initial, "None");
final List<HtmlSubmitInput> compareButtons = (List<HtmlSubmitInput>) history.getByXPath("//input[@type='submit' and @value='Compare']/.");
assertEquals(1, compareButtons.size());
HtmlPage diff = (HtmlPage) compareButtons.get(0).click();
assertEquals("Altered", ((DomNode) diff.getByXPath("//ins").iterator().next()).asText());
assertEquals("Initial", ((DomNode) diff.getByXPath("//del").iterator().next()).asText());
List<HtmlDivision> divs = (List<HtmlDivision>) diff.getByXPath("//div[@id='flash']/.");
assertEquals(0, divs.size());
// Check for the warning when viewing differences backwards
final List<HtmlRadioButtonInput> radioButtons = (List<HtmlRadioButtonInput>) history.getByXPath("//input[@type='radio']/.");
assertEquals(4, radioButtons.size());
radioButtons.get(0).click();
radioButtons.get(3).click();
diff = (HtmlPage) compareButtons.get(0).click();
divs = (List<HtmlDivision>) diff.getByXPath("//div[@id='flash']/.");
assertEquals(1, divs.size());
}
示例14: mineHackerNews
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private void mineHackerNews(String url, int numPages) {
HtmlPage page = null;
String nextUrl = url;
while (numPages-- > 0) {
try {
page = wc.getPage(nextUrl);
} catch (IOException e) {
logger.error("Unable to fetch page {}:", nextUrl, e);
}
List<?> nodes = page.getByXPath("//table//table[position()==1]");
DomNode d = (DomNode)nodes.get(0);
logger.info(d.getTextContent());
}
}
示例15: extractContent
import com.gargoylesoftware.htmlunit.html.DomNode; //导入依赖的package包/类
private String extractContent(final String endUrl, final String xpath) throws IOException {
final String url = base.toExternalForm() + "jbatch/" + endUrl;
final WebClient webClient = newWebClient();
final HtmlPage page = webClient.getPage(url);
final List<?> byXPath = page.getByXPath("//div[@id=\"content\"]" + xpath);
assertTrue(byXPath.size() >= 1);
final Object next = byXPath.iterator().next();
if (!DomNode.class.isInstance(next)) {
throw new IllegalArgumentException("Can't find text for " + next);
}
return DomNode.class.cast(next).asText();
}