use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class DataType method execute.
/**
* Applies the extraction rule to the current iJavelin object.
*
* @param javelin the Javelin object.
* @param block the current block to analyze.
* @param blockFactory the block context of the current block.
*
* @return an ExtractionRuleResult object containing the result of
* the query.
*/
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
Engine.logBeans.trace("Analyzing the data type of block: " + block);
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
xrs.newCurrentBlock = block;
if (block.type.equals("static") || block.type.equals("field")) {
xrs.hasMatched = true;
if (detectionPolicy == DataType.DETECTION_POLICY_EXPLICIT) {
block.setOptionalAttribute("datatype", explicitDataType);
Engine.logBeans.trace("The block has been explicitly data typed to '" + explicitDataType + "'");
return xrs;
} else {
String blockText = block.getText();
// boolean?
String s = blockText.toLowerCase();
if (s.equals("true") || s.equals("false")) {
Engine.logBeans.trace("The block is a boolean.");
block.setOptionalAttribute("dataType", "boolean");
return xrs;
}
Engine.logBeans.trace("The block is not an boolean.");
// integer number?
try {
long l = Math.abs(Long.parseLong(blockText));
if (detectionPolicy == DataType.DETECTION_POLICY_DEFAULT) {
Engine.logBeans.trace("The block seems to be an integer number; defaulting to int.");
block.setOptionalAttribute("datatype", "int");
} else {
if (l < Byte.MAX_VALUE) {
Engine.logBeans.trace("The block is a byte.");
block.setOptionalAttribute("datatype", "byte");
} else if (l < Short.MAX_VALUE) {
Engine.logBeans.trace("The block is a short.");
block.setOptionalAttribute("datatype", "short");
} else if (l < Integer.MAX_VALUE) {
Engine.logBeans.trace("The block is an integer.");
block.setOptionalAttribute("datatype", "int");
} else {
Engine.logBeans.trace("The block is a long.");
block.setOptionalAttribute("datatype", "long");
}
}
return xrs;
} catch (NumberFormatException e) {
Engine.logBeans.trace("The block is not an integer number.");
}
// floating point number?
try {
double d = Math.abs(Double.parseDouble(blockText));
if (detectionPolicy == DataType.DETECTION_POLICY_DEFAULT) {
Engine.logBeans.trace("The block seems to be a floating point number; defaulting to float.");
block.setOptionalAttribute("dataType", "float");
} else {
if (d < Float.MAX_VALUE) {
Engine.logBeans.trace("The block is a float.");
block.setOptionalAttribute("datatype", "float");
} else {
Engine.logBeans.trace("The block is a double.");
block.setOptionalAttribute("datatype", "double");
}
}
return xrs;
} catch (NumberFormatException e) {
Engine.logBeans.trace("The block is not a floating point number.");
}
// TODO: handle this kind of date
// Default type
block.setOptionalAttribute("datatype", "string");
Engine.logBeans.trace("The block is a string.");
return xrs;
}
}
xrs.hasMatched = false;
return xrs;
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Date method execute2.
public JavelinExtractionRuleResult execute2(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
String szBlockText = block.getText();
SimpleDateFormat sdf;
Engine.logBeans.debug("Date: trying to find a date block according to the pattern '" + format + "'");
try {
sdf = new SimpleDateFormat(format, new Locale(language, Locale.getDefault().getDisplayCountry()));
} catch (IllegalArgumentException e) {
Engine.logBeans.debug("Error: the format '" + format + "' is not valid. Please see help for more information.");
sdf = new SimpleDateFormat("dd/MM/yyyy");
} catch (Exception e) {
Engine.logBeans.debug("Warning: the language '" + language + "' is not defined in ISO 639 language codes or is not available. The default language '" + Locale.getDefault().getLanguage() + "' will be used.");
sdf = new SimpleDateFormat(format);
}
java.util.Date myDate = sdf.parse(szBlockText, new ParsePosition(0));
if (myDate == null) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
// check the elements are numeric only
try {
if (bday)
block.setOptionalAttribute("day", day.format(myDate));
if (bday_in_week_short)
block.setOptionalAttribute("day_in_week_short", day_in_week_short.format(myDate));
if (bday_in_week_long)
block.setOptionalAttribute("day_in_week_long", day_in_week_long.format(myDate));
if (bmonth)
block.setOptionalAttribute("month", month.format(myDate));
if (bmonth_name_short)
block.setOptionalAttribute("month_name_short", month_name_short.format(myDate));
if (bmonth_name_long)
block.setOptionalAttribute("month_name_long", month_name_long.format(myDate));
if (bweek_in_year)
block.setOptionalAttribute("week_in_year", week_in_year.format(myDate));
if (bweek_in_month)
block.setOptionalAttribute("week_in_month", week_in_month.format(myDate));
if (byear)
block.setOptionalAttribute("year", year.format(myDate));
if (bam_pm_marker)
block.setOptionalAttribute("am_pm_marker", am_pm_marker.format(myDate));
if (bhour_in_day_0_to_23)
block.setOptionalAttribute("hour_in_day_0_to_23", hour_in_day_0_to_23.format(myDate));
if (bhour_in_day_1_to_24)
block.setOptionalAttribute("hour_in_day_1_to_24", hour_in_day_1_to_24.format(myDate));
if (bhour_in_day_0_to_11)
block.setOptionalAttribute("hour_in_day_0_to_11", hour_in_day_0_to_11.format(myDate));
if (bhour_in_day_1_to_12)
block.setOptionalAttribute("hour_in_day_1_to_12", hour_in_day_1_to_12.format(myDate));
if (bminutes)
block.setOptionalAttribute("minutes", minutes.format(myDate));
if (bseconds)
block.setOptionalAttribute("seconds", seconds.format(myDate));
if (bmilliseconds)
block.setOptionalAttribute("milliseconds", milliseconds.format(myDate));
if (btime_zone_text)
block.setOptionalAttribute("time_zone_text", time_zone_text.format(myDate));
if (btime_zone_number)
block.setOptionalAttribute("time_zone_number", time_zone_number.format(myDate));
block.setOptionalAttribute("pattern", format);
if (block.type.equalsIgnoreCase("field"))
block.type = "date";
xrs.hasMatched = true;
xrs.newCurrentBlock = block;
return (xrs);
} catch (Exception e) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return (xrs);
}
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Field method execute.
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
boolean bLastBlock = false;
if (bFirstTime) {
bFirstTime = false;
if (fieldDesc.x == -1)
fieldDesc.x = 0;
if (fieldDesc.y == -1)
fieldDesc.y = 0;
if (fieldDesc.width == -1)
fieldDesc.width = 6;
if (fieldDesc.height == -1)
fieldDesc.height = 1;
}
// if field has already been added to the blocks
if (bAdded) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
if (blockFactory.getNextBlock(block) == null) {
// case block is the last one : insert block after !
bLastBlock = true;
} else {
// test if the block matches to add the block field just before
if (block.line < fieldDesc.y) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
} else if (block.line == fieldDesc.y) {
if (block.column < fieldDesc.x) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
}
// block matches : add the field block
Block myField = new Block();
myField.name = Name;
myField.type = Type;
myField.setText(Value);
myField.bRender = true;
myField.line = fieldDesc.y;
myField.column = fieldDesc.x;
myField.setOptionalAttribute("hasFocus", "false");
myField.setOptionalAttribute("size", Integer.toString(fieldDesc.width));
myField.attribute = fieldAttrb;
addMashupAttribute(myField);
if (bLastBlock) {
blockFactory.insertBlock(myField, block);
} else {
blockFactory.insertBlock(myField, blockFactory.getPreviousBlock(block));
}
bAdded = true;
xrs.newCurrentBlock = block;
xrs.hasMatched = true;
return xrs;
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Attribute method execute.
/**
* Applies the extraction rule to the current iJavelin object.
*
* @param javelin the Javelin object.
* @param block the current block to analyze.
* @param blockFactory the block context of the current block.
*
* @return an ExtractionRuleResult object containing the result of
* the query.
*/
@Override
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
block.setOptionalAttribute(attributeName, attributeValue);
addMashupAttribute(block);
xrs.hasMatched = true;
xrs.newCurrentBlock = block;
return xrs;
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Radio method execute.
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
Block nextBlock;
XMLVector<XMLVector<Object>> foundOptions = new XMLVector<XMLVector<Object>>();
Collection<Block> blocksToDelete = new LinkedList<Block>();
String currentOption = "";
String direction = "horizontal";
boolean bLastBlock = false;
if (bFirstTime) {
if (radioDesc.x != -1 && radioDesc.y != -1) {
// defined radio button
if (radioDesc.width == -1)
radioDesc.width = 1;
if (radioDesc.height == -1)
radioDesc.height = 1;
} else if (!separatorChars.equals("")) {
// if no position is defined and separator is defined
// searching all radio buttons on the screen
bSearch = true;
bFirstTime = false;
} else if (!options.isEmpty()) {
// initialize the position of the radio button
if (radioDesc.x == -1)
radioDesc.x = 0;
if (radioDesc.y == -1)
radioDesc.y = 0;
if (radioDesc.width == -1)
radioDesc.width = 1;
if (radioDesc.height == -1)
radioDesc.height = 1;
} else {
// no position, no separator, no options defined
// use default check pattern
separatorChars = ". ";
// searching all radio buttons on the screen
bSearch = true;
bFirstTime = false;
}
}
if (bSearch) {
nextBlock = blockFactory.getNextBlock(block);
// if next block = null or is not on the same line : not matching
if (nextBlock == null || nextBlock.line != block.line) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
// if block is a field and next block is a matching option
if (block.type.equals("field") && nextBlock != null && canBlockBeSelected(nextBlock) && nextBlock.type.equals("static") && nextBlock.getText().indexOf(separatorChars) != -1) {
// first option found
foundOptions.add(createOption(nextBlock));
blocksToDelete.add(nextBlock);
currentOption = block.getText().trim();
// searching for the direction of options
if (blockFactory.getEastBlock(nextBlock) != null && canBlockBeSelected(blockFactory.getEastBlock(nextBlock)) && blockFactory.getEastBlock(nextBlock).type.equals("static") && blockFactory.getEastBlock(nextBlock).getText().indexOf(separatorChars) != -1) {
// options are on the same line
direction = "horizontal";
do {
nextBlock = blockFactory.getEastBlock(nextBlock);
foundOptions.add(createOption(nextBlock));
blocksToDelete.add(nextBlock);
} while (blockFactory.getEastBlock(nextBlock) != null && canBlockBeSelected(blockFactory.getEastBlock(nextBlock)) && blockFactory.getEastBlock(nextBlock).type.equals("static") && blockFactory.getEastBlock(nextBlock).getText().indexOf(separatorChars) != -1);
} else if (blockFactory.getSouthBlock(nextBlock) != null && canBlockBeSelected(blockFactory.getSouthBlock(nextBlock)) && blockFactory.getSouthBlock(nextBlock).type.equals("static") && blockFactory.getSouthBlock(nextBlock).getText().indexOf(separatorChars) != -1) {
// options are on the same column
direction = "vertical";
do {
nextBlock = blockFactory.getSouthBlock(nextBlock);
foundOptions.add(createOption(nextBlock));
blocksToDelete.add(nextBlock);
} while (blockFactory.getSouthBlock(nextBlock) != null && canBlockBeSelected(blockFactory.getSouthBlock(nextBlock)) && blockFactory.getSouthBlock(nextBlock).type.equals("static") && blockFactory.getSouthBlock(nextBlock).getText().indexOf(separatorChars) != -1);
} else // no other option
{
}
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
} else if (bAdded) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
if (!bAdded || bSearch) {
bFirstTime = false;
if (!bSearch) {
if (blockFactory.getNextBlock(block) == null) {
// case block is the last one : insert block after !
bLastBlock = true;
} else {
// test if the block matches to add the block checkbox just before
if (block.line < radioDesc.y) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
} else if (block.line == radioDesc.y) {
if (block.column < radioDesc.x) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
}
}
Block myRadio;
// build result block
if (bSearch) {
myRadio = block;
// add the options
for (List<Object> op : foundOptions) {
Element element = dom.createElement("item");
element.setAttribute("action", (String) op.get(0));
element.setAttribute("value", (String) op.get(1));
element.setAttribute("column", String.valueOf((Integer) op.get(2)));
element.setAttribute("line", String.valueOf((Integer) op.get(3)));
element.setAttribute("selected", currentOption.equalsIgnoreCase((String) op.get(0)) ? "true" : "false");
myRadio.addOptionalChildren(element);
}
} else {
myRadio = new Block();
myRadio.bRender = true;
myRadio.name = "untitled";
myRadio.line = radioDesc.y;
myRadio.column = radioDesc.x;
if (bLastBlock) {
blockFactory.insertBlock(myRadio, block);
} else {
blockFactory.insertBlock(myRadio, blockFactory.getPreviousBlock(block));
}
// add the options
int item = 0;
for (List<Object> op : options) {
Element element = dom.createElement("item");
element.setAttribute("action", (String) op.get(1));
element.setAttribute("value", (String) op.get(0));
element.setAttribute("column", String.valueOf(radioDesc.x));
element.setAttribute("line", String.valueOf(radioDesc.y + item));
item++;
element.setAttribute("selected", "false");
myRadio.addOptionalChildren(element);
}
}
myRadio.setText("");
myRadio.type = "radio";
myRadio.setOptionalAttribute("radio", "true");
myRadio.setOptionalAttribute("direction", direction);
// remove old blocks
blockFactory.removeBlocks(blocksToDelete);
bAdded = true;
xrs.newCurrentBlock = bSearch ? myRadio : block;
xrs.hasMatched = true;
}
return xrs;
}
Aggregations