Set<File> filesToIndex = new TreeSet<File>();
if (files != null && !files.isEmpty()) {
resolveDirectories(files, filesToIndex);
}
Indent indent;
PrintWriter pw = null;
try {
String prettySetting = config.get(ResourceIndexer.PRETTY);
String compressedSetting = config.get(ResourceIndexer.COMPRESSED);
/**
* <pre>
* pretty compressed out-pretty out-compressed
* null null Indent.NONE true*
* null false Indent.NONE false
* null true Indent.NONE true
* false null Indent.PRETTY false*
* false false Indent.NONE false
* false true Indent.NONE true
* true null Indent.PRETTY false*
* true false Indent.PRETTY false
* true true Indent.PRETTY true
*
* * = original behaviour, before compressed was introduced
* </pre>
*/
indent = (prettySetting == null || (!Boolean.parseBoolean(prettySetting) && compressedSetting != null)) ? Indent.NONE : Indent.PRETTY;
boolean compressed = (prettySetting == null && compressedSetting == null) || Boolean.parseBoolean(compressedSetting);
if (!compressed) {
pw = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
} else {
pw = new PrintWriter(new GZIPOutputStream(out, Deflater.BEST_COMPRESSION));
}
pw.print(Schema.XML_PROCESSING_INSTRUCTION);
Tag repoTag = new Tag(Schema.ELEM_REPOSITORY);
String repoName = config.get(REPOSITORY_NAME);
if (repoName == null)
repoName = REPOSITORYNAME_DEFAULT;
repoTag.addAttribute(Schema.ATTR_NAME, repoName);
String increment = config.get(REPOSITORY_INCREMENT_OVERRIDE);
if (increment == null)
increment = Long.toString(System.currentTimeMillis());
repoTag.addAttribute(Schema.ATTR_INCREMENT, increment);
repoTag.addAttribute(Schema.ATTR_XML_NAMESPACE, Schema.NAMESPACE);
repoTag.printOpen(indent, pw, false);
for (File file : filesToIndex) {
try {
Tag resourceTag = generateResource(file, config);
resourceTag.print(indent.next(), pw);
} catch (Exception e) {
log(LogService.LOG_WARNING, MessageFormat.format("Could not index {0}, skipped ({1}).", file, e.getMessage()), null);
}
}
repoTag.printClose(indent, pw);