CssImporterResource.java 6.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.siteimporter.internal.resource;

import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResourceVisitor;
import com.day.cq.wcm.siteimporter.internal.resource.ReferenceLocation;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CssImporterResource
extends ImporterResource {
    private final Logger log;

    public CssImporterResource(URL location, ImporterContext ctx) {
        super(location, ctx);
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public Object triggerAction(ImporterResourceVisitor is) {
        return is.performCss(this);
    }

    @Override
    public ArrayList<ReferenceLocation> getExternalReferences() {
        ArrayList<ReferenceLocation> result = new ArrayList<ReferenceLocation>();
        String content = "";
        try {
            content = this.getContent();
        }
        catch (IOException e) {
            return result;
        }
        int next = content.toLowerCase().indexOf("@import");
        while (next > -1) {
            next += 7;
            while (next < content.length() && Character.isWhitespace(content.charAt(next))) {
                ++next;
            }
            switch (content.charAt(next)) {
                int end;
                case '\'': {
                    end = content.indexOf("'", next + 1);
                    if (end <= next) break;
                    try {
                        result.add(new ReferenceLocation(next + 1, end, this.toAbsoluteReference(content.substring(next + 1, end)), 1));
                    }
                    catch (MalformedURLException e) {}
                    break;
                }
                case '\"': {
                    end = content.indexOf("\"", next + 1);
                    if (end <= next) break;
                    try {
                        result.add(new ReferenceLocation(next + 1, end, this.toAbsoluteReference(content.substring(next + 1, end)), 1));
                        break;
                    }
                    catch (MalformedURLException e) {
                        break;
                    }
                }
            }
            next = content.toLowerCase().indexOf("@import", next);
        }
        next = content.toLowerCase().indexOf("url");
        try {
            while (next > -1) {
                int start;
                int end = start = next;
                int pos = start;
                String link = "";
                int state = 0;
                int type = 3;
                if (content.toLowerCase().lastIndexOf("@import", next) > content.lastIndexOf("{", next)) {
                    type = 1;
                }
                while (state < 5) {
                    char nextChar = content.charAt(pos);
                    switch (state) {
                        case 0: {
                            if (nextChar != '(') break;
                            state = 1;
                            break;
                        }
                        case 1: {
                            if (nextChar == '\"') {
                                state = 3;
                                start = pos + 1;
                                break;
                            }
                            if (nextChar == '\'') {
                                state = 4;
                                start = pos + 1;
                                break;
                            }
                            if (nextChar == ')') {
                                state = 6;
                                break;
                            }
                            if (Character.isWhitespace(nextChar)) break;
                            link = link + nextChar;
                            state = 2;
                            start = pos;
                            break;
                        }
                        case 2: {
                            if (Character.isWhitespace(nextChar) || nextChar == ')') {
                                end = pos;
                                state = 5;
                                break;
                            }
                            link = link + nextChar;
                            break;
                        }
                        case 3: {
                            if (nextChar == '\"') {
                                end = pos;
                                state = 5;
                                break;
                            }
                            link = link + nextChar;
                            break;
                        }
                        case 4: {
                            if (nextChar == '\'') {
                                end = pos;
                                state = 5;
                                break;
                            }
                            link = link + nextChar;
                        }
                    }
                    if (++pos <= content.length()) continue;
                    state = 6;
                }
                if (state == 5) {
                    try {
                        result.add(new ReferenceLocation(start, end, this.toAbsoluteReference(link), type));
                    }
                    catch (MalformedURLException e) {
                        // empty catch block
                    }
                }
                next = content.toLowerCase().indexOf("url", next + 3);
            }
        }
        catch (StringIndexOutOfBoundsException e) {
            this.log.debug("Found unsupported pattern in external reference: {}", (Object)content, (Object)e);
        }
        catch (Exception e) {
            this.log.error("Error while extracting external reference from: {}", (Object)content, (Object)e);
        }
        return result;
    }

    public void setContent(String content) {
        this.content = content;
        this.references = new ArrayList();
    }
}