ClientLib.java 3.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.widget.ClientLibrary
 *  com.day.cq.widget.HtmlLibrary
 *  com.day.cq.widget.HtmlLibraryManager
 *  com.day.cq.widget.LibraryType
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang3.StringUtils
 */
package com.adobe.granite.contexthub.impl;

import com.day.cq.widget.ClientLibrary;
import com.day.cq.widget.HtmlLibrary;
import com.day.cq.widget.HtmlLibraryManager;
import com.day.cq.widget.LibraryType;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class ClientLib {
    private LibraryType libraryType;
    private boolean debugMode;
    private HtmlLibraryManager htmlLibraryManager;
    private boolean minified;

    public ClientLib(HtmlLibraryManager htmlLibraryManager, LibraryType libraryType, boolean debugMode) {
        this.htmlLibraryManager = htmlLibraryManager;
        this.libraryType = libraryType;
        this.debugMode = debugMode;
        this.minified = false;
    }

    public HtmlLibraryManager getHtmlLibraryManager() {
        return this.htmlLibraryManager;
    }

    public Collection<ClientLibrary> getLibraries(List<String> categories) {
        return this.getLibraries(categories.toArray(new String[categories.size()]));
    }

    public Collection<ClientLibrary> getLibraries(String[] categories) {
        return this.htmlLibraryManager.getLibraries(categories, this.libraryType, false, true);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public String getContent(ClientLibrary clientLibrary) {
        StringBuilder content;
        block9 : {
            content = new StringBuilder();
            if (clientLibrary == null) {
                return content.toString();
            }
            HtmlLibrary htmlLibrary = this.htmlLibraryManager.getLibrary(this.libraryType, clientLibrary.getPath());
            if (htmlLibrary != null) {
                InputStream is = null;
                StringWriter writer = new StringWriter();
                try {
                    is = htmlLibrary.getInputStream(this.minified);
                    IOUtils.copy((InputStream)is, (Writer)writer);
                    content.append(writer.toString().trim());
                    content.append("\n");
                }
                catch (IOException err) {
                    if (this.debugMode) {
                        content.append(String.format("\n/* Error while preparing library '%s' */\n", clientLibrary.getPath()));
                    }
                    break block9;
                }
                finally {
                    IOUtils.closeQuietly((InputStream)is);
                }
            }
            if (this.debugMode) {
                content.append(String.format("\n/* Can't find library '%s' */\n", clientLibrary.getPath()));
            }
        }
        return content.toString();
    }

    protected long getLastModified(ClientLibrary clientLibrary) {
        return this.getLibraryInfo(clientLibrary).getLastModified(this.minified);
    }

    public HtmlLibrary getLibraryInfo(ClientLibrary clientLibrary) {
        return this.htmlLibraryManager.getLibrary(this.libraryType, clientLibrary.getPath());
    }

    public static /* varargs */ String buildClientlibName(String ... parts) {
        return "contexthub." + StringUtils.join((Object[])parts, (String)".");
    }
}