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 2010/03/15 14:40:19 UTC
svn commit: r923220 - in /pivot/trunk/core/src/org/apache/pivot:
io/EchoReader.java io/EchoWriter.java serialization/CSVSerializer.java
serialization/JSONSerializer.java
Author: gbrown
Date: Mon Mar 15 13:40:18 2010
New Revision: 923220
URL: http://svn.apache.org/viewvc?rev=923220&view=rev
Log:
Add EchoReader and EchoWriter classes that echo reads and writes to the console; add a "verbose" property to JSONSerializer and CSVSerializer that uses these classes when enabled.
Added:
pivot/trunk/core/src/org/apache/pivot/io/EchoReader.java
pivot/trunk/core/src/org/apache/pivot/io/EchoWriter.java
Modified:
pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java
pivot/trunk/core/src/org/apache/pivot/serialization/JSONSerializer.java
Added: pivot/trunk/core/src/org/apache/pivot/io/EchoReader.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/io/EchoReader.java?rev=923220&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/io/EchoReader.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/io/EchoReader.java Mon Mar 15 13:40:18 2010
@@ -0,0 +1,47 @@
+/*
+ * 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 org.apache.pivot.io;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Reader that echoes characters to the console as they are read.
+ */
+public class EchoReader extends Reader {
+ private Reader reader;
+
+ public EchoReader(Reader reader) {
+ if (reader == null) {
+ throw new IllegalArgumentException();
+ }
+
+ this.reader = reader;
+ }
+
+ @Override
+ public void close() throws IOException {
+ reader.close();
+ }
+
+ @Override
+ public int read(char[] cbuf, int off, int len) throws IOException {
+ int n = reader.read(cbuf, off, len);
+ System.out.print(cbuf);
+ return n;
+ }
+}
Added: pivot/trunk/core/src/org/apache/pivot/io/EchoWriter.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/io/EchoWriter.java?rev=923220&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/io/EchoWriter.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/io/EchoWriter.java Mon Mar 15 13:40:18 2010
@@ -0,0 +1,51 @@
+/*
+ * 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 org.apache.pivot.io;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Writer that echoes characters to the console as they are written.
+ */
+public class EchoWriter extends Writer {
+ private Writer writer;
+
+ public EchoWriter(Writer writer) {
+ if (writer == null) {
+ throw new IllegalArgumentException();
+ }
+
+ this.writer = writer;
+ }
+
+ @Override
+ public void close() throws IOException {
+ writer.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ writer.flush();
+ }
+
+ @Override
+ public void write(char[] cbuf, int off, int len) throws IOException {
+ System.out.print(cbuf);
+ writer.write(cbuf, off, len);
+ }
+}
Modified: pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java?rev=923220&r1=923219&r2=923220&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java Mon Mar 15 13:40:18 2010
@@ -35,7 +35,8 @@ import org.apache.pivot.collections.Dict
import org.apache.pivot.collections.HashMap;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Sequence;
-
+import org.apache.pivot.io.EchoReader;
+import org.apache.pivot.io.EchoWriter;
/**
* Implementation of the {@link Serializer} interface that reads data from
@@ -131,16 +132,17 @@ public class CSVSerializer implements Se
private Charset charset;
int c = -1;
- private Class<?> itemClass = HashMap.class;
private ArrayList<String> keys = new ArrayList<String>();
private KeySequence keySequence = new KeySequence();
+ private Class<?> itemClass = HashMap.class;
+ private boolean verbose = false;
+
private LineNumberReader lineNumberReader = null;
public static final String DEFAULT_CHARSET_NAME = "ISO-8859-1";
public static final String MIME_TYPE = "text/csv";
public static final int BUFFER_SIZE = 2048;
-
public CSVSerializer() {
this(Charset.forName(DEFAULT_CHARSET_NAME));
}
@@ -187,6 +189,23 @@ public class CSVSerializer implements Se
}
/**
+ * Returns the serializer's verbosity flag.
+ */
+ public boolean isVerbose() {
+ return verbose;
+ }
+
+ /**
+ * Sets the serializer's verbosity flag. When verbosity is enabled, all data read or
+ * written will be echoed to the console.
+ *
+ * @param verbose
+ */
+ public void setVerbose(boolean verbose) {
+ this.verbose = verbose;
+ }
+
+ /**
* Reads values from a comma-separated value stream.
*
* @param inputStream
@@ -201,8 +220,11 @@ public class CSVSerializer implements Se
throw new IllegalArgumentException("inputStream is null.");
}
- Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset),
- BUFFER_SIZE);
+ Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset), BUFFER_SIZE);
+ if (verbose) {
+ reader = new EchoReader(reader);
+ }
+
return readObject(reader);
}
@@ -411,8 +433,11 @@ public class CSVSerializer implements Se
throw new IllegalArgumentException("outputStream is null.");
}
- Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset),
- BUFFER_SIZE);
+ Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset), BUFFER_SIZE);
+ if (verbose) {
+ writer = new EchoWriter(writer);
+ }
+
writeObject(items, writer);
}
Modified: pivot/trunk/core/src/org/apache/pivot/serialization/JSONSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/serialization/JSONSerializer.java?rev=923220&r1=923219&r2=923220&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/serialization/JSONSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/serialization/JSONSerializer.java Mon Mar 15 13:40:18 2010
@@ -36,6 +36,8 @@ import org.apache.pivot.collections.List
import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.immutable.ImmutableList;
import org.apache.pivot.collections.immutable.ImmutableMap;
+import org.apache.pivot.io.EchoReader;
+import org.apache.pivot.io.EchoWriter;
/**
* Implementation of the {@link Serializer} interface that reads data from
@@ -49,6 +51,8 @@ public class JSONSerializer extends JSON
private int c = -1;
private boolean alwaysDelimitMapKeys = false;
+ private boolean verbose = false;
+
private LineNumberReader lineNumberReader = null;
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
@@ -94,6 +98,42 @@ public class JSONSerializer extends JSON
}
/**
+ * Returns a flag indicating whether or not map keys will always be
+ * quote-delimited.
+ */
+ public boolean getAlwaysDelimitMapKeys() {
+ return alwaysDelimitMapKeys;
+ }
+
+ /**
+ * Sets a flag indicating that map keys should always be quote-delimited.
+ *
+ * @param alwaysDelimitMapKeys
+ * <tt>true</tt> to bound map keys in double quotes; <tt>false</tt> to
+ * only quote-delimit keys as necessary.
+ */
+ public void setAlwaysDelimitMapKeys(boolean alwaysDelimitMapKeys) {
+ this.alwaysDelimitMapKeys = alwaysDelimitMapKeys;
+ }
+
+ /**
+ * Returns the serializer's verbosity flag.
+ */
+ public boolean isVerbose() {
+ return verbose;
+ }
+
+ /**
+ * Sets the serializer's verbosity flag. When verbosity is enabled, all data read or
+ * written will be echoed to the console.
+ *
+ * @param verbose
+ */
+ public void setVerbose(boolean verbose) {
+ this.verbose = verbose;
+ }
+
+ /**
* Reads data from a JSON stream.
*
* @param inputStream
@@ -109,9 +149,11 @@ public class JSONSerializer extends JSON
}
Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset), BUFFER_SIZE);
- Object object = readObject(reader);
+ if (verbose) {
+ reader = new EchoReader(reader);
+ }
- return object;
+ return readObject(reader);
}
/**
@@ -512,8 +554,11 @@ public class JSONSerializer extends JSON
throw new IllegalArgumentException("outputStream is null.");
}
- Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset),
- BUFFER_SIZE);
+ Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset), BUFFER_SIZE);
+ if (verbose) {
+ writer = new EchoWriter(writer);
+ }
+
writeObject(object, writer);
}
@@ -663,25 +708,6 @@ public class JSONSerializer extends JSON
}
/**
- * Returns a flag indicating whether or not map keys will always be
- * quote-delimited.
- */
- public boolean getAlwaysDelimitMapKeys() {
- return alwaysDelimitMapKeys;
- }
-
- /**
- * Sets a flag indicating that map keys should always be quote-delimited.
- *
- * @param alwaysDelimitMapKeys
- * <tt>true</tt> to bound map keys in double quotes; <tt>false</tt> to
- * only quote-delimit keys as necessary.
- */
- public void setAlwaysDelimitMapKeys(boolean alwaysDelimitMapKeys) {
- this.alwaysDelimitMapKeys = alwaysDelimitMapKeys;
- }
-
- /**
* @deprecated
* @see JSON#get(Object, String)
*/