forked from RestComm/jss7
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfix_javolution_xml_remaining.py
More file actions
114 lines (99 loc) · 4.48 KB
/
fix_javolution_xml_remaining.py
File metadata and controls
114 lines (99 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""
Fix remaining Javolution XML blocks where variables are reused (no type declarations).
"""
import os
import re
BASE_DIR = r"C:\Users\Windows\Desktop\ethiopia-working-dir\jSS7\cap\cap-impl\src\test\java"
# Find all test files that still contain XMLObjectWriter or XMLObjectReader
test_files = []
for root, dirs, files in os.walk(BASE_DIR):
for f in files:
if f.endswith("Test.java"):
path = os.path.join(root, f)
with open(path, "r", encoding="utf-8") as fh:
content = fh.read()
if "XMLObjectWriter" in content or "XMLObjectReader" in content:
test_files.append(path)
print(f"Found {len(test_files)} files with remaining Javolution blocks")
# Pattern for reused variables (no type declarations)
# This handles cases like:
# baos = new ByteArrayOutputStream();
# writer = XMLObjectWriter.newInstance(baos);
# writer.setIndentation("\t");
# writer.write(original, "name", Class.class);
# writer.close();
# rawData = baos.toByteArray();
# serializedEvent = new String(rawData);
# System.out.println(serializedEvent);
# bais = new ByteArrayInputStream(rawData);
# reader = XMLObjectReader.newInstance(bais);
# Class copy = reader.read("name", Class.class);
REUSE_PATTERN = re.compile(
r"(?P<indent>\s*)baos = new ByteArrayOutputStream\(\);\s*\n"
r"\s*writer = XMLObjectWriter\.newInstance\(baos\);\s*\n"
r"(?:\s*//[^\n]*\n|\s*writer\.setIndentation\([^\n]*\n)*"
r"\s*writer\.write\((?P<orig>\w+), \"(?P<tag>[^\"]+)\", (?P<class>[\w\.]+\.class)\);\s*\n"
r"\s*writer\.close\(\);\s*\n"
r"(?:[^\n]*\n)*?"
r"\s*rawData = baos\.toByteArray\(\);\s*\n"
r"\s*serializedEvent = new String\(rawData\);\s*\n"
r"(?:[^\n]*\n)*?"
r"\s*System\.out\.println\(serializedEvent\);\s*\n"
r"(?:[^\n]*\n)*?"
r"\s*bais = new ByteArrayInputStream\(rawData\);\s*\n"
r"\s*reader = XMLObjectReader\.newInstance\(bais\);\s*\n"
r"\s*(?P<class2>[\w\.]+) (?P<copy>\w+) = reader\.read\(\"(?P<tag2>[^\"]+)\", (?P<class3>[\w\.]+\.class)\);",
re.MULTILINE,
)
# Even more lenient pattern that allows extra lines between key statements
REUSE_PATTERN2 = re.compile(
r"(?P<indent>\s*)baos = new ByteArrayOutputStream\(\);[^\n]*\n"
r"\s*writer = XMLObjectWriter\.newInstance\(baos\);[^\n]*\n"
r"(?:\s*//[^\n]*\n|\s*writer\.setIndentation\([^\n]*\n|\s*writer\.write\([^\n]*\n|\s*writer\.close\(\);[^\n]*\n)*"
r"(?:\s*rawData = baos\.toByteArray\(\);[^\n]*\n|\s*serializedEvent = new String\(rawData\);[^\n]*\n|\s*System\.out\.println\(serializedEvent\);[^\n]*\n)*"
r"\s*bais = new ByteArrayInputStream\(rawData\);[^\n]*\n"
r"\s*reader = XMLObjectReader\.newInstance\(bais\);[^\n]*\n"
r"\s*(?P<class2>[\w\.]+) (?P<copy>\w+) = reader\.read\(\"(?P<tag2>[^\"]+)\", (?P<class3>[\w\.]+\.class)\);",
re.MULTILINE,
)
def replace_reuse_block(content, pattern):
changed = True
while changed:
changed = False
m = pattern.search(content)
if m:
orig_match = None
class_match = None
# Try to extract orig and class from the writer.write line in the matched text
matched_text = m.group(0)
write_m = re.search(r'writer\.write\((\w+),\s*"([^"]+)",\s*([\w\.]+\.class)\)', matched_text)
if write_m:
orig_name = write_m.group(1)
cls = write_m.group(3)
else:
orig_name = "original"
cls = m.group("class3")
copy_name = m.group("copy")
cls_name = cls.replace('.class', '')
replacement = f"""XmlMapper xmlMapper = new XmlMapper();
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
String serializedEvent = xmlMapper.writeValueAsString({orig_name});
System.out.println(serializedEvent);
{cls_name} {copy_name} = xmlMapper.readValue(serializedEvent, {cls});"""
content = content[:m.start()] + replacement + content[m.end():]
changed = True
return content
changed_files = 0
for path in test_files:
with open(path, "r", encoding="utf-8") as fh:
content = fh.read()
original = content
content = replace_reuse_block(content, REUSE_PATTERN)
content = replace_reuse_block(content, REUSE_PATTERN2)
if content != original:
with open(path, "w", encoding="utf-8") as fh:
fh.write(content)
changed_files += 1
print(f"Fixed: {path}")
print(f"\nTotal files fixed: {changed_files}")