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