[Mulgara-svn] r896 - trunk/src/jar/content-n3/java/org/mulgara/content/n3

pag at mulgara.org pag at mulgara.org
Tue May 6 04:01:15 UTC 2008


Author: pag
Date: 2008-05-05 21:01:14 -0700 (Mon, 05 May 2008)
New Revision: 896

Modified:
   trunk/src/jar/content-n3/java/org/mulgara/content/n3/Parser.java
Log:
Fixed formatting

Modified: trunk/src/jar/content-n3/java/org/mulgara/content/n3/Parser.java
===================================================================
--- trunk/src/jar/content-n3/java/org/mulgara/content/n3/Parser.java	2008-05-06 03:42:25 UTC (rev 895)
+++ trunk/src/jar/content-n3/java/org/mulgara/content/n3/Parser.java	2008-05-06 04:01:14 UTC (rev 896)
@@ -31,9 +31,7 @@
 import java.io.*;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.util.*;
-import org.xml.sax.*;
 
 // Third party packages
 import antlr.collections.AST;        // ANTLR compiler-compiler
@@ -48,26 +46,17 @@
 */
 import org.mulgara.content.Content;
 import org.mulgara.content.NotModifiedException;
-import org.mulgara.query.Constraint;
-import org.mulgara.query.Cursor;
-import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
-import org.mulgara.query.Variable;
 import org.mulgara.query.rdf.*;
 import org.mulgara.resolver.spi.LocalizeException;
 import org.mulgara.resolver.spi.ResolverSession;
