001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.schema.parsers;
021
022
023import java.text.ParseException;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
029import org.apache.directory.api.ldap.model.schema.SchemaObject;
030import org.apache.directory.api.ldap.model.schema.syntaxCheckers.OpenLdapObjectIdentifierMacro;
031import org.apache.directory.api.util.Strings;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035
036/**
037 * Base class of all schema parsers.
038 * 
039 * @param <T> The type of SchemaObject
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public abstract class AbstractSchemaParser<T extends SchemaObject>
044{
045    /** The LoggerFactory used by this class */
046    protected static final Logger LOG = LoggerFactory.getLogger( AbstractSchemaParser.class );
047
048    /** The fast schemaObject parser */
049    protected OpenLdapSchemaParser fastParser;
050
051    /**
052     * Instantiates a new abstract schema parser.
053     * 
054     * @param schemaObjectType The Schema object type
055     * @param errorCodeOnNull error code used when schema element is null
056     * @param errorCodeOnParseExceptionWithPosition error code used on parse error when position is known
057     * @param errorCodeOnParseException error code used on parse error when position is unknown
058     */
059    protected AbstractSchemaParser( Class<T> schemaObjectType, I18n errorCodeOnNull,
060        I18n errorCodeOnParseExceptionWithPosition,
061        I18n errorCodeOnParseException )
062    {
063        fastParser = new OpenLdapSchemaParser();
064    }
065
066
067
068    /**
069     * Sets the quirks mode. 
070     * 
071     * If enabled the parser accepts non-numeric OIDs and some 
072     * special characters in descriptions.
073     * 
074     * @param enabled the new quirks mode
075     */
076    public void setQuirksMode( boolean enabled )
077    {
078        fastParser.setQuirksMode( enabled );
079    }
080
081
082    /**
083     * Checks if quirks mode is enabled.
084     * 
085     * @return true, if is quirks mode is enabled
086     */
087    public boolean isQuirksMode()
088    {
089        return fastParser.isQuirksMode();
090    }
091
092
093    /**
094     * Parse a SchemaObject description and returns back an instance of SchemaObject.
095     * 
096     * @param schemaDescription The SchemaObject description
097     * @return A SchemaObject instance
098     * @throws ParseException If the parsing failed
099     */
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}