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 /** Creates a new instance of this ifpresent tag. */ 044 public IfPresentTag() 045 { 046 attributeType = null; 047 assertionValue = null; 048 } 049 050 051 052 /** 053 * Retrieves the name for this tag. 054 * 055 * @return The name for this tag. 056 */ 057 @Override 058 public String getName() 059 { 060 return "IfPresent"; 061 } 062 063 064 065 /** 066 * Indicates whether this tag is allowed for use in the extra lines for 067 * branches. 068 * 069 * @return <CODE>true</CODE> if this tag may be used in branch definitions, 070 * or <CODE>false</CODE> if not. 071 */ 072 @Override 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 @Override 096 public void initializeForBranch(TemplateFile templateFile, Branch branch, 097 String[] arguments, int lineNumber, 098 List<LocalizableMessage> warnings) 099 throws InitializationException 100 { 101 if (arguments.length < 1 || arguments.length > 2) 102 { 103 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 104 getName(), lineNumber, 1, 2, arguments.length); 105 throw new InitializationException(message); 106 } 107 108 AttributeType t = DirectoryServer.getSchema().getAttributeType(arguments[0]); 109 if (! branch.hasAttribute(t)) 110 { 111 LocalizableMessage message = 112 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 113 throw new InitializationException(message); 114 } 115 116 if (arguments.length == 2) 117 { 118 assertionValue = arguments[1]; 119 } 120 else 121 { 122 assertionValue = null; 123 } 124 } 125 126 127 128 /** 129 * Performs any initialization for this tag that may be needed while parsing 130 * a template definition. 131 * 132 * @param templateFile The template file in which this tag is used. 133 * @param template The template in which this tag is used. 134 * @param arguments The set of arguments provided for this tag. 135 * @param lineNumber The line number on which this tag appears in the 136 * template file. 137 * @param warnings A list into which any appropriate warning messages 138 * may be placed. 139 * 140 * @throws InitializationException If a problem occurs while initializing 141 * this tag. 142 */ 143 @Override 144 public void initializeForTemplate(TemplateFile templateFile, 145 Template template, String[] arguments, 146 int lineNumber, List<LocalizableMessage> warnings) 147 throws InitializationException 148 { 149 if (arguments.length < 1 || arguments.length > 2) 150 { 151 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 152 getName(), lineNumber, 1, 2, arguments.length); 153 throw new InitializationException(message); 154 } 155 156 attributeType = DirectoryServer.getSchema().getAttributeType(arguments[0]); 157 if (! template.hasAttribute(attributeType)) 158 { 159 LocalizableMessage message = 160 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 161 throw new InitializationException(message); 162 } 163 164 if (arguments.length == 2) 165 { 166 assertionValue = arguments[1]; 167 } 168 else 169 { 170 assertionValue = null; 171 } 172 } 173 174 175 176 /** 177 * Generates the content for this tag by appending it to the provided tag. 178 * 179 * @param templateEntry The entry for which this tag is being generated. 180 * @param templateValue The template value to which the generated content 181 * should be appended. 182 * 183 * @return The result of generating content for this tag. 184 */ 185 @Override 186 public TagResult generateValue(TemplateEntry templateEntry, 187 TemplateValue templateValue) 188 { 189 List<TemplateValue> values = templateEntry.getValues(attributeType); 190 if (values == null || values.isEmpty()) 191 { 192 return TagResult.OMIT_FROM_ENTRY; 193 } 194 195 if (assertionValue == null) 196 { 197 return TagResult.SUCCESS_RESULT; 198 } 199 200 for (TemplateValue v : values) 201 { 202 if (assertionValue.equals(v.getValue().toString())) 203 { 204 return TagResult.SUCCESS_RESULT; 205 } 206 } 207 return TagResult.OMIT_FROM_ENTRY; 208 } 209}