View Javadoc
1   package org.apache.maven.plugins.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   */
30  class EnvEntry
31  {
32  
33      static final String ENV_ENTRY = "env-entry";
34  
35      static final String DESCRIPTION = "description";
36  
37      static final String ENV_ENTRY_NAME = "env-entry-name";
38  
39      static final String ENV_ENTRY_TYPE = "env-entry-type";
40  
41      static final String ENV_ENTRY_VALUE = "env-entry-value";
42  
43      static final String ENV_LOOKUP_NAME = "lookup-name";
44  
45      private final String description;
46  
47      private final String name;
48  
49      private final String type;
50  
51      private final String value;
52  
53      private final String lookupName;
54  
55      EnvEntry( String description, String name, String type, String value, String lookupName )
56      {
57          if ( StringUtils.isEmpty( name ) )
58          {
59              throw new IllegalArgumentException( ENV_ENTRY_NAME + " in " + ENV_ENTRY + " element cannot be null." );
60          }
61          else if ( StringUtils.isEmpty( type ) && StringUtils.isEmpty( value ) )
62          {
63              throw new IllegalArgumentException( ENV_ENTRY_TYPE + " in " + ENV_ENTRY + " element cannot be null if no "
64                  + ENV_ENTRY_VALUE + " was specified." );
65  
66          }
67  
68          this.description = description;
69          this.name = name;
70          this.type = type;
71          this.value = value;
72          this.lookupName = lookupName;
73      }
74  
75      public String getDescription()
76      {
77          return description;
78      }
79  
80      public String getName()
81      {
82          return name;
83      }
84  
85      public String getType()
86      {
87          return type;
88      }
89  
90      public String getValue()
91      {
92          return value;
93      }
94  
95      public String getLookupName()
96      {
97          return lookupName;
98      }
99  
100     /**
101      * Appends the {@code XML} representation of this env-entry.
102      * 
103      * @param writer the writer to use
104      */
105     public void appendEnvEntry( XMLWriter writer )
106     {
107         System.out.println( "appendEnvEntry()" );
108         writer.startElement( ENV_ENTRY );
109 
110         // description
111         if ( getDescription() != null )
112         {
113             doWriteElement( writer, DESCRIPTION, getDescription() );
114         }
115 
116         // env entry name
117         doWriteElement( writer, ENV_ENTRY_NAME, getName() );
118 
119         // env entry type
120         if ( getType() != null )
121         {
122             doWriteElement( writer, ENV_ENTRY_TYPE, getType() );
123         }
124 
125         // env entry value
126         if ( getValue() != null )
127         {
128             doWriteElement( writer, ENV_ENTRY_VALUE, getValue() );
129         }
130 
131         // lookup-name
132         if ( getLookupName() != null )
133         {
134             doWriteElement( writer, ENV_LOOKUP_NAME, getLookupName() );
135         }
136 
137         // end of env-entry
138         writer.endElement();
139     }
140 
141     private void doWriteElement( XMLWriter writer, String element, String text )
142     {
143         writer.startElement( element );
144         writer.writeText( text );
145         writer.endElement();
146     }
147 
148     public String toString()
149     {
150         return "env-entry [name=" + getName() + ", type=" + getType() + ", value=" + getValue() + ", lookup-name="
151             + getLookupName() + "]";
152     }
153 
154 }