Search in sources :

Example 1 with FastSimpleGenericEdifactDirectXMLParser

use of org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLParser in project pentaho-kettle by pentaho.

the class Edi2Xml method processRow.

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (Edi2XmlMeta) smi;
    data = (Edi2XmlData) sdi;
    // get row, blocks when needed!
    Object[] r = getRow();
    if (r == null) {
        // no more input to be expected...
        setOutputDone();
        return false;
    }
    String inputValue = "";
    if (first) {
        first = false;
        data.inputRowMeta = getInputRowMeta().clone();
        data.outputRowMeta = getInputRowMeta().clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
        String realInputField = environmentSubstitute(meta.getInputField());
        String realOutputField = environmentSubstitute(meta.getOutputField());
        data.inputFieldIndex = getInputRowMeta().indexOfValue(realInputField);
        int numErrors = 0;
        if (data.inputFieldIndex < 0) {
            logError(BaseMessages.getString(PKG, "Edi2Xml.Log.CouldNotFindInputField", realInputField));
            numErrors++;
        }
        if (!data.inputRowMeta.getValueMeta(data.inputFieldIndex).isString()) {
            logError(BaseMessages.getString(PKG, "Edi2Xml.Log.InputFieldIsNotAString", realInputField));
            numErrors++;
        }
        if (numErrors > 0) {
            setErrors(numErrors);
            stopAll();
            return false;
        }
        data.inputMeta = data.inputRowMeta.getValueMeta(data.inputFieldIndex);
        if (Utils.isEmpty(meta.getOutputField())) {
            // same field
            data.outputMeta = data.outputRowMeta.getValueMeta(data.inputFieldIndex);
            data.outputFieldIndex = data.inputFieldIndex;
        } else {
            // new field
            data.outputMeta = data.outputRowMeta.searchValueMeta(realOutputField);
            data.outputFieldIndex = data.outputRowMeta.size() - 1;
        }
        // create instances of lexer/tokenstream/parser
        // treat null values as empty strings for parsing purposes
        inputValue = Const.NVL(data.inputMeta.getString(r[data.inputFieldIndex]), "");
        lexer = new FastSimpleGenericEdifactDirectXMLLexer(new ANTLRStringStream(inputValue));
        tokens = new CommonTokenStream(lexer);
        parser = new FastSimpleGenericEdifactDirectXMLParser(tokens);
    } else {
        // treat null values as empty strings for parsing purposes
        inputValue = Const.NVL(data.inputMeta.getString(r[data.inputFieldIndex]), "");
        lexer.setCharStream(new ANTLRStringStream(inputValue));
        tokens.setTokenSource(lexer);
        parser.setTokenStream(tokens);
    }
    try {
        parser.edifact();
        // make sure the row is big enough
        r = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
        // place parsing result into output field
        r[data.outputFieldIndex] = parser.buf.toString();
        putRow(data.outputRowMeta, r);
    } catch (MismatchedTokenException e) {
        StringBuilder errorMessage = new StringBuilder(180);
        errorMessage.append("error parsing edi on line " + e.line + " position " + e.charPositionInLine);
        errorMessage.append(": expecting " + ((e.expecting > -1) ? parser.getTokenNames()[e.expecting] : "<UNKNOWN>") + " but found ");
        errorMessage.append((e.token.getType() >= 0) ? parser.getTokenNames()[e.token.getType()] : "<EOF>");
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), r, 1L, errorMessage.toString(), environmentSubstitute(meta.getInputField()), "MALFORMED_EDI");
        } else {
            logError(errorMessage.toString());
            // try to determine the error line
            String errorline = "<UNKNOWN>";
            try {
                errorline = inputValue.split("\\r?\\n")[e.line - 1];
            } catch (Exception ee) {
            // Ignore pattern syntax errors
            }
            logError("Problem line: " + errorline);
            logError(StringUtils.leftPad("^", e.charPositionInLine + "Problem line: ".length() + 1));
            throw new KettleException(e);
        }
    } catch (RecognitionException e) {
        StringBuilder errorMessage = new StringBuilder(180);
        errorMessage.append("error parsing edi on line ").append(e.line).append(" position ").append(e.charPositionInLine).append(". ").append(e.toString());
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), r, 1L, errorMessage.toString(), environmentSubstitute(meta.getInputField()), "MALFORMED_EDI");
        } else {
            logError(errorMessage.toString());
            // try to determine the error line
            String errorline = "<UNKNOWN>";
            try {
                errorline = inputValue.split("\\r?\\n")[e.line - 1];
            } catch (Exception ee) {
            // Ignore pattern syntax errors
            }
            logError("Problem line: " + errorline);
            logError(StringUtils.leftPad("^", e.charPositionInLine + "Problem line: ".length() + 1));
            throw new KettleException(e);
        }
    }
    if (checkFeedback(getLinesRead())) {
        logBasic("Linenr " + getLinesRead());
    }
    return true;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) KettleException(org.pentaho.di.core.exception.KettleException) FastSimpleGenericEdifactDirectXMLParser(org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLParser) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) KettleException(org.pentaho.di.core.exception.KettleException) RecognitionException(org.antlr.runtime.RecognitionException) FastSimpleGenericEdifactDirectXMLLexer(org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLLexer) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)1 RecognitionException (org.antlr.runtime.RecognitionException)1 KettleException (org.pentaho.di.core.exception.KettleException)1 FastSimpleGenericEdifactDirectXMLLexer (org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLLexer)1 FastSimpleGenericEdifactDirectXMLParser (org.pentaho.di.trans.steps.edi2xml.grammar.FastSimpleGenericEdifactDirectXMLParser)1