Author: akarasulu
Date: Sun Oct 10 00:37:15 2004
New Revision: 54223
Added:
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java
(contents, props changed)
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java
(contents, props changed)
Modified:
incubator/directory/eve/trunk/backend/tools/src/antlr/openldap.g
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/OpenLdapSchemaParserTest.java
Log:
Commit changes ...
o added do nothing adapter for monitor
o added reusable thread safe harness for antlr generated parser
o changed test case to use reusable harness
o added terminator "END" to grammar to return without closing the harness pipe
Notes ...
o must use parser harness to handle silent terminator injection
o we need a stream based pump into harness rather than using a String to parse
o need to implement a reset() method to clear parsed objects from maps
Modified: incubator/directory/eve/trunk/backend/tools/src/antlr/openldap.g
==============================================================================
--- incubator/directory/eve/trunk/backend/tools/src/antlr/openldap.g
(original)
+++ incubator/directory/eve/trunk/backend/tools/src/antlr/openldap.g Sun Oct
10 00:37:15 2004
@@ -166,7 +166,7 @@
{
}
:
- ( attributeType )*
+ ( attributeType | objectClass )* "END"
;
Added:
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java
==============================================================================
--- (empty file)
+++
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java
Sun Oct 10 00:37:15 2004
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.eve.tools.schema;
+
+
+import org.apache.ldap.common.util.ExceptionUtils;
+import org.apache.ldap.common.NotImplementedException;
+
+import java.io.PipedOutputStream;
+import java.io.IOException;
+import java.io.PipedInputStream;
+import java.text.ParseException;
+import java.util.Map;
+
+import antlr.RecognitionException;
+import antlr.TokenStreamException;
+
+
+/**
+ * A reusable wrapper for antlr generated OpenLDAP schema parsers.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class OpenLdapSchemaParser
+{
+ private ParserMonitor monitor = new ParserMonitorAdapter();
+ /** The antlr generated parser */
+ private antlrOpenLdapSchemaParser parser = null;
+ /** A pipe into the parser */
+ private PipedOutputStream parserIn = null;
+
+
+ /**
+ * Creates a reusable instance of an OpenLdapSchemaParser.
+ *
+ * @throws IOException if the pipe cannot be formed
+ */
+ public OpenLdapSchemaParser() throws IOException
+ {
+ init();
+ }
+
+
+ /**
+ * Initializes a parser and its plumbing.
+ *
+ * @throws IOException if a pipe cannot be formed.
+ */
+ public void init() throws IOException
+ {
+ parserIn = new PipedOutputStream();
+ PipedInputStream in = new PipedInputStream();
+ parserIn.connect( in );
+ antlrOpenLdapSchemaLexer lexer = new antlrOpenLdapSchemaLexer( in );
+ parser = new antlrOpenLdapSchemaParser( lexer );
+ }
+
+
+ public void reset()
+ {
+ throw new NotImplementedException( "impl to clear all parsed objects"
);
+ }
+
+
+ public Map getAttributeTypes()
+ {
+ return parser.getAttributeTypes();
+ }
+
+
+ public Map getObjectClassTypes()
+ {
+ return parser.getObjectClasses();
+ }
+
+
+ /**
+ * Thread safe method parses an OpenLDAP schema file.
+ */
+ public synchronized void parse( String schema )
+ throws IOException, ParseException
+ {
+ if ( schema == null || schema.trim().equals( "" ) )
+ {
+ throw new ParseException( "The schema is either null or is "
+ + "the empty String!", 0 );
+ }
+
+ if ( null == monitor )
+ {
+ monitor = new ParserMonitorAdapter();
+ }
+
+ parserIn.write( schema.getBytes() );
+
+ // using an input termination token END - need extra space to return
+ parserIn.write( "END ".getBytes() );
+ parserIn.flush();
+
+ try
+ {
+ parser.parseSchema();
+ }
+ catch ( RecognitionException e )
+ {
+ String msg = "Parser failure on schema:\n\t" + schema ;
+ msg += "\nAntlr exception trace:\n" +
ExceptionUtils.getFullStackTrace( e );
+ init();
+ throw new ParseException( msg, e.getColumn() );
+ }
+ catch ( TokenStreamException e2 )
+ {
+ String msg = "Parser failure on schema:\n\t" + schema ;
+ msg += "\nAntlr exception trace:\n" +
ExceptionUtils.getFullStackTrace( e2 );
+ init();
+ throw new ParseException( msg, 0 );
+ }
+ }
+
+
+ public void setParserMonitor( ParserMonitor monitor )
+ {
+ this.monitor = monitor ;
+ this.parser.setParserMonitor( monitor );
+ }
+}
Added:
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java
==============================================================================
--- (empty file)
+++
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java
Sun Oct 10 00:37:15 2004
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.eve.tools.schema;
+
+/**
+ * Document me.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ParserMonitorAdapter implements ParserMonitor
+{
+ public void matchedProduction( String prod )
+ {
+ }
+}
Modified:
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/OpenLdapSchemaParserTest.java
==============================================================================
---
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/OpenLdapSchemaParserTest.java
(original)
+++
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/OpenLdapSchemaParserTest.java
Sun Oct 10 00:37:15 2004
@@ -17,30 +17,27 @@
package org.apache.eve.tools.schema;
-import junit.framework.TestCase;
-
import java.util.Map;
-import java.io.ByteArrayInputStream;
+
+import junit.framework.TestCase;
/**
- * Tests the parser for AttributeTypes.
+ * Tests the OpenLDAP schema parser.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
* @version $Rev$
*/
public class OpenLdapSchemaParserTest extends TestCase
{
- public void testParser() throws Exception
+ private OpenLdapSchemaParser parser;
+
+
+ protected void setUp() throws Exception
{
- String attributeTypeData = "# adding a comment \n" +
- "attributetype ( 2.5.4.2 NAME 'knowledgeInformation'\n" +
- " DESC 'RFC2256: knowledge information'\n" +
- " EQUALITY caseIgnoreMatch\n" +
- " SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )";
- ByteArrayInputStream in = new ByteArrayInputStream(
attributeTypeData.getBytes() );
- antlrOpenLdapSchemaLexer lexer = new antlrOpenLdapSchemaLexer( in );
- antlrOpenLdapSchemaParser parser = new antlrOpenLdapSchemaParser(
lexer );
+ super.setUp();
+
+ parser = new OpenLdapSchemaParser();
parser.setParserMonitor( new ParserMonitor()
{
public void matchedProduction( String prod )
@@ -48,9 +45,24 @@
System.out.println( prod );
}
});
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ parser = null;
+ }
+ public void testParser() throws Exception
+ {
+ String attributeTypeData = "# adding a comment \n" +
+ "attributetype ( 2.5.4.2 NAME 'knowledgeInformation'\n" +
+ " DESC 'RFC2256: knowledge information'\n" +
+ " EQUALITY caseIgnoreMatch\n" +
+ " SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} )";
+ parser.parse( attributeTypeData );
Map attributeTypes = parser.getAttributeTypes();
- parser.parseSchema();
AttributeTypeLiteral type = ( AttributeTypeLiteral )
attributeTypes.get( "2.5.4.2" );
assertNotNull( type );