Search in sources :

Example 16 with QuotedTokenizer

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

the class Clauses method parse.

/**
	 * Standard OSGi header parser. This parser can handle the format clauses
	 * ::= clause ( ',' clause ) + clause ::= name ( ';' name ) (';' key '='
	 * value ) This is mapped to a Map { name => Map { attr|directive => value }
	 * }
	 * 
	 * @param value
	 * @return parsed clauses
	 */
public static Clauses parse(String value, Logger logger) {
    if (value == null || value.trim().length() == 0)
        return new Clauses();
    Clauses result = new Clauses();
    QuotedTokenizer qt = new QuotedTokenizer(value, ";=,");
    char del;
    do {
        boolean hadAttribute = false;
        Clause clause = new Clause();
        List<String> aliases = new ArrayList<String>();
        aliases.add(qt.nextToken());
        del = qt.getSeparator();
        while (del == ';') {
            String adname = qt.nextToken();
            if ((del = qt.getSeparator()) != '=') {
                if (hadAttribute)
                    throw new IllegalArgumentException("Header contains name field after attribute or directive: " + adname + " from " + value);
                aliases.add(adname);
            } else {
                String advalue = qt.nextToken();
                clause.put(adname, advalue);
                del = qt.getSeparator();
                hadAttribute = true;
            }
        }
        for (Iterator<String> i = aliases.iterator(); i.hasNext(); ) {
            String packageName = i.next();
            if (result.containsKey(packageName)) {
                if (logger != null)
                    logger.warning("Duplicate package name in header: " + packageName + ". Multiple package names in one clause not supported in Bnd.");
            } else
                result.put(packageName, clause);
        }
    } while (del == ',');
    return result;
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) ArrayList(java.util.ArrayList)

Example 17 with QuotedTokenizer

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

the class RunRequirementsPart method convertLegacyRequireList.

private List<Requirement> convertLegacyRequireList(String input) throws IllegalArgumentException {
    List<Requirement> result = new ArrayList<Requirement>();
    if (Constants.EMPTY_HEADER.equalsIgnoreCase(input.trim()))
        return result;
    QuotedTokenizer qt = new QuotedTokenizer(input, ",");
    String token = qt.nextToken();
    while (token != null) {
        String item = token.trim();
        int index = item.indexOf(":");
        if (index < 0)
            throw new IllegalArgumentException("Invalid format for requirement");
        String name = item.substring(0, index);
        String filter = item.substring(index + 1);
        Requirement req = new CapReqBuilder(name).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter).buildSyntheticRequirement();
        result.add(req);
        token = qt.nextToken();
    }
    return result;
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) ArrayList(java.util.ArrayList)

Example 18 with QuotedTokenizer

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

the class OSGiHeader method parseProperties.

public static Attrs parseProperties(String input, Reporter logger) {
    if (input == null || input.trim().length() == 0)
        return new Attrs();
    Attrs result = new Attrs();
    QuotedTokenizer qt = new QuotedTokenizer(input, "=,");
    char del = ',';
    while (del == ',') {
        String key = qt.nextToken(",=");
        if (key == null) {
            // happens at a trailing ',' without a followup
            if (logger == null)
                throw new IllegalArgumentException("Trailing comma found, forgot to escape the newline? Input=" + input);
            logger.error("Trailing comma found, forgot to escape the newline? Input=", input);
            break;
        }
        String value = "";
        del = qt.getSeparator();
        if (del == '=') {
            value = qt.nextToken(",=");
            if (value == null)
                value = "";
            del = qt.getSeparator();
        }
        result.put(key.trim(), value);
    }
    if (del != 0) {
        if (logger == null)
            throw new IllegalArgumentException("Invalid syntax for properties: " + input);
        logger.error("Invalid syntax for properties: %s", input);
    }
    return result;
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer)

Example 19 with QuotedTokenizer

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

the class Header method parseHeader.

public static Header parseHeader(String value, Reporter logger, Header result) {
    if (value == null || value.trim().length() == 0)
        return result;
    QuotedTokenizer qt = new QuotedTokenizer(value, ";=,");
    char del = 0;
    do {
        boolean hadAttribute = false;
        Props clause = new Props();
        List<String> aliases = Create.list();
        String name = qt.nextToken(",;");
        del = qt.getSeparator();
        if (name == null || name.length() == 0) {
            if (logger != null && logger.isPedantic()) {
                logger.warning("Empty clause, usually caused by repeating a comma without any name field or by having spaces after the backslash of a property file: %s", value);
            }
            if (name == null)
                break;
        } else {
            name = name.trim();
            aliases.add(name);
            while (del == ';') {
                String adname = qt.nextToken();
                if ((del = qt.getSeparator()) != '=') {
                    if (hadAttribute)
                        if (logger != null) {
                            logger.error("Header contains name field after attribute or directive: %s from %s. Name fields must be consecutive, separated by a ';' like a;b;c;x=3;y=4", adname, value);
                        }
                    if (adname != null && adname.length() > 0)
                        aliases.add(adname.trim());
                } else {
                    String advalue = qt.nextToken();
                    if (clause.containsKey(adname)) {
                        if (logger != null && logger.isPedantic())
                            logger.warning("Duplicate attribute/directive name %s in %s. This attribute/directive will be ignored", adname, value);
                    }
                    if (advalue == null) {
                        if (logger != null)
                            logger.error("No value after '=' sign for attribute %s", adname);
                        advalue = "";
                    }
                    clause.put(adname.trim(), advalue);
                    del = qt.getSeparator();
                    hadAttribute = true;
                }
            }
            // add a number of "~" to make it unique.
            for (String clauseName : aliases) {
                if (result.containsKey(clauseName)) {
                    if (logger != null && logger.isPedantic())
                        logger.warning("Duplicate name %s used in header: '%s'. Duplicate names are specially marked in Bnd with a ~ at the end (which is stripped at printing time).", clauseName, clauseName);
                    while (result.containsKey(clauseName)) clauseName += "~";
                }
                result.put(clauseName, clause);
            }
        }
    } while (del == ',');
    return result;
}
Also used : QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer)

Example 20 with QuotedTokenizer

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

the class TestQuotedTokenizer method testImplicitEmptyStringTurnedToNull.

public static void testImplicitEmptyStringTurnedToNull() {
    QuotedTokenizer qt = new QuotedTokenizer("literal=", ";=,");
    qt.nextToken();
    assertNull(qt.nextToken());
}
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