Search in sources :

Example 31 with JavelinExtractionRuleResult

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.

the class EditField method execute.

public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
    xrs = new JavelinExtractionRuleResult();
    xrs.newCurrentBlock = block;
    xrs.hasMatched = false;
    if (javelin.isConnected() && isCursorInField(block, javelin.getCurrentLine(), javelin.getCurrentColumn())) {
        // must be first
        block.type = "field";
        String param = block.getOptionalAttribute("nbLines");
        if (param == null) {
            param = "1";
        }
        numberOfLines = (int) Integer.parseInt(param);
        trimEndingDotsAndSpaces(xrs.newCurrentBlock = buildBlock(block, blockFactory));
        blockFactory.setBlockName(block);
        block.setOptionalAttribute("hasFocus", "true");
        xrs.hasMatched = true;
    }
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult)

Example 32 with JavelinExtractionRuleResult

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.

the class Menu method execute.

public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    xrs.newCurrentBlock = block;
    boolean menuDetected = false;
    Block blk = null;
    /*
        if ((blk = blockFactory.getBlockByType("field")) == null)
            return xrs;

        // if more than 3,  don't use menus
        menuWidth = Math.min(3, (int)Integer.parseInt(blk.getOptionalAttribute("size")));
*/
    String str1 = block.getText();
    // check with line & column for other 1 char strings
    if (isLengthOK(str1) && isLineOK(block.line) && isCharTypeOK(str1)) {
        String str;
        refColumn = block.column;
        refAttrib = block.attribute;
        blk = block;
        while ((blk = blockFactory.getNextBlock(blk)) != null) {
            str = blk.getText();
            if (isLengthOK(str) && isLineOK(block.line) && isCharTypeOK(str) && isColumnOK(blk) && isAttributeOK(blk.attribute)) {
                menuDetected = true;
                blk.setOptionalAttribute("item", ' ' + str + ' ');
                // decrease colum because we gonna add a space before
                if (refColumn > 0)
                    blk.column--;
                blk.type = "menu";
            }
        }
        if (menuDetected) {
            block.setOptionalAttribute("item", ' ' + str1 + ' ');
            // decrease colum because we gonna add a space before
            if (refColumn > 0)
                block.column--;
            block.type = "menu";
            xrs.newCurrentBlock = block;
            xrs.hasMatched = true;
        }
    }
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Block(com.twinsoft.convertigo.beans.core.Block)

Example 33 with JavelinExtractionRuleResult

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult in project convertigo by convertigo.

