Hello,

Please find attached an email with code that I contributed to Jena in 2007. I 
would like to re-contribute that code to the Apache project Jena.

Cheers,
Olaf

----------  Forwarded Message  ----------

Subject: ARQ - registry for parsers
Date: Sunday 30 December 2007, 19:29:39
From: Olaf Hartig <[email protected]>
To: "Seaborne, Andy" <[email protected]>

Hello Andy,

For my extension I added new clauses to the query language. Therefore, I need 
an extended parser. Since it was impossible to register other parsers I 
developed a parser registry for ARQ. Attached to this mail you find the 
following three new Java files.

* ParserRegistry.java - the new registry
* ParserFactory.java - an interface for parser factories
* ParserFactoryImpl.java - a factory for the ARQ parsers

These classes go in the directory src/com/hp/hpl/jena/sparql/lang/ .

To use the registry I adjusted three existing files. You find the patches 
attached. I tried to stick to your indentation style.

I hope you can add this registry to your code base.

Bye,
Olaf

-----------------------------------------
Index: src/com/hp/hpl/jena/query/QueryFactory.java
===================================================================
--- src/com/hp/hpl/jena/query/QueryFactory.java	(revision 2766)
+++ src/com/hp/hpl/jena/query/QueryFactory.java	(working copy)
@@ -8,6 +8,7 @@
 
 import com.hp.hpl.jena.n3.IRIResolver;
 import com.hp.hpl.jena.sparql.lang.Parser;
+import com.hp.hpl.jena.sparql.lang.ParserRegistry;
 import com.hp.hpl.jena.sparql.lang.ParserSPARQL;
 import com.hp.hpl.jena.sparql.syntax.Element;
 import com.hp.hpl.jena.sparql.syntax.Template;
@@ -110,7 +111,7 @@
         else
             query.setSyntax(syntaxURI) ;
 
-        Parser parser = Parser.createParser(syntaxURI) ;
+        Parser parser = ParserRegistry.findParser(syntaxURI) ;
         
         if ( parser == null )
             throw new UnsupportedOperationException("Unrecognized syntax for parsing: "+syntaxURI) ;
@@ -126,7 +127,7 @@
     
     static boolean knownParserSyntax(Syntax syntaxURI)
     {
-        Parser parser = Parser.createParser(syntaxURI) ;
+        Parser parser = ParserRegistry.findParser(syntaxURI) ;
         return ( parser != null ) ;
     }
 
Index: src/com/hp/hpl/jena/sparql/lang/Parser.java
===================================================================
--- src/com/hp/hpl/jena/sparql/lang/Parser.java	(revision 2766)
+++ src/com/hp/hpl/jena/sparql/lang/Parser.java	(working copy)
@@ -24,30 +24,12 @@
     
     public static boolean canParse(Syntax syntaxURI)
     {
-        Parser p = createParser(syntaxURI) ;
-        return p != null ;
+        return ParserRegistry.containsParser(syntaxURI) ;
     }
     
     public static Parser createParser(Syntax syntaxURI)
     {
-        if (syntaxURI.equals(Syntax.syntaxSPARQL))
-            return new ParserSPARQL() ;
-        
-//        if (syntaxURI.equals(Syntax.syntaxSPARQLdev))
-//            return new ParserSPARQLdev() ;
-//        if (syntaxURI.equals(Syntax.syntaxPrefix))
-//            return new ParserPrefix() ;
-//        
-//        if (syntaxURI.equals(Syntax.syntaxSPARQL_X))
-//            return new ParserXML() ;
-
-        if (syntaxURI.equals(Syntax.syntaxARQ))
-            return new ParserARQ() ;
-
-        if (syntaxURI.equals(Syntax.syntaxRDQL))
-            return new ParserRDQL();
-
-        return null;
+        return ParserRegistry.findParser(syntaxURI) ;
     }
 
     // Do any testing of queries after the construction of the parse tree.
Index: src/com/hp/hpl/jena/sparql/util/QueryUtils.java
===================================================================
--- src/com/hp/hpl/jena/sparql/util/QueryUtils.java	(revision 2766)
+++ src/com/hp/hpl/jena/sparql/util/QueryUtils.java	(working copy)
@@ -12,7 +12,7 @@
 import com.hp.hpl.jena.sparql.algebra.Algebra;
 import com.hp.hpl.jena.sparql.algebra.Op;
 import com.hp.hpl.jena.sparql.core.QueryCheckException;
-import com.hp.hpl.jena.sparql.lang.Parser;
+import com.hp.hpl.jena.sparql.lang.ParserRegistry;
 import com.hp.hpl.jena.sparql.sse.SSE;
 import com.hp.hpl.jena.sparql.sse.SSEParseException;
 import com.hp.hpl.jena.sparql.sse.WriterSSE;
