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 2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import static org.forgerock.util.Reject.*;
020import static org.opends.server.util.ServerConstants.*;
021
022import java.util.LinkedHashMap;
023import java.util.LinkedList;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * This class defines a data structure for storing and interacting
029 * with an ldap syntax, which defines the custom ldap syntaxes.
030 */
031@org.opends.server.types.PublicAPI(
032     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
033     mayInstantiate=false,
034     mayExtend=false,
035     mayInvoke=true)
036
037public final class LDAPSyntaxDescription
038       implements SchemaFileElement
039{
040  /**
041   * The set of additional name-value pairs associated with this ldap
042   * syntax definition.
043   */
044  private final Map<String,List<String>> extraProperties;
045
046  /** The definition string used to create this ldap syntax description. */
047  private final String definition;
048
049  /** The OID of the enclosed ldap syntax description. */
050  private final String oid;
051
052  /**
053   * Creates a new ldap syntax definition with the provided information.
054   *
055   * @param definition
056   *          The definition string used to create this ldap syntax. It must not be {@code null}.
057   * @param oid
058   *          oid of the syntax
059   * @param extraProperties
060   *          A set of extra properties for this ldap syntax description.
061   */
062  public LDAPSyntaxDescription(String definition, String oid, Map<String,List<String>> extraProperties)
063  {
064    ifNull(definition, oid);
065
066    this.oid = oid;
067
068    int schemaFilePos = definition.indexOf(SCHEMA_PROPERTY_FILENAME);
069    if (schemaFilePos > 0)
070    {
071      String defStr;
072      try
073      {
074        int firstQuotePos = definition.indexOf('\'', schemaFilePos);
075        int secondQuotePos = definition.indexOf('\'', firstQuotePos+1);
076
077        defStr = definition.substring(0, schemaFilePos).trim() + " "
078                 + definition.substring(secondQuotePos+1).trim();
079      }
080      catch (Exception e)
081      {
082        defStr = definition;
083      }
084
085      this.definition = defStr;
086    }
087    else
088    {
089      this.definition = definition;
090    }
091
092    if (extraProperties == null || extraProperties.isEmpty())
093    {
094      this.extraProperties = new LinkedHashMap<>(0);
095    }
096    else
097    {
098      this.extraProperties = new LinkedHashMap<>(extraProperties);
099    }
100  }
101
102  /**
103   * Returns the oid.
104   *
105   * @return the oid
106   */
107  public String getOID()
108  {
109    return oid;
110  }
111
112
113    /**
114   * Retrieves a mapping between the names of any extra non-standard
115   * properties that may be associated with this ldap syntax
116   * description and the value for that property.
117   *
118   * @return  A mapping between the names of any extra non-standard
119   *          properties that may be associated with this ldap syntax
120   *          description and the value for that property.
121   */
122  @Override
123  public Map<String,List<String>> getExtraProperties()
124  {
125    return extraProperties;
126  }
127
128
129
130  /**
131   * Retrieves the value of the specified "extra" property for this
132   * ldap syntax description.
133   *
134   * @param  propertyName  The name of the "extra" property for which
135   *                       to retrieve the value.
136   *
137   * @return  The value of the specified "extra" property for this
138   *          ldap syntax description, or {@code null} if no such
139   *          property is defined.
140   */
141  public List<String> getExtraProperty(String propertyName)
142  {
143    return extraProperties.get(propertyName);
144  }
145
146
147
148  /**
149   * Specifies the provided "extra" property for this ldap syntax
150   * description.
151   *
152   * @param  name    The name for the "extra" property.  It must not
153   *                 be {@code null}.
154   * @param  values  The set of value for the "extra" property, or
155   *                 {@code null} if the property is to be removed.
156   */
157  public void setExtraProperty(String name, List<String> values)
158  {
159    ifNull(name);
160
161    if (values == null || values.isEmpty())
162    {
163      extraProperties.remove(name);
164    }
165    else
166    {
167      LinkedList<String> valuesCopy = new LinkedList<>(values);
168      extraProperties.put(name, valuesCopy);
169    }
170  }
171
172
173
174  /**
175   * Indicates whether the provided object is equal to this ldap
176   * syntax. The object will be considered equal if it is a ldap
177   * syntax with the same OID as the current ldap syntax description.
178   *
179   * @param  o  The object for which to make the determination.
180   *
181   * @return  {@code true} if the provided object is equal to this
182   *          ldap syntax description, or {@code true} if not.
183   */
184  @Override
185  public boolean equals(Object o)
186  {
187    if (this == o)
188    {
189      return true;
190    }
191    if (!(o instanceof LDAPSyntaxDescription))
192    {
193      return false;
194    }
195    return oid.equals(((LDAPSyntaxDescription) o).oid);
196  }
197
198
199
200  /**
201   * Retrieves the hash code for this ldap syntax description.  It
202   * will be  based on the sum of the bytes of the OID.
203   *
204   * @return  The hash code for this ldap syntax description.
205   */
206  @Override
207  public int hashCode()
208  {
209    int oidLength = oid.length();
210    int hashCode  = 0;
211    for (int i=0; i < oidLength; i++)
212    {
213      hashCode += oid.charAt(i);
214    }
215
216    return hashCode;
217  }
218
219
220
221   /**
222   * Retrieves the string representation of this ldap syntax
223   * description in the form specified in RFC 2252.
224   *
225   * @return  The string representation of this ldap syntax in the
226    *             form specified in RFC 2252.
227   */
228  @Override
229  public String toString()
230  {
231    return definition;
232  }
233
234}