gpt4 book ai didi

java - 从 COBOL 文件生成属性名称

转载 作者:行者123 更新时间:2023-11-30 06:52:16 25 4
gpt4 key购买 nike

我对 java 比较陌生,我希望更改 COBOL 的 XSD 文件转换中的属性名称。我有以下 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);
}

}

从其扩展的基本元素类

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

}

和接口(interface)SchemaConvertable

public interface SchemaConvertable {

void parse(SchemaElement aElem);

void toSchema(SchemaElement aElem);

}

属性名称位于“:PR-”之后,并且始终为大写字母(ABCD-EFGH-IJKL)。我想更改它,以便只有名称的第一个字母是大写的(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);
}

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

这是 JRecord 代码生成器的一部分。它生成 Java 代码来读取/写入 Cobol fata 文件/Csv 文件等。它可能包含对您有用的代码。

关于java - 从 COBOL 文件生成属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com