[Mulgara-svn] r1490 - trunk/src/jar/content-mp3/java/org/mulgara/content/mp3

pag at mulgara.org pag at mulgara.org
Fri Feb 13 07:06:39 UTC 2009


Author: pag
Date: 2009-02-12 23:06:38 -0800 (Thu, 12 Feb 2009)
New Revision: 1490

Modified:
   trunk/src/jar/content-mp3/java/org/mulgara/content/mp3/MP3Statements.java
Log:
Fixed a problem where all known blank node mappings were being ignored due to using badly typed maps

Modified: trunk/src/jar/content-mp3/java/org/mulgara/content/mp3/MP3Statements.java
===================================================================
--- trunk/src/jar/content-mp3/java/org/mulgara/content/mp3/MP3Statements.java	2009-02-13 07:05:36 UTC (rev 1489)
+++ trunk/src/jar/content-mp3/java/org/mulgara/content/mp3/MP3Statements.java	2009-02-13 07:06:38 UTC (rev 1490)
@@ -39,13 +39,10 @@
 // JRDF
 import org.jrdf.graph.*;
 import org.jrdf.graph.mem.*;
-import org.jrdf.graph.*;
 import org.jrdf.util.ClosableIterator;
 
 // Mp3 Library
 import org.blinkenlights.id3.*;
-import org.blinkenlights.id3.v1.*;
-import org.blinkenlights.id3.v2.*;
 
 // Locally written packages
 import org.mulgara.content.Content;
@@ -98,9 +95,6 @@
   /** The session used to globalize the RDF nodes from the stream. */
   private ResolverSession resolverSession;
 
-  /** The queue of triples generated by the ID3 parser. */
-  private ArrayList triples;
-
   /** The number of statements in the ID3 tag. */
   private long rowCount;
 
@@ -108,7 +102,7 @@
   private Triple tripleStatement;
 
   /** An interator into triples positioned at the next triple. */
-  private ClosableIterator nextTriple;
+  private ClosableIterator<Triple> nextTriple;
 
   /** The content representing the MP3 file */
   private Content content;
@@ -121,7 +115,7 @@
    *
    * This is <code>null</code> if no parsing is in progress.
    */
-  private Map blankNodeMap = null;
+  private Map<Node,Long> blankNodeMap = null;
 
   //
   // Constructors
@@ -155,7 +149,6 @@
     // Initialize fields
     this.content = content;
     this.resolverSession = resolverSession;
-    this.triples = new ArrayList();
 
     // Fix the magical column names for RDF statements
     setVariables(new Variable[] {new Variable("subject"),
@@ -173,7 +166,7 @@
     }
 
     // Find the blank node map of our content object
-    blankNodeMap = content.getBlankNodeMap();
+    blankNodeMap = convertToLocalBlankNodeMap(content.getBlankNodeMap());
 
     // Load in the RDF conversion of the given mp3 content
     loadURL();
@@ -186,40 +179,29 @@
    * @throws TuplesException
    */
   private void loadURL() throws NotModifiedException, TuplesException {
+    if (content.getURI() == null) throw new TuplesException("Unable to load MP3 from a stream. Use a file.");
 
-    // Discard any existing statements
-    triples.clear();
-
     try {
-
       // Initialise the model to be a memory based graph
       model = new GraphImpl();
     } catch (GraphException graphException) {
-
-      throw new TuplesException("Unable to create a new graph object.",
-                                graphException);
+      throw new TuplesException("Unable to create a new graph object.", graphException);
     }
 
     // Create a container for our file
     File contentFile = null;
 
     if (!content.getURI().getScheme().equals("file")) {
-
-      // If we are dealing with anything other than a file then use the
-      // caching process
-
+      // If we are dealing with anything other than a file then use the caching process
       try {
-
         // Convert the URI into a file
         contentFile = getCachedFile(content.newInputStream(), content.getURI());
       } catch (IOException ioException) {
-
         throw new TuplesException(
             "Unable to open a stream to the content file [" +
             content.getURI().toString() + "]", ioException);
       }
     } else {
-
       // Files are local and do not need caching
       contentFile = new File(content.getURI());
     }
@@ -228,24 +210,18 @@
     parseFile(contentFile);
 
     // Parse the stream into RDF statements
-    blankNodeMap = new HashMap();
+    blankNodeMap = new HashMap<Node,Long>();
 
     try {
-
       // Initialize the metadata now that we know the statements
       rowCount = model.getNumberOfTriples();
-
     } catch (GraphException graphException) {
-
       throw new TuplesException(
           "Unable to retrieve number of triples in graph.",
           graphException);
     }
 
-    if (logger.isDebugEnabled()) {
-
-      logger.debug("Parsed MP3: Found " + rowCount + " triples");
-    }
+    if (logger.isDebugEnabled()) logger.debug("Parsed MP3: Found " + rowCount + " triples");
   }
 
   //
@@ -260,7 +236,6 @@
    * @throws TuplesException
    */
   public long getSubject() throws TuplesException {
-
     return getColumnValue(SUBJECT);
   }
 
@@ -272,7 +247,6 @@
    * @throws TuplesException
    */
   public long getPredicate() throws TuplesException {
-
     return getColumnValue(PREDICATE);
   }
 
@@ -284,7 +258,6 @@
    * @throws TuplesException
    */
   public long getObject() throws TuplesException {
-
     return getColumnValue(OBJECT);
   }
 
@@ -300,23 +273,16 @@
    *
    * @throws TuplesException
    */
-  public void beforeFirst(long[] prefix, int suffixTruncation) throws
-      TuplesException {
-
+  public void beforeFirst(long[] prefix, int suffixTruncation) throws TuplesException {
     try {
-
       // Get the iterator for statements in the model
       nextTriple = model.find(null, null, null);
     } catch (GraphException graphException) {
-
-      throw new TuplesException("Unable to retrieve triple iterator for graph.",
-                                graphException);
+      throw new TuplesException("Unable to retrieve triple iterator for graph.", graphException);
     }
 
     if (logger.isDebugEnabled()) {
-
       try {
-
         logger.debug("-- Getting the before first value from model " + model +
                      " which has statements " + nextTriple.hasNext() + " from " +
                      model.getNumberOfTriples() + " triples");
@@ -338,9 +304,6 @@
     cloned.tripleStatement = tripleStatement;
     cloned.content = content;
 
-    // Copy mutable fields by value
-    cloned.triples = (ArrayList) triples.clone();
-
     return cloned;
   }
 
@@ -351,7 +314,6 @@
 
     resolverSession = null;
     tripleStatement = null;
-    triples = null;
     content = null;
   }
 
@@ -359,71 +321,57 @@
    * @param column  0 for the subject, 1 for the predicate, 2 for the object
    */
   public long getColumnValue(int column) throws TuplesException {
-
     // Pull the appropriate field from the current triple as a JRDF Node
     Node node = null;
 
     switch (column) {
       case SUBJECT:
-
         // Try creating the node with a URI reference
         node = tripleStatement.getSubject();
-
         break;
       case PREDICATE:
-
         // Try to create a URI reference node to represent the predicate
         node = tripleStatement.getPredicate();
-
         break;
       case OBJECT:
-
         // Create a literal node with the value for objects
         node = tripleStatement.getObject();
-
         break;
       default:
-
         throw new TuplesException("No such column " + column);
     }
     assert node != null;
 
     // Container for our result
- Long result = null;
+    Long result = null;
 
- if (blankNodeMap.containsKey(node)) {
+    if (blankNodeMap.containsKey(node)) {
+      // If the node is already mapped then get the value
+      result = blankNodeMap.get(node);
+    } else {
 
-   // If the node is already mapped then get the value
-   result = (Long) blankNodeMap.get(node);
- } else {
+      // If we haven't mapped the node already then create a new value and store it
 
-   // If we haven't mapped the node already then create a new value and
-   // store it
+      // Localize the node and store the long object value
+      try {
+        result = new Long(resolverSession.localize(node));
+      } catch (LocalizeException e) {
+        throw new TuplesException("Couldn't get column " + column + " value", e);
+      }
 
-   // Localize the node and store the long object value
-   try {
+      // Store the new mapping
+      blankNodeMap.put(node, result);
+    }
 
-     result = new Long(resolverSession.localize(node));
-   } catch (LocalizeException e) {
+    if (column == SUBJECT && logger.isInfoEnabled()) {
+      logger.info("!! Using node value of: " + result.longValue());
+    }
 
-     throw new TuplesException("Couldn't get column " + column + " value", e);
-   }
-
-   // Store the new mapping
-   blankNodeMap.put(node, result);
- }
-
- if (column == SUBJECT && logger.isInfoEnabled()) {
-
-   logger.info("!! Using node value of: " + result.longValue());
- }
-
-      return result.longValue();
+    return result.longValue();
   }
 
-  public List getOperands() {
-
-    return Collections.EMPTY_LIST;
+  public List<Tuples> getOperands() {
+    return Collections.emptyList();
   }
 
   public long getRowCount() throws TuplesException {
@@ -442,39 +390,24 @@
   }
 
   public boolean isColumnEverUnbound(int column) throws TuplesException {
-
     switch (column) {
-
       case 0:
-
       case 1:
-
       case 2:
-
         return false;
       default:
-
         throw new TuplesException("No such column " + column);
     }
   }
 
   public boolean next() throws TuplesException {
-
     if (nextTriple.hasNext()) {
-
       // Get the next statement in the iterator
-      tripleStatement = (Triple) nextTriple.next();
-
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("-- Getting next statement: " + tripleStatement.toString());
-      }
-
+      tripleStatement = nextTriple.next();
+      if (logger.isDebugEnabled()) logger.debug("-- Getting next statement: " + tripleStatement.toString());
       return true;
     } else {
-
       tripleStatement = null;
-
       return false;
     }
   }
@@ -492,7 +425,6 @@
   private void parseFile(File file) throws TuplesException {
 
     if (file.getName().endsWith(".mp3")) {
-
       // If the file is a valid mp3 file then parse the content into the model
 
       // Container for our mp3 file
@@ -505,14 +437,10 @@
       URI escapedURI = null;
 
       try {
-
         // Create an extended version of the URI for the file
-        escapedURI = new URI(file.toURI().getScheme() + "://" +
-                             file.toURI().getRawPath());
+        escapedURI = new URI(file.toURI().getScheme() + "://" + file.toURI().getRawPath());
       } catch (URISyntaxException uriSyntaxException) {
-
-        throw new TuplesException("Failed to create a valid extended uri from" +
-                                  file.toURI(), uriSyntaxException);
+        throw new TuplesException("Failed to create a valid extended uri from" + file.toURI(), uriSyntaxException);
       }
 
       // Create a new conversion object
@@ -522,29 +450,20 @@
       ID3Parser parser = null;
 
       try {
-
         // Get a parser instance
         parser = ParserFactory.getInstance().createID3Parser();
       } catch (FactoryException factoryException) {
-
-        throw new TuplesException(
-            "Unable to create a new ID3Parser due to a factory error.",
-            factoryException);
+        throw new TuplesException("Unable to create a new ID3Parser due to a factory error.", factoryException);
       }
 
       try {
-
         // Parse the mp3 into the model
         parser.parseTags(conversion);
       } catch (ParserException parserException) {
-
-        throw new TuplesException("Unable to parse tags for file: " +
-                                  content.getURI().toString(), parserException);
+        throw new TuplesException("Unable to parse tags for file: " + file, parserException);
       }
     } else {
-
-      throw new TuplesException("Content object did not contain a valid mime " +
-                                "type for parsing.");
+      throw new TuplesException("Content object did not contain a valid mime  type for parsing.");
     }
   }
 
@@ -571,7 +490,6 @@
                                            remotePath.length());
 
     if (logger.isDebugEnabled()) {
-
       logger.debug("Transferring [" + uri + "] to cached file [" + fileName + "]");
     }
 
@@ -662,4 +580,14 @@
 
     return file;
   }
+
+  private Map<Node,Long> convertToLocalBlankNodeMap(Map<Object,BlankNode> map) {
+    Map<Node,Long> localMap = new HashMap<Node,Long>();
+    for (Map.Entry<Object,BlankNode> entry: map.entrySet()) {
+      if (entry.getKey() instanceof Node && entry.getValue() instanceof BlankNodeImpl) {
+        localMap.put((Node)entry.getKey(), ((BlankNodeImpl)entry.getValue()).getId());
+      }
+    }
+    return localMap;
+  }
 }




More information about the Mulgara-svn mailing list