Skip to content

Commit

Permalink
Fix bug resulting from hyphens in *NAME parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
erssebaggala committed Jul 26, 2019
1 parent 1757c02 commit 9bfbb51
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
Binary file modified dist/boda-huaweimmlparser.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.bodastage.boda_huaweimmlparser</groupId>
<artifactId>boda-huaweimmlparser</artifactId>
<packaging>jar</packaging>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.1-SNAPSHOT</version>
<name>boda-huaweimmlparser-pr</name>
<url>http://maven.apache.org</url>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand All @@ -40,7 +42,7 @@ public class HuaweiMMLParser {
*
* Since 1.3.0
*/
final static String VERSION = "1.3.0";
final static String VERSION = "1.3.1";


Logger logger = LoggerFactory.getLogger(HuaweiMMLParser.class);
Expand Down Expand Up @@ -75,6 +77,15 @@ public HuaweiMMLParser(){
*/
private String tagData = "";

/**
* Line number
*
* Tracks line numbers
*
* @since 1.3.1
*/
private Integer lineNumber = 0;

/**
* Output directory.
*
Expand Down Expand Up @@ -380,6 +391,20 @@ public static void main( String[] args )
}
}

//Confirm that the output directory is a directory and has write
//privileges
if(outputDirectory != null ){
File fOutputDir = new File(outputDirectory);
if (!fOutputDir.isDirectory()) {
System.err.println("ERROR: The specified output directory is not a directory!.");
System.exit(1);
}

if (!fOutputDir.canWrite()) {
System.err.println("ERROR: Cannot write to output directory!");
System.exit(1);
}
}


//Get parser instance
Expand Down Expand Up @@ -539,7 +564,10 @@ public void parse() throws IOException {

parserState = ParserStates.EXTRACTING_VALUES;
}


//Reset line count
lineNumber = 0;

//Extracting values
if (parserState == ParserStates.EXTRACTING_VALUES) {
processFileOrDirectory();
Expand Down Expand Up @@ -617,6 +645,7 @@ public void processFileOrDirectory() throws IOException {

} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Error at line:" + lineNumber + " className:" + className);
System.out.println("Skipping file: " + this.baseFileName + "\n");
}
}
Expand All @@ -625,8 +654,10 @@ public void processFileOrDirectory() throws IOException {
}

public void processLine(String line) throws FileNotFoundException{
++lineNumber;
//logger.debug("processLine");
//Handle first line

if(line.startsWith("//Export start time:")){
String [] sArray = line.split("time:");
this.dateTime = sArray[1].trim();
Expand Down Expand Up @@ -672,7 +703,7 @@ public void processLine(String line) throws FileNotFoundException{
//Get the MO
String [] moPartArray = moPart.split(" ");
String moName = moPartArray[1].trim();

this.className = moName;

//Parameter Extraction Stage
Expand All @@ -688,9 +719,12 @@ public void processLine(String line) throws FileNotFoundException{
if(classNameAttrsMap.containsKey(moName)){ //this.className
attrStack = classNameAttrsMap.get(moName);
}

//Get the parameters
String [] paramPartArray = paramPart.split(", ");
//(?<=[^=]+=[^=]+),\s(?=[^=^"]+=[^=]+) --
//(?<=[^=]+),\\s(?=[^=^\"]+=[^=]+)
String [] paramPartArray = paramPart.split("(?<=[^=]+),\\s(?=[^=^\"]+=[^=]+)");

for(int i = 0, len = paramPartArray.length; i < len; i++){
String [] sArray = paramPartArray[i].split("=");
String paramName = sArray[0].trim();
Expand All @@ -705,8 +739,10 @@ public void processLine(String line) throws FileNotFoundException{
}

//Collect multivalue parameters
String tempValue = sArray[1].trim();
if(tempValue.matches("([^-]+-[^-]+&).*") ){
//Skip/ignore parameters that end with NAME such GSMCELLNAME. The reason for this is
//when there is hypen the parser was mistakenly treating the parameter has multivalued
String tempValue = sArray[1].trim();
if(tempValue.matches("([^-]+-[^-]+&).*") && !paramName.endsWith("NAME")){
String mvParameter = className + "_" + paramName;
//logger.debug("mvParameter: " + mvParameter);
//System.out.println("mvParameter:" + mvParameter);
Expand All @@ -726,25 +762,24 @@ public void processLine(String line) throws FileNotFoundException{
String childParameter = vArray[0];
if( !children.contains(childParameter)){
children.push(childParameter);
}

}
}

parameterChildMap.put(mvParameter, children);
}
//EOF: MV Parameters

}

classNameAttrsMap.put(moName,attrStack);
attrValueMap.clear();
return; //Stop here if we on the parameter extraction stage
}


if(ParserStates.EXTRACTING_VALUES == parserState){
//Get the parameters
String [] paramPartArray = paramPart.split(", ");
//String [] paramPartArray = paramPart.split("(?<=[^=]+=[^=]+),\\s(?=[^=^\"]+=[^=]+)");
String [] paramPartArray = paramPart.split("(?<=[^=]+),\\s(?=[^=^\"]+=[^=]+)");
for(int i = 0, len = paramPartArray.length; i < len; i++){
String [] sArray = paramPartArray[i].split("=");
String paramName = sArray[0].trim();
Expand All @@ -770,7 +805,6 @@ public void processLine(String line) throws FileNotFoundException{

//This list of parameters are added by default. Ignore to prevent duplicates
//String ignoreList = "FileName,varDateTime,BSCID,BAM_VERSION,OMU_IP,MBSC MODE";

Stack attrStack =classNameAttrsMap.get(className);

for(int y =0; y < attrStack.size(); y++){
Expand Down Expand Up @@ -812,7 +846,8 @@ public void processLine(String line) throws FileNotFoundException{
//Add the parameter values
Stack attrStack;
attrStack = classNameAttrsMap.get(moName);



Iterator <String> sIter = attrStack.iterator();
while(sIter.hasNext()){
String pName = sIter.next();
Expand All @@ -828,7 +863,7 @@ public void processLine(String line) throws FileNotFoundException{
String mvParameter = moName + "_" + pName;

String pValue = "";

if( parameterChildMap.containsKey(mvParameter)){

//Fix for bug where parser can't tell if parametr is multivalued or not
Expand Down Expand Up @@ -955,7 +990,8 @@ private void extactParameterAndValues(String line, String keyWord) throws FileNo
Stack attrStack = new Stack();

//Get the parameters
String [] paramPartArray = paramPart.split(", ");
//String [] paramPartArray = paramPart.split(", ");
String [] paramPartArray = paramPart.split("(?<=[^=]+),\\s(?=[^=^\"]+=[^=]+)");
for(int i = 0, len = paramPartArray.length; i < len; i++){
String [] sArray = paramPartArray[i].split("=");
String paramName = sArray[0].trim();
Expand All @@ -970,8 +1006,10 @@ private void extactParameterAndValues(String line, String keyWord) throws FileNo
}

//Collect multivalue parameters
//Skip/ignore parameters that end with NAME such GSMCELLNAME. The reason for this is
//when there is hypen the parser was mistakenly treating the parameter has multivalued
String tempValue = sArray[1].trim();
if(tempValue.matches("([^-]+-[^-]+&).*") ){
if(tempValue.matches("([^-]+-[^-]+&).*") && !paramName.endsWith("NAME")){
String mvParameter = className + "_" + paramName;


Expand Down Expand Up @@ -1060,8 +1098,10 @@ private void extactParameterAndValues(String line, String keyWord) throws FileNo
attrStack.push(pName);

//Handle multivalued parameter or parameters with children
//Skip/ignore parameters that end with NAME such GSMCELLNAME. The reason for this is
//when there is hypen the parser was mistakenly treating the parameter has multivalued
String tempValue = attrValueMap.get(pName);
if(tempValue.matches("([^-]+-[^-]+&).*") ){
if(tempValue.matches("([^-]+-[^-]+&).*") && !pName.endsWith("NAME")){
String mvParameter = className + "_" + pName;
parameterChildMap.put(mvParameter, null);
Stack children = new Stack();
Expand Down
61 changes: 52 additions & 9 deletions src/test/java/com/bodastage/boda_huaweimmlparser/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import static junit.framework.Assert.assertTrue;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
public class AppTest extends TestCase {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HuaweiMMLParser.class);
/**
* Create the test case
*
Expand All @@ -28,11 +36,46 @@ public static Test suite()
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );

public void testGeneralParsing(){

ClassLoader classLoader = getClass().getClassLoader();
File inFile = new File(classLoader.getResource("CFGMML1.txt").getFile());
String inputFile = inFile.getAbsolutePath();

String outputFolder = System.getProperty("java.io.tmpdir");

HuaweiMMLParser parser = new HuaweiMMLParser();

String[] args = { "-i", inputFile, "-o", outputFolder};

parser.main(args);

String expectedResult [] = {
"FILENAME,DATETIME,BSCID,BAM_VERSION,OMU_IP,MBSC MODE,PARAM1,PARAM2,PARAM3,PARAM4",
"CFGMML1.txt,2050-22-23 75:68:11,999,V111R050ABCDEFGHIJKLMNOPQRSTUVWXYZ,99.999.9.99,UO,\"VALUE ONE\",\"VALUE2\",NOT_SUPPORTED,\"Some string, with, commas\""
};

try{
String csvFile = outputFolder + File.separator + "MONAME.csv";

BufferedReader br = new BufferedReader(new FileReader(csvFile));
String csvResult [] = new String[expectedResult.length];

int i = 0;
String st;
while ((st = br.readLine()) != null) {
csvResult[i] = st;
++i;
}

assertTrue(Arrays.equals(expectedResult, csvResult));

}catch(FileNotFoundException ex){
LOGGER.error(ex.toString());
}catch(IOException ex){
LOGGER.error(ex.toString());
}

}
}
9 changes: 9 additions & 0 deletions src/test/resources/CFGMML1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//****************MML script file****************
//Export start time: 2050-22-23 75:68:11
//Export Workstation IP address: 11.999.77.8
//System BSCID: 999
//For BAM version: V111R050ABCDEFGHIJKLMNOPQRSTUVWXYZ
//MBSC Mode: UO
//OMU IP: 99.999.9.99
//***********************************************
SET MONAME:PARAM1="VALUE ONE", PARAM2="VALUE2", PARAM3=NOT_SUPPORTED, PARAM4="Some string, with, commas";
Empty file added src/test/resources/CFGMML2.txt
Empty file.

0 comments on commit 9bfbb51

Please sign in to comment.