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 *    http://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.io.StringReader;
024import java.text.ParseException;
025import java.util.List;
026
027import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
028import org.apache.directory.api.ldap.model.schema.SchemaObject;
029import org.apache.directory.api.util.Strings;
030
031
032/**
033 * 
034 * TODO AbstractSchemaParser.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public abstract class AbstractSchemaParser
039{
040
041    /** the monitor to use for this parser */
042    protected ParserMonitor monitor = new ParserMonitorAdapter();
043
044    /** the antlr generated parser being wrapped */
045    protected ReusableAntlrSchemaParser parser;
046
047    /** the antlr generated lexer being wrapped */
048    protected ReusableAntlrSchemaLexer lexer;
049
050
051    /**
052     * Instantiates a new abstract schema parser.
053     */
054    protected AbstractSchemaParser()
055    {
056        lexer = new ReusableAntlrSchemaLexer( new StringReader( "" ) );
057        parser = new ReusableAntlrSchemaParser( lexer );
058    }
059
060
061    /**
062     * Initializes the plumbing by creating a pipe and coupling the parser/lexer
063     * pair with it. param spec the specification to be parsed
064     *
065     * @param spec the spec
066     */
067    protected void reset( String spec )
068    {
069        StringReader in = new StringReader( spec );
070        lexer.prepareNextInput( in );
071        parser.resetState();
072    }
073
074
075    /**
076     * Sets the parser monitor.
077     * 
078     * @param parserMonitor the new parser monitor
079     */
080    public void setParserMonitor( ParserMonitor parserMonitor )
081    {
082        this.monitor = parserMonitor;
083        parser.setParserMonitor( parserMonitor );
084    }
085
086
087    /**
088     * Sets the quirks mode. 
089     * 
090     * If enabled the parser accepts non-numeric OIDs and some 
091     * special characters in descriptions.
092     * 
093     * @param enabled the new quirks mode
094     */
095    public void setQuirksMode( boolean enabled )
096    {
097        parser.setQuirksMode( enabled );
098    }
099
100
101    /**
102     * Checks if quirks mode is enabled.
103     * 
104     * @return true, if is quirks mode is enabled
105     */
106    public boolean isQuirksMode()
107    {
108        return parser.isQuirksMode();
109    }
110
111
112    /**
113     * Parse a SchemaObject description and returns back an instance of SchemaObject.
114     * 
115     * @param schemaDescription The SchemaObject description
116     * @return A SchemaObject instance
117     * @throws ParseException If the parsing failed
118     */
119    public abstract SchemaObject parse( String schemaDescription ) throws ParseException;
120
121
122    /**
123     * Update the schemaName for the given SchemaObject, accordingly to the X-SCHEMA parameter. If
124     * not present, default to 'other'
125     *
126     * @param schemaObject the schema object where the name should be updated
127     */
128    protected static void updateSchemaName( SchemaObject schemaObject )
129    {
130        // Update the Schema if we have the X-SCHEMA extension
131        List<String> schemaExtension = schemaObject.getExtensions().get( MetaSchemaConstants.X_SCHEMA_AT );
132
133        if ( schemaExtension != null )
134        {
135            String schemaName = schemaExtension.get( 0 );
136
137            if ( Strings.isEmpty( schemaName ) )
138            {
139                schemaObject.setSchemaName( MetaSchemaConstants.SCHEMA_OTHER );
140            }
141            else
142            {
143                schemaObject.setSchemaName( schemaName );
144            }
145        }
146        else
147        {
148            schemaObject.setSchemaName( MetaSchemaConstants.SCHEMA_OTHER );
149        }
150    }
151}