我正在编写一个java程序,如果它包含OCCURS子句,我必须在其中展开COBOL副本。
OCCURS关键字旁边的数字定义了子行与OCCURS子句的重复。第一行定义级别。一个OCCURS可能有多个子元素。 假设输入字帖如下所示:
01 TEST-REC.
05 VAR1 PIC X(02).
05 VAR2 OCCURS 2.
10 VAR3 PIC X(3).
05 VAR6 PIC X(02).
然后输出应该是(忽略输入的第一行):
05 VAR1 PIC X(02).
10 VAR3 PIC X(3).
10 VAR3 PIC X(3).
05 VAR6 PIC X(02).
我已编写以下代码来处理此问题,并且工作正常。
public class Test{
private static String copyBookExpansaion = "";
private static BufferedReader br1 = null;
public static void parseLine() throws IOException{
String line;
while((line = br1.readLine()) != null){
if(line.indexOf(" OCCURS ") == -1){
copyBookExpansaion = copyBookExpansaion + line.trim() + "\n";
}
else{
String s[] = line.trim().replaceAll(" +"," ").split(" ");
int size = Integer.parseInt(s[3].replaceAll("[^0-9]", ""));
line = br1.readLine();
for(int i = 0; i < size;i++){
copyBookExpansaion = copyBookExpansaion + line.trim() + "\n";
}
}
}
return;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream copyBook= new FileInputStream("C:\\Users\\DadMadhR\\Desktop\\temp\\copybook\\test_Copybook.cpy");
br1 = new BufferedReader(new InputStreamReader(copyBook));
br1.readLine();
parseLine();
System.out.println(copyBookExpansaion);
}
}
我无法处理嵌套的OCCURS子句。假设我的输入如下所示:
01 TEST-REC.
05 VAR1 PIC X(02).
05 VAR2 OCCURS 2.
10 VAR3 PIC X(3).
10 VAR4 OCCURS 2.
15 VAR5 PIC X(2).
05 VAR6 PIC X(02).
然后输出应如下所示:
05 VAR1 PIC X(02).
10 VAR3 PIC X(3).
15 VAR5 PIC X(2).
15 VAR5 PIC X(2).
10 VAR3 PIC X(3).
15 VAR5 PIC X(2).
15 VAR5 PIC X(2).
05 VAR6 PIC X(02).
嵌套OCCURS的数量没有限制。我没有办法处理嵌套的OCCURS。
有人可以建议一种方法来处理这种情况吗?
答案 0 :(得分:1)
试试这个
步骤1.制作树形结构。 每一行都有一个父母。 父级别(01,05,...)小于线路级别。 所以结构是
01 TEST-REC.
|
+--05 VAR1 PIC X(02).
|
+--05 VAR2 OCCURS 2.
| |
| +--10 VAR3 PIC X(3).
| |
| +--10 VAR4 OCCURS 2.
| |
| +--15 VAR5 PIC X(2).
|
+--05 VAR6 PIC X(02).
步骤2.将结构写入流。
(a)如果该行包含PIC
,则写入自行。
(b)递归地写下每个孩子。
(如果该行包含OCCURS N
,则重复N次。)
public class Test {
static class Node {
final int level;
final String line;
final List<Node> children = new ArrayList<>();
Node(int level, String line) {
this.level = level;
this.line = line;
}
void write(PrintStream out) {
int n = 1;
if (line.contains("OCCURS"))
n = Integer.parseInt(line.replaceAll("^.* |[^\\d]*$", ""));
if (line.contains("PIC"))
out.println(line);
for (int i = 0; i < n; ++i)
for (Node child : children)
child.write(out);
}
}
static Node parse(String inputFile) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(inputFile)))) {
List<Node> lines = new ArrayList<>();
String line;
int no = 0;
while ((line = reader.readLine()) != null) {
++no;
line = line.trim();
int level = Integer.parseInt(line.replaceFirst("\\s.*", ""));
Node child = new Node(level, line);
if (lines.size() > 0) {
boolean found = false;
for (int i = lines.size() - 1; i >= 0; --i) {
Node parent = lines.get(i);
if (parent.level < level) {
parent.children.add(child);
found = true;
break;
}
}
if (!found)
throw new RuntimeException(
"parent not found for line " + no + " : " + line);
}
lines.add(child);
}
return lines.get(0);
}
}
public static void main(String[] args) throws IOException {
// Step 1
Node top = parse("C:\\Users\\DadMadhR\\Desktop\\temp\\copybook\\test_Copybook.cpy");
// Step 2
top.write(System.out);
}
}