You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/10/10 10:37:54 UTC
svn commit: rev 54228 - in incubator/directory/eve/trunk/backend/tools/src: java/org/apache/eve/tools/schema test/org/apache/eve/tools/schema
Author: akarasulu
Date: Sun Oct 10 01:37:53 2004
New Revision: 54228
Added:
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/ConsoleParserMonitor.java (contents, props changed)
Modified:
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitor.java
incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java
incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/OpenLdapSchemaParserTest.java
Log:
Commit changes ...
o added a simple console logging monitor for unit tests
o added methods to detect parse session starts and stops
o created additional parse() overloads for parsing on streams and files
o refactored (consolidated) common parse() code shared by overloaded methods
Modified: incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java
==============================================================================
--- incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java (original)
+++ incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/OpenLdapSchemaParser.java Sun Oct 10 01:37:53 2004
@@ -20,10 +20,8 @@
import org.apache.ldap.common.util.ExceptionUtils;
import java.util.Map;
-import java.io.IOException;
-import java.io.PipedInputStream;
import java.text.ParseException;
-import java.io.PipedOutputStream;
+import java.io.*;
import antlr.RecognitionException;
import antlr.TokenStreamException;
@@ -37,6 +35,9 @@
*/
public class OpenLdapSchemaParser
{
+ /** a buffer to use while streaming data into the parser */
+ private byte[] buf = new byte[128];
+ /** the monitor to use for this parser */
private ParserMonitor monitor = new ParserMonitorAdapter();
/** The antlr generated parser */
private antlrOpenLdapSchemaParser parser = null;
@@ -89,45 +90,85 @@
/**
- * Thread safe method parses an OpenLDAP schema file.
+ * Thread safe method parses an OpenLDAP schemaObject element/object.
+ *
+ * @param schemaObject the String image of a complete schema object
*/
- public synchronized void parse( String schema ) throws IOException, ParseException
+ public synchronized void parse( String schemaObject ) throws IOException, ParseException
{
- if ( schema == null || schema.trim().equals( "" ) )
+ if ( schemaObject == null || schemaObject.trim().equals( "" ) )
{
- throw new ParseException( "The schema is either null or is "
+ throw new ParseException( "The schemaObject is either null or is "
+ "the empty String!", 0 );
}
- if ( null == monitor )
- {
- monitor = new ParserMonitorAdapter();
- }
+ parserIn.write( schemaObject.getBytes() );
+ invokeParser( schemaObject );
+ }
- parserIn.write( schema.getBytes() );
+ private void invokeParser( String subject ) throws IOException, ParseException
+ {
// using an input termination token END - need extra space to return
parserIn.write( "END ".getBytes() );
parserIn.flush();
try
{
+ monitor.startedParse( "starting parse ..." );
parser.parseSchema();
+ monitor.finishedParse( "Done parsing!" );
}
catch ( RecognitionException e )
{
- String msg = "Parser failure on schema:\n\t" + schema ;
+ String msg = "Parser failure on:\n\t" + subject ;
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 ;
+ String msg = "Parser failure on:\n\t" + subject ;
msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e2 );
init();
throw new ParseException( msg, 0 );
}
+ }
+
+
+ /**
+ * Thread safe method parses a stream of OpenLDAP schemaObject elements/objects.
+ *
+ * @param schemaIn a stream of schema objects
+ */
+ public synchronized void parse( InputStream schemaIn ) throws IOException, ParseException
+ {
+ int count = -1;
+ while ( ( count = schemaIn.read( buf ) ) != -1 )
+ {
+ parserIn.write( buf, 0, count );
+ }
+
+ invokeParser( "schema input stream ==> " + schemaIn.toString() );
+ }
+
+
+ /**
+ * Thread safe method parses a file of OpenLDAP schemaObject elements/objects.
+ *
+ * @param schemaFile a file of schema objects
+ */
+ public synchronized void parse( File schemaFile ) throws IOException, ParseException
+ {
+ FileInputStream schemaIn = new FileInputStream( schemaFile );
+
+ int count = -1;
+ while ( ( count = schemaIn.read( buf ) ) != -1 )
+ {
+ parserIn.write( buf, 0, count );
+ }
+
+ invokeParser( "schema file ==> " + schemaFile.getAbsolutePath() );
}
Modified: incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitor.java
==============================================================================
--- incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitor.java (original)
+++ incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitor.java Sun Oct 10 01:37:53 2004
@@ -25,4 +25,8 @@
public interface ParserMonitor
{
void matchedProduction( String prod );
+
+ void startedParse( String s );
+
+ void finishedParse( String s );
}
Modified: incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java
==============================================================================
--- incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java (original)
+++ incubator/directory/eve/trunk/backend/tools/src/java/org/apache/eve/tools/schema/ParserMonitorAdapter.java Sun Oct 10 01:37:53 2004
@@ -27,4 +27,12 @@
public void matchedProduction( String prod )
{
}
+
+ public void startedParse( String s )
+ {
+ }
+
+ public void finishedParse( String s )
+ {
+ }
}
Added: incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/ConsoleParserMonitor.java
==============================================================================
--- (empty file)
+++ incubator/directory/eve/trunk/backend/tools/src/test/org/apache/eve/tools/schema/ConsoleParserMonitor.java Sun Oct 10 01:37:53 2004
@@ -0,0 +1,41 @@
+/*
+ * 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:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ConsoleParserMonitor implements ParserMonitor
+{
+ public void matchedProduction( String prod )
+ {
+ System.out.println( prod );
+ }
+
+ public void startedParse( String s )
+ {
+ System.out.println( s );
+ }
+
+ public void finishedParse( String s )
+ {
+ System.out.println( s );
+ }
+}
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 01:37:53 2004
@@ -39,13 +39,7 @@
super.setUp();
parser = new OpenLdapSchemaParser();
- parser.setParserMonitor( new ParserMonitor()
- {
- public void matchedProduction( String prod )
- {
- System.out.println( prod );
- }
- });
+ parser.setParserMonitor( new ConsoleParserMonitor() );
}