This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxem
executable file
·215 lines (159 loc) · 7.76 KB
/
xem
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/sh
exec scala -J-Xmx2g -savecompiled "$0" "$@"
!#
import java.io.{File, PrintWriter}
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import scala.xml.{Elem, Node, XML}
object XmlElementsMasker {
val usage =
"""xem - d:swarm XML elements masker
xem is a Scala program wrapped in a shell script. It takes the source files directory, the record tag, the mask elements and the target directory as input and masks the sub trees of the wanted XML elements in CDATA tags.
Usage: xem
--source-files-directory [ABSOLUTE PATH TO SOURCE FILES DIRECTORY]
--record-tag [XML RECORD TAG]
--mask-elements [a comma-separated list of XML element tags, whose content and sub elements should be masked with CDATA]
--target-files-directory [ABSOLUTE PATH TO TARGET FILES DIRECTORY]
--content-only utilise only the content of the sub elements (i.e. without further XML elements tags inside)
--exclude-empty-elements exclude empty XML element tags, which should be masked with CDATA
--help print this help
"""
type OptionMap = Map[Symbol, String]
private def nextOption(map: OptionMap, list: List[String]): OptionMap = {
list match {
case Nil => map
case "--source-files-directory" :: value :: tail =>
nextOption(map ++ Map('sourcefilesdirectory -> value.toString), tail)
case "--record-tag" :: value :: tail =>
nextOption(map ++ Map('recordtag -> value.toString), tail)
case "--mask-elements" :: value :: tail =>
nextOption(map ++ Map('maskelements -> value.toString), tail)
case "--target-files-directory" :: value :: tail =>
nextOption(map ++ Map('targetfilesdirectory -> value.toString), tail)
case "--content-only" :: tail =>
nextOption(map ++ Map('contentonly -> "true"), tail)
case "--exclude-empty-elements" :: tail =>
nextOption(map ++ Map('excludeemptyelements -> "true"), tail)
case "--help" :: tail =>
println(usage)
sys.exit(1)
case option :: tail =>
println("Unknown option " + option)
map
}
}
private def getListOfFiles(dir: String): List[(String, String)] = {
val d = new File(dir)
if (!d.exists || !d.isDirectory)
List[(String, String)]()
d.listFiles.filter(_.isFile).toList.map(file => (file.getName, file.getAbsolutePath))
}
private def getMaskElements(maskElementsString: String): List[String] = maskElementsString.split(",").toList
private def printStartingTag(nodeLabel: String): String = "<" + nodeLabel + ">"
private def printClosingTag(nodeLabel: String): String = "</" + nodeLabel + ">"
private def printClosingTag(node: Node): String = printClosingTag(node.label)
private def maskElementsInRecord(publisher: Node, maskElements: List[String], contentOnly: Boolean, excludeEmptyElements: Boolean): String = {
def printStartingTag(element: Node, closingTag: String): String = {
val nodeAsString = element.mkString
if (!nodeAsString.endsWith(closingTag)) {
nodeAsString
}
else {
nodeAsString.substring(0, nodeAsString.indexOf(">") + 1)
}
}
def getInnerContent(e: Elem, startingTag: String, closingTag: String, contentOnly: Boolean): String = {
if (contentOnly) {
e.text
} else {
e.mkString.replaceFirst(startingTag, "").split(closingTag).toList.head
}
}
def maskElementsInNodes(node: Node): String = {
val sb = new StringBuilder
val closingTag = printClosingTag(node)
val startingTag = printStartingTag(node, closingTag)
val noClosingTag = startingTag.endsWith("/>")
sb.append(startingTag)
if (!noClosingTag) {
node.child.foreach {
case e: Elem if maskElements.contains(e.label) =>
val closingTag = printClosingTag(e)
val startingTag = printStartingTag(e, closingTag)
val noClosingTag = startingTag.endsWith("/>")
val innerContent = getInnerContent(e, startingTag, closingTag, contentOnly)
if (!noClosingTag) {
sb.append(startingTag)
.append("<![CDATA[")
.append(innerContent)
.append("]]>")
.append(closingTag)
} else if (!excludeEmptyElements) {
sb.append(startingTag)
}
case e: Elem => sb.append(maskElementsInNodes(e))
case b => sb.append(b.mkString)
}
sb.append(closingTag)
}
sb.toString
}
maskElementsInNodes(publisher)
}
private def processSourceFiles(sourceFiles: List[(String, String)], recordTag: String, maskElements: List[String], contentOnly: Boolean, excludeEmptyElements: Boolean, targetFilesDirectory: String) = {
def writeResultToFile(fileName: String, content: String) = {
val pw = new PrintWriter(new File(fileName), StandardCharsets.UTF_8.toString)
pw.write(content)
pw.close()
}
def processSourceFile(sourceFile: (String, String), maskElements: List[String]) = {
val sb = new StringBuilder
val sourceXML = XML.loadFile(sourceFile._2)
val publishers = sourceXML \ recordTag
println(publishers.length + " '" + recordTag + "' records in file '" + sourceFile._2 + "'")
val recordsTag = recordTag + "s"
sb.append(printStartingTag(recordsTag))
publishers.foreach(publisher => sb.append(maskElementsInRecord(publisher, maskElements, contentOnly, excludeEmptyElements)).append("\n"))
sb.append(printClosingTag(recordsTag))
val outputFilePath = Paths.get(targetFilesDirectory, "enhanced_" + sourceFile._1)
val outFilePathString = outputFilePath.toString
val enhancedXML = sb.toString
println("wrote enhanced version of '" + sourceFile._2 + "' in '" + outFilePathString + "'")
writeResultToFile(outFilePathString, enhancedXML)
}
println("process '" + sourceFiles.length + "' source files")
sourceFiles.foreach(sourceFile => processSourceFile(sourceFile, maskElements))
}
def main(args: Array[String]) {
if (args.length == 0) {
println(usage)
sys.exit(1)
}
val arglist = args.toList
val options = nextOption(Map(), arglist)
val sourceFilesDirectoryOption = options.get('sourcefilesdirectory)
val recordTagOption = options.get('recordtag)
val maskElementsStringOption = options.get('maskelements)
val targetFilesDirectoryOption = options.get('targetfilesdirectory)
val contentOnlyOption = options.get('contentonly)
val excludeEmptyElementsOption = options.get('excludeemptyelements)
if (sourceFilesDirectoryOption.isEmpty || recordTagOption.isEmpty || maskElementsStringOption.isEmpty) {
println("Please define --source-files-directory, --record-tag and --mask-elements at least parameter")
println(usage)
sys.exit(1)
}
val sourceFilesDirectory = sourceFilesDirectoryOption.get
val sourceFiles = getListOfFiles(sourceFilesDirectory)
val recordTag = recordTagOption.get
val maskElements = getMaskElements(maskElementsStringOption.get)
val targetFilesDirectory = targetFilesDirectoryOption.orElse(sourceFilesDirectoryOption).get
val contentOnly = contentOnlyOption.orElse(Option("false")).get.toBoolean
val excludeEmptyElements = excludeEmptyElementsOption.orElse(Option("false")).get.toBoolean
println("source files directory = '" + sourceFilesDirectory + "'")
println("record tag = '" + recordTag + "'")
println("mask elements = '" + maskElements.mkString(", ") + "'")
println("target files directory = '" + targetFilesDirectory + "'")
processSourceFiles(sourceFiles, recordTag, maskElements, contentOnly, excludeEmptyElements, targetFilesDirectory)
}
}
XmlElementsMasker.main(args)