@@ -53,7 +53,7 @@
     
     public static void checkParse(Query query)
     {
-        if ( ! Parser.canParse(query.getSyntax()) )
+        if ( ! ParserRegistry.containsParser(query.getSyntax()) )
             return ;
         
         IndentedLineBuffer buff = new IndentedLineBuffer() ;
package com.hp.hpl.jena.sparql.lang;

import com.hp.hpl.jena.query.Syntax;

public class ParserFactoryImpl implements ParserFactory
{
    public boolean accept ( Syntax syntax )
    {
        return ( syntax.equals(Syntax.syntaxSPARQL) ||
//                 syntax.equals(Syntax.syntaxSPARQLdev) ||
//                 syntax.equals(Syntax.syntaxPrefix) ||
//                 syntax.equals(Syntax.syntaxSPARQL_X) ||
                 syntax.equals(Syntax.syntaxARQ) ||
                 syntax.equals(Syntax.syntaxRDQL) );
    }
    
    public Parser create ( Syntax syntax )
    {
        if (syntax.equals(Syntax.syntaxSPARQL))
            return new ParserSPARQL() ;
        
//        if (syntax.equals(Syntax.syntaxSPARQLdev))
//            return new ParserSPARQLdev() ;
//        if (syntax.equals(Syntax.syntaxPrefix))
//            return new ParserPrefix() ;
//        
//        if (syntax.equals(Syntax.syntaxSPARQL_X))
//            return new ParserXML() ;

        if (syntax.equals(Syntax.syntaxARQ))
            return new ParserARQ() ;

        if (syntax.equals(Syntax.syntaxRDQL))
            return new ParserRDQL();

        return null;
    }
}
package com.hp.hpl.jena.sparql.lang;

import com.hp.hpl.jena.query.Syntax;

public interface ParserFactory
{
    /** Return true if this factory can create a parser for the given syntax */
    public boolean accept ( Syntax syntax );
    
    /** Return a parser for the given syntax */
    public Parser create ( Syntax syntax );
}
package com.hp.hpl.jena.sparql.lang;

import java.util.HashMap;
import java.util.Map;

import com.hp.hpl.jena.query.Syntax;

public class ParserRegistry
{
    // the map contains the registered factories hashed by the syntaxes
    Map factories = new HashMap() ;
    static { init() ; }
    
    // Singleton
    static ParserRegistry registry = null ;
    static public ParserRegistry get()
    {
        if ( registry == null )
            init() ;
        return registry;
    }
    
    private ParserRegistry() { }
    
    private static void init()
    {
        registry = new ParserRegistry() ;
        ParserFactory f = new ParserFactoryImpl() ;
        registry.add(Syntax.syntaxSPARQL, f) ;
//        registry.add(Syntax.syntaxSPARQLdev, f) ;
//        registry.add(Syntax.syntaxPrefix, f) ;
//        registry.add(Syntax.syntaxSPARQL_X, f) ;
        registry.add(Syntax.syntaxARQ, f) ;
        registry.add(Syntax.syntaxRDQL, f) ;
    }
    
    /** Return a suitable factory for the given syntax
     *
     * @param syntax the syntax to be processed
     * @return a parser factory or null if none accept the request
     */
    
    public static ParserFactory findFactory(Syntax syntax)
    { return get().getFactory(syntax) ; }
    
    /** Return a suitable parser for the given syntax
     *
     * @param syntax the syntax to be processed
     * @return a parser or null if none accept the request
     */
    
    public static Parser findParser(Syntax syntax)
    { return get().getParser(syntax) ; }
    
    /** Return a suitable parser factory for the given syntax
     *
     * @param syntax the syntax to be processed
     * @return a parser factory or null if none accept the request
     */
    
    public ParserFactory getFactory(Syntax syntax)
    { return (ParserFactory) factories.get(syntax) ; }
    
    /** Return a suitable parser for the given syntax
     *
     * @param syntax the syntax to be processed
     * @return a parser or null if none accept the request
     */
    
    public Parser getParser(Syntax syntax)
    {
        ParserFactory f = getFactory(syntax) ;
        return ( f != null ) ? f.create(syntax) : null ;
    }
    
    /** Register the given parser factory for the specified syntax.
     *  If another factory is registered for the syntax it is replaced by the
     *  given one.
     */
    public static void addFactory(Syntax syntax, ParserFactory f)
    { get().add(syntax, f) ; }
    
    /** Register the given parser factory for the specified syntax.
     *  If another factory is registered for the syntax it is replaced by the
     *  given one.
     */
    public void add(Syntax syntax, ParserFactory f)
    {
        if ( ! f.accept(syntax) )
            throw new IllegalArgumentException( "The given parser factory does not accept the specified syntax." );
        factories.put(syntax, f) ;
    }
    
    /** Unregister the parser factory associated with the given syntax */
    public static void removeFactory(Syntax syntax)
    { get().remove(syntax) ; }
    
    /** Unregister the parser factory associated with the given syntax */
    public void remove(Syntax syntax)
    { factories.remove(syntax) ; }
    
    /** Checks wether a parser factory is registered for the given syntax */
    public static void containsFactory(Syntax syntax)
    { get().contains(syntax) ; }
    
    /** Checks wether a parser factory is registered for the given syntax */
    public static boolean containsParser(Syntax syntax)
    { return get().contains(syntax) ; }
    
    /** Checks wether a parser factory is registered for the given syntax */
    public boolean contains(Syntax syntax)
    { return factories.containsKey(syntax) ; }

}

Reply via email to