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