You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/16 02:11:17 UTC

svn commit: r765433 - in /incubator/pivot/trunk: core/src/pivot/serialization/CSVSerializer.java core/src/pivot/serialization/CSVSerializerListener.java wtk/src/pivot/wtk/BrowserApplicationContext.java wtk/src/pivot/wtk/DesktopApplicationContext.java

Author: gbrown
Date: Thu Apr 16 00:11:17 2009
New Revision: 765433

URL: http://svn.apache.org/viewvc?rev=765433&view=rev
Log:
Add support for event-based processing of CSV files to CSVSerializer; set application context origin in DesktopApplicationContext.

Added:
    incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java
Modified:
    incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java

Modified: incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java?rev=765433&r1=765432&r2=765433&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java (original)
+++ incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializer.java Thu Apr 16 00:11:17 2009
@@ -39,8 +39,6 @@
  * and writes data to a comma-separated value (CSV) file.
  * <p>
  * TODO Add "firstLineContainsKeys" flag.
- * <p>
- * TODO Add support for variable delimiters.
  *
  * @author gbrown
  */
@@ -159,10 +157,7 @@
      * @param reader
      * The reader from which data will be read.
      *
-     * @return
-     * A list containing the data read from the CSV file.
-     * The list items are instances of Dictionary<String, Object> populated by
-     * mapping columns in the CSV file to keys in the key sequence.
+     * @see #readObject(Reader)
      */
     public List<?> readObject(Reader reader)
         throws IOException, SerializationException {
@@ -190,6 +185,60 @@
         return items;
     }
 
+    /**
+     * Reads values from a comma-separated value stream and notifies a listener
+     * as items are processed.
+     *
+     * @param inputStream
+     * The input stream from which data will be read.
+     *
+     * @param listener
+     * The listener to notify.
+     *
+     * @see #readObject(Reader, CSVSerializerListener)
+     */
+    public void readObject(InputStream inputStream, CSVSerializerListener listener)
+        throws IOException, SerializationException {
+        Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset),
+            BUFFER_SIZE);
+        readObject(reader, listener);
+    }
+
+    /**
+     * Reads values from a comma-separated value stream and notifies a listener
+     * as items are processed. Items are instances of Dictionary<String, Object>
+     * populated by mapping columns in the CSV file to keys in the key sequence.
+     *
+     * @param reader
+     * The reader from which data will be read.
+     *
+     * @param listener
+     * The listener to notify.
+     */
+    public void readObject(Reader reader, CSVSerializerListener listener)
+        throws IOException, SerializationException {
+        // Move to the first character
+        c = reader.read();
+
+        while (c != -1) {
+            Object item = readItem(reader);
+            while (item != null) {
+                listener.itemRead(this, item);
+
+                // Move to next line
+                while (c != -1
+                    && (c == '\r' || c == '\n')) {
+                    c = reader.read();
+                }
+
+                // Read the next item
+                item = readItem(reader);
+            }
+        }
+
+        listener.allItemsRead(this);
+    }
+
     @SuppressWarnings("unchecked")
     private Object readItem(Reader reader)
         throws IOException, SerializationException {

Added: incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java?rev=765433&view=auto
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java (added)
+++ incubator/pivot/trunk/core/src/pivot/serialization/CSVSerializerListener.java Thu Apr 16 00:11:17 2009
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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 pivot.serialization;
+
+/**
+ * CSV serializer listener interface.
+ *
+ * @author gbrown
+ */
+public interface CSVSerializerListener {
+    /**
+     * Called when an item has been read by a CSV serializer.
+     *
+     * @param csvSerializer
+     * @param item
+     */
+    public void itemRead(CSVSerializer csvSerializer, Object item);
+
+    /**
+     * Called when all items have been read by a CSV serializer.
+     *
+     * @param csvSerializer
+     */
+    public void allItemsRead(CSVSerializer csvSerializer);
+}

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java?rev=765433&r1=765432&r2=765433&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/BrowserApplicationContext.java Thu Apr 16 00:11:17 2009
@@ -69,10 +69,12 @@
             public void run() {
                // Set the origin
                URL codeBase = getCodeBase();
+
                if (codeBase != null) {
                   try {
                      origin = new URL(codeBase.getProtocol(), codeBase.getHost(),
                         codeBase.getPort(), "");
+                     System.out.println("Origin: " + origin);
                   } catch(Exception exception) {
                      System.out.print("Unable to determine application origin: "
                         + exception);

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java?rev=765433&r1=765432&r2=765433&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/DesktopApplicationContext.java Thu Apr 16 00:11:17 2009
@@ -19,6 +19,12 @@
 import java.awt.AWTEvent;
 import java.awt.Graphics;
 import java.awt.event.WindowEvent;
+import java.io.File;
+import java.net.URL;
+
+import javax.jnlp.BasicService;
+import javax.jnlp.ServiceManager;
+import javax.jnlp.UnavailableServiceException;
 
 import pivot.collections.HashMap;
 import pivot.collections.immutable.ImmutableMap;
@@ -169,6 +175,10 @@
     }
 
     public static void main(String[] args) throws Exception {
+        if (application != null) {
+            throw new IllegalStateException();
+        }
+
         // Get the application class name and startup properties
         String applicationClassName = null;
         properties = new HashMap<String, String>();
@@ -206,6 +216,27 @@
             }
         }
 
+        // Set the origin
+        try {
+            // If a JNLP context is available, get the code base
+            BasicService basicService = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
+            URL codeBase = basicService.getCodeBase();
+
+            if (codeBase != null) {
+                origin = new URL(codeBase.getProtocol(), codeBase.getHost(), codeBase.getPort(), "");
+            }
+        } catch(UnavailableServiceException exception) {
+            // No-op
+        }
+
+        if (origin == null) {
+            // Could not obtain origin from JNLP; use user's home directory
+            File userHome = new File(System.getProperty("user.home"));
+            origin = userHome.toURI().toURL();
+        }
+
+        System.out.println("Origin: " + origin);
+
         // Create the application context
         applicationContext = new DesktopApplicationContext();