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