private Set getImports()

in src/main/java/org/apache/sling/feature/maven/mojos/reports/ScriptsImportPackagesReporter.java [107:174]


    private Set<String> getImports(final ZipInputStream is) throws IOException {
        final Set<String> imports = new HashSet<>();
        final String script = readScript(is);
        // this is a very rough search
        int start = 0;
        int sectionStart = -1;
        while (start < script.length()) {
            // <%@page session="false" pageEncoding="utf-8" import=""
            if (sectionStart != -1) {
                final int pos = script.indexOf("%>", start);
                if (pos == -1) {
                    start = script.length();
                } else {
                    start = pos + 2;
                    final String section = script.substring(sectionStart, pos);
                    sectionStart = -1;
                    final int pagePos = section.indexOf("page");
                    if (pagePos != -1) {
                        final String page = section.substring(pagePos + 4);
                        if (Character.isWhitespace(page.charAt(0))) {
                            final int importPos = page.indexOf("import");
                            if (importPos != -1) {
                                // searchStart
                                int importStart = importPos + 6;
                                while (importStart < page.length() && page.charAt(importStart) != '=') {
                                    importStart++;
                                }
                                if (importStart < page.length()) {
                                    importStart++;
                                    while (importStart < page.length()
                                            && Character.isWhitespace(page.charAt(importStart))) {
                                        importStart++;
                                    }
                                    if (importStart < page.length() && page.charAt(importStart) == '"') {
                                        importStart++;
                                        int importEnd = importStart;
                                        while (importEnd < page.length() && page.charAt(importEnd) != '"') {
                                            importEnd++;
                                        }
                                        if (importEnd < page.length()) {
                                            final String imp = page.substring(importStart, importEnd);
                                            final StringTokenizer st = new StringTokenizer(imp, ",");
                                            while (st.hasMoreTokens()) {
                                                final String statement =
                                                        st.nextToken().trim();
                                                final int lastDot = statement.lastIndexOf('.');
                                                imports.add(
                                                        lastDot == -1 ? statement : statement.substring(0, lastDot));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                final int pos = script.indexOf("<%@", start);
                if (pos == -1) {
                    start = script.length();
                } else {
                    sectionStart = start + 3;
                    start = sectionStart;
                }
            }
        }
        return imports;
    }