the class Subfile 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) {
    // save the screenZone so that we can restore it after the extraction of tableCUA
    XMLRectangle oldScreenZone = getSelectionScreenZone();
    XMLVector<XMLVector<Object>> oldColumns = getColumns();
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    Block downRightBlock, topLeftBlock, blockTmp, newBlock, topActionBlock;
    int columnDataLastCharEast, columnDataLastCharWest, dataLastLine;
    int subfileMarkerLine;
    // attributes of the field of action in the table
    int actionLength = 0;
    int actionColumn = 0;
    // int actionColumnAttributes = 0;
    boolean actionColumnExists = false;
    Block lastField = null;
    boolean screenZoneSet = false;
    StringTokenizer st = new StringTokenizer(endString, ",", false);
    ArrayList<String> token = new ArrayList<String>(st.countTokens());
    // if screen zone has been set, use it...
    if (oldScreenZone.x != -1 || oldScreenZone.y != -1 || oldScreenZone.height != -1 || oldScreenZone.width != -1) {
        screenZoneSet = true;
    }
    // for each string
    while (st.hasMoreTokens()) {
        token.add((String) st.nextToken());
    }
    try {
        // search of the endString among the blocks of the blockFactory
        if (token.contains(block.getText().trim()) && JavelinUtils.isSameAttribute(block.attribute, endStringAttribute) && block.line >= subFileDetectionStartLine) {
            WindowedBlockManager windowedBlockManager = new WindowedBlockManager(blockFactory);
            Engine.logBeans.trace("Searching the end block in a panel.");
            if (!windowedBlockManager.searchPanel(block)) {
                xrs.hasMatched = false;
                xrs.newCurrentBlock = block;
                return xrs;
            }
            // this line of code was not commented. I believe this was to prevent SubFile rule to be run twice. I prefer to set the xrs.newCurrentBlock
            // to the correct value. This also corrects the bug that prevented the delete blocks rule to act on the subfile marker as it was marked
            // as bFinal to true.
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!
            // !! block.bFinal = true;   !!
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!
            subfileMarkerLine = block.line;
            // if endString = "+" -> special case (Non-CUA but we can try to define the subfile)
            if (block.getText().equals("+")) {
                /**
                 * CASE "+" in the last row, in the last column
                 */
                downRightBlock = block;
                dataLastLine = downRightBlock.line;
                // as '+' is on same line as the alst row of data , add one line to subFileMarkerLine
                subfileMarkerLine++;
                // do not include marker block in data
                downRightBlock = windowedBlockManager.getPreviousBlockInPanel(block);
                Engine.logBeans.trace("Down Right Block = " + downRightBlock.getText());
                block.line = block.line + 1;
            } else // if endString != "+" (most common case)
            {
                // we look for a block directly above the endString
                downRightBlock = windowedBlockManager.getPreviousBlockInPanel(block);
                Engine.logBeans.trace("Down Right Block = " + downRightBlock.toString());
                while (downRightBlock != null && downRightBlock.line == block.line) {
                    downRightBlock = windowedBlockManager.getPreviousBlockInPanel(downRightBlock);
                    Engine.logBeans.trace("New Down Right Block = " + downRightBlock.toString());
                }
                dataLastLine = downRightBlock.line;
            }
            // if previous block do not contain any text, we give up creation of the table
            if (downRightBlock.length == 0) {
                Engine.logBeans.info("Give up subfile creation => Down Right Block length is 0 : " + downRightBlock.toString());
                xrs.hasMatched = false;
                xrs.newCurrentBlock = block;
                return xrs;
            }
            if ((block.column + block.getText().length()) <= (downRightBlock.column + downRightBlock.getText().length()))
                columnDataLastCharEast = downRightBlock.column + downRightBlock.getText().length();
            else
                columnDataLastCharEast = block.column + block.getText().length();
            // we move to west and search for the last block on the line (last row)
            blockTmp = downRightBlock;
            while (windowedBlockManager.getWestBlock(blockTmp) != null && windowedBlockManager.getWestBlock(blockTmp).line == dataLastLine) {
                blockTmp = windowedBlockManager.getWestBlock(blockTmp);
            }
            Engine.logBeans.trace("last block on the line (west) = " + blockTmp.toString());
            columnDataLastCharWest = blockTmp.column;
            // we check if action column exists
            if (blockTmp.type.equalsIgnoreCase("field")) {
                actionColumnExists = true;
                actionLength = blockTmp.length;
                actionColumn = blockTmp.column;
            // actionColumnAttributes = blockTmp.attribute;
            } else // maybe action column exists but not on this line
            {
                List<Block> blockField = windowedBlockManager.getBlocksByType("field");
                for (Block blockF : blockField) {
                    Engine.logBeans.trace("block field found = " + blockF.toString());
                    if (lastField == null) {
                        if (blockF.column <= blockTmp.column && blockF.line < blockTmp.line) {
                            lastField = blockF;
                        }
                    } else {
                        if (blockF.line > lastField.line || blockF.column > lastField.column) {
                            lastField = blockF;
                        }
                    }
                }
            }
            // an action column, it can happen that it doesn't have a title
            if (actionColumnExists) {
                // if we are in the action column
                Engine.logBeans.trace("+++ action column exists +++");
                if (columnDataLastCharWest > blockTmp.column) {
                    columnDataLastCharWest = blockTmp.column;
                }
                topLeftBlock = null;
            } else {
                // we are in a data column
                Engine.logBeans.trace("Searching title row in data column : " + blockTmp.getText());
                while ((windowedBlockManager.getNorthBlock(blockTmp) != null) && (!JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, titleRowAttribute))) {
                    blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                    Engine.logBeans.trace("Moving to north block : " + blockTmp.getText());
                }
                // check if the last field is in the data
                if (lastField == null || lastField.line < blockTmp.line) {
                    Engine.logBeans.trace("---actionColumnDoesNotExists");
                    blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                    while (windowedBlockManager.getWestBlock(blockTmp) != null) {
                        blockTmp = windowedBlockManager.getWestBlock(blockTmp);
                        Engine.logBeans.trace("Moving to west block : " + blockTmp.getText());
                        if (columnDataLastCharWest > blockTmp.column) {
                            columnDataLastCharWest = blockTmp.column;
                        }
                    }
                    topLeftBlock = blockTmp;
                    Engine.logBeans.trace("Top Left Block = " + (topLeftBlock == null ? "null" : topLeftBlock.getText()));
                } else {
                    Engine.logBeans.trace("+++ action column exists +++");
                    actionColumnExists = true;
                    if (columnDataLastCharWest > lastField.column) {
                        columnDataLastCharWest = lastField.column;
                    }
                    blockTmp = lastField;
                    topLeftBlock = null;
                }
            }
            if (actionColumnExists) {
                // we search the bottom of the title rows (but maybe action column doesn't have a title)
                while (// while north block of blocktmp not null
                (windowedBlockManager.getNorthBlock(blockTmp) != null) && // and north block of blocktmp is not same attribute as titlerow
                (!JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, titleRowAttribute)) && (!JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, actionLineAttribute))) {
                    // and north block of blocktmp is not same attribute as actionsline
                    Engine.logBeans.trace("Wrong title line found (" + blockTmp.line + ") ... keep searching...");
                    blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                }
                // is north block of blocktmp the first title block of first column ?
                if (// if north block of blocktmp not null
                (windowedBlockManager.getNorthBlock(blockTmp) != null) && // and north block of blocktmp has titlerow attribute
                (JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, titleRowAttribute)) && (goodTitleLine(windowedBlockManager, windowedBlockManager.getNorthBlock(blockTmp)))) {
                    // and north bolck of blocktmp is on a good title line
                    // blocktmp is the last top data block of the first column
                    Engine.logBeans.trace("Top of the action column= " + blockTmp.toString());
                    topActionBlock = blockTmp;
                } else {
                    // action column is not filled until top, blocktmp is not the last top line of data
                    // or action column doesn't have a title
                    Block actiontop = blockTmp;
                    // Searching a title block
                    blockTmp = windowedBlockManager.getEastBlock(blockTmp);
                    blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                    // look for a title block of the second column
                    while (// while north block of blocktmp not null
                    windowedBlockManager.getNorthBlock(blockTmp) != null && // and north block of blocktmp is the same attribute as titlerow
                    JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, titleRowAttribute)) {
                        Engine.logBeans.trace("Wrong title line found (" + blockTmp.line + ") ... keep searching...");
                        blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                    }
                    if (blockTmp == null) {
                        Engine.logBeans.info("Give up subfile creation => Looking for title block : blockTmp is null, no more block to test.");
                        xrs.hasMatched = false;
                        xrs.newCurrentBlock = block;
                        return xrs;
                    } else {
                        if (// if blocktmp has same attribute as titlerow
                        JavelinUtils.isSameAttribute(blockTmp.attribute, titleRowAttribute) && (goodTitleLine(windowedBlockManager, blockTmp))) {
                            // and blocktmp is on a good title line
                            Engine.logBeans.trace("Top of the action column= " + actiontop.toString());
                            topActionBlock = actiontop;
                        } else {
                            Engine.logBeans.info("Give up subfile creation => blockTmp is not of title attribute or is not on a good title line : " + blockTmp.toString());
                            xrs.hasMatched = false;
                            xrs.newCurrentBlock = block;
                            return xrs;
                        }
                    }
                }
                // go one column to the east of top action line block, and one block to the north
                blockTmp = windowedBlockManager.getEastBlock(topActionBlock);
                blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                // block tmp is the first down title block of second column
                // or another column if there is no data on the east of top action line block
                Block firstTitleBlock = blockTmp;
                // go to the first row of the title rows (if many)
                while (windowedBlockManager.getNorthBlock(blockTmp) != null && JavelinUtils.isSameAttribute(windowedBlockManager.getNorthBlock(blockTmp).attribute, titleRowAttribute) && blockTmp.line - windowedBlockManager.getNorthBlock(blockTmp).line == 1) blockTmp = windowedBlockManager.getNorthBlock(blockTmp);
                // we check if action column has a title
                while (// while no more block is on the top
                topActionBlock != null && // and the title row is not found
                !JavelinUtils.isSameAttribute(topActionBlock.attribute, titleRowAttribute) && // and an action line is not found (we suppose that the title row must be under the actions line)
                !JavelinUtils.isSameAttribute(topActionBlock.attribute, actionLineAttribute)) topActionBlock = windowedBlockManager.getNorthBlock(topActionBlock);
                if (topActionBlock != null && JavelinUtils.isSameAttribute(topActionBlock.attribute, titleRowAttribute)) {
                    Engine.logBeans.trace("Action column has a title");
                    topLeftBlock = topActionBlock;
                } else {
                    // we arrived on the top of the screen or on the actions line
                    Engine.logBeans.trace("Action column does not have a title");
                    Engine.logBeans.trace("Value of the first block of the title row : " + blockTmp.toString());
                    newBlock = new Block();
                    newBlock.column = actionColumn;
                    newBlock.length = actionLength;
                    newBlock.line = firstTitleBlock.line;
                    newBlock.attribute = titleRowAttribute;
                    newBlock.setText("A");
                    newBlock.bRender = false;
                    Engine.logBeans.trace("Creation of the title block of the action column : " + newBlock.toString());
                    // maybe firstTitleBlock is not the second column title, look to the west to see if there are other titles
                    while (windowedBlockManager.getWestBlock(firstTitleBlock) != null) firstTitleBlock = windowedBlockManager.getWestBlock(firstTitleBlock);
                    // now firstTitleBlock is the second column title, can insert the created block after its preceding block
                    Engine.logBeans.trace("Insert new title block after : " + windowedBlockManager.getPreviousBlock(firstTitleBlock).toString());
                    windowedBlockManager.insertBlock(newBlock, windowedBlockManager.getPreviousBlock(firstTitleBlock));
                    Engine.logBeans.trace("Next of the new Block : " + windowedBlockManager.getNextBlock(newBlock).toString());
                    // if getNextBlock(newBlock) and newBlock are not on the same line, we've inserted the new block in the wrong place
                    if (windowedBlockManager.getNextBlock(newBlock).line != newBlock.line) {
                        Engine.logBeans.trace("The new block and its next block are not on the same line. Trying to insert it on the right place.");
                        Block newBlockTmp = newBlock;
                        while (windowedBlockManager.getNextBlock(newBlockTmp).line != newBlock.line) {
                            newBlockTmp = windowedBlockManager.getNextBlock(newBlockTmp);
                        }
                        Engine.logBeans.trace("The previous block of the right place is : " + newBlockTmp.toString());
                        windowedBlockManager.removeBlock(newBlock);
                        windowedBlockManager.insertBlock(newBlock, newBlockTmp);
                    }
                    topLeftBlock = newBlock;
                }
            }
            // Engine.logBeans.trace("---topLeftBlock.attribute="+ topLeftBlock.attribute);
            if (topLeftBlock != null && downRightBlock != null && goodTitleLine(windowedBlockManager, topLeftBlock) && topLeftBlock.line != 0 && JavelinUtils.isSameAttribute(topLeftBlock.attribute, titleRowAttribute)) {
                // We search the first row of data, to give to super.execute method
                Block topLeftDataRow = topLeftBlock;
                while (windowedBlockManager.getNextBlock(topLeftDataRow) != null && windowedBlockManager.getNextBlock(topLeftDataRow).line == topLeftBlock.line) {
                    topLeftDataRow = windowedBlockManager.getNextBlock(topLeftDataRow);
                }
                topLeftDataRow = windowedBlockManager.getNextBlock(topLeftDataRow);
                // Set line of actions and finding columns of the table
                if (actionColumnExists) {
                    setColumnSelection(0);
                    int checkAction = extractActionsLine(windowedBlockManager, topLeftBlock);
                    if (checkAction == 0)
                        setColumnSelection(-1);
                    else
                        setLineActions(checkAction);
                } else
                    setColumnSelection(-1);
                columnTitleWest = javelin.getScreenWidth();
                columnTitleEast = 0;
                setColumns(createTitleColumns(windowedBlockManager, topLeftBlock, downRightBlock.line, columnDataLastCharEast));
                Engine.logBeans.trace("##### Columns: " + getColumns());
                XMLRectangle mySelectionScreenZone = new XMLRectangle(Math.min(columnDataLastCharWest, columnTitleWest), topLeftBlock.line + 1, (Math.max(columnDataLastCharEast, columnTitleEast) - topLeftBlock.column) + 1, subfileMarkerLine - topLeftBlock.line - 1);
                if (screenZoneSet)
                    setSelectionScreenZone(oldScreenZone);
                else
                    setSelectionScreenZone(mySelectionScreenZone);
                // set the offset to 1 this will result in the table to be shifted up one line
                if (actionColumnExists)
                    this.setOffset(2);
                else
                    this.setOffset(1);
                // removing the subfile blocks from their panel.
                Block panel = windowedBlockManager.getPanel();
                if (panel != null) {
                    Engine.logBeans.trace("SelectionScreenZone: " + getSelectionScreenZone().toString());
                    for (Block panelChildBlock : new ArrayList<Block>(panel.getOptionalBlockChildren())) {
                        Engine.logBeans.trace("Trying to remove from Panel: " + panelChildBlock.toString());
                        if (// data line
                        getSelectionScreenZone().contains(panelChildBlock.column, panelChildBlock.line, panelChildBlock.length, 1) || (JavelinUtils.isSameAttribute(panelChildBlock.attribute, actionLineAttribute) && panelChildBlock.line < getSelectionScreenZone().y && panelChildBlock.line >= getSelectionScreenZone().y + getLineActions())) {
                            // action line
                            panel.removeOptionalChildren(panelChildBlock);
                            Engine.logBeans.trace("Removed.");
                        }
                    }
                }
                Engine.logBeans.trace("columnDataLastCharWest=" + columnDataLastCharWest + "; topLeftBlock-line=" + topLeftBlock.line + "; downright-col= " + columnDataLastCharEast + "; downright-line= " + downRightBlock.line);
                xrs = super.execute(javelin, topLeftDataRow, blockFactory, dom);
                table.setOptionalAttribute("subfile", "true");
                table.setOptionalAttribute("titleheight", Integer.toString(titleHeight + 1));
                setSelectionScreenZone(oldScreenZone);
                setColumns(oldColumns);
                // block still contains the end of sub file marker .. set the newCurrentBlock to this block
                xrs.newCurrentBlock = block;
                return xrs;
            } else {
                Engine.logBeans.info("Give up subfile creation => Down Right Block is null              				: " + downRightBlock.toString());
                Engine.logBeans.info("                         OR Top Left Block is null or not of title attribute 	: " + (topLeftBlock == null ? "null" : topLeftBlock.toString()));
                xrs.hasMatched = false;
                xrs.newCurrentBlock = block;
                return xrs;
            }
        } else {
            xrs.hasMatched = false;
            xrs.newCurrentBlock = block;
            return xrs;
        }
    } catch (NullPointerException e) {
        Engine.logBeans.error("NullPointerException while executing the Subfile extraction rule", e);
        xrs.hasMatched = false;
        xrs.newCurrentBlock = blockFactory.getLastBlock();
        return xrs;
    }
}
Also used : XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) XMLRectangle(com.twinsoft.convertigo.beans.common.XMLRectangle) Block(com.twinsoft.convertigo.beans.core.Block)

