private int validateMessageInterchangeHeader()

in SdmxEdiParser/src/main/java/org/sdmxsource/sdmx/ediparser/engine/impl/EdiParseEngineImpl.java [207:351]


        private int validateMessageInterchangeHeader(EDIMetadata metadata, int currentnum) {
            int segmentStart = ediReader.getLineNumber();
            EDIUtil.assertPrefix(ediReader, EDI_PREFIX.MESSAGE_IDENTIFICATION, true);
            String[] splitOnPlus = EDIUtil.splitOnPlus(currentLine, 2);
            String messageIdentification = splitOnPlus[0];

            if (!splitOnPlus[1].equals("GESMES:2:1:E6")) {
                throw new IllegalArgumentException("Expecting GESMES:2:1:E6 but was " + splitOnPlus[1]);
            }

            readNextLine();
            EDIUtil.assertPrefix(ediReader, EDI_PREFIX.MESSAGE_FUNCTION, true);
            MESSSAGE_FUNCTION messageFunction = MESSSAGE_FUNCTION.getFromEdiStr(currentLine);

            //Fifth Line is the message agency
            readNextLine();
            EDIUtil.assertPrefix(ediReader, EDI_PREFIX.MESSAGE_AGENCY, true);
            String messageAgency = currentLine;


            //Sixth Line is the receiving agency
            readNextLine();
            EDIUtil.assertPrefix(ediReader, EDI_PREFIX.RECIEVING_AGENCY, true);
            String recievingAgency = currentLine;

            PartyBean sendingAgency = processMessageSender();

            int documentStart = ediReader.getLineNumber();
            if (messageFunction.isData()) {
                determineDatasetMetadata();
                metadata.setReportingBegin(reportingBegin);
                metadata.setReportingEnd(reportingEnd);
            }

            boolean inRecursive = false;
            List<KeyValue> datasetAttributes = new ArrayList<KeyValue>();
            while (currentLine != null) {
                try {
                    readNextLine();
                    if (!messageFunction.isData()) {
                        if (ediReader.getLineType().isDataSegment()) {
                            documentStart = ediReader.getLineNumber() + 1;
                            throw new IllegalArgumentException("Message function is " + messageFunction.getEDIString() + " but mesage contains a data segment");
                        }
                    } else if (!messageFunction.isStructure()) {
                        if (ediReader.getLineType().isStructureSegment()) {
                            throw new IllegalArgumentException("Message function is " + messageFunction.getEDIString() + " but mesage contains a structure segment");
                        }
                    }

                    //CHECK FOR ANY DATASET ATTRIBUTES AND STORE THEM
                    if (messageFunction.isData()) {
                        if (ediReader.getLineType() == EDI_PREFIX.DATASET_ATTRIBUTE_SCOPE) {
                            int scope = Integer.parseInt(ediReader.getCurrentLine());
                            //1 = dataset, 4=mix of dimensions, 5=observation
                            if (scope == 1) {
                                readNextLine();
                                EDIUtil.assertPrefix(ediReader, EDI_PREFIX.DATASET_DATAATTRIBUTE, true);
                                while (true) {
                                    readNextLine();
                                    if (ediReader.getLineType() != EDI_PREFIX.DATASET_ATTRIBUTE_CODED
                                            && ediReader.getLineType() != EDI_PREFIX.DATASET_ATTRIBUTE_UNCODED) {
                                        ediReader.moveBackLine();
                                        break;
                                    }
                                    String attributeConceptId = ediReader.getCurrentLine();
                                    String attributeValue = null;
                                    if (ediReader.getLineType() == EDI_PREFIX.DATASET_ATTRIBUTE_CODED) {
                                        //Move to the code value Line
                                        assertMoveNext();
                                        //If the current line is the attribute value then store it, otherwise
                                        if (EDIUtil.assertPrefix(ediReader, EDI_PREFIX.CODE_VALUE, false)) {
                                            attributeValue = ediReader.getCurrentLine();
                                        } else {
                                            ediReader.moveBackLine();
                                        }
                                    } else if (ediReader.getLineType() == EDI_PREFIX.DATASET_ATTRIBUTE_UNCODED) {
                                        String compositeValue = "";
                                        while (true) {
                                            // Move to the next line and see if it is FREE TEXT
                                            assertMoveNext();
                                            if (EDIUtil.assertPrefix(ediReader, EDI_PREFIX.STRING, false)) {
                                                compositeValue += ediReader.parseTextString();
                                            } else {
                                                break;
                                            }
                                        }
                                        attributeValue = compositeValue;
                                        ediReader.moveBackLine();
                                    }
                                    datasetAttributes.add(new KeyValueImpl(attributeValue, attributeConceptId));
                                }
                            }
                        }
                    }


                    if (ediReader.getLineType().isEndMessageAdministration()) {
                        if (ediReader.isBackLine()) {
                            ediReader.moveNext();
                        }

                        String[] splitMessAdminOnPlus = EDIUtil.splitOnPlus(currentLine, 2);
                        String numLinesString = splitMessAdminOnPlus[0];
                        int numLines = EDIUtil.parseStringAsInt(numLinesString);
                        int segmentCount = ediReader.getLineNumber() - segmentStart + 1;
                        if (segmentCount != numLines) {
                            throw new SdmxSemmanticException("Expected segment count '" + numLines + "' does not match actual segment count '" + segmentCount + "'");
                        }
                        EDIDocumentPosition documentPosition = new EDIDocumentPositionImpl(documentStart,
                                ediReader.getLineNumber(),
                                messageFunction.isStructure(),
                                datasetId,
                                messageAgency,
                                sendingAgency,
                                recievingAgency,
                                this.datasetAction,
                                this.keyFamilyIdentifier,
                                this.missingValue,
                                this.datasetPreperation,
                                this.reportingPeriod,
                                datasetAttributes);
                        metadata.addDocumentIndex(documentPosition);

                        String messageRef = splitMessAdminOnPlus[1];
                        if (!messageIdentification.equals(messageRef)) {
                            throw new SdmxSemmanticException("Message ref expected to be '" + messageIdentification + "' but was '" + messageRef + "'");
                        }
                        //Either we have another message identification or an end message
                        readNextLine();
                        if (ediReader.getLineType().isMessageIdentification()) {
                            inRecursive = true;
                            return validateMessageInterchangeHeader(metadata, ++currentnum);
                        }
                        return currentnum;
                    }
                } catch (SdmxException th) {
                    if (inRecursive) {
                        throw th;
                    }
                    throw new SdmxException(th, "Error while trying to validate EDI Message:" + messageIdentification);
                }
            }
            throw new SdmxSyntaxException("Message identification" + EDI_PREFIX.MESSAGE_IDENTIFICATION + " is not terminated with an end identification " + EDI_PREFIX.END_MESSAGE_ADMINISTRATION);
        }