将文本内容解析为java对象



我有这样的文本文件:

CREATE EntityGroup ORG_KPKALX_CFM
EntityChild ORG_UNIT.ORGKALX
EntityParent  ORG_UNIT.OU_ORG_UNIT
SCHEMA OU
(IDNO = IDNO,
IDTYPE = IDTYPE);
CREATE EntityGroup ORG_KPKAMR_CFM38
EntityParent  ORG_UNIT.OU_ORG_UNIT
EntityChild ORG_UNIT.ORGKAMR
SCHEMA OU
(CIFNO = CFRCIF);
CREATE EntityGroup ORG_WDPYMT_DDM
EntityChild ORG_UNIT.ORGPYMT
EntityParent  ORG_UNIT.OU_ORG_UNIT
SCHEMA OU
(BANKNO = DMBNO,
ACTYPE = WDMBTY);

上面的例子有3个entityGroup。通常,一个文件中最多可以有100个这样的实体。实体组是父表和子表之间的关系。但关系可以有多列。列在"="是父表列,并且在"="符号是子表列。我需要将它映射到一个对象,我已经创建了如下的类-

package com.test.common;
import java.util.ArrayList;
public class Realationship {
String parentTable;
String childTable;
String creator;
//parentcol=ChildCOL
ArrayList<RelationshipColumns> cols;
String EntityGroup;
public String getParentTable() {
return parentTable;
}
public void setParentTable(String parentTable) {
this.parentTable = parentTable;
}
public String getChildTable() {
return childTable;
}
public void setChildTable(String childTable) {
this.childTable = childTable;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public ArrayList<RelationshipColumns> getCols() {
return cols;
}
public void setCols(ArrayList<RelationshipColumns> cols) {
this.cols = cols;
}
public String getEntityGroup() {
return EntityGroup;
}
public void setEntityGroup(String EntityGroup) {
this.EntityGroup = EntityGroup;
}

}

package com.test.common;
public class RelationshipColumns {
String parentColumn;
String childColumn;
}

我使用的是java版本8。你能帮我如何将所有关系添加到类对象中吗?这里可以有单个或多个关系列。感谢您的支持。

下面的代码只处理问题中的示例数据。

  • 我假设CREATE EntityGroup行不包含任何前导空格
  • 我假设CCD_ 2和CCD_
  • 我假设每个父子行都包含一个等号
  • 我假设父列名和子列名只包含大写字母字符
  • 希望我的所有其他假设都能从下面的代码中清晰明了

代码后面的注释。

Relationship

package com.test.common;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Relationship { // Fixed typo in class name: Realationship
private static final Pattern COLUMN = Pattern.compile("[A-Z]+");
private String parentTable;
private String childTable;
private String creator;
private List<RelationshipColumns> cols; // Always code to the interface.
private String entityGroup;
public Relationship() {
cols = new ArrayList<>();
}
public String getParentTable() {
return parentTable;
}
public void setParentTable(String parentTable) {
this.parentTable = parentTable;
}
public String getChildTable() {
return childTable;
}
public void setChildTable(String childTable) {
this.childTable = childTable;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public List<RelationshipColumns> getCols() {
return cols;
}
public void setCols(ArrayList<RelationshipColumns> cols) {
this.cols = cols;
}
public String getEntityGroup() {
return entityGroup;
}
public void setEntityGroup(String entityGroup) {
this.entityGroup = entityGroup;
}
public String toString() {
return String.format("%s|%s|%s|%s|%s", entityGroup, parentTable, childTable, creator, cols);
}
public static void main(String[] args) {
List<Relationship> relationships = new ArrayList<>();
String line;
try (FileReader fr = new FileReader("entities.txt");
BufferedReader br = new BufferedReader(fr)) {
Relationship relationship = null;
line = br.readLine();
while (line != null) {
if (line.startsWith("CREATE EntityGroup ")) {
relationship = new Relationship();
relationship.setEntityGroup(line.substring(19));
}
else if (line.startsWith("  EntityChild ")) {
relationship.setChildTable(line.substring(14));
}
else if (line.startsWith("  EntityParent  ")) {
relationship.setParentTable(line.substring(16));
}
else if (line.startsWith("  SCHEMA ")) {
relationship.setCreator(line.substring(9));
}
else if (line.contains(" = ")) {
RelationshipColumns rc = new RelationshipColumns();
Matcher matcher = COLUMN.matcher(line);
matcher.find();
rc.setParentColumn(matcher.group());
matcher.find();
rc.setChildColumn(matcher.group());
relationship.getCols().add(rc);
}
if (line.endsWith(";")) {
relationships.add(relationship);
relationship = null;
}
line = br.readLine();
}
relationships.forEach(System.out::println);
}
catch (IOException x) {
x.printStackTrace();
}
}
}

RelationshipColumns

package com.test.common;
public class RelationshipColumns {
private String parentColumn;
private String childColumn;
public String getParentColumn() {
return parentColumn;
}
public void setParentColumn(String parentColumn) {
this.parentColumn = parentColumn;
}
public String getChildColumn() {
return childColumn;
}
public void setChildColumn(String childColumn) {
this.childColumn = childColumn;
}
public String toString() {
return String.format("%s=%s", parentColumn, childColumn);
}
}
  • 我向类Relationship添加了一个main方法,仅用于测试目的
  • 我假设您想要从文本文件的内容创建一个Relationship实例的列表
  • 两个类中的toString()方法纯粹用于调试目的
  • 我创建了一个名为entitys.txt的文本文件,其中包含您问题的示例数据

运行上述代码会打印以下内容:

ORG_KPKALX_CFM|ORG_UNIT.OU_ORG_UNIT|ORG_UNIT.ORGKALX|OU|[IDNO=IDNO, IDTYPE=IDTYPE]
ORG_KPKAMR_CFM38|ORG_UNIT.OU_ORG_UNIT|ORG_UNIT.ORGKAMR|OU|[CIFNO=CFRCIF]
ORG_WDPYMT_DDM|ORG_UNIT.OU_ORG_UNIT|ORG_UNIT.ORGPYMT|OU|[BANKNO=DMBNO, ACTYPE=WDMBTY]

最新更新