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.core.DirectoryServer;
035    import org.opends.server.types.AttributeType;
036    import org.opends.server.types.InitializationException;
037    
038    import static org.opends.messages.ToolMessages.*;
039    
040    import static org.opends.server.util.StaticUtils.*;
041    
042    
043    
044    /**
045     * This class defines a tag that is used to base presence of one attribute on
046     * the absence of another attribute and/or attribute value.
047     */
048    public class IfAbsentTag
049           extends Tag
050    {
051      // The attribute type for which to make the determination.
052      private AttributeType attributeType;
053    
054      // The value for which to make the determination.
055      private String assertionValue;
056    
057    
058    
059      /**
060       * Creates a new instance of this ifabsent tag.
061       */
062      public IfAbsentTag()
063      {
064        attributeType  = null;
065        assertionValue = null;
066      }
067    
068    
069    
070      /**
071       * Retrieves the name for this tag.
072       *
073       * @return  The name for this tag.
074       */
075      public String getName()
076      {
077        return "IfAbsent";
078      }
079    
080    
081    
082      /**
083       * Indicates whether this tag is allowed for use in the extra lines for
084       * branches.
085       *
086       * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
087       *          or <CODE>false</CODE> if not.
088       */
089      public boolean allowedInBranch()
090      {
091        return true;
092      }
093    
094    
095    
096      /**
097       * Performs any initialization for this tag that may be needed while parsing
098       * a branch definition.
099       *
100       * @param  templateFile  The template file in which this tag is used.
101       * @param  branch        The branch in which this tag is used.
102       * @param  arguments     The set of arguments provided for this tag.
103       * @param  lineNumber    The line number on which this tag appears in the
104       *                       template file.
105       * @param  warnings      A list into which any appropriate warning messages
106       *                       may be placed.
107       *
108       * @throws  InitializationException  If a problem occurs while initializing
109       *                                   this tag.
110       */
111      public void initializeForBranch(TemplateFile templateFile, Branch branch,
112                                      String[] arguments, int lineNumber,
113                                      List<Message> warnings)
114             throws InitializationException
115      {
116        if ((arguments.length < 1) || (arguments.length > 2))
117        {
118          Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
119              getName(), lineNumber, 1, 2, arguments.length);
120          throw new InitializationException(message);
121        }
122    
123        String lowerName = toLowerCase(arguments[0]);
124        AttributeType t = DirectoryServer.getAttributeType(lowerName, true);
125        if (! branch.hasAttribute(t))
126        {
127          Message message =
128              ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
129          throw new InitializationException(message);
130        }
131    
132        if (arguments.length == 2)
133        {
134          assertionValue = arguments[1];
135        }
136        else
137        {
138          assertionValue = null;
139        }
140      }
141    
142    
143    
144      /**
145       * Performs any initialization for this tag that may be needed while parsing
146       * a template definition.
147       *
148       * @param  templateFile  The template file in which this tag is used.
149       * @param  template      The template in which this tag is used.
150       * @param  arguments     The set of arguments provided for this tag.
151       * @param  lineNumber    The line number on which this tag appears in the
152       *                       template file.
153       * @param  warnings      A list into which any appropriate warning messages
154       *                       may be placed.
155       *
156       * @throws  InitializationException  If a problem occurs while initializing
157       *                                   this tag.
158       */
159      public void initializeForTemplate(TemplateFile templateFile,
160                                        Template template, String[] arguments,
161                                        int lineNumber, List<Message> warnings)
162             throws InitializationException
163      {
164        if ((arguments.length < 1) || (arguments.length > 2))
165        {
166          Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
167              getName(), lineNumber, 1, 2, arguments.length);
168          throw new InitializationException(message);
169        }
170    
171        String lowerName = toLowerCase(arguments[0]);
172        AttributeType t = DirectoryServer.getAttributeType(lowerName, true);
173        if (! template.hasAttribute(t))
174        {
175          Message message =
176              ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
177          throw new InitializationException(message);
178        }
179    
180        if (arguments.length == 2)
181        {
182          assertionValue = arguments[1];
183        }
184        else
185        {
186          assertionValue = null;
187        }
188      }
189    
190    
191    
192      /**
193       * Generates the content for this tag by appending it to the provided tag.
194       *
195       * @param  templateEntry  The entry for which this tag is being generated.
196       * @param  templateValue  The template value to which the generated content
197       *                        should be appended.
198       *
199       * @return  The result of generating content for this tag.
200       */
201      public TagResult generateValue(TemplateEntry templateEntry,
202                                     TemplateValue templateValue)
203      {
204        List<TemplateValue> values = templateEntry.getValues(attributeType);
205        if ((values == null) || values.isEmpty())
206        {
207          return TagResult.SUCCESS_RESULT;
208        }
209    
210        if (assertionValue == null)
211        {
212          return TagResult.OMIT_FROM_ENTRY;
213        }
214        else
215        {
216          for (TemplateValue v : values)
217          {
218            if (assertionValue.equals(v.getValue().toString()))
219            {
220              return TagResult.OMIT_FROM_ENTRY;
221            }
222          }
223    
224          return TagResult.SUCCESS_RESULT;
225        }
226      }
227    }
228