private static List normalizeImportClauses()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/bundlerepository/ResourceBuilder.java [275:331]


    private static List<ParsedHeaderClause> normalizeImportClauses(List<ParsedHeaderClause> clauses) throws BundleException {
        // Verify that the values are equals if the package specifies
        // both version and specification-version attributes.
        Set<String> dupeSet = new HashSet<>();
        for (ParsedHeaderClause clause : clauses) {
            // Check for "version" and "specification-version" attributes
            // and verify they are the same if both are specified.
            Object v = clause.attrs.get(Constants.VERSION_ATTRIBUTE);
            Object sv = clause.attrs.get(Constants.PACKAGE_SPECIFICATION_VERSION);
            if ((v != null) && (sv != null)) {
                // Verify they are equal.
                if (!((String) v).trim().equals(((String) sv).trim())) {
                    throw new IllegalArgumentException(
                            "Both version and specification-version are specified, but they are not equal.");
                }
            }

            // Ensure that only the "version" attribute is used and convert
            // it to the VersionRange type.
            if ((v != null) || (sv != null)) {
                clause.attrs.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
                v = (v == null) ? sv : v;
                clause.attrs.put(Constants.VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
            }

            // If bundle version is specified, then convert its type to VersionRange.
            v = clause.attrs.get(Constants.BUNDLE_VERSION_ATTRIBUTE);
            if (v != null) {
                clause.attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, VersionRange.parseVersionRange(v.toString()));
            }

            // Verify java.* is not imported, nor any duplicate imports.
            for (String pkgName : clause.paths) {
                if (!dupeSet.contains(pkgName)) {
                    // Verify that java.* packages are not imported.
                    if (pkgName.startsWith("java.")) {
                        throw new BundleException("Importing java.* packages not allowed: " + pkgName);
                        // The character "." has no meaning in the OSGi spec except
                        // when placed on the bundle class path. Some people, however,
                        // mistakenly think it means the default package when imported
                        // or exported. This is not correct. It is invalid.
                    } else if (pkgName.equals(".")) {
                        throw new BundleException("Importing '.' is invalid.");
                        // Make sure a package name was specified.
                    } else if (pkgName.length() == 0) {
                        throw new BundleException(
                                "Imported package names cannot be zero length.");
                    }
                    dupeSet.add(pkgName);
                } else {
                    throw new BundleException("Duplicate import: " + pkgName);
                }
            }
        }

        return clauses;
    }