Example 34 with JavelinExtractionRuleResult

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult 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);
    }
}
Also used : PanelBlockFactory(com.twinsoft.convertigo.beans.common.PanelBlockFactory) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) ExtractionRule(com.twinsoft.convertigo.beans.core.ExtractionRule) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) TabBox(com.twinsoft.convertigo.beans.common.TabBox) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) XMLRectangle(com.twinsoft.convertigo.beans.common.XMLRectangle) Block(com.twinsoft.convertigo.beans.core.Block) Nptui(com.twinsoft.convertigo.beans.sna.Nptui) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent)

Example 35 with JavelinExtractionRuleResult

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult 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;
}
Also used : StringTokenizer(java.util.StringTokenizer) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Element(org.w3c.dom.Element) Block(com.twinsoft.convertigo.beans.core.Block)

Aggregations

JavelinExtractionRuleResult (com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult)39 Block (com.twinsoft.convertigo.beans.core.Block)24 LinkedList (java.util.LinkedList)5 StringTokenizer (java.util.StringTokenizer)5 Element (org.w3c.dom.Element)4 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)3 XMLRectangle (com.twinsoft.convertigo.beans.common.XMLRectangle)2 EngineException (com.twinsoft.convertigo.engine.EngineException)2 RESyntaxException (org.apache.regexp.RESyntaxException)2 ACLNptuiComponent (com.eicon.iConnect.acl.ACLNptuiComponent)1 ACLNptuiScreen (com.eicon.iConnect.acl.ACLNptuiScreen)1 ACLNptuiWindow (com.eicon.iConnect.acl.ACLNptuiWindow)1 PanelBlockFactory (com.twinsoft.convertigo.beans.common.PanelBlockFactory)1 TabBox (com.twinsoft.convertigo.beans.common.TabBox)1 ExtractionRule (com.twinsoft.convertigo.beans.core.ExtractionRule)1 JavelinExtractionRule (com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule)1 Nptui (com.twinsoft.convertigo.beans.sna.Nptui)1 EngineEvent (com.twinsoft.convertigo.engine.EngineEvent)1 StringEx (com.twinsoft.util.StringEx)1 File (java.io.File)1