| 1 |
package com.thaiopensource.relaxng.translate; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.SchemaCollection; |
| 4 |
import com.thaiopensource.relaxng.input.InputFailedException; |
| 5 |
import com.thaiopensource.relaxng.input.InputFormat; |
| 6 |
import com.thaiopensource.relaxng.input.MultiInputFormat; |
| 7 |
import com.thaiopensource.relaxng.output.LocalOutputDirectory; |
| 8 |
import com.thaiopensource.relaxng.output.OutputDirectory; |
| 9 |
import com.thaiopensource.relaxng.output.OutputFailedException; |
| 10 |
import com.thaiopensource.relaxng.output.OutputFormat; |
| 11 |
import com.thaiopensource.relaxng.translate.util.InvalidParamsException; |
| 12 |
import com.thaiopensource.resolver.Resolver; |
| 13 |
import com.thaiopensource.resolver.catalog.CatalogResolver; |
| 14 |
import com.thaiopensource.util.Localizer; |
| 15 |
import com.thaiopensource.util.OptionParser; |
| 16 |
import com.thaiopensource.util.UriOrFile; |
| 17 |
import com.thaiopensource.util.Version; |
| 18 |
import com.thaiopensource.xml.sax.ErrorHandlerImpl; |
| 19 |
import org.xml.sax.SAXException; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.io.IOException; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
public class Driver { |
| 27 |
static private final Localizer localizer = new Localizer(Driver.class); |
| 28 |
private String inputType; |
| 29 |
private String outputType; |
| 30 |
private final ErrorHandlerImpl eh; |
| 31 |
private static final String DEFAULT_OUTPUT_ENCODING = "UTF-8"; |
| 32 |
private static final int DEFAULT_LINE_LENGTH = 72; |
| 33 |
private static final int DEFAULT_INDENT = 2; |
| 34 |
|
| 35 |
public Driver() { |
| 36 |
this(new ErrorHandlerImpl()); |
| 37 |
} |
| 38 |
|
| 39 |
public Driver(ErrorHandlerImpl eh) { |
| 40 |
this.eh = eh; |
| 41 |
} |
| 42 |
|
| 43 |
static public void main(String[] args) { |
| 44 |
System.exit(new Driver().run(args)); |
| 45 |
} |
| 46 |
|
| 47 |
public int run(String[] args) { |
| 48 |
List<String> inputParams = new ArrayList<String>(); |
| 49 |
List<String> outputParams = new ArrayList<String>(); |
| 50 |
List<String> catalogUris = new ArrayList<String>(); |
| 51 |
try { |
| 52 |
OptionParser op = new OptionParser("C:I:O:i:o:", args); |
| 53 |
try { |
| 54 |
while (op.moveToNextOption()) { |
| 55 |
switch (op.getOptionChar()) { |
| 56 |
case 'C': |
| 57 |
catalogUris.add(UriOrFile.toUri(op.getOptionArg())); |
| 58 |
break; |
| 59 |
case 'I': |
| 60 |
inputType = op.getOptionArg(); |
| 61 |
break; |
| 62 |
case 'O': |
| 63 |
outputType = op.getOptionArg(); |
| 64 |
break; |
| 65 |
case 'i': |
| 66 |
inputParams.add(op.getOptionArg()); |
| 67 |
break; |
| 68 |
case 'o': |
| 69 |
outputParams.add(op.getOptionArg()); |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
catch (OptionParser.InvalidOptionException e) { |
| 75 |
error(localizer.message("invalid_option", op.getOptionCharString())); |
| 76 |
return 2; |
| 77 |
} |
| 78 |
catch (OptionParser.MissingArgumentException e) { |
| 79 |
error(localizer.message("option_missing_argument", op.getOptionCharString())); |
| 80 |
return 2; |
| 81 |
} |
| 82 |
args = op.getRemainingArgs(); |
| 83 |
if (args.length < 2) { |
| 84 |
error(localizer.message("too_few_arguments")); |
| 85 |
eh.print(localizer.message("usage", Version.getVersion(Driver.class))); |
| 86 |
return 2; |
| 87 |
} |
| 88 |
if (inputType == null) { |
| 89 |
inputType = extension(args[0]); |
| 90 |
if (inputType.length() > 0) |
| 91 |
inputType = inputType.substring(1); |
| 92 |
} |
| 93 |
final InputFormat inputFormat = Formats.createInputFormat(inputType); |
| 94 |
if (inputFormat == null) { |
| 95 |
error(localizer.message("unrecognized_input_type", inputType)); |
| 96 |
return 2; |
| 97 |
} |
| 98 |
String ext = extension(args[args.length - 1]); |
| 99 |
if (outputType == null) { |
| 100 |
outputType = ext; |
| 101 |
if (outputType.length() > 0) |
| 102 |
outputType = outputType.substring(1); |
| 103 |
} |
| 104 |
final OutputFormat outputFormat = Formats.createOutputFormat(outputType); |
| 105 |
if (outputFormat == null) { |
| 106 |
error(localizer.message("unrecognized_output_type", outputType)); |
| 107 |
return 2; |
| 108 |
} |
| 109 |
Resolver resolver; |
| 110 |
if (catalogUris.isEmpty()) |
| 111 |
resolver = null; |
| 112 |
else { |
| 113 |
try { |
| 114 |
resolver = new CatalogResolver(catalogUris); |
| 115 |
} |
| 116 |
catch (LinkageError e) { |
| 117 |
eh.print(localizer.message("resolver_not_found")); |
| 118 |
return 2; |
| 119 |
} |
| 120 |
} |
| 121 |
String[] inputParamArray = inputParams.toArray(new String[inputParams.size()]); |
| 122 |
outputType = outputType.toLowerCase(); |
| 123 |
SchemaCollection sc; |
| 124 |
if (args.length > 2) { |
| 125 |
if (!(inputFormat instanceof MultiInputFormat)) { |
| 126 |
error(localizer.message("too_many_arguments")); |
| 127 |
return 2; |
| 128 |
} |
| 129 |
String[] uris = new String[args.length - 1]; |
| 130 |
for (int i = 0; i < uris.length; i++) |
| 131 |
uris[i] = UriOrFile.toUri(args[i]); |
| 132 |
sc = ((MultiInputFormat)inputFormat).load(uris, inputParamArray, outputType, eh, resolver); |
| 133 |
} |
| 134 |
else |
| 135 |
sc = inputFormat.load(UriOrFile.toUri(args[0]), inputParamArray, outputType, eh, resolver); |
| 136 |
if (ext.length() == 0) |
| 137 |
ext = outputType; |
| 138 |
OutputDirectory od = new LocalOutputDirectory(sc.getMainUri(), |
| 139 |
new File(args[args.length - 1]), |
| 140 |
ext, |
| 141 |
DEFAULT_OUTPUT_ENCODING, |
| 142 |
DEFAULT_LINE_LENGTH, |
| 143 |
DEFAULT_INDENT); |
| 144 |
outputFormat.output(sc, od, outputParams.toArray(new String[outputParams.size()]), inputType.toLowerCase(), eh); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
catch (OutputFailedException e) { |
| 148 |
} |
| 149 |
catch (InputFailedException e) { |
| 150 |
} |
| 151 |
catch (InvalidParamsException e) { |
| 152 |
} |
| 153 |
catch (IOException e) { |
| 154 |
eh.printException(e); |
| 155 |
} |
| 156 |
catch (SAXException e) { |
| 157 |
eh.printException(e); |
| 158 |
} |
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
private void error(String message) { |
| 163 |
eh.printException(new SAXException(message)); |
| 164 |
} |
| 165 |
|
| 166 |
static private String extension(String s) { |
| 167 |
int dot = s.lastIndexOf("."); |
| 168 |
if (dot < 0) |
| 169 |
return ""; |
| 170 |
return s.substring(dot); |
| 171 |
} |
| 172 |
} |