From 787471814a5badcf599a5af539a8ca7d3a5d4909 Mon Sep 17 00:00:00 2001 From: Tobias Schwarz Date: Tue, 29 Jan 2013 10:21:02 +0100 Subject: TE: add list read and write --- .../delegates/GsonMapPersistenceDelegate.java | 190 +++++++++++++++------ .../interfaces/IPersistenceDelegate.java | 6 +- 2 files changed, 147 insertions(+), 49 deletions(-) (limited to 'target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence') diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/delegates/GsonMapPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/delegates/GsonMapPersistenceDelegate.java index 01c6d5ae5..52f788cde 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/delegates/GsonMapPersistenceDelegate.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/delegates/GsonMapPersistenceDelegate.java @@ -19,7 +19,9 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -61,8 +63,11 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I this.defaultFileExtension = defaultFileExtension; } - /* (non-Javadoc) - * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#getPersistedClass(java.lang.Object) + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#getPersistedClass( + * java.lang.Object) */ @Override public Class getPersistedClass(Object context) { @@ -76,16 +81,34 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I return defaultFileExtension; } - /* (non-Javadoc) - * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#write(java.lang.Object, java.lang.Object) + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#writeList(java.lang + * .Object[], java.lang.Object) + */ + @Override + public Object writeList(Object[] context, Object container) throws IOException { + return write(context, container, true); + } + + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#write(java.lang.Object + * , java.lang.Object) */ @Override public final Object write(Object context, Object container) throws IOException { + return write(context, container, false); + } + + private Object write(Object context, Object container, boolean isList) throws IOException { Assert.isNotNull(context); Assert.isNotNull(container); if (container instanceof URI) { - URI uri = (URI)container; + URI uri = (URI) container; // Only "file:" URIs are supported if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$ @@ -106,14 +129,25 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I file = path.addFileExtension(getDefaultFileExtension()).toFile(); } - Writer writer = null; - try { - writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); //$NON-NLS-1$ - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - gson.toJson(internalToMap(context), Map.class, writer); - } finally { - if (writer != null) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); //$NON-NLS-1$ + if (!isList) { + try { + gson.toJson(internalToMap(context), Map.class, writer); + } + finally { + writer.close(); + } + } + else { + List encoded = new ArrayList(); + for (Object entry : (Object[]) context) { + encoded.add(gson.toJson(internalToMap(entry))); + } + try { + gson.toJson(encoded, List.class, writer); + } + finally { writer.close(); } } @@ -121,22 +155,33 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I else if (String.class.equals(container)) { Gson gson = new GsonBuilder().create(); - container = gson.toJson(internalToMap(context)); + if (!isList) { + container = gson.toJson(internalToMap(context)); + } + else { + List encoded = new ArrayList(); + for (Object entry : (Object[]) context) { + encoded.add(gson.toJson(internalToMap(entry))); + } + container = gson.toJson(encoded); + } } return container; } /* - * Convert the context to a Map, extract and use variables and add them to the map as key VARIABLE. + * Convert the context to a Map, extract and use variables and add them to the map as key + * VARIABLE. */ - private Map internalToMap(Object context) { + private Map internalToMap(Object context) { try { - Map data = toMap(context); + Map data = toMap(context); if (data != null) { - Map variables = null; - IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this); + Map variables = null; + IVariableDelegate[] delegates = PersistenceManager.getInstance() + .getVariableDelegates(this); for (IVariableDelegate delegate : delegates) { variables = delegate.getVariables(data); } @@ -153,18 +198,36 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I return null; } - /* (non-Javadoc) - * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#read(java.lang.Object, java.lang.Object) + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#readList(java.lang + * .Class, java.lang.Object) + */ + @Override + public Object[] readList(Class contextClass, Object container) throws IOException { + return (Object[])read(contextClass, container, true); + } + + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#read(java.lang.Object, + * java.lang.Object) */ @Override public final Object read(Object context, Object container) throws IOException { + return read(context, container, false); + } + + private Object read(Object context, Object container, boolean isList) throws IOException { Assert.isNotNull(container); Gson gson = new GsonBuilder().create(); - Map data = null; + List> data = new ArrayList>(); if (container instanceof URI) { - URI uri = (URI)container; + URI uri = (URI) container; // Only "file:" URIs are supported if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$ @@ -186,43 +249,74 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I } } - Reader reader = null; - try { - reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); //$NON-NLS-1$ - data = gson.fromJson(reader, Map.class); - } finally { - if (reader != null) { + Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); //$NON-NLS-1$ + if (!isList) { + try { + data.add(gson.fromJson(reader, Map.class)); + } + finally { + reader.close(); + } + } + else { + try { + List strings = gson.fromJson(reader, List.class); + for (String string : strings) { + data.add(gson.fromJson(string, Map.class)); + } + } + finally { reader.close(); } } } else if (container instanceof String) { - data = gson.fromJson((String)container, Map.class); + if (!isList) { + data.add(gson.fromJson((String) container, Map.class)); + } + else { + List strings = gson.fromJson((String) container, List.class); + for (String string : strings) { + data.add(gson.fromJson(string, Map.class)); + } + } } - if (data != null) { - Map variables = new HashMap(); - if (data.containsKey(VARIABLES)) { - variables = (Map)data.remove(VARIABLES); + for (Map entry : data) { + Map variables = new HashMap(); + if (entry.containsKey(VARIABLES)) { + variables = (Map) entry.remove(VARIABLES); } - IVariableDelegate[] delegates = PersistenceManager.getInstance().getVariableDelegates(this); + IVariableDelegate[] delegates = PersistenceManager.getInstance() + .getVariableDelegates(this); for (IVariableDelegate delegate : delegates) { - data = delegate.putVariables(data, variables); + entry = delegate.putVariables(entry, variables); } } - return data != null ? fromMap(data, context) : context; + if (!isList) { + return !data.isEmpty() ? fromMap(data.get(0), context) : context; + } + + List list = new ArrayList(); + for (Map entry : data) { + list.add(fromMap(entry, context)); + } + return list.toArray(); } - /* (non-Javadoc) - * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#delete(java.lang.Object, java.lang.Object) + /* + * (non-Javadoc) + * @see + * org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate#delete(java.lang.Object + * , java.lang.Object) */ @Override public boolean delete(Object context, Object container) throws IOException { Assert.isNotNull(container); if (container instanceof URI) { - URI uri = (URI)container; + URI uri = (URI) container; // Only "file:" URIs are supported if (!"file".equalsIgnoreCase(uri.getScheme())) { //$NON-NLS-1$ @@ -266,15 +360,15 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I */ @SuppressWarnings("unchecked") protected Map toMap(final Object context) throws IOException { - Map result = new HashMap(); + Map result = new HashMap(); - Map attrs = null; + Map attrs = null; if (context instanceof Map) { - attrs = (Map)context; + attrs = (Map) context; } else if (context instanceof IPropertiesContainer) { - IPropertiesContainer container = (IPropertiesContainer)context; - attrs = new HashMap(container.getProperties()); + IPropertiesContainer container = (IPropertiesContainer) context; + attrs = new HashMap(container.getProperties()); } if (attrs != null) { @@ -297,13 +391,13 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I * * @throws IOException */ - protected Object fromMap(Map map, Object context) throws IOException { + protected Object fromMap(Map map, Object context) throws IOException { if (context == null || Map.class.equals(context)) { return map; } else if (context instanceof Map) { @SuppressWarnings({ "rawtypes", "unchecked" }) - Map newMap = new HashMap((Map)context); + Map newMap = new HashMap((Map) context); newMap.putAll(map); return newMap; } @@ -314,7 +408,7 @@ public class GsonMapPersistenceDelegate extends ExecutableExtension implements I return container; } else if (context instanceof IPropertiesContainer) { - IPropertiesContainer container = (IPropertiesContainer)context; + IPropertiesContainer container = (IPropertiesContainer) context; container.setProperties(map); return container; diff --git a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java index bf8a72c70..50aa36fa5 100644 --- a/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java +++ b/target_explorer/plugins/org.eclipse.tcf.te.runtime.persistence/src/org/eclipse/tcf/te/runtime/persistence/interfaces/IPersistenceDelegate.java @@ -29,9 +29,11 @@ public interface IPersistenceDelegate extends IExecutableExtension { */ public Object write(Object context, Object container) throws IOException; + public Object writeList(Object[] context, Object container) throws IOException; + /** * Get the class or interface for the context. - * + * * @param context The context to persist. Must not be null. * @return The class or interface for the given context. */ @@ -48,6 +50,8 @@ public interface IPersistenceDelegate extends IExecutableExtension { */ public Object read(Object context, Object container) throws IOException; + public Object[] readList(Class contextClass, Object container) throws IOException; + /** * Deletes the given context inside the container or the whole container. * -- cgit v1.2.3