Skip to content

Instantly share code, notes, and snippets.

@keeganwitt
Created May 29, 2012 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeganwitt/2830035 to your computer and use it in GitHub Desktop.
Save keeganwitt/2830035 to your computer and use it in GitHub Desktop.
A Groovy script to generate Java source files to convert between ONIX short and reference tags
/**
* @author Keegan Witt
* @version 1.0
*/
class GenerateJavaOnixTagConverter {
private static final String SCHEMA_2_FILENAME = "ONIX_BookProduct_Release2.1_reference.xsd"
private static final String SCHEMA_3_FILENAME = "ONIX_BookProduct_3.0_reference.xsd"
private static final String ONIX_2_CLASS = "Onix2TagConverter"
private static final String ONIX_3_CLASS = "Onix3TagConverter"
public static void main(String[] args) {
if (argsValid(args)) {
def runner = new GenerateJavaOnixTagConverter()
runner.run(ONIXVersions.parse(args[0]), args[1], args[2])
} else {
usage()
System.exit(1)
}
}
private static boolean argsValid(String[] args) {
if (args.length == 3) {
ONIXVersions onixVersion = ONIXVersions.parse(args[0])
return ((onixVersion == ONIXVersions.ONIX_2_1 ? new File(args[1], SCHEMA_2_FILENAME).isFile() : new File(args[1], SCHEMA_3_FILENAME).isFile()) && new File(args[2]).isDirectory())
} else {
return false
}
}
private static void usage() {
println "Usage: GenerateJavaOnixTagConverter <2.1|3.0> <schemaDirectory> <outputDirectory>"
}
private void run(ONIXVersions onixVersion, String schemaDirectory, String outputDirectory) {
def refs = []
def shorts = []
def found = ""
String outputClass = (onixVersion == ONIXVersions.ONIX_2_1 ? ONIX_2_CLASS : ONIX_3_CLASS)
String outputFilename = "${outputClass}.java"
def out = new FileWriter(new File(outputDirectory, outputFilename))
// build lists of ref names and short names
if (onixVersion == ONIXVersions.ONIX_2_1) {
def inFile = new File(schemaDirectory, SCHEMA_2_FILENAME)
inFile.eachLine() { line ->
found = line.find(/<xs:attribute name="refname" type="xs:\w*" fixed="\w*"\/>/)
if (found != null && !found.isEmpty()) {
refs.add(found.split(/fixed="/)[1].find(/\w*/))
}
found = line.find(/<xs:attribute name="shortname" type="xs:\w*" fixed="\w*"\/>/)
if (found != null && !found.isEmpty()) {
shorts.add(found.split(/fixed="/)[1].find(/\w*/))
}
}
} else {
def inFile = new File(schemaDirectory, SCHEMA_3_FILENAME)
inFile.text.findAll(/<xs:attribute name="shortname">\s*<xs:simpleType>\s*<xs:restriction base="xs:token">\s*<xs:enumeration value=".+/).each() { result ->
shorts.add(result.replaceAll(/<xs:attribute name="shortname">\s*<xs:simpleType>\s*<xs:restriction base="xs:token">\s*<xs:enumeration value="/, "").replaceAll("\"/>", ""))
}
inFile.text.findAll(/<xs:attribute name="refname">\s*<xs:simpleType>\s*<xs:restriction base="xs:token">\s*<xs:enumeration value=".+/).each() { result ->
refs.add(result.replaceAll(/<xs:attribute name="refname">\s*<xs:simpleType>\s*<xs:restriction base="xs:token">\s*<xs:enumeration value="/, "").replaceAll("\"/>", ""))
}
}
// write results
out.write("/**\n")
out.write(" * @author Keegan Witt\n")
out.write(" * @version 1.0\n")
out.write(" */\n")
out.write("public class ${outputClass} {\n\n")
out.write(" private ${outputClass}() {}\n\n")
out.write(" public static String short2Ref(final String inputRecord) {\n")
out.write(" String result = inputRecord;\n")
for (int i = 0; i < refs.size(); i++) {
out.write(" result = result.replaceAll(\"<${shorts[i]}>\", \"<${refs[i]}>\");\n")
out.write(" result = result.replaceAll(\"<${shorts[i]} \", \"<${refs[i]} \");\n")
out.write(" result = result.replaceAll(\"</${shorts[i]}>\", \"</${refs[i]}>\");\n")
out.write(" result = result.replaceAll(\"<${shorts[i]}/>\", \"<${refs[i]}/>\");\n")
}
out.write(" return result;\n")
out.write(" }\n\n")
out.write(" public static String ref2Short(final String inputRecord) {\n")
out.write(" String result = inputRecord;\n")
for (int i = 0; i < refs.size(); i++) {
out.write(" result = result.replaceAll(\"<${refs[i]}>\", \"<${shorts[i]}>\");\n")
out.write(" result = result.replaceAll(\"<${refs[i]} \", \"<${shorts[i]} \");\n")
out.write(" result = result.replaceAll(\"</${refs[i]}>\", \"</${shorts[i]}>\");\n")
out.write(" result = result.replaceAll(\"<${refs[i]}/>\", \"<${shorts[i]}/>\");\n")
}
out.write(" return result;\n")
out.write(" }\n\n")
out.write("}\n")
out.flush()
out.close()
}
public enum ONIXVersions {
ONIX_2_1 {
public String toString() {
return "2.1"
}
},
ONIX_3_0 {
public String toString() {
return "3.0"
}
};
public static ONIXVersions parse(String str) {
if ("2.1".equals(str)) {
return ONIX_2_1
} else if ("3.0".equals(str)) {
return ONIX_3_0
} else {
throw new IllegalArgumentException("No such ONIX version (" + str + ").")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment