我对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();
}
}
和接口SchemaConvertable
public interface SchemaConvertable {
void parse(SchemaElement aElem);
void toSchema(SchemaElement aElem);
}
属性的名称位于“:PR-”之后,它始终使用大写字母(ABCD-EFGH-IJKL)。我想改变它,只有第一个字母是名字的资本(Abcd-Efgh-Ijkl)。我怎样才能实现上述目标?
答案 0 :(得分:1)
这将实现您的目标:
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
这是JRecord代码生成器的一部分。它生成Java代码以读取/写入Cobol fata文件/ Csv文件等。它可能包含对您有用的代码。