Search in sources :

Example 26 with Block

use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.

the class Table 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) {
    try {
        JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
        Block row;
        Block item;
        Block curBlock;
        int curLine, firstColumn;
        boolean automaticColumn = false;
        table = null;
        colonDispatch = new LinkedList<Block>();
        firstLine = getSelectionScreenZone().y;
        firstColumn = getSelectionScreenZone().x;
        if (accumulatedData == null) {
            // not accumulating, create a new table element
            try {
                // create the main block of the table
                table = new Block();
                table.tagName = this.tagName;
                table.name = "table";
                table.type = "table";
                table.setText("");
                table.bRender = true;
                table.line = firstLine;
                table.column = firstColumn;
                table.setOptionalAttribute("offset", "" + getOffset());
                // table.setOptionalAttribute("autoValidate", "" + isAutoValidate());
                // insert this block before the current block
                blockFactory.insertBlock(table, blockFactory.getPreviousBlock(block));
                curBlock = block;
                // create the table of actions if needed
                if (columnSelection >= 0) {
                    boolean hasExtractActions = extractActions(blockFactory, dom, table);
                    if (!hasExtractActions) {
                        if (actions == null || actions.isEmpty())
                            Engine.logBeans.debug("Warning: no actions defined.");
                        else {
                            Block actionsDefined = new Block();
                            actionsDefined.name = "";
                            actionsDefined.type = "actionsTable";
                            actionsDefined.tagName = "actionsTable";
                            actionsDefined.setText("");
                            for (List<String> myAction : actions) {
                                Element element = dom.createElement("action");
                                element.setAttribute("char", myAction.get(0));
                                element.setAttribute("key", myAction.get(1));
                                element.setAttribute("label", myAction.get(2));
                                actionsDefined.addOptionalChildren(element);
                            }
                            table.addOptionalChildren(actionsDefined);
                        }
                    }
                }
                // create the first row
                row = new Block();
                row.name = "";
                row.type = "row";
                row.tagName = "row";
                row.setText("");
                row.setOptionalAttribute("index", Integer.toString(indexRow));
                indexRow++;
                row.setOptionalAttribute("page", Integer.toString(numPage));
                // append the row block as a child to the main block only if we include titles
                if (!doNotIncludeTitles) {
                    table.addOptionalChildren(row);
                }
                // note the current line.
                curLine = curBlock.line;
            } catch (Exception e) {
                xrs.hasMatched = false;
                xrs.newCurrentBlock = block;
                return xrs;
            }
        } else {
            // there is accumulated data, use it
            table = accumulatedData;
            // As we are in accumulate mode, some new actions may have been defined, exract them
            extractActions(blockFactory, dom, table);
            // prepare a new row in the table
            row = new Block();
            row.name = "";
            row.type = "row";
            row.tagName = "row";
            row.setText("");
            row.line = block.line;
            row.column = block.column;
            row.setOptionalAttribute("index", Integer.toString(indexRow));
            indexRow++;
            row.setOptionalAttribute("page", Integer.toString(numPage));
            // set the first row in the table
            // table.appendChild(row);
            table.addOptionalChildren(row);
            curBlock = block;
            curLine = curBlock.line;
        }
        Engine.logBeans.trace("First block in the table, line: " + curBlock.line + " col: " + curBlock.column + " len : " + curBlock.length);
        if (columns.size() == 0) {
            Block temp;
            // no columns were defined, so assume the first row will define the columns
            // set the automatic Column mode flag.
            automaticColumn = true;
            curBlock = block;
            while (curBlock != null && (curBlock.line == curLine)) {
                if (!canBlockBeSelected(curBlock)) {
                    curBlock = blockFactory.getNextBlock(curBlock);
                    continue;
                }
                String title = StringUtils.normalize(curBlock.getText());
                columns.add(new TableColumn(title, curBlock.column, curBlock.column + curBlock.length, 0));
                Engine.logBeans.trace("Table defining '" + curBlock.getText() + "' from column: " + curBlock.column + " to column: " + (curBlock.column + curBlock.length));
                temp = blockFactory.getNextBlock(curBlock);
                blockFactory.removeBlock(curBlock);
                curBlock = temp;
            }
            curLine = curBlock.line;
        }
        // we do not accumulate
        if (accumulatedData == null) {
            if (!doNotIncludeTitles) {
                if (columnsBlocks == null) {
                    int tableSize = 0;
                    for (TableColumn tc : columns) {
                        item = new Block();
                        item.setText("");
                        item.name = "";
                        item.type = "item";
                        item.tagName = "Title";
                        item.column = tc.startColumn;
                        item.line = curBlock.line - 1;
                        item.setOptionalAttribute("size", Integer.toString(tc.endColumn - tc.startColumn + 1));
                        tableSize += tc.endColumn - tc.startColumn + 1;
                        row.addOptionalChildren(item);
                        String title = tc.title;
                        StringTokenizer st = new StringTokenizer(title, "\\", false);
                        if (st.countTokens() > titleH) {
                            titleH = st.countTokens();
                        }
                        String titleLabel;
                        Block titleBlock;
                        try {
                            while (true) {
                                titleLabel = st.nextToken();
                                titleBlock = new Block();
                                titleBlock.setText(titleLabel);
                                titleBlock.line = item.line - st.countTokens();
                                titleBlock.column = item.column;
                                item.addOptionalChildren(titleBlock);
                            }
                        } catch (NoSuchElementException e) {
                        // normal exit
                        }
                    }
                    table.setOptionalAttribute("size", "" + tableSize);
                } else {
                    int tableSize = 0;
                    for (TableColumn tc : columns) {
                        item = new Block();
                        item.setText("");
                        item.name = "";
                        item.type = "item";
                        item.tagName = "Title";
                        item.column = tc.startColumn;
                        item.setOptionalAttribute("size", Integer.toString(tc.endColumn - tc.startColumn + 1));
                        tableSize += tc.endColumn - tc.startColumn + 1;
                        row.addOptionalChildren(item);
                        for (Block columnBlock : columnsBlocks) {
                            if ((tc.startColumn <= columnBlock.column) && (columnBlock.column <= tc.endColumn)) {
                                item.line = Math.min(item.line, columnBlock.line);
                                item.addOptionalChildren(columnBlock);
                            }
                        }
                    }
                    table.setOptionalAttribute("size", "" + tableSize);
                }
            }
            row = new Block();
            row.name = "";
            row.type = "row";
            row.tagName = "row";
            row.setText("");
            row.line = block.line;
            row.column = block.column;
            // set the first row in the table
            table.addOptionalChildren(row);
            row.setOptionalAttribute("index", Integer.toString(indexRow));
            indexRow++;
            row.setOptionalAttribute("page", Integer.toString(numPage));
            // store table in accumulatedData
            accumulatedData = table;
        }
        // we found a matching block; scan all other blocks to get other table items
        while (curBlock != null) {
            Block temp;
            if (!canBlockBeSelected(curBlock)) {
                curBlock = blockFactory.getNextBlock(curBlock);
                continue;
            }
            if ((curBlock.line - row.line) > max_index) {
                // calc the number of lines skipped
                // this will handle the empty lines...
                int skip = (curBlock.line - row.line) / (max_index + 1);
                for (int i = 0; i < skip; i++) {
                    // we changed line, so write all the columns built in the prevous row held in colonDispatch
                    writeColumns(row);
                    // clear the colonDispatch vector for next row
                    colonDispatch = new LinkedList<Block>();
                    // set the line reference
                    curLine = curBlock.line;
                    // build a new row
                    row = new Block();
                    row.name = "";
                    row.type = "row";
                    row.tagName = "row";
                    row.setText("");
                    // row line depends on  lines skipped
                    row.line = curBlock.line - skip + i + 1;
                    row.column = curBlock.column;
                    table.addOptionalChildren(row);
                    row.setOptionalAttribute("index", Integer.toString(indexRow));
                    indexRow++;
                    row.setOptionalAttribute("page", Integer.toString(numPage));
                    if (keepEmptyLines && skip > 1) {
                        // if we skipped lines (Empty lines) insert a fake block as the writeColumns function
                        // will discard any empty block.
                        Block fakeBlock = new Block();
                        fakeBlock.line = row.line;
                        fakeBlock.column = row.column;
                        fakeBlock.setText("");
                        fakeBlock.name = "";
                        fakeBlock.type = "static";
                        dispatchBlockInColumn(fakeBlock);
                    }
                }
            }
            dispatchBlockInColumn(curBlock);
            temp = blockFactory.getNextBlock(curBlock);
            blockFactory.removeBlock(curBlock);
            curBlock = temp;
        }
        // write the last row
        writeColumns(row);
        if (automaticColumn)
            columns = new XMLVector<TableColumn>();
        table.setOptionalAttribute("rows", Integer.toString(indexRow - 1));
        table.setOptionalAttribute("titleheight", "" + titleH);
        int tableWidth = getSelectionScreenZone().width;
        int tableHeight = getSelectionScreenZone().height;
        Engine.logBeans.debug("SelectionScreenZone tableWidth : " + tableWidth + "tableHeight" + tableHeight);
        if (tableWidth != -1)
            table.setOptionalAttribute("width", Integer.toString(tableWidth));
        else
            table.setOptionalAttribute("width", Integer.toString(javelin.getScreenWidth()));
        if (tableHeight != -1)
            table.setOptionalAttribute("height", Integer.toString(tableHeight + resize));
        else
            table.setOptionalAttribute("height", Integer.toString(javelin.getScreenHeight() + resize));
        addMashupAttribute(table);
        xrs.hasMatched = true;
        xrs.newCurrentBlock = block;
        return xrs;
    } finally {
        if (doNotAccumulate) {
            accumulatedData = null;
        } else {
            numPage++;
        }
    }
}
Also used : Element(org.w3c.dom.Element) NoSuchElementException(java.util.NoSuchElementException) StringTokenizer(java.util.StringTokenizer) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Block(com.twinsoft.convertigo.beans.core.Block) NoSuchElementException(java.util.NoSuchElementException)

