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.maven.plugins.ear;
20  
21  import org.codehaus.plexus.util.xml.XMLWriter;
22  
23  /**
24   * The representation of a env-entry entry within an application.xml file.
25   *
26   * @author Jim Brownfield based on code by <a href="snicoll@apache.org">Stephane Nicoll</a>
27   */
28  class EnvEntry {
29  
30      static final String ENV_ENTRY = "env-entry";
31  
32      static final String DESCRIPTION = "description";
33  
34      static final String ENV_ENTRY_NAME = "env-entry-name";
35  
36      static final String ENV_ENTRY_TYPE = "env-entry-type";
37  
38      static final String ENV_ENTRY_VALUE = "env-entry-value";
39  
40      static final String ENV_LOOKUP_NAME = "lookup-name";
41  
42      private final String description;
43  
44      private final String name;
45  
46      private final String type;
47  
48      private final String value;
49  
50      private final String lookupName;
51  
52      EnvEntry(String description, String name, String type, String value, String lookupName) {
53          if (name == null || name.isEmpty()) {
54              throw new IllegalArgumentException(ENV_ENTRY_NAME + " in " + ENV_ENTRY + " element cannot be null.");
55          } else if ((type == null || type.isEmpty()) && (value == null || value.isEmpty())) {
56  
57              throw new IllegalArgumentException(ENV_ENTRY_TYPE + " in " + ENV_ENTRY + " element cannot be null if no "
58                      + ENV_ENTRY_VALUE + " was specified.");
59          }
60  
61          this.description = description;
62          this.name = name;
63          this.type = type;
64          this.value = value;
65          this.lookupName = lookupName;
66      }
67  
68      public String getDescription() {
69          return description;
70      }
71  
72      public String getName() {
73          return name;
74      }
75  
76      public String getType() {
77          return type;
78      }
79  
80      public String getValue() {
81          return value;
82      }
83  
84      public String getLookupName() {
85          return lookupName;
86      }
87  
88      /**
89       * Appends the {@code XML} representation of this env-entry.
90       *
91       * @param writer the writer to use
92       */
93      public void appendEnvEntry(XMLWriter writer) {
94          writer.startElement(ENV_ENTRY);
95  
96          // description
97          if (getDescription() != null) {
98              doWriteElement(writer, DESCRIPTION, getDescription());
99          }
100 
101         // env entry name
102         doWriteElement(writer, ENV_ENTRY_NAME, getName());
103 
104         // env entry type
105         if (getType() != null) {
106             doWriteElement(writer, ENV_ENTRY_TYPE, getType());
107         }
108 
109         // env entry value
110         if (getValue() != null) {
111             doWriteElement(writer, ENV_ENTRY_VALUE, getValue());
112         }
113 
114         // lookup-name
115         if (getLookupName() != null) {
116             doWriteElement(writer, ENV_LOOKUP_NAME, getLookupName());
117         }
118 
119         // end of env-entry
120         writer.endElement();
121     }
122 
123     private void doWriteElement(XMLWriter writer, String element, String text) {
124         writer.startElement(element);
125         writer.writeText(text);
126         writer.endElement();
127     }
128 
129     public String toString() {
130         return "env-entry [name=" + getName() + ", type=" + getType() + ", value=" + getValue() + ", lookup-name="
131                 + getLookupName() + "]";
132     }
133 }