use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.
the class JavelinTransaction method searchPanelsAndApplyExtractionRules.
public void searchPanelsAndApplyExtractionRules(JavelinScreenClass screenClass, BlockFactory blockFactory, iJavelin javelin, boolean bNotFirstLoop) throws EngineException {
// if there are panel type blocks, apply extraction rules on the panel block children
Block tmpBlock = null;
try {
tmpBlock = blockFactory.getFirstBlock();
} catch (NoSuchElementException e) {
Engine.logContext.trace("(JavelinTransaction) Cannot apply extraction rules in panels : no block left in block factory.");
return;
}
PanelBlockFactory bf = null;
while (tmpBlock != null) {
if (tmpBlock.type.equals("panel") || tmpBlock.type.equals("container") || tmpBlock.type.equals("tabBox")) {
// creation of a BlockFactory containing the child blocks
try {
bf = PanelBlockFactory.cloneBlockFactory((DefaultBlockFactory) blockFactory);
} catch (CloneNotSupportedException e) {
throw new EngineException("(JavelinTransaction) Exception when cloning blockFactory", e);
}
bf.setName("Panel_block_factory");
bf.make(tmpBlock);
if (tmpBlock.type.equals("panel")) {
// if type = panel, apply extraction rules
Engine.logContext.trace("(JavelinTransaction) Applying extraction rules in a panel.");
applyExtractionRules(screenClass, bf, javelin, bNotFirstLoop);
Engine.logContext.trace("(JavelinTransaction) End applying extraction rules in a panel.");
// replace panel content with blockFactory blocks
tmpBlock.clearOptionalChildren();
for (Block block : bf.list) tmpBlock.addOptionalChildren(block);
} else if (tmpBlock.type.equals("container") || tmpBlock.type.equals("tabBox")) {
// if type = container or tabBox, recurse on this method to find inside panels
searchPanelsAndApplyExtractionRules(screenClass, bf, javelin, bNotFirstLoop);
}
}
// go on the next block
tmpBlock = blockFactory.getNextBlock(tmpBlock);
}
}
use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.
the class JavelinTransaction method applyExtractionRules.
public void applyExtractionRules(JavelinScreenClass screenClass, BlockFactory blockFactory, iJavelin javelin, boolean bNotFirstLoop) throws EngineException {
String t = context.statistics.start(EngineStatistics.APPLY_EXTRACTION_RULES);
try {
// We apply the extraction rules for this screen class to
// the words list.
Block block = null;
JavelinExtractionRuleResult extractionRuleResult;
int extractionRuleInitReason;
boolean panelExtractionRuleFound = false;
List<ExtractionRule> vExtractionRules = screenClass.getExtractionRules();
for (ExtractionRule extractionRule : vExtractionRules) {
JavelinExtractionRule javelinExtractionRule = (JavelinExtractionRule) extractionRule;
if (!runningThread.bContinue)
break;
/*
if ((Engine.objectsProvider == null) && !javelin.isConnected()) {
throw new ConnectionException("The emulator has been disconnected! See the emulator logs for more details...");
}
*/
blockFactory.moveToFirstBlock();
if (!extractionRule.isEnabled()) {
// if extraction rule is disabled
Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been disabled.");
continue;
}
if (blockFactory instanceof PanelBlockFactory && !panelExtractionRuleFound) {
// if we apply the rules in a panel and the rule is before the rule which has created the panel
Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been applied before the panel creation.");
if (extractionRule instanceof Nptui)
panelExtractionRuleFound = true;
continue;
}
if (blockFactory instanceof PanelBlockFactory && extractionRule instanceof TabBox) {
// if we try to apply tabbox rule in a panel
Block panel = ((PanelBlockFactory) blockFactory).getPanel();
XMLRectangle zone = ((TabBox) extractionRule).getSelectionScreenZone();
if (zone.contains(panel.column, panel.line, Integer.parseInt(panel.getOptionalAttribute("width")), Integer.parseInt(panel.getOptionalAttribute("height")))) {
// if the tabbox screen zone is larger than the panel
Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because the screen zone is larger than the panel.");
continue;
}
}
Engine.logContext.debug("(JavelinTransaction) Applying the extraction rule \"" + extractionRule.getName() + "\" on blocks");
String extractionRuleQName = extractionRule.getQName();
if (vExtractionRulesInited.contains(extractionRuleQName)) {
extractionRuleInitReason = ExtractionRule.ACCUMULATING;
} else {
extractionRuleInitReason = ExtractionRule.INITIALIZING;
vExtractionRulesInited.add(extractionRuleQName);
}
Engine.logContext.trace("(JavelinTransaction) Initializing extraction rule (reason = " + extractionRuleInitReason + ")...");
extractionRule.init(extractionRuleInitReason);
// We fire engine events only in studio mode.
if (Engine.isStudioMode()) {
Engine.theApp.fireObjectDetected(new EngineEvent(extractionRule));
}
// We try to apply the current extraction rule on each block.
while (runningThread.bContinue && ((block = blockFactory.getNextBlock(block)) != null)) {
Engine.logContext.trace("(JavelinTransaction) Analyzing block \"" + block.getText() + "\"");
// We skip final blocks.
if (block.bFinal) {
Engine.logContext.trace("(JavelinTransaction) The block has been marked as final; skipping it.");
continue;
}
extractionRuleResult = javelinExtractionRule.apply(javelin, block, blockFactory, context.outputDocument);
if (extractionRuleResult.hasMatched) {
// extraction rules on this block and pass on the next block.
if (javelinExtractionRule.isFinal()) {
block.bFinal = true;
Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching and final");
} else {
Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching");
}
// We need to update the current block only if the
// rule has matched.
block = extractionRuleResult.newCurrentBlock;
} else {
Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': not matching");
}
}
// We fire engine events only in studio mode.
if (Engine.isStudioMode()) {
Engine.theApp.fireBlocksChanged(new EngineEvent(blockFactory));
Engine.logContext.debug("(JavelinTransaction) Step reached after having applied the extraction rule \"" + extractionRule.getName() + "\".");
Engine.theApp.fireStepReached(new EngineEvent(extractionRule));
}
}
vExtractionRules = null;
extractionRuleResult = null;
} finally {
context.statistics.stop(t, bNotFirstLoop);
}
}
use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.
the class Choice 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;
// if we have to choose values of options on the screen
if (actionsFromScreen) {
String textBlock = block.getText().trim();
if ((!textBlock.startsWith(startPattern) && startPattern.length() != 0) || (!textBlock.endsWith(endPattern) && endPattern.length() != 0)) {
return xrs;
}
Block previousBlock = block;
boolean fieldFound = false;
while (((previousBlock = blockFactory.getPreviousBlock(previousBlock)) != null) && (block.line == previousBlock.line)) {
if (previousBlock.type.equals("field")) {
fieldFound = true;
break;
}
}
if (!fieldFound) {
Engine.logBeans.debug("Criteria Choice not matching because of not finding field block before on the same line");
return xrs;
}
// handle the start and end patterns
int beginIndex = startPattern.length();
int endIndex;
if (endPattern.length() == 0)
endIndex = textBlock.length();
else
endIndex = textBlock.indexOf(endPattern);
textBlock = textBlock.substring(beginIndex, endIndex);
// Try to tokenize
StringTokenizer st = new StringTokenizer(textBlock, separatorChars, false);
if (st.countTokens() == 1) {
Engine.logBeans.debug("Only one token found this is not a choice...");
return xrs;
}
// we have a choice block now build it.
block.type = "choice";
if (tagName.equals("")) {
block.tagName = "choice";
} else
block.tagName = tagName;
String currentAction = previousBlock.getText().trim();
boolean currentActionExist = false;
block.setText(previousBlock.getText());
block.setOptionalAttribute("size", Integer.toString(previousBlock.length));
if (previousBlock.getOptionalAttribute("hasFocus") == "true")
block.setOptionalAttribute("hasFocus", "true");
block.line = previousBlock.line;
block.column = previousBlock.column;
block.attribute = previousBlock.attribute;
block.name = previousBlock.name;
blockFactory.removeBlock(previousBlock);
// Build the sub elemnts of the choice...
Element element;
String token;
int index = 0;
while (st.hasMoreTokens()) {
token = st.nextToken().trim();
element = dom.createElement("item");
element.setAttribute("type", "choiceItem");
switch(choiceCharacterPolicy) {
case Choice.CHOICE_CHARACTER_POLICY_INDEX_INDEX:
element.setAttribute("value", token);
element.setAttribute("action", Integer.toString(index));
if (currentAction.equalsIgnoreCase(token)) {
element.setAttribute("selected", "true");
currentActionExist = true;
} else
element.setAttribute("selected", "false");
break;
case Choice.CHOICE_CHARACTER_POLICY_INDEX_SEPARATOR:
try {
StringTokenizer st2 = new StringTokenizer(token, separatorCharsForTokens, false);
String action = st2.nextToken().trim();
element.setAttribute("action", action);
String value = st2.nextToken().trim();
element.setAttribute("value", value);
if (currentAction.equalsIgnoreCase(action) || currentAction.equalsIgnoreCase(value)) {
element.setAttribute("selected", "true");
currentActionExist = true;
} else
element.setAttribute("selected", "false");
} catch (Exception e) {
// could not separate token
Engine.logBeans.debug("Could not find separator, assume token is value ...");
element.setAttribute("action", token);
element.setAttribute("value", token);
// bSetToRadio = true;
if (currentAction.equalsIgnoreCase(token.trim())) {
element.setAttribute("selected", "true");
currentActionExist = true;
}
}
break;
case Choice.CHOICE_CHARACTER_POLICY_INDEX_FIRST_UPPERCASE_LETTER:
int i = 0;
int len = token.length();
while (i < len) {
char c = token.charAt(i);
if (Character.isUpperCase(c)) {
element.setAttribute("value", token);
element.setAttribute("action", "" + c);
if (currentAction.equalsIgnoreCase(token) || currentAction.equalsIgnoreCase("" + c)) {
element.setAttribute("selected", "true");
currentActionExist = true;
} else
element.setAttribute("selected", "false");
break;
}
i++;
}
break;
}
index++;
block.addOptionalChildren(element);
}
if (!currentActionExist) {
element = dom.createElement("item");
element.setAttribute("type", "choiceItem");
element.setAttribute("value", currentAction);
element.setAttribute("action", currentAction);
element.setAttribute("selected", "true");
block.addOptionalChildren(element);
}
// specify if this is a radio like button
if (isRadio())
block.setOptionalAttribute("radio", "true");
else
block.setOptionalAttribute("radio", "false");
} else {
// s'il faut les prendre dans le vecteur
if (actions == null || actions.isEmpty()) {
Engine.logBeans.debug("Criteria Choice not matching because of no action defined");
return xrs;
}
if (!block.type.equals("field")) {
Engine.logBeans.debug("Criteria Choice not matching because it is not a field block");
return xrs;
}
block.type = "choice";
if (tagName.equals("")) {
block.tagName = "choice";
} else {
block.tagName = tagName;
}
String currentAction = block.getText().trim();
block.setText("");
block.setOptionalAttribute("radio", "false");
boolean currentActionExist = false;
for (List<String> myAction : actions) {
Element element = dom.createElement("item");
element.setAttribute("type", "choiceItem");
element.setAttribute("value", myAction.get(0));
element.setAttribute("action", myAction.get(1));
if (currentAction.equalsIgnoreCase(myAction.get(0)) || currentAction.equalsIgnoreCase(myAction.get(1))) {
element.setAttribute("selected", "true");
currentActionExist = true;
} else
element.setAttribute("selected", "false");
block.addOptionalChildren(element);
}
if (!currentActionExist) {
Element element = dom.createElement("item");
element.setAttribute("type", "choiceItem");
element.setAttribute("value", currentAction);
element.setAttribute("action", currentAction);
element.setAttribute("selected", "true");
block.addOptionalChildren(element);
}
}
xrs.hasMatched = true;
return xrs;
}
use of com.twinsoft.convertigo.beans.core.Block 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.core.Block in project convertigo by convertigo.
the class Button method createButton.
/**
* Creates the button block
* @param label
* @param zone
* @param bTransaction
* @return the built block
*/
private Block createButton(String label, XMLRectangle zone, boolean bTransaction) {
Block myButton = new Block();
myButton.name = "untitled";
myButton.type = "keyword";
myButton.setText(label);
myButton.bRender = true;
myButton.setOptionalAttribute("action", Action);
myButton.line = zone.y;
myButton.column = zone.x;
myButton.setOptionalAttribute("width", "" + zone.width);
myButton.setOptionalAttribute("height", "" + zone.height);
if (bTransaction)
myButton.setOptionalAttribute("dotransaction", "true");
return myButton;
}
Aggregations