Example 27 with Block

use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.

the class TagName 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) {
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    String tagName = "";
    Block prevBlock;
    xrs.hasMatched = false;
    xrs.newCurrentBlock = block;
    // test if we have a tag name explicitly defined, if it is the case return the tag name
    if (this.tagName.length() != 0) {
        Engine.logBeans.trace("FieldName extraction rule matched by explicitly defined tag : " + this.tagName);
        block.tagName = this.tagName;
        block.setOptionalAttribute("history", (bSaveHistory ? "true" : "false"));
        addMashupAttribute(block);
        xrs.hasMatched = true;
        xrs.newCurrentBlock = block;
        return xrs;
    }
    // first search in our list of described fields for a field matching with
    // coordinates
    tagName = findFieldByCoord(block);
    if (labelPolicy == TagName.LABEL_POLICY_EXPLICIT) {
        if (tagName.length() == 0) {
            throw new IllegalArgumentException("TagName extraction rule: \"" + getName() + "\"\nThe label policy is set to 'Explicit' while the tag name property is empty!");
        }
        block.tagName = tagName;
        Engine.logBeans.trace("FieldName extraction rule matched by coord. tag is : " + block.tagName);
        block.setOptionalAttribute("history", (bSaveHistory ? "true" : "false"));
        addMashupAttribute(block);
        xrs.hasMatched = true;
        xrs.newCurrentBlock = block;
        return xrs;
    }
    // we did not match by coordinates, so try to get a valid tag name by scanning all
    // the prevous or next blocks on the same line according to the scanForward setting
    prevBlock = block;
    String blockName;
    while (prevBlock.line == block.line) {
        if (labelPolicy == TagName.LABEL_POLICY_FROM_PREVIOUS_BLOCK)
            prevBlock = blockFactory.getPreviousBlock(prevBlock);
        else if (labelPolicy == TagName.LABEL_POLICY_FROM_NEXT_BLOCK)
            prevBlock = blockFactory.getNextBlock(prevBlock);
        if (prevBlock != null) {
            if (prevBlock.type.equalsIgnoreCase("static")) {
                blockName = prevBlock.getText();
                tagName = StringUtils.normalize(blockName, false);
                Engine.logBeans.trace("Found tag name: " + tagName + " (from " + blockName + ")");
                if (tagName.length() != 0) {
                    if (prevBlock.line == block.line) {
                        // we created a valid tag, so stop here
                        break;
                    } else {
                        // the block is not on the same line, prepare for artificial tag
                        tagName = "";
                    }
                } else {
                    tagName = "default-tagname";
                }
            }
        } else {
            tagName = "block";
            break;
        }
    }
    if (tagName.length() == 0) {
        // we did not find on the same line a valid tag, so create one artificially
        tagName = "default-tagname";
    }
    // we have a valid tagName, search in our Field list if we  have a literal
    String temp = findFieldByLiteral(tagName);
    if (temp.length() != 0) {
        block.tagName = temp;
        Engine.logBeans.trace("FieldName extraction rule matched by literal tag is : " + block.tagName);
    } else {
        block.tagName = tagName;
        Engine.logBeans.trace("FieldName extraction rule matched tag is : " + block.tagName);
    }
    addMashupAttribute(block);
    block.setOptionalAttribute("history", (bSaveHistory ? "true" : "false"));
    xrs.hasMatched = true;
    xrs.newCurrentBlock = block;
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Block(com.twinsoft.convertigo.beans.core.Block)

Example 28 with Block

use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.

the class PanelBlockFactory method make.

/**
 * Makes all blockFactory's blocks from a panel type Block.
 * These blocks should be rearranged by application of extraction rules.
 *
 * @param panelBlock the block from which extract the blockFactory's blocks (only a panel, tabBox or container type block).
 */
public void make(Block panelBlock) {
    setPanel(panelBlock);
    nbColumns = Integer.parseInt(panelBlock.getOptionalAttribute("width"));
    nbLines = Integer.parseInt(panelBlock.getOptionalAttribute("height"));
    list = new LinkedList<Block>();
    fields = new HashMap<String, Block>();
    nbItems = panelBlock.getNumberOfOptionalChildren();
    Block lastBlock = null;
    for (Block block : panelBlock.getOptionalBlockChildren()) {
        insertBlock(block, lastBlock);
        if (block.type.equals("field"))
            fields.put(block.name, block);
        lastBlock = block;
    }
}
Also used : Block(com.twinsoft.convertigo.beans.core.Block)

Example 29 with Block

use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.

the class Record method execute.

public JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    Block curBlock = block, eorBlock = block, temp;
    boolean found = false;
    // recreate the regular expression object.
    if (borRE == null) {
        try {
            setBorType(borType);
        } catch (RESyntaxException e) {
            Engine.logBeans.error("Unable to create the regular expression object for BOR type", e);
        }
    }
    if (eorRE == null) {
        try {
            setEorType(eorType);
        } catch (RESyntaxException e) {
            Engine.logBeans.error("Unable to create the regular expression object for EOR type", e);
        }
    }
    if (perPage) {
        Engine.logBeans.trace("Applying Record rule to the current page");
        // Test if it's the first block of the page
        if (blockFactory.getPreviousBlock(block) != null) {
            Engine.logBeans.trace("The block isn't the first block of the page, aborting ...");
            xrs.hasMatched = false;
            xrs.newCurrentBlock = block;
            return xrs;
        }
        // Creation and initialisation of the parent bloc of the record
        Block record = new Block();
        Engine.logBeans.trace("Creation du bloc parent");
        record.type = "record";
        record.setText("");
        record.name = "";
        if (this.tagName.length() != 0)
            record.tagName = this.tagName;
        else
            record.tagName = "record";
        // Insert the main bloc in the blockFactory in first position
        blockFactory.insertBlock(record, null);
        Engine.logBeans.trace("Insertion du bloc parent");
        // After being added, each block is deleted
        while (curBlock != null) {
            record.addOptionalChildren(curBlock);
            Engine.logBeans.trace("appending child " + curBlock.tagName + " to record " + record.tagName);
            temp = blockFactory.getNextBlock(curBlock);
            blockFactory.removeBlock(curBlock);
            curBlock = temp;
        }
        xrs.hasMatched = true;
        xrs.newCurrentBlock = record;
        return xrs;
    } else {
        Engine.logBeans.trace("Applying Record rule to block " + block.name);
        // Tests if the block is in the specified zone and if it's a bor block
        if (isBor(block)) {
            // we look for an eor block
            while ((curBlock = blockFactory.getNextBlock(curBlock)) != null) {
                if (!canBlockBeSelected(curBlock))
                    continue;
                if (isEor(curBlock)) {
                    eorBlock = curBlock;
                    found = true;
                    break;
                }
            }
            // If the end of the screen has been reached, set the 'found' flag to true.
            if (curBlock == null) {
                found = true;
            }
        }
        // of the main block of the record
        if (found) {
            Engine.logBeans.trace("Begining and End of Record " + this.tagName + " found");
            Block recordBlock;
            recordBlock = new Block();
            Engine.logBeans.trace("Creation du bloc BOR");
            recordBlock.type = "record";
            recordBlock.setText("");
            recordBlock.name = "";
            recordBlock.line = block.line;
            recordBlock.column = block.column;
            if (this.tagName.length() != 0)
                recordBlock.tagName = this.tagName;
            // Insert the main block in the blockFactory in first position
            blockFactory.insertBlock(recordBlock, blockFactory.getPreviousBlock(block));
            Engine.logBeans.trace("BOR block inserted ...");
            // Starts the block just after the bor
            curBlock = blockFactory.getNextBlock(recordBlock);
            Block nextBlock = null;
            if (eorBlock != null) {
                nextBlock = blockFactory.getNextBlock(eorBlock);
            }
            // and deleted from memory
            while (curBlock != null) {
                if (isEor(curBlock)) {
                    if (bor.equals(eor)) {
                        if (recordBlock.hasOptionalChildren()) {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                recordBlock.addOptionalChildren(curBlock);
                Engine.logBeans.trace("Appending child " + curBlock.tagName + " to record " + recordBlock.tagName);
                temp = blockFactory.getNextBlock(curBlock);
                blockFactory.removeBlock(curBlock);
                curBlock = temp;
            }
            // Deletes the eor separator only if it's followed  by a bor separator
            // and sets the new current block
            xrs.newCurrentBlock = recordBlock;
            if (isBor(nextBlock) || (nextBlock == null)) {
                if (eorBlock != null) {
                    blockFactory.removeBlock(eorBlock);
                }
            }
            xrs.hasMatched = true;
            return xrs;
        }
    }
    xrs.hasMatched = false;
    xrs.newCurrentBlock = block;
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Block(com.twinsoft.convertigo.beans.core.Block) RESyntaxException(org.apache.regexp.RESyntaxException)

Example 30 with Block

use of com.twinsoft.convertigo.beans.core.Block in project convertigo by convertigo.

the class RemoveBlocks 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.
 */
protected JavelinExtractionRuleResult execute(iJavelin javelin, Block block, BlockFactory blockFactory, org.w3c.dom.Document dom) {
    JavelinExtractionRuleResult xrs = new JavelinExtractionRuleResult();
    Block curBlock;
    Block tempBlock;
    int blockLen;
    xrs.hasMatched = false;
    // recreate the regular expression object.
    if (regexp == null) {
        try {
            Engine.logBeans.debug("Setting regular expression to : " + regularExpression);
            setRegularExpression(regularExpression);
        } catch (RESyntaxException e) {
            Engine.logBeans.error("Unable to create the regular expression object", e);
        }
    }
    curBlock = block;
    do {
        tempBlock = blockFactory.getNextBlock(curBlock);
        if (canBlockBeSelected(curBlock)) {
            if ((blockTag.length() == 0) || blockTag.equalsIgnoreCase(curBlock.tagName)) {
                blockLen = curBlock.getText().trim().length();
                if ((length == -1) || (blockLen == length)) {
                    // Avoid to try to match on empty strings
                    if (blockLen != 0) {
                        if (regexp.match(curBlock.getText())) {
                            blockFactory.removeBlock(curBlock);
                            xrs.hasMatched = true;
                        }
                    } else {
                        blockFactory.removeBlock(curBlock);
                        xrs.hasMatched = true;
                    }
                }
            }
        }
        if (tempBlock == null)
            break;
        curBlock = tempBlock;
    } while (true);
    xrs.newCurrentBlock = curBlock;
    return xrs;
}
Also used : JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) Block(com.twinsoft.convertigo.beans.core.Block) RESyntaxException(org.apache.regexp.RESyntaxException)

Aggregations

Block (com.twinsoft.convertigo.beans.core.Block)48 JavelinExtractionRuleResult (com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult)24 Element (org.w3c.dom.Element)8 StringTokenizer (java.util.StringTokenizer)7 LinkedList (java.util.LinkedList)5 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)4 EngineException (com.twinsoft.convertigo.engine.EngineException)4 NoSuchElementException (java.util.NoSuchElementException)4 XMLRectangle (com.twinsoft.convertigo.beans.common.XMLRectangle)3 ArrayList (java.util.ArrayList)3 PanelBlockFactory (com.twinsoft.convertigo.beans.common.PanelBlockFactory)2 RESyntaxException (org.apache.regexp.RESyntaxException)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ACLNptuiChoice (com.eicon.iConnect.acl.ACLNptuiChoice)1 ACLNptuiComponent (com.eicon.iConnect.acl.ACLNptuiComponent)1 ACLNptuiScrollBar (com.eicon.iConnect.acl.ACLNptuiScrollBar)1 ACLNptuiSelectionField (com.eicon.iConnect.acl.ACLNptuiSelectionField)1 ACLNptuiWindow (com.eicon.iConnect.acl.ACLNptuiWindow)1 DefaultBlockFactory (com.twinsoft.convertigo.beans.common.DefaultBlockFactory)1