从 COBOL 文件生成属性名称

Generating attribute names from COBOL file

我是 java 的新手,我想更改 XSD 从 COBOL 文件转换中的属性名称。我有以下 Java 代码

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);
}

}

基本元素class 从哪里延伸

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();
}

}

和接口 SchemaConvertable

public interface SchemaConvertable {

void parse(SchemaElement aElem);

void toSchema(SchemaElement aElem);

}

属性名称在“:PR-”之后,并且始终为大写字母(ABCD-EFGH-IJKL)。我想更改它,以便只有第一个字母是 name(Abcd-Efgh-Ijkl) 的大写字母。我怎样才能实现上述目标?

这将实现你想要的:

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();
}

您可以更新 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);
}

取自方法 cobolName2JavaName in https://sourceforge.net/p/jrecord/code/HEAD/tree/jrecord/Source/JRecord_CodeGen/src/net/sf/JRecord/cg/common/CCode.java#l99

这是 JRecord 代码生成器的一部分。它生成 Java 代码到 read/write Cobol fata 文件/Csv 文件等。它可能有对你有用的代码。