Search in sources :

Example 1 with Indent

use of org.osgi.service.indexer.impl.util.Indent in project bnd by bndtools.

the class RepoIndex method index.

/*
	 * See ResourceIndexer interface
	 */
public void index(Set<File> files, OutputStream out, Map<String, String> config) throws Exception {
    if (config == null)
        config = new HashMap<String, String>(0);
    Set<File> filesToIndex = new TreeSet<File>();
    if (files != null && !files.isEmpty()) {
        resolveDirectories(files, filesToIndex);
    }
    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 indent = (prettySetting == null || (!Boolean.parseBoolean(prettySetting) && compressedSetting != null)) ? Indent.NONE : Indent.PRETTY;
    boolean compressed = (prettySetting == null && compressedSetting == null) || Boolean.parseBoolean(compressedSetting);
    try (PrintWriter pw = compressed ? new PrintWriter(new GZIPOutputStream(out, Deflater.BEST_COMPRESSION)) : new PrintWriter(new OutputStreamWriter(out, UTF_8))) {
        pw.print(Schema.XML_PROCESSING_INSTRUCTION);
        String stylesheet = config.get(STYLESHEET);
        if (stylesheet != null) {
            indent.print(pw);
            pw.printf(Schema.XML_STYLESHEET_INSTRUCTION, stylesheet, "text/xsl");
        }
        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), null);
            }
        }
        repoTag.printClose(indent, pw);
    }
}
Also used : Indent(org.osgi.service.indexer.impl.util.Indent) HashMap(java.util.HashMap) GZIPOutputStream(java.util.zip.GZIPOutputStream) TreeSet(java.util.TreeSet) OutputStreamWriter(java.io.OutputStreamWriter) Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) PrintWriter(java.io.PrintWriter)

Aggregations

File (java.io.File)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 Indent (org.osgi.service.indexer.impl.util.Indent)1 Tag (org.osgi.service.indexer.impl.util.Tag)1