View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.weaver.ant;
20  
21  import java.util.Properties;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.tools.ant.DynamicElementNS;
25  
26  /**
27   * <p>Structure to allow inline specification of properties.</p>
28   * <p>Example:
29   * {pre}&lt;foo&gt;foo-value&lt;/foo&gt;
30   * &lt;bar&gt;bar-value&lt;/bar&gt;
31   * &lt;baz&gt;baz
32   * -nextline-value&lt;/baz&gt;
33   * {/pre}
34   * </p>
35   */
36  public class InlineProperties implements DynamicElementNS {
37      /**
38       * Represents a single inline property.
39       */
40      public final class InlineProperty {
41          private final String name;
42  
43          private InlineProperty(final String name) {
44              this.name = name;
45          }
46  
47          /**
48           * Add text to this property.
49           * @param text to add
50           */
51          public void addText(final String text) {
52              final String value;
53              if (properties.containsKey(name)) {
54                  value = StringUtils.join(properties.getProperty(name), text);
55              } else {
56                  value = text;
57              }
58              properties.setProperty(name, value);
59          }
60      }
61  
62      /**
63       * {@link Properties} object maintained by the {@link InlineProperties}.
64       */
65      final Properties properties = new Properties();
66  
67      /**
68       * Handle the specified nested element.
69       * @param uri String URI
70       * @param localName local element name
71       * @param qName qualified name
72       * @return InlineProperty
73       */
74      @Override
75      public InlineProperty createDynamicElement(final String uri, final String localName, final String qName) {
76          return new InlineProperty(localName);
77      }
78  }