-import org.mulgara.resolver.spi.Statements;
-import org.mulgara.resolver.spi.StatementsWrapperResolution;
-import org.mulgara.store.nodepool.NodePool;
-import org.mulgara.store.nodepool.NodePoolException;
-import org.mulgara.store.tuples.AbstractTuples;
-import org.mulgara.store.tuples.Tuples;
 import org.mulgara.util.IntFile;
 import org.mulgara.util.StringToLongMap;
 import org.mulgara.util.TempDir;
 
 /**
- * This {@link Runnable}
+ * This class parses N3 data. It is implemented as a {@link Runnable} to allow it to be running in
+ * the background filling a queue, while a consumer thread drains the queue. 
  *
  * @created 2004-04-02
  * @author <a href="http://staff.pisoftware.com/anewman">Andrew Newman</a>
@@ -80,11 +69,8 @@
  *      Software Pty Ltd</a>
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
-class Parser extends Thread implements N3ParserEventHandler
-{
-  /**
-   * Logger.
-   */
+class Parser extends Thread implements N3ParserEventHandler {
+  /** Logger. */
   private static final Logger logger = Logger.getLogger(Parser.class.getName());
 
   private static final String ANON_TAG = "_:";
@@ -100,20 +86,17 @@
   /** Mapping between parsed blank node IDs and local node numbers. */
   private IntFile blankNodeIdMap;
 
-
   /** Mapping between blank node rdf:nodeIDs and local node numbers. */
   private StringToLongMap blankNodeNameMap;
   
   /** The resolverSession to create new internal identifiers for blank nodes. */
   private ResolverSession resolverSession;
-  
 
+  /** The stream containing the data to be parsed. */
   private InputStream inputStream;
 
-  /**
-   * The queue of triples generated by the Notation-3 parser.
-   */
-  private LinkedList triples = new LinkedList();
+  /** The queue of triples generated by the Notation-3 parser. */
+  private LinkedList<Triple> triples = new LinkedList<Triple>();
 
   /**
    * The number of statements parsed so far.
@@ -124,21 +107,15 @@
   private long statementCount = 0;
 
   /**
-   * true if statementCount is the count of the total number of statements in
-   * the entire file because the parser has reached the end of the file without
-   * error.
+   * <code>true</code> if statementCount is the count of the total number of statements in
+   * the entire file because the parser has reached the end of the file without error.
    */
   private boolean statementCountIsTotal = false;
 
-  /**
-   * Flag used to indicate that the end of the RDF/XML file has been reached.
-   */
+  /** Flag used to indicate that the end of the RDF/XML file has been reached. */
   private boolean complete = false;
 
-  /**
-   * The exception which interrupted parsing, or <code>null</code> is parsing
-   * is successful.
-   */
+  /** The exception which interrupted parsing, or <code>null</code> is parsing is successful. */
   private Throwable exception = null;
 
   /**
@@ -159,7 +136,7 @@
    * Values are also {@link String}s, and of the form
    * <code>http://www.example.org/meeting_organization#</code>.
    */
-  private final Map prefixMap = new HashMap();
+  private final Map<String,String> prefixMap = new HashMap<String,String>();
 
   //
   // Constructor
@@ -177,9 +154,7 @@
     this.resolverSession = resolverSession;
     this.baseURI = content.getURI();
     try {
-      this.blankNodeIdMap = IntFile.open(
-        TempDir.createTempFile("n3idmap", null), true
-      );
+      this.blankNodeIdMap = IntFile.open(TempDir.createTempFile("n3idmap", null), true);
       this.blankNodeNameMap = new StringToLongMap();
       this.inputStream = content.newInputStream();
     } catch (IOException e) {
@@ -190,8 +165,7 @@
   /**
    * @return the number of statements parsed so far
    */
-  synchronized long getStatementCount() throws TuplesException
-  {
+  synchronized long getStatementCount() throws TuplesException {
     checkForException();
     return statementCount;
   }
@@ -199,8 +173,7 @@
   /**
    * @return the total number of statements in the file
    */
-  synchronized long waitForStatementTotal() throws TuplesException
-  {
+  synchronized long waitForStatementTotal() throws TuplesException {
     while (!complete) {
       checkForException();
 
@@ -223,8 +196,7 @@
    * Returns true if getStatementCount() would return the total number
    * of statements in the file.
    */
-  synchronized boolean isStatementCountTotal() throws TuplesException
-  {
+  synchronized boolean isStatementCountTotal() throws TuplesException {
     checkForException();
     return statementCountIsTotal;
   }
@@ -233,15 +205,12 @@
   // Method implementing Runnable
   //
 
-  public void run()
-  {
+  public void run() {
     Throwable t = null;
 
     try {
       (new N3Parser(inputStream, this)).parse();
-      if (logger.isDebugEnabled()) {
-        logger.debug("Parsed Notation-3");
-      }
+      if (logger.isDebugEnabled()) logger.debug("Parsed Notation-3");
       return;
     } catch (Throwable th) {
       t = th;
@@ -258,62 +227,42 @@
       }
     }
 
-    if (logger.isDebugEnabled()) {
-      logger.debug("Exception while parsing RDF/XML", exception);
-    }
+    if (logger.isDebugEnabled()) logger.debug("Exception while parsing RDF/XML", exception);
   }
 
   //
   // Methods implementing N3ParserEventHandler
   //
 
-  public void startDocument()
-  {
-    if (logger.isDebugEnabled()) {
-      logger.debug("Start N3 document");
-    }
-
+  public void startDocument() {
+    if (logger.isDebugEnabled()) logger.debug("Start N3 document");
     prefixMap.clear();
-  };
+  }
 
-  public void endDocument()
-  {
-    if (logger.isDebugEnabled()) {
-      logger.debug("End N3 document");
-    }
-  };
+  public void endDocument() {
+    if (logger.isDebugEnabled()) logger.debug("End N3 document");
+  }
 
-  public void error(Exception ex, String message)
-  {
-    if (logger.isDebugEnabled()) {
-      logger.debug(message, ex);
-    }
-  };
+  public void error(Exception ex, String message) {
+    if (logger.isDebugEnabled()) logger.debug(message, ex);
+  }
 
-  public void startFormula(int line, String context)
-  {
-    if (logger.isDebugEnabled()) {
-      logger.debug("Start formula " + context);
-    }
-  };
+  public void startFormula(int line, String context) {
+    if (logger.isDebugEnabled()) logger.debug("Start formula " + context);
+  }
 
-  public void endFormula(int line, String context)
-  {
-    if (logger.isDebugEnabled()) {
-      logger.debug("End formula " + context);
-    }
-  };
+  public void endFormula(int line, String context) {
+    if (logger.isDebugEnabled()) logger.debug("End formula " + context);
+  }
 
   public void quad(int    line,
                    AST    subj,
                    AST    pred,
                    AST    obj,
-                   String context)
-  {
+                   String context) {
 
     if (logger.isDebugEnabled()) {
-      logger.debug("Parsing " + subj + " " + pred + " " +
-                   obj + " from " + baseURI);
+      logger.debug("Parsing " + subj + " " + pred + " " + obj + " from " + baseURI);
     }
 
     // convert the triple components to JRDF Nodes
@@ -322,8 +271,7 @@
     ObjectNode    objectNode    = (ObjectNode)    toNode(obj);
 
     if (logger.isDebugEnabled()) {
-      logger.debug("Parsed " + subjectNode + " " + predicateNode + " " +
-                   objectNode + " from " + baseURI);
+      logger.debug("Parsed " + subjectNode + " " + predicateNode + " " + objectNode + " from " + baseURI);
     }
 
     synchronized (this) {
@@ -331,8 +279,7 @@
       while (triples.size() >= MAX_TRIPLES) {
         try {
           wait();
-        }
-        catch (InterruptedException ex) {
+        } catch (InterruptedException ex) {
           throw new RuntimeException("Abort");
         }
       }
@@ -344,8 +291,7 @@
     }
   }
 
-  public void directive(int line, AST directive, AST[] args, String context)
-  {
+  public void directive(int line, AST directive, AST[] args, String context) {
     switch (directive.getType()) {
     case N3Parser.AT_PREFIX:
       assert args.length == 2;
@@ -375,9 +321,7 @@
    * @return a {@link Node} matching the AST object.
    */
   private Node toNode(AST ast) {
-    if (ast == null) {
-      throw new IllegalArgumentException("Unable to load NULL nodes");
-    }
+    if (ast == null) throw new IllegalArgumentException("Unable to load NULL nodes");
 
     switch (ast.getType()) {
       case N3Parser.LITERAL:
@@ -392,23 +336,16 @@
 
         // find the language
         lang = getLang(a1);
-        if (lang == null) {
-          lang = getLang(a2);
-        }
-        if (lang == null) {
-          lang = "";
-        }
+        if (lang == null) lang = getLang(a2);
+        if (lang == null) lang = "";
 
         // find the datatype
         datatype = getDatatype(a1);
-        if (datatype == null) {
-          datatype = getDatatype(a2);
-        }
+        if (datatype == null) datatype = getDatatype(a2);
 
         if (datatype == null) {
           return new LiteralImpl(ast.toString(), lang);
-        }
-        else {
+        } else {
           return new LiteralImpl(ast.toString(), datatype);
         }
 
@@ -422,10 +359,8 @@
           int colonIndex = s.indexOf(':');
           assert colonIndex != -1;
           String qnamePrefix = s.substring(0, colonIndex + 1);
-          String uriPrefix = (String) prefixMap.get(qnamePrefix);
-          if (uriPrefix == null) {
-            throw new RuntimeException("No @prefix for " + s);
-          }
+          String uriPrefix = prefixMap.get(qnamePrefix);
+          if (uriPrefix == null) throw new RuntimeException("No @prefix for " + s);
           return toURIReference(uriPrefix + s.substring(colonIndex + 1));
         }
       case N3Parser.URIREF:
@@ -435,18 +370,16 @@
     }
   }
 
-  private URIReference toURIReference(String string)
-  {
+  private URIReference toURIReference(String string) {
     try {
       return new URIReferenceImpl(new URI(string));
-    }
-    catch (URISyntaxException e) {
+    } catch (URISyntaxException e) {
       throw new Error("Invalid URI reference generated", e);
     }
   }
 
   /**
-   * Tests if a node is anonymous.i
+   * Tests if a node is anonymous.
    *
    * This is done by looking for the {@link #ANON_TAG} prefix.
    *
@@ -465,7 +398,7 @@
    * @return An anonymous node that the AST node maps to.
    */
   private BlankNode getBlankNode(AST n) {
-    // yes it is, so parse its ID
+    // this is anonymous, so parse its ID
     long anonId = parseAnonId(n);
     String anonIdStr = null;
     try {
@@ -500,15 +433,9 @@
 
       return blankNode;
 
-    }
-    catch (IOException e) {
+    } catch (IOException e) {
       throw new RuntimeException("Couldn't generate anonymous resource", e);
     }
-    /*
-    catch (NodePoolException e) {
-      throw new RuntimeException("Couldn't generate anonymous resource", e);
-    }
-    */
   }
   
   /**
@@ -549,9 +476,7 @@
    */
   private String getLang(AST a) {
     // empty nodes have no info
-    if (a == null) {
-      return null;
-    }
+    if (a == null) return null;
     return a.getType() == N3Parser.AT_LANG ? a.getText() : null;
   }
 
@@ -564,13 +489,9 @@
    */
   private URI getDatatype(AST a) {
     // empty nodes have no info
-    if (a == null) {
-      return null;
-    }
+    if (a == null) return null;
     // check if this is a datatype node
-    if (a.getType() != N3Parser.DATATYPE) {
-      return null;
-    }
+    if (a.getType() != N3Parser.DATATYPE) return null;
 
     // get the datatype details
     AST dt = a.getFirstChild();
@@ -586,19 +507,16 @@
    * If an exception occurred in the parser, throws a TuplesException that
    * wraps the exception.
    */
-  private void checkForException() throws TuplesException
-  {
+  private void checkForException() throws TuplesException {
     if (exception != null) {
-      throw new TuplesException("Exception while reading " + baseURI,
-                                exception);
+      throw new TuplesException("Exception while reading " + baseURI, exception);
     }
   }
 
   /**
    * Returns a new triple from the queue or null if there are no more triples.
    */
-  synchronized Triple getTriple() throws TuplesException
-  {
+  synchronized Triple getTriple() throws TuplesException {
     while (triples.isEmpty()) {
       checkForException();
       if (complete) {
@@ -616,14 +534,13 @@
     checkForException();
 
     notifyAll();
-    return (Triple)triples.removeFirst();
+    return triples.removeFirst();
   }
 
   /**
    * Stops the thread.
    */
-  synchronized void abort()
-  {
+  synchronized void abort() {
     interrupt();
 
     // Clear the triples list and notify in case ARP uses an internal thread




More information about the Mulgara-svn mailing list