in src/main/java/org/apache/maven/doxia/DefaultConverter.java [521:629]
private File convert(
File inputFile,
String inputEncoding,
DoxiaFormat parserFormat,
OutputFileWrapper output,
File relativeOutputDirectory)
throws ConverterException, UnsupportedFormatException {
File outputDirectoryOrFile = relativeOutputDirectory != null
? new File(output.getFile(), relativeOutputDirectory.getPath())
: output.getFile();
LOGGER.debug(
"Parsing file from '{}' with the encoding '{}' to '{}' with the encoding '{}'",
inputFile.getAbsolutePath(),
inputEncoding,
outputDirectoryOrFile.getAbsolutePath(),
output.getEncoding());
if (InputFileWrapper.AUTO_ENCODING.equals(inputEncoding)) {
inputEncoding = autoDetectEncoding(inputFile);
LOGGER.debug("Auto detected encoding: '{}'", inputEncoding);
}
Parser parser;
try {
parser = parserFormat.getParser(plexus, MacroFormatter.forFormat(output.getFormat()));
} catch (ComponentLookupException e) {
throw new ConverterException("ComponentLookupException: " + e.getMessage(), e);
}
File outputFile;
if (outputDirectoryOrFile.isDirectory()
|| !SelectorUtils.match("**.*", output.getFile().getName())
|| relativeOutputDirectory != null) {
// assume it is a directory
outputDirectoryOrFile.mkdirs();
outputFile = new File(
outputDirectoryOrFile,
FileUtils.removeExtension(inputFile.getName()) + "."
+ output.getFormat().getExtension());
} else {
outputDirectoryOrFile.getParentFile().mkdirs();
outputFile = output.getFile();
}
Reader reader;
try {
if (inputEncoding != null) {
if (parser.getType() == Parser.XML_TYPE) {
reader = ReaderFactory.newXmlReader(inputFile);
} else {
reader = ReaderFactory.newReader(inputFile, inputEncoding);
}
} else {
reader = ReaderFactory.newPlatformReader(inputFile);
}
} catch (IOException e) {
throw new ConverterException("IOException: " + e.getMessage(), e);
}
SinkFactory sinkFactory;
try {
sinkFactory = output.getFormat().getSinkFactory(plexus);
} catch (ComponentLookupException e) {
throw new ConverterException("ComponentLookupException: " + e.getMessage(), e);
}
Sink sink;
try {
String outputEncoding;
if (StringUtils.isEmpty(output.getEncoding())
|| output.getEncoding().equals(OutputFileWrapper.AUTO_ENCODING)) {
outputEncoding = inputEncoding;
} else {
outputEncoding = output.getEncoding();
}
OutputStream out = new FileOutputStream(outputFile);
sink = sinkFactory.createSink(out, outputEncoding);
} catch (IOException e) {
throw new ConverterException("IOException: " + e.getMessage(), e);
}
LOGGER.debug("Sink used: {}", sink.getClass().getName());
parse(parser, reader, sink);
if (formatOutput && output.getFormat().isXml()) {
try (Reader r = ReaderFactory.newXmlReader(outputFile);
Writer w = WriterFactory.newXmlWriter(outputFile)) {
CharArrayWriter caw = new CharArrayWriter();
XmlUtil.prettyFormat(r, caw);
w.write(caw.toString());
} catch (IOException e) {
throw new ConverterException("IOException: " + e.getMessage(), e);
}
}
LOGGER.info(
"Successfully converted file \"{}\" to \"{}\"",
inputFile.getAbsolutePath(),
outputFile.getAbsolutePath());
try {
postProcessFile(inputFile, outputFile);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ConverterException("Error post processing files: " + e.getMessage(), e);
} catch (IOException e) {
throw new ConverterException("Error post processing files: " + e.getMessage(), e);
}
return outputFile;
}