001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.tools.makeldif;
018import org.forgerock.i18n.LocalizableMessage;
019
020
021
022import java.util.List;
023
024import org.opends.server.types.InitializationException;
025
026import static org.opends.messages.ToolMessages.*;
027
028
029
030/**
031 * This class defines a tag that is used to hold static text (i.e., text that
032 * appears outside of any tag).
033 */
034public class StaticTextTag
035       extends Tag
036{
037  /** The static text to include in the LDIF. */
038  private String text;
039
040
041
042  /** Creates a new instance of this static text tag. */
043  public StaticTextTag()
044  {
045    text = "";
046  }
047
048
049
050  /**
051   * Retrieves the name for this tag.
052   *
053   * @return  The name for this tag.
054   */
055  @Override
056  public String getName()
057  {
058    return "StaticText";
059  }
060
061
062
063  /**
064   * Indicates whether this tag is allowed for use in the extra lines for
065   * branches.
066   *
067   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
068   *          or <CODE>false</CODE> if not.
069   */
070  @Override
071  public boolean allowedInBranch()
072  {
073    return true;
074  }
075
076
077
078  /**
079   * Performs any initialization for this tag that may be needed while parsing
080   * a branch definition.
081   *
082   * @param  templateFile  The template file in which this tag is used.
083   * @param  branch        The branch in which this tag is used.
084   * @param  arguments     The set of arguments provided for this tag.
085   * @param  lineNumber    The line number on which this tag appears in the
086   *                       template file.
087   * @param  warnings      A list into which any appropriate warning messages
088   *                       may be placed.
089   *
090   * @throws  InitializationException  If a problem occurs while initializing
091   *                                   this tag.
092   */
093  @Override
094  public void initializeForBranch(TemplateFile templateFile, Branch branch,
095                                  String[] arguments, int lineNumber,
096                                  List<LocalizableMessage> warnings)
097         throws InitializationException
098  {
099    if (arguments.length != 1)
100    {
101      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
102          getName(), lineNumber, 1, arguments.length);
103      throw new InitializationException(message);
104    }
105
106    text = arguments[0];
107  }
108
109
110
111  /**
112   * Performs any initialization for this tag that may be needed while parsing
113   * a template definition.
114   *
115   * @param  templateFile  The template file in which this tag is used.
116   * @param  template      The template in which this tag is used.
117   * @param  arguments     The set of arguments provided for this tag.
118   * @param  lineNumber    The line number on which this tag appears in the
119   *                       template file.
120   * @param  warnings      A list into which any appropriate warning messages
121   *                       may be placed.
122   *
123   * @throws  InitializationException  If a problem occurs while initializing
124   *                                   this tag.
125   */
126  @Override
127  public void initializeForTemplate(TemplateFile templateFile,
128                                    Template template, String[] arguments,
129                                    int lineNumber, List<LocalizableMessage> warnings)
130         throws InitializationException
131  {
132    if (arguments.length != 1)
133    {
134      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
135          getName(), lineNumber, 1, arguments.length);
136      throw new InitializationException(message);
137    }
138
139    text = arguments[0];
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  @Override
154  public TagResult generateValue(TemplateEntry templateEntry,
155                                 TemplateValue templateValue)
156  {
157    templateValue.append(text);
158    return TagResult.SUCCESS_RESULT;
159  }
160}
161