View Javadoc

1   package org.apache.maven.plugin.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.StringUtils;
23  import org.codehaus.plexus.util.xml.XMLWriter;
24  
25  /**
26   * The representation of a env-entry entry within an application.xml file.
27   * 
28   * @author Jim Brownfield based on code by <a href="snicoll@apache.org">Stephane Nicoll</a>
29   * @version $Id: EnvEntry.java 1542508 2013-11-16 13:21:35Z rfscholte $
30   */
31  class EnvEntry
32  {
33  
34      static final String ENV_ENTRY = "env-entry";
35  
36      static final String DESCRIPTION = "description";
37  
38      static final String ENV_ENTRY_NAME = "env-entry-name";
39  
40      static final String ENV_ENTRY_TYPE = "env-entry-type";
41  
42      static final String ENV_ENTRY_VALUE = "env-entry-value";
43  
44      private final String description;
45  
46      private final String name;
47  
48      private final String type;
49  
50      private final String value;
51  
52      public EnvEntry( String description, String name, String type, String value )
53      {
54          if ( StringUtils.isEmpty( name ) )
55          {
56              throw new IllegalArgumentException( ENV_ENTRY_NAME + " in " + ENV_ENTRY + " element cannot be null." );
57          }
58          else if ( StringUtils.isEmpty( type ) && StringUtils.isEmpty( value ) )
59          {
60              throw new IllegalArgumentException( ENV_ENTRY_TYPE + " in " + ENV_ENTRY + " element cannot be null if no "
61                  + ENV_ENTRY_VALUE + " was specified." );
62  
63          }
64  
65          this.description = description;
66          this.name = name;
67          this.type = type;
68          this.value = value;
69      }
70  
71      public String getDescription()
72      {
73          return description;
74      }
75  
76      public String getName()
77      {
78          return name;
79      }
80  
81      public String getType()
82      {
83          return type;
84      }
85  
86      public String getValue()
87      {
88          return value;
89      }
90  
91      /**
92       * Appends the <tt>XML</tt> representation of this env-entry.
93       * 
94       * @param writer the writer to use
95       */
96      public void appendEnvEntry( XMLWriter writer )
97      {
98          writer.startElement( ENV_ENTRY );
99  
100         // description
101         if ( getDescription() != null )
102         {
103             doWriteElement( writer, DESCRIPTION, getDescription() );
104         }
105 
106         // env entry name
107         doWriteElement( writer, ENV_ENTRY_NAME, getName() );
108 
109         // env entry type
110         if ( getType() != null )
111         {
112             doWriteElement( writer, ENV_ENTRY_TYPE, getType() );
113         }
114 
115         // env entry value
116         if ( getValue() != null )
117         {
118             doWriteElement( writer, ENV_ENTRY_VALUE, getValue() );
119         }
120 
121         // end of env-entry
122         writer.endElement();
123     }
124 
125     private void doWriteElement( XMLWriter writer, String element, String text )
126     {
127         writer.startElement( element );
128         writer.writeText( text );
129         writer.endElement();
130     }
131 
132     public String toString()
133     {
134         return "env-entry [name=" + getName() + ", type=" + getType() + ", value=" + getValue() + "]";
135     }
136 
137 }