View Javadoc
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.parsers;
21  
22  
23  import java.io.StringReader;
24  import java.text.ParseException;
25  import java.util.List;
26  
27  import org.apache.directory.api.ldap.model.constants.MetaSchemaConstants;
28  import org.apache.directory.api.ldap.model.schema.SchemaObject;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * 
34   * TODO AbstractSchemaParser.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public abstract class AbstractSchemaParser
39  {
40  
41      /** the monitor to use for this parser */
42      protected ParserMonitor monitor = new ParserMonitorAdapter();
43  
44      /** the antlr generated parser being wrapped */
45      protected ReusableAntlrSchemaParser parser;
46  
47      /** the antlr generated lexer being wrapped */
48      protected ReusableAntlrSchemaLexer lexer;
49  
50  
51      /**
52       * Instantiates a new abstract schema parser.
53       */
54      protected AbstractSchemaParser()
55      {
56          lexer = new ReusableAntlrSchemaLexer( new StringReader( "" ) );
57          parser = new ReusableAntlrSchemaParser( lexer );
58      }
59  
60  
61      /**
62       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
63       * pair with it. param spec the specification to be parsed
64       *
65       * @param spec the spec
66       */
67      protected void reset( String spec )
68      {
69          StringReader in = new StringReader( spec );
70          lexer.prepareNextInput( in );
71          parser.resetState();
72      }
73  
74  
75      /**
76       * Sets the parser monitor.
77       * 
78       * @param parserMonitor the new parser monitor
79       */
80      public void setParserMonitor( ParserMonitor parserMonitor )
81      {
82          this.monitor = parserMonitor;
83          parser.setParserMonitor( parserMonitor );
84      }
85  
86  
87      /**
88       * Sets the quirks mode. 
89       * 
90       * If enabled the parser accepts non-numeric OIDs and some 
91       * special characters in descriptions.
92       * 
93       * @param enabled the new quirks mode
94       */
95      public void setQuirksMode( boolean enabled )
96      {
97          parser.setQuirksMode( enabled );
98      }
99  
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 }