All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wisdom.framework.osgi.Clauses Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.wisdom.framework.osgi;

import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Represents a parsed header value from an OSGi manifest.
 * Code taken from BND.
 */
public class Clauses extends LinkedHashMap> {

    private static final Logger LOGGER = LoggerFactory.getLogger(Clauses.class);

    /**
     * Standard OSGi header parser. This parser can handle the format:
     * 
    *
  • clauses ::= clause ( ',' clause ) +
  • *
  • clause ::= name ( ';' name ) (';' key '=' value )
  • *
*

* The return structure is a Map of Map: Map {name => Map {attribute | directive} => value}. * * @param header the header to parse * @return the clauses */ static public Clauses parse(String header) { if (Strings.isNullOrEmpty(header)) { return new Clauses(); } Clauses result = new Clauses(); QuotedTokenizer qt = new QuotedTokenizer(header, ";=,"); char del; do { boolean hadAttribute = false; Map clause = new LinkedHashMap<>(); List aliases = new ArrayList(); aliases.add(qt.nextToken()); del = qt.getSeparator(); while (del == ';') { String name = qt.nextToken(); if ((del = qt.getSeparator()) != '=') { // Found an ; or , - it's valid if we didn't find an attribute. if (hadAttribute) { throw new IllegalArgumentException("Header contains name field after " + "attribute or directive: " + name + " from " + header); } aliases.add(name); } else { // Parse attribute or directive. String value = qt.nextToken(); clause.put(name, value); del = qt.getSeparator(); hadAttribute = true; } } for (String packageName : aliases) { if (result.containsKey(packageName)) { LOGGER.warn("Duplicate package name in header: " + packageName + ". Multiple package names in one clause are supported."); } else { result.put(packageName, clause); } } } while (del == ','); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy