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 * https://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.parsers; 21 22 23 import java.text.ParseException; 24 import java.util.List; 25 import java.util.Map; 26 27 import org.apache.directory.api.i18n.I18n; 28 import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants; 29 import org.apache.directory.api.ldap.model.schema.SchemaObject; 30 import org.apache.directory.api.ldap.model.schema.syntaxCheckers.OpenLdapObjectIdentifierMacro; 31 import org.apache.directory.api.util.Strings; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 36 /** 37 * Base class of all schema parsers. 38 * 39 * @param <T> The type of SchemaObject 40 * 41 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 42 */ 43 public abstract class AbstractSchemaParser<T extends SchemaObject> 44 { 45 /** The LoggerFactory used by this class */ 46 protected static final Logger LOG = LoggerFactory.getLogger( AbstractSchemaParser.class ); 47 48 /** The fast schemaObject parser */ 49 protected OpenLdapSchemaParser fastParser; 50 51 /** 52 * Instantiates a new abstract schema parser. 53 * 54 * @param schemaObjectType The Schema object type 55 * @param errorCodeOnNull error code used when schema element is null 56 * @param errorCodeOnParseExceptionWithPosition error code used on parse error when position is known 57 * @param errorCodeOnParseException error code used on parse error when position is unknown 58 */ 59 protected AbstractSchemaParser( Class<T> schemaObjectType, I18n errorCodeOnNull, 60 I18n errorCodeOnParseExceptionWithPosition, 61 I18n errorCodeOnParseException ) 62 { 63 fastParser = new OpenLdapSchemaParser(); 64 } 65 66 67 68 /** 69 * Sets the quirks mode. 70 * 71 * If enabled the parser accepts non-numeric OIDs and some 72 * special characters in descriptions. 73 * 74 * @param enabled the new quirks mode 75 */ 76 public void setQuirksMode( boolean enabled ) 77 { 78 fastParser.setQuirksMode( enabled ); 79 } 80 81 82 /** 83 * Checks if quirks mode is enabled. 84 * 85 * @return true, if is quirks mode is enabled 86 */ 87 public boolean isQuirksMode() 88 { 89 return fastParser.isQuirksMode(); 90 } 91 92 93 /** 94 * Parse a SchemaObject description and returns back an instance of SchemaObject. 95 * 96 * @param schemaDescription The SchemaObject description 97 * @return A SchemaObject instance 98 * @throws ParseException If the parsing failed 99 */ 100 public abstract T parse( String schemaDescription ) throws ParseException; 101 102 103 /** 104 * Update the schemaName for the given SchemaObject, accordingly to the X-SCHEMA parameter. If 105 * not present, default to 'other' 106 * 107 * @param schemaObject the schema object where the name should be updated 108 */ 109 protected void updateSchemaName( SchemaObject schemaObject ) 110 { 111 // Update the Schema if we have the X-SCHEMA extension 112 List<String> schemaExtension = schemaObject.getExtension( MetaSchemaConstants.X_SCHEMA_AT ); 113 114 if ( schemaExtension != null ) 115 { 116 String schemaName = schemaExtension.get( 0 ); 117 118 if ( Strings.isEmpty( schemaName ) ) 119 { 120 schemaObject.setSchemaName( MetaSchemaConstants.SCHEMA_OTHER ); 121 } 122 else 123 { 124 schemaObject.setSchemaName( schemaName ); 125 } 126 } 127 else 128 { 129 schemaObject.setSchemaName( MetaSchemaConstants.SCHEMA_OTHER ); 130 } 131 } 132 133 134 /** 135 * Get the defined macros. 136 * 137 * @return The map of defined macros 138 */ 139 public Map<String, OpenLdapObjectIdentifierMacro> getObjectIdentifiers() 140 { 141 return fastParser.getObjectIdentifierMacros(); 142 } 143 }