Menu

#9 HtmlUnitExtractText

open
nobody
5
2008-03-05
2008-03-05
kampret77
No

/*
* File: HtmlUnitExtractText.java
* Module: net.sf.jameleon.plugin.htmlunit.tags
* Author: jens zastrow
* Last modified by: $Author: jzastrow $
* Date: $Date: 2008-01-30 10:33:15 $
* Version: $Revision: 1.2 $
* Description:
* Preconditions:
*/

package net.sf.jameleon.plugin.htmlunit.tags;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
this tag extract text based on the given regex.

/**
* TODO: Please insert description here.
*
* @author jzastrow
* @jameleon.function name="htmlunit-extract-text" type="action"
*/
public class HtmlUnitExtractText extends HtmlUnitFunctionTag {

/**
* The regular-expression pattern used to extract
* Example: 'passwd:(\d{6})'
* @jameleon.attribute required="true"
*/
String regex;

/**
* Index of the regex-group with should set to the result-variable - is 1 based.
* @jameleon.attribute required="true"
*/
int group;

/**
* The resutl should be stored to this variable.
* @jameleon.attribute required="true"
*/
String resultVariable;

@Override
public void testBlock() throws Exception {
String html = helper.getCurrentPageContent();

Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(html);

if (!m.find()) fail("html not matching the pattern '"+regex+"'");

if (group > m.groupCount()) fail("Match only has " + m.groupCount() +
" groups, but requested group-index is " + group);

String text = m.group(group);

setVariable(resultVariable, text );

log.info("Extracted text was '"+text+"'\n");
}

}

Discussion


Log in to post a comment.