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 RDN of the current entry
043     * in the attribute value.
044     */
045    public class RDNTag
046           extends Tag
047    {
048      /**
049       * Creates a new instance of this RDN tag.
050       */
051      public RDNTag()
052      {
053        // No implementation required.
054      }
055    
056    
057    
058      /**
059       * Retrieves the name for this tag.
060       *
061       * @return  The name for this tag.
062       */
063      public String getName()
064      {
065        return "RDN";
066      }
067    
068    
069    
070      /**
071       * Indicates whether this tag is allowed for use in the extra lines for
072       * branches.
073       *
074       * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
075       *          or <CODE>false</CODE> if not.
076       */
077      public boolean allowedInBranch()
078      {
079        return true;
080      }
081    
082    
083    
084      /**
085       * Performs any initialization for this tag that may be needed while parsing
086       * a branch definition.
087       *
088       * @param  templateFile  The template file in which this tag is used.
089       * @param  branch        The branch in which this tag is used.
090       * @param  arguments     The set of arguments provided for this tag.
091       * @param  lineNumber    The line number on which this tag appears in the
092       *                       template file.
093       * @param  warnings      A list into which any appropriate warning messages
094       *                       may be placed.
095       *
096       * @throws  InitializationException  If a problem occurs while initializing
097       *                                   this tag.
098       */
099      public void initializeForBranch(TemplateFile templateFile, Branch branch,
100                                      String[] arguments, int lineNumber,
101                                      List<Message> warnings)
102             throws InitializationException
103      {
104        if (arguments.length != 0)
105        {
106          Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
107              getName(), lineNumber, 0, arguments.length);
108          throw new InitializationException(message);
109        }
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        if (arguments.length != 0)
135        {
136          Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
137              getName(), lineNumber, 0, arguments.length);
138          throw new InitializationException(message);
139        }
140      }
141    
142    
143    
144      /**
145       * Generates the content for this tag by appending it to the provided tag.
146       *
147       * @param  templateEntry  The entry for which this tag is being generated.
148       * @param  templateValue  The template value to which the generated content
149       *                        should be appended.
150       *
151       * @return  The result of generating content for this tag.
152       */
153      public TagResult generateValue(TemplateEntry templateEntry,
154                                     TemplateValue templateValue)
155      {
156        DN dn = templateEntry.getDN();
157        if ((dn == null) || dn.isNullDN())
158        {
159          return TagResult.SUCCESS_RESULT;
160        }
161        else
162        {
163          dn.getRDN().toString(templateValue.getValue());
164          return TagResult.SUCCESS_RESULT;
165        }
166      }
167    }
168