1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.api.ldap.model.schema; 21 22 23 import java.util.List; 24 import java.util.Map; 25 26 27 /** 28 * Utility class used to generate schema object specifications. Some of the 29 * latest work coming out of the LDAPBIS working body adds optional extensions 30 * to these syntaxes. Descriptions can be generated for 31 * the following objects: 32 * <ul> 33 * <li><a href="./AttributeType.html">AttributeType</a></li> 34 * <li><a href="./DitContentRule.html">DitContentRule</a></li> 35 * <li><a href="./DitContentRule.html">DitStructureRule</a></li> 36 * <li><a href="./LdapComparator.html">Syntax</a></li> 37 * <li><a href="./MatchingRule.html">MatchingRule</a></li> 38 * <li><a href="./MatchingRuleUse.html">MatchingRuleUse</a></li> 39 * <li><a href="./NameForm.html">NameForm</a></li> 40 * <li><a href="./Normalizer.html">Syntax</a></li> 41 * <li><a href="./ObjectClass.html">ObjectClass</a></li> 42 * <li><a href="./LdapSyntax.html">Syntax</a></li> 43 * <li><a href="./SyntaxChecker.html">Syntax</a></li> 44 * </ul> 45 * 46 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 47 */ 48 public final class DescriptionUtils 49 { 50 /** 51 * Private constructor. 52 */ 53 private DescriptionUtils() 54 { 55 } 56 57 58 /** 59 * Generates the ComparatorDescription for a LdapComparator. Only the right 60 * hand side of the description starting at the opening parenthesis is 61 * generated: that is 'ComparatorDescription = ' is not generated. 62 * 63 * <pre> 64 * ComparatorDescription = "(" 65 * numericoid 66 * ["DESC" qdstring ] 67 * "FQCN" whsp fqcn 68 * ["BYTECODE" whsp base64 ] 69 * extensions 70 * ")" 71 * </pre> 72 * 73 * @param comparator 74 * the Comparator to generate the description for 75 * @return the ComparatorDescription string 76 */ 77 public static String getDescription( LdapComparator<?> comparator ) 78 { 79 return getLoadableDescription( comparator ); 80 } 81 82 83 /** 84 * Generates the NormalizerDescription for a Normalizer. Only the right 85 * hand side of the description starting at the opening parenthesis is 86 * generated: that is 'NormalizerDescription = ' is not generated. 87 * 88 * <pre> 89 * NormalizerDescription = "(" 90 * numericoid 91 * ["DESC" qdstring ] 92 * "FQCN" whsp fqcn 93 * ["BYTECODE" whsp base64 ] 94 * extensions 95 * ")" 96 * </pre> 97 * 98 * @param normalizer 99 * the Normalizer to generate the description for 100 * @return the NormalizerDescription string 101 */ 102 public static String getDescription( Normalizer normalizer ) 103 { 104 return getLoadableDescription( normalizer ); 105 } 106 107 108 /** 109 * Generates the SyntaxCheckerDescription for a SyntaxChecker. Only the right 110 * hand side of the description starting at the opening parenthesis is 111 * generated: that is 'SyntaxCheckerDescription = ' is not generated. 112 * 113 * <pre> 114 * SyntaxCheckerDescription = "(" 115 * numericoid 116 * ["DESC" qdstring ] 117 * "FQCN" whsp fqcn 118 * ["BYTECODE" whsp base64 ] 119 * extensions 120 * ")" 121 * </pre> 122 * 123 * @param syntaxChecker 124 * the SyntaxChecker to generate the description for 125 * @return the SyntaxCheckerDescription string 126 */ 127 public static String getDescription( SyntaxChecker syntaxChecker ) 128 { 129 return getLoadableDescription( syntaxChecker ); 130 } 131 132 133 private static void getExtensions( StringBuilder sb, Map<String, List<String>> extensions ) 134 { 135 for ( Map.Entry<String, List<String>> extension : extensions.entrySet() ) 136 { 137 sb.append( " " + extension.getKey() ).append( " " ); 138 139 List<String> values = extension.getValue(); 140 141 if ( ( values != null ) && ( values.size() != 0 ) ) 142 { 143 if ( values.size() == 1 ) 144 { 145 sb.append( values.get( 0 ) ); 146 } 147 else 148 { 149 boolean isFirst = true; 150 sb.append( "( " ); 151 152 for ( String value : values ) 153 { 154 if ( isFirst ) 155 { 156 isFirst = false; 157 } 158 else 159 { 160 sb.append( " " ); 161 } 162 163 sb.append( value ); 164 } 165 166 sb.append( " )" ); 167 } 168 } 169 170 sb.append( '\n' ); 171 } 172 } 173 174 175 /** 176 * Generate the description for Comparators, Normalizers and SyntaxCheckers. 177 */ 178 private static String getLoadableDescription( LoadableSchemaObject schemaObject ) 179 { 180 StringBuilder buf = new StringBuilder( "( " ); 181 buf.append( schemaObject.getOid() ); 182 buf.append( '\n' ); 183 184 if ( schemaObject.getDescription() != null ) 185 { 186 buf.append( " DESC " ); 187 buf.append( schemaObject.getDescription() ); 188 buf.append( '\n' ); 189 } 190 191 if ( schemaObject.getFqcn() != null ) 192 { 193 buf.append( " FQCN " ); 194 buf.append( schemaObject.getFqcn() ); 195 buf.append( '\n' ); 196 } 197 198 if ( schemaObject.getBytecode() != null ) 199 { 200 buf.append( " BYTECODE " ); 201 202 // We will dump only the 16 first bytes 203 if ( schemaObject.getBytecode().length() > 16 ) 204 { 205 buf.append( schemaObject.getBytecode().substring( 0, 16 ) ); 206 } 207 else 208 { 209 buf.append( schemaObject.getBytecode() ); 210 } 211 212 buf.append( '\n' ); 213 } 214 215 if ( schemaObject.getExtensions() != null ) 216 { 217 getExtensions( buf, schemaObject.getExtensions() ); 218 } 219 220 buf.append( " ) " ); 221 222 return buf.toString(); 223 } 224 }