Tai Lung
Tai Lung

Reputation: 89

Generating attribute names from COBOL file

I am relatively new to java and I am looking to change the attribute names in the XSD file conversion from COBOL. I have the following Java code

public class LineElement extends BasicElement {

// prelude of code to remove
String[] preludeArray = {":PR:-", "M-"};

private String name;
private String originalName;

public String getName() {
    return name;
}

public void setName(String aName) {
    this.name = aName;
}

public String getOriginalName() {
    return originalName;
}

public void setOriginalName(String aOriginalName) {
    this.originalName = aOriginalName;
}

@Override
public void parse(SchemaElement aElem) {
    Object[] splittedObjLine = splitLine(aElem);

    // every line got an identifier
    this.setId(SchemaUtil.parseNumber((String) splittedObjLine[0]));
    // every line got a variable name
    this.setOriginalName(filterName((String) splittedObjLine[1]));
    // set original name
    this.setName(getOriginalName());

    this.toSchema(aElem);
}

protected String filterName(String aToken) {
    String attributeName = aToken;
    for (String prelude : preludeArray) {
        int preLength = prelude.length();
        int preIndex = aToken.indexOf(prelude);
        if (preIndex == 0) {
            attributeName = aToken.substring(preIndex + preLength);
        }
    }
    return attributeName;
}

@Override
public void toSchema(SchemaElement aElem) {
    aElem.setId(this.getId());
    aElem.setName(this.getName());
    aElem.setCopyBookName(this.originalName);
    // set empty type as default
    ElementType elemType = new ElementType();
    elemType.setSchemaType(SchemaType.WRAPPER);
    aElem.setType(elemType);
}

}

The Basic Element class from where it extends

public abstract class BasicElement implements SchemaConvertable {

private int id;

public int getId() {
    return id;
}

public void setId(int aId) {
    this.id = aId;
}

/**
 * Split lines into separate token.
 *
 * @param aElem Element containing all information of source line.
 * @return cleaned up and split object array.
 */
protected Object[] splitLine(SchemaElement aElem) {
    // TODO Blanks in Strings ersetzen, damit im 'split' 
    // TODO nicht separate Objekte entstehen.

    String line = aElem.getSourceLine();
    // remove first 6 chars and end char '.'
    line = line.substring(0, line.lastIndexOf("."));

    String[] splitLine = line.split(" ");
    return removeEmptyToken(splitLine);
}

/**
 * Remove array entry if empty
 *
 * @param aLineToken array of token
 *
 * @return object array without empty or blank string token
 */
protected Object[] removeEmptyToken(String[] aLineToken) {
    List<String> cleanedSplittedLine = new ArrayList();
    for (String aLineToken1 : aLineToken) {
        if (!aLineToken1.trim().isEmpty()) {
            cleanedSplittedLine.add(aLineToken1);
        }
    }
    return cleanedSplittedLine.toArray();
}

}

and the interface SchemaConvertable

public interface SchemaConvertable {

void parse(SchemaElement aElem);

void toSchema(SchemaElement aElem);

}

The name of the attribute is after the ":PR-" and it is always in Capital letters(ABCD-EFGH-IJKL). I want to change it so that only the First letter is capital from the name(Abcd-Efgh-Ijkl) . How can I achieve the above?

Upvotes: 1

Views: 98

Answers (1)

Bruce Martin
Bruce Martin

Reputation: 10543

This will achieve what you want:

public static String cobolName2JavaName(String cobolName) {
    String lcCobolName = cobolName.toLowerCase();
    String ucCobolName = cobolName.toUpperCase();
    int length = cobolName.length();
    StringBuilder b = new StringBuilder(length);

    boolean toUCase = true; 
    char c;

    for (int i = 0; i < length; i++) {
        c = lcCobolName.charAt(i);
        switch (c) {
        case '-':
        case '_':
            toUCase = true;
            b.append(c)
            break;
        default:
            if (toUCase) {
                b.append(ucCobolName.charAt(i));
                toUCase = false;
            } else {
                b.append(c);
            }
        }
    }
    return b.toString();
}

You could update filterName:

protected String filterName(String aToken) {
    String attributeName = aToken;
    for (String prelude : preludeArray) {
        int preLength = prelude.length();
        int preIndex = aToken.indexOf(prelude);
        if (preIndex == 0) {
            attributeName = aToken.substring(preIndex + preLength);
        }
    }
    return cobolName2JavaName(attributeName);
}

Taken from method cobolName2JavaName in https://sourceforge.net/p/jrecord/code/HEAD/tree/jrecord/Source/JRecord_CodeGen/src/net/sf/JRecord/cg/common/CCode.java#l99

This is part ofg yhe Code-Generator for JRecord. It generates Java code to read/write Cobol fata files / Csv files etc. It may have code that is useful to you.

Upvotes: 1

Related Questions