001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.tools.makeldif;
028 import org.opends.messages.Message;
029
030
031
032 import java.util.List;
033
034 import org.opends.server.types.DN;
035 import org.opends.server.types.InitializationException;
036
037 import static org.opends.messages.ToolMessages.*;
038
039
040
041 /**
042 * This class defines a tag that is used to include the DN of the current entry
043 * in the attribute value.
044 */
045 public class DNTag
046 extends Tag
047 {
048 // The number of DN components to include.
049 private int numComponents;
050
051
052
053 /**
054 * Creates a new instance of this DN tag.
055 */
056 public DNTag()
057 {
058 numComponents = 0;
059 }
060
061
062
063 /**
064 * Retrieves the name for this tag.
065 *
066 * @return The name for this tag.
067 */
068 public String getName()
069 {
070 return "DN";
071 }
072
073
074
075 /**
076 * Indicates whether this tag is allowed for use in the extra lines for
077 * branches.
078 *
079 * @return <CODE>true</CODE> if this tag may be used in branch definitions,
080 * or <CODE>false</CODE> if not.
081 */
082 public boolean allowedInBranch()
083 {
084 return true;
085 }
086
087
088
089 /**
090 * Performs any initialization for this tag that may be needed while parsing
091 * a branch definition.
092 *
093 * @param templateFile The template file in which this tag is used.
094 * @param branch The branch in which this tag is used.
095 * @param arguments The set of arguments provided for this tag.
096 * @param lineNumber The line number on which this tag appears in the
097 * template file.
098 * @param warnings A list into which any appropriate warning messages
099 * may be placed.
100 *
101 * @throws InitializationException If a problem occurs while initializing
102 * this tag.
103 */
104 public void initializeForBranch(TemplateFile templateFile, Branch branch,
105 String[] arguments, int lineNumber,
106 List<Message> warnings)
107 throws InitializationException
108 {
109 initializeInternal(templateFile, arguments, lineNumber);
110 }
111
112
113
114 /**
115 * Performs any initialization for this tag that may be needed while parsing
116 * a template definition.
117 *
118 * @param templateFile The template file in which this tag is used.
119 * @param template The template in which this tag is used.
120 * @param arguments The set of arguments provided for this tag.
121 * @param lineNumber The line number on which this tag appears in the
122 * template file.
123 * @param warnings A list into which any appropriate warning messages
124 * may be placed.
125 *
126 * @throws InitializationException If a problem occurs while initializing
127 * this tag.
128 */
129 public void initializeForTemplate(TemplateFile templateFile,
130 Template template, String[] arguments,
131 int lineNumber, List<Message> warnings)
132 throws InitializationException
133 {
134 initializeInternal(templateFile, arguments, lineNumber);
135 }
136
137
138
139 /**
140 * Performs any initialization for this tag that may be needed for this tag.
141 *
142 * @param templateFile The template file in which this tag is used.
143 * @param arguments The set of arguments provided for this tag.
144 * @param lineNumber The line number on which this tag appears in the
145 * template file.
146 *
147 * @throws InitializationException If a problem occurs while initializing
148 * this tag.
149 */
150 private void initializeInternal(TemplateFile templateFile, String[] arguments,
151 int lineNumber)
152 throws InitializationException
153 {
154 if (arguments.length == 0)
155 {
156 numComponents = 0;
157 }
158 else if (arguments.length == 1)
159 {
160 try
161 {
162 numComponents = Integer.parseInt(arguments[0]);
163 }
164 catch (NumberFormatException nfe)
165 {
166 Message message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
167 arguments[0], getName(), lineNumber);
168 throw new InitializationException(message);
169 }
170 }
171 else
172 {
173 Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
174 getName(), lineNumber, 0, 1, arguments.length);
175 throw new InitializationException(message);
176 }
177 }
178
179
180
181 /**
182 * Generates the content for this tag by appending it to the provided tag.
183 *
184 * @param templateEntry The entry for which this tag is being generated.
185 * @param templateValue The template value to which the generated content
186 * should be appended.
187 *
188 * @return The result of generating content for this tag.
189 */
190 public TagResult generateValue(TemplateEntry templateEntry,
191 TemplateValue templateValue)
192 {
193 DN dn = templateEntry.getDN();
194 if ((dn == null) || dn.isNullDN())
195 {
196 return TagResult.SUCCESS_RESULT;
197 }
198
199 if (numComponents == 0)
200 {
201 dn.toString(templateValue.getValue());
202 }
203 else if (numComponents > 0)
204 {
205 int count = Math.min(numComponents, dn.getNumComponents());
206
207 dn.getRDN(0).toString(templateValue.getValue());
208 for (int i = 1; i < count; i++)
209 {
210 templateValue.append(",");
211 dn.getRDN(i).toString(templateValue.getValue());
212 }
213 }
214 else
215 {
216 int sz = dn.getNumComponents();
217 int count = Math.min(Math.abs(numComponents), sz);
218
219 dn.getRDN(sz - count).toString(templateValue.getValue());
220 for (int i = 1; i < count; i++)
221 {
222 templateValue.append(",");
223 dn.getRDN(sz - count + i).toString(templateValue.getValue());
224 }
225 }
226
227 return TagResult.SUCCESS_RESULT;
228 }
229 }
230