directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r485821 - in /directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser: ./ ObjectClassDescriptionSchemaParser.java
Date Mon, 11 Dec 2006 19:04:49 GMT
Author: elecharny
Date: Mon Dec 11 11:04:47 2006
New Revision: 485821

URL: http://svn.apache.org/viewvc?view=rev&rev=485821
Log:
Moved and renamed the SchemaParser to a new package dedicated to parsers.

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/ObjectClassDescriptionSchemaParser.java

Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/ObjectClassDescriptionSchemaParser.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/ObjectClassDescriptionSchemaParser.java?view=auto&rev=485821
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/ObjectClassDescriptionSchemaParser.java
(added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntax/parser/ObjectClassDescriptionSchemaParser.java
Mon Dec 11 11:04:47 2006
@@ -0,0 +1,217 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.schema.syntax.parser;
+
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.text.ParseException;
+
+import org.apache.directory.shared.ldap.schema.syntax.AntlrSchemaLexer;
+import org.apache.directory.shared.ldap.schema.syntax.AntlrSchemaParser;
+import org.apache.directory.shared.ldap.schema.syntax.ObjectClassDescription;
+
+import antlr.CharBuffer;
+import antlr.LexerSharedInputState;
+import antlr.RecognitionException;
+import antlr.TokenStream;
+import antlr.TokenStreamException;
+
+
+/**
+ * A parser for RFC 4512 schema objects
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ObjectClassDescriptionSchemaParser
+{
+
+    /** the antlr generated parser being wrapped */
+    private ReusableAntlrSchemaParser parser;
+
+    /** the antlr generated lexer being wrapped */
+    private ReusableAntlrSchemaLexer lexer;
+    
+    static
+    {
+    }
+
+
+    /**
+     * Creates a schema parser instance.
+     */
+    public ObjectClassDescriptionSchemaParser()
+    {
+        lexer = new ReusableAntlrSchemaLexer( new StringReader( "" ) );
+        parser = new ReusableAntlrSchemaParser( lexer );
+    }
+    
+    /**
+     * Initializes the plumbing by creating a pipe and coupling the parser/lexer
+     * pair with it. param spec the specification to be parsed
+     */
+    private void reset( String spec )
+    {
+        StringReader in = new StringReader( spec );
+        lexer.prepareNextInput( in );
+        parser.resetState();
+    }
+
+
+    /**
+     * Parses a object class definition according to RFC 4512:
+     * 
+     * <pre>
+     * ObjectClassDescription = LPAREN WSP
+     *     numericoid                 ; object identifier
+     *     [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
+     *     [ SP "DESC" SP qdstring ]  ; description
+     *     [ SP "OBSOLETE" ]          ; not active
+     *     [ SP "SUP" SP oids ]       ; superior object classes
+     *     [ SP kind ]                ; kind of class
+     *     [ SP "MUST" SP oids ]      ; attribute types
+     *     [ SP "MAY" SP oids ]       ; attribute types
+     *     extensions WSP RPAREN
+     *
+     * kind = "ABSTRACT" / "STRUCTURAL" / "AUXILIARY"
+     * 
+     * extensions = *( SP xstring SP qdstrings )
+     * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
+     * </pre>
+     * 
+     * @param objectClassDescription the object class description to be parsed
+     * @return the parsed ObjectClassDescription bean
+     * @throws ParseException if there are any recognition errors (bad syntax)
+     */
+    public synchronized ObjectClassDescription parseObjectClassDescription( String objectClassDescription
)
+        throws ParseException
+    {
+
+        if ( objectClassDescription == null )
+        {
+            throw new ParseException( "Null", 0 );
+        }
+
+        reset( objectClassDescription ); // reset and initialize the parser / lexer pair
+
+        try
+        {
+            ObjectClassDescription ocd = parser.objectClassDescription();
+            return ocd;
+        }
+        catch ( RecognitionException re )
+        {
+            String msg = "Parser failure on object class description:\n\t" + objectClassDescription;
+            msg += "\nAntlr message: " + re.getMessage();
+            msg += "\nAntlr column: " + re.getColumn();
+            throw new ParseException( msg, re.getColumn() );
+        }
+        catch ( TokenStreamException tse )
+        {
+            String msg = "Parser failure on object class description:\n\t" + objectClassDescription;
+            msg += "\nAntlr message: " + tse.getMessage();
+            throw new ParseException( msg, 0 );
+        }
+
+    }
+
+    /**
+     * A reusable lexer class extended from antlr generated lexer for an LDAP
+     * schema as defined in RFC 4512. This class
+     * enables the reuse of the antlr lexer without having to recreate the it every
+     * time as stated in <a
+     * href="http://www.antlr.org:8080/pipermail/antlr-interest/2003-April/003631.html">
+     * a Antlr Interest Group mail</a> .
+     * 
+     * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
+     */
+    private static class ReusableAntlrSchemaLexer extends AntlrSchemaLexer
+    {
+        private boolean savedCaseSensitive;
+
+        private boolean savedCaseSensitiveLiterals;
+
+
+        /**
+         * Creates a ReusableAntlrSchemaLexer instance.
+         * 
+         * @param in
+         *            the input to the lexer
+         */
+        public ReusableAntlrSchemaLexer( Reader in )
+        {
+            super( in );
+            savedCaseSensitive = getCaseSensitive();
+            savedCaseSensitiveLiterals = getCaseSensitiveLiterals();
+        }
+
+
+        /**
+         * Resets the state of an antlr lexer and initializes it with new input.
+         * 
+         * @param in
+         *            the input to the lexer
+         */
+        public void prepareNextInput( Reader in )
+        {
+            CharBuffer buf = new CharBuffer( in );
+            LexerSharedInputState state = new LexerSharedInputState( buf );
+            this.setInputState( state );
+
+            this.setCaseSensitive( savedCaseSensitive );
+
+            // no set method for this protected field.
+            this.caseSensitiveLiterals = savedCaseSensitiveLiterals;
+        }
+    }
+
+    /**
+     * A reusable parser class extended from antlr generated parser for an LDAP
+     * schema as defined in RFC 4512. This class
+     * enables the reuse of the antlr parser without having to recreate the it every
+     * time as stated in <a
+     * href="http://www.antlr.org:8080/pipermail/antlr-interest/2003-April/003631.html">
+     * a Antlr Interest Group mail</a> .
+     * 
+     * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
+     */
+    private static class ReusableAntlrSchemaParser extends AntlrSchemaParser
+    {
+        /**
+         * Creates a ReusableAntlrSchemaParser instance.
+         */
+        public ReusableAntlrSchemaParser( TokenStream lexer )
+        {
+            super( lexer );
+        }
+
+
+        /**
+         * Resets the state of an antlr parser.
+         */
+        public void resetState()
+        {
+            // no set method for this protected field.
+            this.traceDepth = 0;
+
+            this.getInputState().reset();
+        }
+    }
+}



Mime
View raw message