107N/A/*
107N/A * CDDL HEADER START
107N/A *
107N/A * The contents of this file are subject to the terms of the
107N/A * Common Development and Distribution License, Version 1.0 only
107N/A * (the "License"). You may not use this file except in compliance
107N/A * with the License.
107N/A *
6983N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6983N/A * or http://forgerock.org/license/CDDLv1.0.html.
107N/A * See the License for the specific language governing permissions
107N/A * and limitations under the License.
107N/A *
107N/A * When distributing Covered Code, include this CDDL HEADER in each
6983N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6983N/A * If applicable, add the following below this CDDL HEADER, with the
6983N/A * fields enclosed by brackets "[]" replaced with your own identifying
6983N/A * information:
107N/A * Portions Copyright [yyyy] [name of copyright owner]
107N/A *
107N/A * CDDL HEADER END
107N/A *
107N/A *
5099N/A * Copyright 2006-2010 Sun Microsystems, Inc.
107N/A */
107N/Apackage org.opends.server.tools.makeldif;
2086N/Aimport org.opends.messages.Message;
107N/A
107N/A
107N/A
107N/Aimport java.util.List;
107N/A
107N/Aimport org.opends.server.core.DirectoryServer;
107N/Aimport org.opends.server.types.AttributeType;
338N/Aimport org.opends.server.types.InitializationException;
107N/A
2086N/Aimport static org.opends.messages.ToolMessages.*;
2086N/A
107N/Aimport static org.opends.server.util.StaticUtils.*;
107N/A
107N/A
107N/A
107N/A/**
107N/A * This class defines a tag that is used to base presence of one attribute on
107N/A * the presence of another attribute and/or attribute value.
107N/A */
107N/Apublic class IfPresentTag
107N/A extends Tag
107N/A{
107N/A // The attribute type for which to make the determination.
107N/A private AttributeType attributeType;
107N/A
107N/A // The value for which to make the determination.
107N/A private String assertionValue;
107N/A
107N/A
107N/A
107N/A /**
107N/A * Creates a new instance of this ifpresent tag.
107N/A */
107N/A public IfPresentTag()
107N/A {
107N/A attributeType = null;
107N/A assertionValue = null;
107N/A }
107N/A
107N/A
107N/A
107N/A /**
107N/A * Retrieves the name for this tag.
107N/A *
107N/A * @return The name for this tag.
107N/A */
107N/A public String getName()
107N/A {
107N/A return "IfPresent";
107N/A }
107N/A
107N/A
107N/A
107N/A /**
107N/A * Indicates whether this tag is allowed for use in the extra lines for
107N/A * branches.
107N/A *
107N/A * @return <CODE>true</CODE> if this tag may be used in branch definitions,
107N/A * or <CODE>false</CODE> if not.
107N/A */
107N/A public boolean allowedInBranch()
107N/A {
107N/A return true;
107N/A }
107N/A
107N/A
107N/A
107N/A /**
107N/A * Performs any initialization for this tag that may be needed while parsing
107N/A * a branch definition.
107N/A *
107N/A * @param templateFile The template file in which this tag is used.
107N/A * @param branch The branch in which this tag is used.
107N/A * @param arguments The set of arguments provided for this tag.
107N/A * @param lineNumber The line number on which this tag appears in the
107N/A * template file.
107N/A * @param warnings A list into which any appropriate warning messages
107N/A * may be placed.
107N/A *
107N/A * @throws InitializationException If a problem occurs while initializing
107N/A * this tag.
107N/A */
107N/A public void initializeForBranch(TemplateFile templateFile, Branch branch,
107N/A String[] arguments, int lineNumber,
2086N/A List<Message> warnings)
107N/A throws InitializationException
107N/A {
107N/A if ((arguments.length < 1) || (arguments.length > 2))
107N/A {
2086N/A Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
2086N/A getName(), lineNumber, 1, 2, arguments.length);
2086N/A throw new InitializationException(message);
107N/A }
107N/A
107N/A String lowerName = toLowerCase(arguments[0]);
107N/A AttributeType t = DirectoryServer.getAttributeType(lowerName, true);
107N/A if (! branch.hasAttribute(t))
107N/A {
2086N/A Message message =
2086N/A ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
2086N/A throw new InitializationException(message);
107N/A }
107N/A
107N/A if (arguments.length == 2)
107N/A {
107N/A assertionValue = arguments[1];
107N/A }
107N/A else
107N/A {
107N/A assertionValue = null;
107N/A }
107N/A }
107N/A
107N/A
107N/A
107N/A /**
107N/A * Performs any initialization for this tag that may be needed while parsing
107N/A * a template definition.
107N/A *
107N/A * @param templateFile The template file in which this tag is used.
107N/A * @param template The template in which this tag is used.
107N/A * @param arguments The set of arguments provided for this tag.
107N/A * @param lineNumber The line number on which this tag appears in the
107N/A * template file.
107N/A * @param warnings A list into which any appropriate warning messages
107N/A * may be placed.
107N/A *
107N/A * @throws InitializationException If a problem occurs while initializing
107N/A * this tag.
107N/A */
107N/A public void initializeForTemplate(TemplateFile templateFile,
107N/A Template template, String[] arguments,
2086N/A int lineNumber, List<Message> warnings)
107N/A throws InitializationException
107N/A {
107N/A if ((arguments.length < 1) || (arguments.length > 2))
107N/A {
2086N/A Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
2086N/A getName(), lineNumber, 1, 2, arguments.length);
2086N/A throw new InitializationException(message);
107N/A }
107N/A
107N/A String lowerName = toLowerCase(arguments[0]);
5099N/A attributeType = DirectoryServer.getAttributeType(lowerName, true);
5099N/A if (! template.hasAttribute(attributeType))
107N/A {
2086N/A Message message =
2086N/A ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
2086N/A throw new InitializationException(message);
107N/A }
107N/A
107N/A if (arguments.length == 2)
107N/A {
107N/A assertionValue = arguments[1];
107N/A }
107N/A else
107N/A {
107N/A assertionValue = null;
107N/A }
107N/A }
107N/A
107N/A
107N/A
107N/A /**
107N/A * Generates the content for this tag by appending it to the provided tag.
107N/A *
107N/A * @param templateEntry The entry for which this tag is being generated.
107N/A * @param templateValue The template value to which the generated content
107N/A * should be appended.
107N/A *
107N/A * @return The result of generating content for this tag.
107N/A */
107N/A public TagResult generateValue(TemplateEntry templateEntry,
107N/A TemplateValue templateValue)
107N/A {
107N/A List<TemplateValue> values = templateEntry.getValues(attributeType);
107N/A if ((values == null) || values.isEmpty())
107N/A {
107N/A return TagResult.OMIT_FROM_ENTRY;
107N/A }
107N/A
107N/A if (assertionValue == null)
107N/A {
107N/A return TagResult.SUCCESS_RESULT;
107N/A }
107N/A else
107N/A {
107N/A for (TemplateValue v : values)
107N/A {
107N/A if (assertionValue.equals(v.getValue().toString()))
107N/A {
107N/A return TagResult.SUCCESS_RESULT;
107N/A }
107N/A }
107N/A
107N/A return TagResult.OMIT_FROM_ENTRY;
107N/A }
107N/A }
107N/A}
107N/A