use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Command 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;
String textBlock = block.getText().trim();
// test if the current block matches a keyword
for (List<String> keyword : keywordTable) {
String textKeyword = keyword.get(0);
String dataKeyword = keyword.get(1);
String replaceTextKeyword = keyword.get(2);
String actionKeyword = keyword.get(3);
if (textBlock.equals(textKeyword) || (textBlock.equalsIgnoreCase(textKeyword) && !caseDependency)) {
// set its type to "keyword"
block.type = "keyword";
// sets the "data" attribute
if (dataKeyword.length() != 0) {
block.setOptionalAttribute("data", dataKeyword);
} else // otherwise it keeps the old value
if (block.getOptionalAttribute("data") == null)
block.setOptionalAttribute("data", "");
// keep the original size
if (replaceTextKeyword.length() != 0) {
int len = block.length;
block.setText(replaceTextKeyword);
block.length = len;
}
// set the "action" attribute
if (actionKeyword.length() != 0) {
block.setOptionalAttribute("action", actionKeyword);
} else // otherwise it keeps the old value
if (block.getOptionalAttribute("action") == null)
block.setOptionalAttribute("action", "");
addMashupAttribute(block);
xrs.hasMatched = true;
xrs.newCurrentBlock = block;
return (xrs);
}
}
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Date method execute1.
public JavelinExtractionRuleResult execute1(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
Engine.logBeans.debug("Date: trying to find a date form according to the pattern '" + format + "'");
if (!block.type.equals("field")) {
Engine.logBeans.debug("Date: block is not field; aborting");
}
try {
char separator1, separator2;
char c;
int i = 0;
int fieldLen1 = 0, fieldLen2 = 0, fieldLen3 = 0;
int len = format.length();
String day = "", month = "", year = "";
try {
// First field
c = format.charAt(i);
if (c == 'd')
day = "1";
else if (c == 'm')
month = "1";
else if (c == 'y')
year = "1";
do {
i++;
fieldLen1++;
} while ((separator1 = format.charAt(i)) == c);
// Second field
i++;
c = format.charAt(i);
if (c == 'd')
day = "2";
else if (c == 'm')
month = "2";
else if (c == 'y')
year = "2";
do {
i++;
fieldLen2++;
} while ((separator2 = format.charAt(i)) == c);
// Third field
c = format.charAt(i + 1);
if (c == 'd')
day = "3";
else if (c == 'm')
month = "3";
else if (c == 'y')
year = "3";
fieldLen3 = len - i - 1;
} catch (StringIndexOutOfBoundsException e) {
Engine.logBeans.warn("Date: wrong format!");
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
Block block1, block2, block3;
// Verify the first field
block1 = block;
int blockFieldLen1 = -1;
if (block.getOptionalAttribute("size") != null)
blockFieldLen1 = Integer.parseInt(block.getOptionalAttribute("size"));
if (blockFieldLen1 != fieldLen1) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
block = blockFactory.getNextBlock(block);
if (separator1 == ' ') {
// Two cases: separator block can be a space character or blank
if (block.type.equals("field")) {
// Nothing to do
} else if (blockFactory.getNextBlock(block).getText().equals(" ")) {
block = blockFactory.getNextBlock(block);
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
} else {
if (block.getText().equals("" + separator1)) {
block = blockFactory.getNextBlock(block);
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
// Verify the second field
block2 = block;
if (!block.type.equals("field")) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
int blockFieldLen2 = Integer.parseInt(block.getOptionalAttribute("size"));
if (blockFieldLen2 != fieldLen2) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
block = blockFactory.getNextBlock(block);
if (separator2 == ' ') {
// Two cases: separator block can be a space character or blank
if (block.type.equals("field")) {
// Nothing to do
} else if (blockFactory.getNextBlock(block).getText().equals(" ")) {
block = blockFactory.getNextBlock(block);
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
} else {
if (block.getText().equals("" + separator2)) {
block = blockFactory.getNextBlock(block);
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
// Verify the third field
block3 = block;
if (!block.type.equals("field")) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
int blockFieldLen3 = Integer.parseInt(block.getOptionalAttribute("size"));
if (blockFieldLen3 != fieldLen3) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
Block newBlock = new Block();
newBlock.setText("");
newBlock.type = "date";
newBlock.name = StringUtils.normalize(getName()) + "_" + nDate;
nDate++;
newBlock.setOptionalAttribute("day", day);
newBlock.setOptionalAttribute("month", month);
newBlock.setOptionalAttribute("year", year);
blockFactory.insertBlock(newBlock, block1);
newBlock.addOptionalChildren(block1);
newBlock.addOptionalChildren(block2);
newBlock.addOptionalChildren(block3);
blockFactory.removeBlock(block1);
blockFactory.removeBlock(block2);
blockFactory.removeBlock(block3);
xrs.hasMatched = true;
xrs.newCurrentBlock = newBlock;
return xrs;
} catch (Exception e) {
Engine.logBeans.error("Date: exception while processing the rule", e);
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class Date 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.
* @param dom
* the XML DOM.
*
* @return an ExtractionRuleResult object containing the result of the
* query.
*/
public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
xrs = execute1(javelin, block, blockFactory, dom);
if (xrs.hasMatched)
return xrs;
xrs = execute2(javelin, block, blockFactory, dom);
return xrs;
}
use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.
the class CheckBox method execute.
@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 nextBlock;
XMLRectangle position = new XMLRectangle(-1, -1, -1, -1);
String label = "";
boolean checked = false;
Collection<Block> blocksToDelete = new LinkedList<Block>();
boolean bLastBlock = false;
if (bFirstTime) {
// if position is defined
if (checkBoxDesc.x != -1 && checkBoxDesc.y != -1) {
// defined checkbox
if (checkBoxDesc.width == -1)
checkBoxDesc.width = 1;
if (checkBoxDesc.height == -1)
checkBoxDesc.height = 1;
// if no position is defined and check pattern or uncheck pattern is defined
} else if (!checkPattern.equals("") || !uncheckPattern.equals("")) {
// searching all checkboxes on the screen
bSearch = true;
bFirstTime = false;
// no position and no check/uncheck pattern but label defined
} else if (!Label.equals("")) {
// initialize the position of the checkbox
if (checkBoxDesc.x == -1)
checkBoxDesc.x = 0;
if (checkBoxDesc.y == -1)
checkBoxDesc.y = 0;
if (checkBoxDesc.width == -1)
checkBoxDesc.width = 1;
if (checkBoxDesc.height == -1)
checkBoxDesc.height = 1;
// no position, no check pattern, no label defined
} else {
// use default check and uncheck patterns
checkPattern = "/";
uncheckPattern = "";
// searching all checkboxes on the screen
bSearch = true;
bFirstTime = false;
}
}
if (bSearch) {
nextBlock = blockFactory.getNextBlock(block);
if (block.type.equals("field") && block.length == 1 && (block.getText().equals(checkPattern) || block.getText().equals(uncheckPattern))) {
// test if the next block is a label
if (nextBlock != null && canBlockBeSelected(nextBlock) && nextBlock.type.equals("static")) {
// label found
label = nextBlock.getText();
name = block.name;
position.y = block.line;
position.x = block.column;
checked = block.getText().equals(checkPattern) ? true : false;
blocksToDelete.add(block);
blocksToDelete.add(nextBlock);
} else {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
} 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 < checkBoxDesc.y) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
} else if (block.line == checkBoxDesc.y) {
if (block.column < checkBoxDesc.x) {
xrs.hasMatched = false;
xrs.newCurrentBlock = block;
return xrs;
}
}
}
}
Block myCheckBox;
if (bSearch) {
myCheckBox = createCheckBox(label, position, checked);
} else {
myCheckBox = createCheckBox(Label, checkBoxDesc, false);
}
if (bLastBlock) {
blockFactory.insertBlock(myCheckBox, block);
} else {
blockFactory.insertBlock(myCheckBox, blockFactory.getPreviousBlock(block));
}
blockFactory.removeBlocks(blocksToDelete);
bAdded = true;
if (bSearch) {
xrs.newCurrentBlock = myCheckBox;
} else {
xrs.newCurrentBlock = block;
}
xrs.hasMatched = true;
}
return xrs;
}
Aggregations