001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.weaver.ant;
020
021import java.util.Properties;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.tools.ant.DynamicElementNS;
025
026/**
027 * <p>Structure to allow inline specification of properties.</p>
028 * <p>Example:
029 * {pre}&lt;foo&gt;foo-value&lt;/foo&gt;
030 * &lt;bar&gt;bar-value&lt;/bar&gt;
031 * &lt;baz&gt;baz
032 * -nextline-value&lt;/baz&gt;
033 * {/pre}
034 * </p>
035 */
036public class InlineProperties implements DynamicElementNS {
037    /**
038     * Represents a single inline property.
039     */
040    public final class InlineProperty {
041        private final String name;
042
043        private InlineProperty(final String name) {
044            this.name = name;
045        }
046
047        /**
048         * Add text to this property.
049         * @param text to add
050         */
051        public void addText(final String text) {
052            final String value;
053            if (properties.containsKey(name)) {
054                value = StringUtils.join(properties.getProperty(name), text);
055            } else {
056                value = text;
057            }
058            properties.setProperty(name, value);
059        }
060    }
061
062    /**
063     * {@link Properties} object maintained by the {@link InlineProperties}.
064     */
065    final Properties properties = new Properties();
066
067    /**
068     * Handle the specified nested element.
069     * @param uri String URI
070     * @param localName local element name
071     * @param qName qualified name
072     * @return InlineProperty
073     */
074    @Override
075    public InlineProperty createDynamicElement(final String uri, final String localName, final String qName) {
076        return new InlineProperty(localName);
077    }
078}