001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017
018package org.apache.xbean.osgi.bundle.util;
019
020import java.util.List;
021import java.util.Map;
022import java.util.regex.Pattern;
023
024import org.apache.xbean.osgi.bundle.util.HeaderParser.HeaderElement;
025
026/**
027 * @version $Rev: 1160977 $ $Date: 2011-08-24 07:02:21 +0200 (Wed, 24 Aug 2011) $
028 */
029public class HeaderBuilder {
030
031    private static final Pattern EXTENDED_PATTERN = Pattern.compile("[\\w_\\-\\.]+");
032
033    public static String build(List<HeaderElement> headerElements) {
034        if (headerElements == null || headerElements.size() == 0) {
035            return "";
036        }
037        StringBuilder header = new StringBuilder();
038        for (HeaderElement headerElement : headerElements) {
039            String name = headerElement.getName();
040            if (name == null || name.length() == 0) {
041                throw new IllegalArgumentException("Invalid header name for the header elment " + headerElement);
042            }
043            if (header.length() > 0) {
044                header.append(",");
045            }
046            header.append(name);
047            for (Map.Entry<String, String> attribute : headerElement.getAttributes().entrySet()) {
048                header.append(";").append(attribute.getKey()).append("=");
049                if (EXTENDED_PATTERN.matcher(attribute.getValue()).matches()) {
050                    header.append(attribute.getValue());
051                } else {
052                    header.append("\"").append(attribute.getValue()).append("\"");
053                }
054            }
055            for (Map.Entry<String, String> directive : headerElement.getDirectives().entrySet()) {
056                header.append(";").append(directive.getKey()).append(":=");
057                if (EXTENDED_PATTERN.matcher(directive.getValue()).matches()) {
058                    header.append(directive.getValue());
059                } else {
060                    header.append("\"").append(directive.getValue()).append("\"");
061                }
062            }
063        }
064        return header.toString();
065    }
066}