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-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.tools.makeldif; 018 019import java.util.List; 020 021import org.forgerock.i18n.LocalizableMessage; 022import org.forgerock.opendj.ldap.schema.AttributeType; 023import org.opends.server.core.DirectoryServer; 024import org.opends.server.types.InitializationException; 025 026import static org.opends.messages.ToolMessages.*; 027 028/** 029 * This class defines a tag that is used to base presence of one attribute on 030 * the presence of another attribute and/or attribute value. 031 */ 032public class IfPresentTag 033 extends Tag 034{ 035 /** The attribute type for which to make the determination. */ 036 private AttributeType attributeType; 037 038 /** The value for which to make the determination. */ 039 private String assertionValue; 040 041 042 043 /** 044 * Creates a new instance of this ifpresent tag. 045 */ 046 public IfPresentTag() 047 { 048 attributeType = null; 049 assertionValue = null; 050 } 051 052 053 054 /** 055 * Retrieves the name for this tag. 056 * 057 * @return The name for this tag. 058 */ 059 public String getName() 060 { 061 return "IfPresent"; 062 } 063 064 065 066 /** 067 * Indicates whether this tag is allowed for use in the extra lines for 068 * branches. 069 * 070 * @return <CODE>true</CODE> if this tag may be used in branch definitions, 071 * or <CODE>false</CODE> if not. 072 */ 073 public boolean allowedInBranch() 074 { 075 return true; 076 } 077 078 079 080 /** 081 * Performs any initialization for this tag that may be needed while parsing 082 * a branch definition. 083 * 084 * @param templateFile The template file in which this tag is used. 085 * @param branch The branch in which this tag is used. 086 * @param arguments The set of arguments provided for this tag. 087 * @param lineNumber The line number on which this tag appears in the 088 * template file. 089 * @param warnings A list into which any appropriate warning messages 090 * may be placed. 091 * 092 * @throws InitializationException If a problem occurs while initializing 093 * this tag. 094 */ 095 public void initializeForBranch(TemplateFile templateFile, Branch branch, 096 String[] arguments, int lineNumber, 097 List<LocalizableMessage> warnings) 098 throws InitializationException 099 { 100 if (arguments.length < 1 || arguments.length > 2) 101 { 102 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 103 getName(), lineNumber, 1, 2, arguments.length); 104 throw new InitializationException(message); 105 } 106 107 AttributeType t = DirectoryServer.getAttributeType(arguments[0]); 108 if (! branch.hasAttribute(t)) 109 { 110 LocalizableMessage message = 111 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 112 throw new InitializationException(message); 113 } 114 115 if (arguments.length == 2) 116 { 117 assertionValue = arguments[1]; 118 } 119 else 120 { 121 assertionValue = null; 122 } 123 } 124 125 126 127 /** 128 * Performs any initialization for this tag that may be needed while parsing 129 * a template definition. 130 * 131 * @param templateFile The template file in which this tag is used. 132 * @param template The template in which this tag is used. 133 * @param arguments The set of arguments provided for this tag. 134 * @param lineNumber The line number on which this tag appears in the 135 * template file. 136 * @param warnings A list into which any appropriate warning messages 137 * may be placed. 138 * 139 * @throws InitializationException If a problem occurs while initializing 140 * this tag. 141 */ 142 public void initializeForTemplate(TemplateFile templateFile, 143 Template template, String[] arguments, 144 int lineNumber, List<LocalizableMessage> warnings) 145 throws InitializationException 146 { 147 if (arguments.length < 1 || arguments.length > 2) 148 { 149 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 150 getName(), lineNumber, 1, 2, arguments.length); 151 throw new InitializationException(message); 152 } 153 154 attributeType = DirectoryServer.getAttributeType(arguments[0]); 155 if (! template.hasAttribute(attributeType)) 156 { 157 LocalizableMessage message = 158 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 159 throw new InitializationException(message); 160 } 161 162 if (arguments.length == 2) 163 { 164 assertionValue = arguments[1]; 165 } 166 else 167 { 168 assertionValue = null; 169 } 170 } 171 172 173 174 /** 175 * Generates the content for this tag by appending it to the provided tag. 176 * 177 * @param templateEntry The entry for which this tag is being generated. 178 * @param templateValue The template value to which the generated content 179 * should be appended. 180 * 181 * @return The result of generating content for this tag. 182 */ 183 public TagResult generateValue(TemplateEntry templateEntry, 184 TemplateValue templateValue) 185 { 186 List<TemplateValue> values = templateEntry.getValues(attributeType); 187 if (values == null || values.isEmpty()) 188 { 189 return TagResult.OMIT_FROM_ENTRY; 190 } 191 192 if (assertionValue == null) 193 { 194 return TagResult.SUCCESS_RESULT; 195 } 196 197 for (TemplateValue v : values) 198 { 199 if (assertionValue.equals(v.getValue().toString())) 200 { 201 return TagResult.SUCCESS_RESULT; 202 } 203 } 204 return TagResult.OMIT_FROM_ENTRY; 205 } 206}