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 absence of another attribute and/or attribute value. 031 */ 032public class IfAbsentTag 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 ifabsent tag. */ 044 public IfAbsentTag() 045 { 046 attributeType = null; 047 assertionValue = null; 048 } 049 050 @Override 051 public String getName() 052 { 053 return "IfAbsent"; 054 } 055 056 @Override 057 public boolean allowedInBranch() 058 { 059 return true; 060 } 061 062 @Override 063 public void initializeForBranch(TemplateFile templateFile, Branch branch, 064 String[] arguments, int lineNumber, 065 List<LocalizableMessage> warnings) 066 throws InitializationException 067 { 068 if (arguments.length < 1 || arguments.length > 2) 069 { 070 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 071 getName(), lineNumber, 1, 2, arguments.length); 072 throw new InitializationException(message); 073 } 074 075 AttributeType t = DirectoryServer.getSchema().getAttributeType(arguments[0]); 076 if (! branch.hasAttribute(t)) 077 { 078 LocalizableMessage message = 079 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 080 throw new InitializationException(message); 081 } 082 083 if (arguments.length == 2) 084 { 085 assertionValue = arguments[1]; 086 } 087 else 088 { 089 assertionValue = null; 090 } 091 } 092 093 @Override 094 public void initializeForTemplate(TemplateFile templateFile, 095 Template template, String[] arguments, 096 int lineNumber, List<LocalizableMessage> warnings) 097 throws InitializationException 098 { 099 if (arguments.length < 1 || arguments.length > 2) 100 { 101 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 102 getName(), lineNumber, 1, 2, arguments.length); 103 throw new InitializationException(message); 104 } 105 106 attributeType = DirectoryServer.getSchema().getAttributeType(arguments[0]); 107 if (! template.hasAttribute(attributeType)) 108 { 109 LocalizableMessage message = 110 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 111 throw new InitializationException(message); 112 } 113 114 115 if (arguments.length == 2) 116 { 117 assertionValue = arguments[1]; 118 } 119 else 120 { 121 assertionValue = null; 122 } 123 } 124 125 @Override 126 public TagResult generateValue(TemplateEntry templateEntry, 127 TemplateValue templateValue) 128 { 129 List<TemplateValue> values = templateEntry.getValues(attributeType); 130 if (values == null || values.isEmpty()) 131 { 132 return TagResult.SUCCESS_RESULT; 133 } 134 135 if (assertionValue == null) 136 { 137 return TagResult.OMIT_FROM_ENTRY; 138 } 139 140 for (TemplateValue v : values) 141 { 142 if (assertionValue.equals(v.getValue().toString())) 143 { 144 return TagResult.OMIT_FROM_ENTRY; 145 } 146 } 147 return TagResult.SUCCESS_RESULT; 148 } 149}