Search in sources :

Example 1 with QuotedTokenizer

use of aQute.libg.qtokens.QuotedTokenizer in project bnd by bndtools.

the class bnd method _grep.

@Description("Grep the manifest of bundles/jar files. ")
public void _grep(grepOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String s = args.remove(0);
    Pattern pattern = Glob.toPattern(s);
    if (pattern == null) {
        messages.InvalidGlobPattern_(s);
        return;
    }
    if (args.isEmpty()) {
        args = new ExtList<String>(getBase().list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        }));
    }
    Set<String> headers = opts.headers();
    if (headers == null)
        headers = new TreeSet<String>();
    if (opts.exports())
        headers.add(Constants.EXPORT_PACKAGE);
    if (opts.bsn())
        headers.add(Constants.BUNDLE_SYMBOLICNAME);
    if (opts.imports())
        headers.add(Constants.IMPORT_PACKAGE);
    Instructions instructions = new Instructions(headers);
    for (String fileName : args) {
        File file = getFile(fileName);
        if (!file.isFile()) {
            messages.NoSuchFile_(file);
            continue;
        }
        try (JarInputStream in = new JarInputStream(IO.stream(file))) {
            Manifest m = in.getManifest();
            for (Object header : m.getMainAttributes().keySet()) {
                Attributes.Name name = (Name) header;
                if (instructions.isEmpty() || instructions.matches(name.toString())) {
                    String h = m.getMainAttributes().getValue(name);
                    QuotedTokenizer qt = new QuotedTokenizer(h, ",;=");
                    for (String value : qt.getTokenSet()) {
                        Matcher matcher = pattern.matcher(value);
                        while (matcher.find()) {
                            int start = matcher.start() - 8;
                            if (start < 0)
                                start = 0;
                            int end = matcher.end() + 8;
                            if (end > value.length())
                                end = value.length();
                            out.printf("%40s : %20s ...%s[%s]%s...\n", fileName, name, value.substring(start, matcher.start()), value.substring(matcher.start(), matcher.end()), value.substring(matcher.end(), end));
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) JarInputStream(java.util.jar.JarInputStream) Matcher(java.util.regex.Matcher) Attributes(java.util.jar.Attributes) Instructions(aQute.bnd.osgi.Instructions) Name(java.util.jar.Attributes.Name) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Name(java.util.jar.Attributes.Name) FilenameFilter(java.io.FilenameFilter) TreeSet(java.util.TreeSet) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 2 with QuotedTokenizer

use of aQute.libg.qtokens.QuotedTokenizer in project bnd by bndtools.

the class SimpleListConverter method convert.

public List<R> convert(String input) throws IllegalArgumentException {
    if (input == null)
        return null;
    List<R> result = new ArrayList<R>();
    if (input == null || Constants.EMPTY_HEADER.equalsIgnoreCase(input.trim()))
        return result;
    QuotedTokenizer qt = new QuotedTokenizer(input, ",");
    String token = qt.nextToken();
    while (token != null) {
        result.add(itemConverter.convert(token.trim()));
        token = qt.nextToken();
    }
    return result;
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) ArrayList(java.util.ArrayList)

Example 3 with QuotedTokenizer

use of aQute.libg.qtokens.QuotedTokenizer in project bnd by bndtools.

the class Verifier method verifyHeader.

private boolean verifyHeader(String name, Pattern regex, boolean error) {
    String value = manifest.getMainAttributes().getValue(name);
    if (value == null)
        return false;
    QuotedTokenizer st = new QuotedTokenizer(value.trim(), ",");
    for (Iterator<String> i = st.getTokenSet().iterator(); i.hasNext(); ) {
        if (!verify(i.next(), regex)) {
            if (error)
                error("Invalid value for %s, %s does not match %s", name, value, regex.pattern());
            else
                warning("Invalid value for %s, %s does not match %s", name, value, regex.pattern());
        }
    }
    return true;
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer)

Example 4 with QuotedTokenizer

use of aQute.libg.qtokens.QuotedTokenizer in project bnd by bndtools.

the class Verifier method doNative.

public void doNative(String nc) {
    if (nc != null) {
        QuotedTokenizer qt = new QuotedTokenizer(nc, ",;=", false);
        char del;
        do {
            do {
                String name = qt.nextToken();
                if (name == null) {
                    error("Can not parse name from bundle native code header: %s", nc);
                    return;
                }
                del = qt.getSeparator();
                if (del == ';') {
                    if (dot != null && !dot.exists(name)) {
                        error("Native library not found in JAR: %s", name);
                    }
                } else {
                    String value = null;
                    if (del == '=')
                        value = qt.nextToken();
                    String key = name.toLowerCase();
                    if (key.equals("osname")) {
                    // ...
                    } else if (key.equals("osversion")) {
                        // verify version range
                        verify(value, VERSIONRANGE);
                    } else if (key.equals("language")) {
                        verify(value, ISO639);
                    } else if (key.equals("processor")) {
                    // verify(value, PROCESSORS);
                    } else if (key.equals("selection-filter")) {
                        // verify syntax filter
                        verifyFilter(value);
                    } else if (name.equals("*") && value == null) {
                        // Wildcard must be at end.
                        if (qt.nextToken() != null)
                            error("Bundle-Native code header may only END in wildcard %s", nc);
                    } else {
                        warning("Unknown attribute in native code: %s=%s", name, value);
                    }
                    del = qt.getSeparator();
                }
            } while (del == ';');
        } while (del == ',');
    }
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer)

Example 5 with QuotedTokenizer

use of aQute.libg.qtokens.QuotedTokenizer in project bnd by bndtools.

the class TestQuotedTokenizer method testSimple.

public static void testSimple() {
    String[] s = new QuotedTokenizer("1.jar, 2.jar,    \t   3.jar", ",").getTokens();
    assertEquals("Length", 3, s.length);
    assertEquals("1.jar", s[0]);
    assertEquals("2.jar", s[1]);
    assertEquals("3.jar", s[2]);
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer)

Aggregations

QuotedTokenizer (aQute.libg.qtokens.QuotedTokenizer)22 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)2 PomFromManifest (aQute.bnd.maven.PomFromManifest)1 Instructions (aQute.bnd.osgi.Instructions)1 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)1 Description (aQute.lib.getopt.Description)1 FilenameFilter (java.io.FilenameFilter)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Attributes (java.util.jar.Attributes)1 Name (java.util.jar.Attributes.Name)1 JarInputStream (java.util.jar.JarInputStream)1 Manifest (java.util.jar.Manifest)1 Matcher (java.util.regex.Matcher)1 Requirement (org.osgi.resource.Requirement)1