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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS
026 */
027package org.opends.server.tools.makeldif;
028
029import org.opends.server.types.AttributeType;
030
031/**
032 * This class defines a line that may appear in a template or branch.  It may
033 * contain any number of tags to be evaluated.
034 */
035public class TemplateLine
036{
037  /** The attribute type for this template line. */
038  private AttributeType attributeType;
039
040  /** The line number on which this template line appears in the template file. */
041  private int lineNumber;
042
043  /** The set of tags for this template line. */
044  private Tag[] tags;
045
046  /** Whether this line corresponds to an URL value or not. */
047  private boolean isURL;
048
049  /** Whether this line corresponds to a base64 encoded value or not. */
050  private boolean isBase64;
051
052
053  /**
054   * Retrieves the attribute type for this template line.
055   *
056   * @return  The attribute type for this template line.
057   */
058  public AttributeType getAttributeType()
059  {
060    return attributeType;
061  }
062
063
064
065  /**
066   * Retrieves the line number on which this template line appears in the
067   * template file.
068   *
069   * @return  The line number on which this template line appears in the
070   *          template file.
071   */
072  public int getLineNumber()
073  {
074    return lineNumber;
075  }
076
077
078
079  /**
080   * Returns whether the value of this template line corresponds to an URL
081   * or not.
082   *
083   * @return <CODE>true</CODE> if the value of this template line corresponds
084   * to an URL and <CODE>false</CODE> otherwise.
085   */
086  public boolean isURL()
087  {
088    return isURL;
089  }
090
091  /**
092   * Returns whether the value of this template line corresponds to a Base64
093   * encoded value or not.
094   *
095   * @return <CODE>true</CODE> if the value of this template line corresponds
096   * to a Base64 encoded value and <CODE>false</CODE> otherwise.
097   */
098  public boolean isBase64()
099  {
100    return isBase64;
101  }
102
103
104  /**
105   * Creates a new template line with the provided information.
106   *
107   * @param  attributeType  The attribute type for this template line.
108   * @param  lineNumber     The line number on which this template line appears
109   *                        in the template file.
110   * @param  tags           The set of tags for this template line.
111   */
112  public TemplateLine(AttributeType attributeType, int lineNumber, Tag[] tags)
113  {
114    this(attributeType, lineNumber, tags, false, false);
115  }
116
117
118  /**
119   * Creates a new template line with the provided information.
120   *
121   * @param  attributeType  The attribute type for this template line.
122   * @param  lineNumber     The line number on which this template line appears
123   *                        in the template file.
124   * @param  tags           The set of tags for this template line.
125   * @param  isURL          Whether this template line's value is an URL or not.
126   * @param  isBase64       Whether this template line's value is Base64 encoded
127   *                        or not.
128   */
129  public TemplateLine(AttributeType attributeType, int lineNumber, Tag[] tags,
130      boolean isURL, boolean isBase64)
131  {
132    this.attributeType = attributeType;
133    this.lineNumber    = lineNumber;
134    this.tags          = tags;
135    this.isURL         = isURL;
136    this.isBase64      = isBase64;
137  }
138
139
140  /**
141   * Generates the content for this template line and places it in the provided
142   * template entry.
143   *
144   * @param  templateEntry  The template entry being generated.
145   *
146   * @return  The result of generating the template line.
147   */
148  public TagResult generateLine(TemplateEntry templateEntry)
149  {
150    TemplateValue value = new TemplateValue(this);
151
152    for (Tag t : tags)
153    {
154      TagResult result = t.generateValue(templateEntry, value);
155      if (!result.keepProcessingLine()
156          || !result.keepProcessingEntry()
157          || !result.keepProcessingParent()
158          || !result.keepProcessingTemplateFile())
159      {
160        return result;
161      }
162    }
163
164    templateEntry.addValue(value);
165    return TagResult.SUCCESS_RESULT;
166  }
167}
168