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/07/05 18:58:06 UTC
svn commit: r960637 - in /pivot/trunk/core: src/org/apache/pivot/beans/
src/org/apache/pivot/json/ src/org/apache/pivot/serialization/
test/org/apache/pivot/serialization/test/
Author: gbrown
Date: Mon Jul 5 16:58:06 2010
New Revision: 960637
URL: http://svn.apache.org/viewvc?rev=960637&view=rev
Log:
Add "mimeType" attribute to <bxml:include>; add octet stream and Java serialization support to BXMLSerializer by default; add embedded column name support to CSVSerializer so CSV files can be used in BXML includes; fix some minor parsing issues with CSVSerializer.
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
pivot/trunk/core/src/org/apache/pivot/beans/Bindable.java
pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java
pivot/trunk/core/test/org/apache/pivot/serialization/test/CSVSerializerTest.java
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=960637&r1=960636&r2=960637&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Mon Jul 5 16:58:06 2010
@@ -54,6 +54,8 @@ import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.json.JSON;
import org.apache.pivot.json.JSONSerializer;
+import org.apache.pivot.serialization.BinarySerializer;
+import org.apache.pivot.serialization.ByteArraySerializer;
import org.apache.pivot.serialization.CSVSerializer;
import org.apache.pivot.serialization.PropertiesSerializer;
import org.apache.pivot.serialization.SerializationException;
@@ -312,6 +314,7 @@ public class BXMLSerializer implements S
public static final String INCLUDE_TAG = "include";
public static final String INCLUDE_SRC_ATTRIBUTE = "src";
public static final String INCLUDE_RESOURCES_ATTRIBUTE = "resources";
+ public static final String INCLUDE_MIME_TYPE_ATTRIBUTE = "mimeType";
public static final String INCLUDE_INLINE_ATTRIBUTE = "inline";
public static final String SCRIPT_TAG = "script";
@@ -325,17 +328,19 @@ public class BXMLSerializer implements S
public static final String MIME_TYPE = "application/bxml";
static {
- fileExtensions.put(BXML_EXTENSION, MIME_TYPE);
mimeTypes.put(MIME_TYPE, BXMLSerializer.class);
- fileExtensions.put(CSVSerializer.CSV_EXTENSION, CSVSerializer.MIME_TYPE);
+ mimeTypes.put(BinarySerializer.MIME_TYPE, BinarySerializer.class);
+ mimeTypes.put(ByteArraySerializer.MIME_TYPE, ByteArraySerializer.class);
mimeTypes.put(CSVSerializer.MIME_TYPE, CSVSerializer.class);
-
- fileExtensions.put(JSONSerializer.JSON_EXTENSION, JSONSerializer.MIME_TYPE);
mimeTypes.put(JSONSerializer.MIME_TYPE, JSONSerializer.class);
+ mimeTypes.put(PropertiesSerializer.MIME_TYPE, PropertiesSerializer.class);
+
+ fileExtensions.put(BXML_EXTENSION, MIME_TYPE);
+ fileExtensions.put(CSVSerializer.CSV_EXTENSION, CSVSerializer.MIME_TYPE);
+ fileExtensions.put(JSONSerializer.JSON_EXTENSION, JSONSerializer.MIME_TYPE);
fileExtensions.put(PropertiesSerializer.PROPERTIES_EXTENSION, PropertiesSerializer.MIME_TYPE);
- mimeTypes.put(PropertiesSerializer.MIME_TYPE, PropertiesSerializer.class);
}
public BXMLSerializer() {
@@ -674,9 +679,10 @@ public class BXMLSerializer implements S
ArrayList<Attribute> staticPropertyAttributes = new ArrayList<Attribute>();
if (element.type == Element.Type.INCLUDE) {
- // Process attributes looking for src, resources, and property setters
+ // Process attributes looking for include parameters and property setters
String src = null;
Resources resources = this.resources;
+ String mimeType = null;
boolean inline = false;
for (Attribute attribute : element.attributes) {
@@ -684,6 +690,8 @@ public class BXMLSerializer implements S
src = attribute.value;
} else if (attribute.localName.equals(INCLUDE_RESOURCES_ATTRIBUTE)) {
resources = new Resources(resources, attribute.value);
+ } else if (attribute.localName.equals(INCLUDE_MIME_TYPE_ATTRIBUTE)) {
+ mimeType = attribute.value;
} else if (attribute.localName.equals(INCLUDE_INLINE_ATTRIBUTE)) {
inline = Boolean.parseBoolean(attribute.value);
} else if (Character.isUpperCase(attribute.localName.charAt(0))) {
@@ -699,11 +707,6 @@ public class BXMLSerializer implements S
+ " tag.");
}
- // Determine the MIME type of the include
- // TODO Read this from an attribute first; if null, attempt to infer
- // from file extension
- String mimeType = null;
-
if (mimeType == null) {
// Get the file extension
int i = src.lastIndexOf(".");
Modified: pivot/trunk/core/src/org/apache/pivot/beans/Bindable.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/Bindable.java?rev=960637&r1=960636&r2=960637&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/Bindable.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/Bindable.java Mon Jul 5 16:58:06 2010
@@ -36,11 +36,12 @@ public interface Bindable {
* variables.
*
* @param location
- * The location of the BXML source. May be <tt>null</tt>.
+ * The location of the BXML source. May be <tt>null</tt> if the location of the
+ * source is not known.
*
* @param resources
* The resources that were used to localize the deserialized content. May be
- * <tt>null</tt>.
+ * <tt>null</tt> if no resources were specified.
*/
public void initialize(Dictionary<String, Object> namespace, URL location, Resources resources);
}
Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java?rev=960637&r1=960636&r2=960637&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Mon Jul 5 16:58:06 2010
@@ -179,12 +179,11 @@ public class JSONSerializer implements S
}
// Move to the first character
- c = reader.read();
+ LineNumberReader lineNumberReader = new LineNumberReader(reader);
+ c = lineNumberReader.read();
// Read the root value
- LineNumberReader lineNumberReader = new LineNumberReader(reader);
Object object;
-
try {
object = readValue(lineNumberReader, type);
} catch (SerializationException exception) {
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=960637&r1=960636&r2=960637&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/serialization/CSVSerializer.java Mon Jul 5 16:58:06 2010
@@ -41,56 +41,9 @@ import org.apache.pivot.io.EchoWriter;
/**
* Implementation of the {@link Serializer} interface that reads data from
* and writes data to a comma-separated value (CSV) file.
- * <p>
- * TODO Add "firstLineContainsKeys" flag.
*/
public class CSVSerializer implements Serializer<List<?>> {
/**
- * Class representing the serializers key sequence.
- */
- public class KeySequence implements Sequence<String> {
- @Override
- public int add(String item) {
- return keys.add(item);
- }
-
- @Override
- public void insert(String item, int index) {
- keys.insert(item, index);
- }
-
- @Override
- public String update(int index, String item) {
- return keys.update(index, item);
- }
-
- @Override
- public int remove(String item) {
- return keys.remove(item);
- }
-
- @Override
- public Sequence<String> remove(int index, int count) {
- return keys.remove(index, count);
- }
-
- @Override
- public String get(int index) {
- return keys.get(index);
- }
-
- @Override
- public int indexOf(String item) {
- return keys.indexOf(item);
- }
-
- @Override
- public int getLength() {
- return keys.getLength();
- }
- }
-
- /**
* Allows a caller to retrieve the contents of a CSV stream iteratively.
*/
public class StreamIterator {
@@ -133,8 +86,8 @@ public class CSVSerializer implements Se
int c = -1;
private ArrayList<String> keys = new ArrayList<String>();
- private KeySequence keySequence = new KeySequence();
private Class<?> itemClass = HashMap.class;
+ private boolean writeKeys = false;
private boolean verbose = false;
public static final String DEFAULT_CHARSET_NAME = "ISO-8859-1";
@@ -162,8 +115,8 @@ public class CSVSerializer implements Se
* Returns a sequence representing the fields that will be read or written
* by this serializer.
*/
- public KeySequence getKeys() {
- return keySequence;
+ public Sequence<String> getKeys() {
+ return keys;
}
/**
@@ -188,6 +141,24 @@ public class CSVSerializer implements Se
}
/**
+ * Returns the serializer's write keys flag.
+ */
+ public boolean getWriteKeys() {
+ return writeKeys;
+ }
+
+ /**
+ * Sets the serializer's write keys flag.
+ *
+ * @param writeKeys
+ * If <tt>true</tt>, the first line of the output will contain the keys.
+ * Otherwise, the first line will contain the first line of data.
+ */
+ public void setWriteKeys(boolean writeKeys) {
+ this.writeKeys = writeKeys;
+ }
+
+ /**
* Returns the serializer's verbosity flag.
*/
public boolean isVerbose() {
@@ -237,6 +208,9 @@ public class CSVSerializer implements Se
* 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.
+ * <p>
+ * If no keys have been specified when this method is called, they are assumed
+ * to be defined in the first line of the file.
*/
public List<?> readObject(Reader reader)
throws IOException, SerializationException {
@@ -244,12 +218,28 @@ public class CSVSerializer implements Se
throw new IllegalArgumentException("reader is null.");
}
+ LineNumberReader lineNumberReader = new LineNumberReader(reader);
+
+ if (keys.getLength() == 0) {
+ // Read keys from first line
+ String line = lineNumberReader.readLine();
+ if (line == null) {
+ throw new SerializationException("Could not read keys from input.");
+ }
+
+ String[] keys = line.split(",");
+ this.keys = new ArrayList<String>(keys.length);
+
+ for (int i = 0; i < keys.length; i++) {
+ String key = keys[i];
+ this.keys.add(key.trim());
+ }
+ }
+
ArrayList<Object> items = new ArrayList<Object>();
// Move to the first character
- c = reader.read();
-
- LineNumberReader lineNumberReader = new LineNumberReader(reader);
+ c = lineNumberReader.read();
try {
while (c != -1) {
@@ -338,6 +328,19 @@ public class CSVSerializer implements Se
+ key + " from input stream.");
}
+ if (c == '\r' || c == '\n') {
+ if (i < n - 1) {
+ throw new SerializationException("Line data is incomplete.");
+ }
+
+ // Move to next char; if LF, move again
+ c = reader.read();
+
+ if (c == '\n') {
+ c = reader.read();
+ }
+ }
+
itemDictionary.put(key, value);
}
}
@@ -393,8 +396,11 @@ public class CSVSerializer implements Se
value = valueBuilder.toString();
- // Move to the next character after ','
- c = reader.read();
+ // Move to the next character after ',' (don't automatically advance to
+ // the next line)
+ if (c == ',') {
+ c = reader.read();
+ }
}
// Trim the value
@@ -456,6 +462,19 @@ public class CSVSerializer implements Se
throw new IllegalArgumentException("writer is null.");
}
+ if (writeKeys) {
+ // Write keys as first line
+ for (int i = 0, n = keys.getLength(); i < n; i++) {
+ String key = keys.get(i);
+
+ if (i > 0) {
+ writer.append(",");
+ }
+
+ writer.append(key);
+ }
+ }
+
for (Object item : items) {
Dictionary<String, Object> itemDictionary;
if (item instanceof Dictionary<?, ?>) {
Modified: pivot/trunk/core/test/org/apache/pivot/serialization/test/CSVSerializerTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/serialization/test/CSVSerializerTest.java?rev=960637&r1=960636&r2=960637&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/serialization/test/CSVSerializerTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/serialization/test/CSVSerializerTest.java Mon Jul 5 16:58:06 2010
@@ -123,7 +123,7 @@ public class CSVSerializerTest {
@SuppressWarnings("unchecked")
public void testQuotedNewlineReadObject() throws IOException, SerializationException {
StringBuilder buf = new StringBuilder();
- buf.append("a,\"\nb\n\",c\r\n");
+ buf.append("a,\"b\nb \",c\r\n");
StringReader reader = new StringReader(buf.toString());
@@ -136,7 +136,7 @@ public class CSVSerializerTest {
Dictionary<String, Object> row = (Dictionary<String, Object>)result.get(0);
assertEquals("a", row.get("A"));
- assertEquals("\nb\n", row.get("B"));
+ assertEquals("b\nb", row.get("B"));
assertEquals("c", row.get("C"));
}