Author: tfennelly
Date: 2009-12-19 05:26:20 -0500 (Sat, 19 Dec 2009)
New Revision: 19484
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/MarshallingStrategy.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializer.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/XPathMarshaller.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/META-INF/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/META-INF/MANIFEST.MF
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/build.properties
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xmlunit-1.1.jar
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xstream-1.2.2.jar
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/plugin.properties
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Header.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializerText.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Order.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/OrderItem.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/TestUtil.java
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/expected-01.txt
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/.classpath
trunk/smooks/plugins/org.jboss.tools.smooks.ui/.classpath
trunk/smooks/plugins/org.jboss.tools.smooks.ui/.project
trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLaunchConfigurationDelegate.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLauncher.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-5409
Replace XML Serialized Objects with something easier to read
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.templating/.classpath
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.templating/.classpath 2009-12-19 02:38:14
UTC (rev 19483)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.templating/.classpath 2009-12-19 10:26:20
UTC (rev 19484)
@@ -3,6 +3,6 @@
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
- <classpathentry exported="true" kind="lib"
path="libs/xstream-1.2.2.jar"/>
+ <classpathentry exported="true" kind="lib"
path="libs/xstream-1.2.2.jar" sourcepath="/xstream"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.ui/.classpath
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.ui/.classpath 2009-12-19 02:38:14 UTC (rev
19483)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.ui/.classpath 2009-12-19 10:26:20 UTC (rev
19484)
@@ -1,7 +1,8 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
- <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+ <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry combineaccessrules="false" kind="src"
path="/xstream"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.ui/.project
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.ui/.project 2009-12-19 02:38:14 UTC (rev
19483)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.ui/.project 2009-12-19 10:26:20 UTC (rev
19484)
@@ -1,40 +1,40 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>org.jboss.tools.smooks.ui</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.ManifestBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.SchemaBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.pde.PluginNature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
- <linkedResources>
- <link>
- <name>smooks-core</name>
- <type>2</type>
- <location>D:/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/milyn/trunk/smooks-core/src/main/java</location>
- </link>
- <link>
- <name>smooks-csv</name>
- <type>2</type>
- <location>D:/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/milyn/trunk/smooks-cartridges/csv/src/main/java</location>
- </link>
- </linkedResources>
-</projectDescription>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.jboss.tools.smooks.ui</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+ <linkedResources>
+ <link>
+ <name>smooks-core</name>
+ <type>2</type>
+ <location>/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/milyn/trunk/smooks-core/src/main/java</location>
+ </link>
+ <link>
+ <name>smooks-csv</name>
+ <type>2</type>
+ <location>/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/packs/eclipse/Eclipse.app/Contents/MacOS/D::/Users/tfennelly/milyn/trunk/smooks-cartridges/csv/src/main/java</location>
+ </link>
+ </linkedResources>
+</projectDescription>
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF 2009-12-19
02:38:14 UTC (rev 19483)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF 2009-12-19
10:26:20 UTC (rev 19484)
@@ -55,4 +55,5 @@
org.jboss.tools.smooks.edimap.editor,
org.jboss.tools.smooks.editor,
org.jboss.tools.smooks.graphical.editors,
- org.jboss.tools.smooks.launch
+ org.jboss.tools.smooks.launch,
+ org.jboss.tools.smooks.launch.serialize
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLaunchConfigurationDelegate.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLaunchConfigurationDelegate.java 2009-12-19
02:38:14 UTC (rev 19483)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLaunchConfigurationDelegate.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -51,6 +51,9 @@
import org.jboss.tools.smooks.configuration.RuntimeMetadata;
import org.jboss.tools.smooks.configuration.editors.uitls.ProjectClassLoader;
import org.jboss.tools.smooks.core.SmooksInputType;
+import org.jboss.tools.smooks.launch.serialize.MarshallingStrategy;
+import org.jboss.tools.smooks.launch.serialize.ObjectSerializer;
+import org.jboss.tools.smooks.launch.serialize.XPathMarshaller;
/**
* Smooks Launch Configuration Delegate.
@@ -125,6 +128,9 @@
addToCP(wsTempClasses, SmooksLauncher.class);
addToCP(wsTempClasses, SmooksInputType.class);
addToCP(wsTempClasses, ProcessNodeType.class);
+ addToCP(wsTempClasses, ObjectSerializer.class);
+ addToCP(wsTempClasses, MarshallingStrategy.class);
+ addToCP(wsTempClasses, XPathMarshaller.class);
classpath.add(wsTempClasses.getAbsolutePath());
VMRunnerConfiguration runConfig= new
VMRunnerConfiguration(SmooksLauncher.class.getName(), classpath.toArray(new
String[classpath.size()]));
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLauncher.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLauncher.java 2009-12-19
02:38:14 UTC (rev 19483)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/SmooksLauncher.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -24,21 +24,20 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
+import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
-import java.util.Map.Entry;
import javax.xml.transform.stream.StreamSource;
import org.jboss.tools.smooks.configuration.ProcessNodeType;
import org.jboss.tools.smooks.core.SmooksInputType;
+import org.jboss.tools.smooks.launch.serialize.ObjectSerializer;
import org.milyn.Smooks;
import org.milyn.payload.JavaResult;
import org.milyn.payload.StringResult;
import org.xml.sax.SAXException;
-import com.thoughtworks.xstream.XStream;
-
/**
* Smooks runtime Launcher class.
*
@@ -83,14 +82,13 @@
smooks.filterSource(new StreamSource(new FileInputStream(input)), javaResult);
}
- Set<Entry<String, Object>> bindings =
javaResult.getResultMap().entrySet();
- if(!bindings.isEmpty()) {
- System.out.println("[JavaResult Objects (XML Serialized)...]");
//$NON-NLS-1$
+ Collection<ObjectSerializer> serializedJavaResults =
ObjectSerializer.serialize(javaResult);
+ if(!serializedJavaResults.isEmpty()) {
+ System.out.println("[JavaResult Objects...]"); //$NON-NLS-1$
- for(Entry<String, Object> binding : bindings) {
- System.out.println("\n" + binding.getKey() + ":");
//$NON-NLS-1$ //$NON-NLS-2$
+ for(ObjectSerializer serializedJavaResult : serializedJavaResults) {
System.out.println(" |--"); //$NON-NLS-1$
- System.out.println(indent((new XStream()).toXML(binding.getValue())));
+ System.out.println(indent(serializedJavaResult.getSerializedForm()));
System.out.println(" |--"); //$NON-NLS-1$
}
nothingDisplayed = false;
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/MarshallingStrategy.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/MarshallingStrategy.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/MarshallingStrategy.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,51 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2009, JBoss Inc.
+ */
+package org.jboss.tools.smooks.launch.serialize;
+
+import com.thoughtworks.xstream.converters.ConverterLookup;
+import com.thoughtworks.xstream.converters.DataHolder;
+import com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.Mapper;
+
+/**
+ *
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class MarshallingStrategy extends ReferenceByXPathMarshallingStrategy {
+
+ private ObjectSerializer objectSerializer;
+
+ /**
+ * @param objectSerializer
+ */
+ public MarshallingStrategy(ObjectSerializer objectSerializer) {
+ this.objectSerializer = objectSerializer;
+ }
+
+ /* (non-Javadoc)
+ * @see
com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy#marshal(com.thoughtworks.xstream.io.HierarchicalStreamWriter,
java.lang.Object, com.thoughtworks.xstream.converters.ConverterLookup,
com.thoughtworks.xstream.mapper.Mapper, com.thoughtworks.xstream.converters.DataHolder)
+ */
+ @Override
+ public void marshal(HierarchicalStreamWriter writer, Object obj, ConverterLookup
converterLookup, Mapper mapper, DataHolder dataHolder) {
+ XPathMarshaller marshaller = new XPathMarshaller(writer, converterLookup, mapper,
ReferenceByXPathMarshallingStrategy.RELATIVE, objectSerializer);
+ marshaller.start(obj, dataHolder);
+ }
+}
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializer.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializer.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializer.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,281 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2009, JBoss Inc.
+ */
+package org.jboss.tools.smooks.launch.serialize;
+
+import java.io.StringWriter;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.milyn.payload.JavaResult;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.AbstractXmlWriter;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+/**
+ * Object Graph Serializer.
+ *
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class ObjectSerializer extends AbstractXmlWriter {
+
+ private Object rootObject;
+ private String rootBeanId;
+ private List<Object> items = new ArrayList<Object>();
+ private Set<String> referencedBeans = new LinkedHashSet<String>();
+ private Map<String, Object> beanContext;
+ private int indent = 0;
+ private boolean writeNL = false;
+ private String serializedForm;
+ private StringWriter writer;
+
+ public ObjectSerializer(Object object, String beanId, Map<String, Object> beanMap)
{
+ assertNotNull(object, "object");
+ assertNotNull(beanId, "beanId");
+ assertNotNull(beanMap, "beanMap");
+ this.rootObject = object;
+ this.rootBeanId = beanId;
+ this.beanContext = beanMap;
+ this.serializedForm = writeObject();
+ }
+
+ public static String serialize(Object object, Map<String, Object> beanMap) {
+ ObjectSerializer serializer = new ObjectSerializer(object,
object.getClass().getSimpleName(), beanMap);
+ return serializer.getSerializedForm();
+ }
+
+ public static Collection<ObjectSerializer> serialize(JavaResult javaResult) {
+ StringBuilder stringBuilder = new StringBuilder();
+ Map<String, Object> beanMap = javaResult.getResultMap();
+ Set<Entry<String, Object>> beanMapEntries = beanMap.entrySet();
+ Map<String, ObjectSerializer> serializedObjectMap = new HashMap<String,
ObjectSerializer>();
+ Set<String> beanNames = beanMap.keySet();
+
+ // Create all the ObjectSerializer instances...
+ for(Entry<String, Object> beanMapEntry : beanMapEntries) {
+ serializedObjectMap.put(beanMapEntry.getKey(), new
ObjectSerializer(beanMapEntry.getValue(), beanMapEntry.getKey(), beanMap));
+ }
+
+ // Now try to work out which can be eliminated by analysing the beans used in each
+ // ObjectSerializer instance...
+ Set<Entry<String, ObjectSerializer>> serializedObjectMapEntries =
serializedObjectMap.entrySet();
+ for(Entry<String, ObjectSerializer> serializedObjectMapEntry :
serializedObjectMapEntries) {
+ beanNames.removeAll(serializedObjectMapEntry.getValue().getReferencedBeans());
+ }
+
+ if(!beanNames.isEmpty()) {
+ // If the beanNames Set is not empty, only return those ObjectSerializer instances...
+ List<ObjectSerializer> rootBeans = new ArrayList<ObjectSerializer>();
+
+ for(String beanName : beanNames) {
+ rootBeans.add(serializedObjectMap.get(beanName));
+ }
+
+ return rootBeans;
+ } else {
+ // If the beanNames Set is empty, return all ObjectSerializer instances i.e.
+ // we're not able to work out the "root" bean(s)...
+ return serializedObjectMap.values();
+ }
+ }
+
+ public Set<String> getReferencedBeans() {
+ return referencedBeans;
+ }
+
+ public String getSerializedForm() {
+ return serializedForm;
+ }
+
+ private synchronized String writeObject() {
+ XStream xstream = new XStream(new DomDriver());
+ MarshallingStrategy marshallingStrategy = new MarshallingStrategy(this);
+
+ writer = new StringWriter();
+ items.clear();
+ referencedBeans.clear();
+ xstream.setMarshallingStrategy(marshallingStrategy);
+ xstream.marshal(rootObject, this);
+
+ return writer.toString();
+ }
+
+ /* (non-Javadoc)
+ * @see com.thoughtworks.xstream.io.xml.AbstractXmlWriter#startNode(java.lang.String,
java.lang.Class)
+ */
+ @Override
+ public void startNode(String name, Class clazz) {
+ if(writeNL) {
+ writer.write("\n");
+ }
+
+ if(indent == 0) {
+ // Root object...
+ writer.write("> " + rootBeanId);
+ } else {
+ writeIndent(indent);
+ writer.write("> " + name);
+ }
+
+ writeNL = true;
+ indent++;
+ }
+
+ public void startNode(Object item) {
+ //write(" (@" + Integer.toHexString(System.identityHashCode(item)) +
")");
+ items.add(item);
+
+ String beanId = getBeanId(item, true);
+ if(beanId != null) {
+ writer.write(" (beanId = \"" + beanId + "\")");
+ if(!beanId.equals(rootBeanId)) {
+ referencedBeans.add(beanId);
+ }
+ }
+ }
+
+ private String getBeanId(Object item, boolean checkCollections) {
+ // First see it it one of the root beans...
+ Set<Entry<String, Object>> beans = beanContext.entrySet();
+ for(Entry<String, Object> bean : beans) {
+ if(bean.getValue() == item) {
+ return bean.getKey();
+ }
+ }
+
+ if(checkCollections) {
+ // Then, see is it in a Collection bean in one of the root beans...
+ for(Entry<String, Object> bean : beans) {
+ Object beanObj = bean.getValue();
+
+ if(beanObj instanceof Collection) {
+ Collection collectionBean = (Collection)beanObj;
+ if(collectionContains(collectionBean, item)) {
+ // Get the beanId of the first object in the collection
+ // that also exists in the root of the beanContext...
+ for (Object collectionItem : collectionBean) {
+ String beanId = getBeanId(collectionItem, false);
+ if(beanId != null) {
+ return beanId;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private boolean collectionContains(Collection collection, Object item) {
+ // Intentionally not using Collection.contains() because it uses
+ // Object.equals(), which is not what we want... we need an exact object ref match...
+ for(Object object : collection) {
+ if(object == item) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see com.thoughtworks.xstream.io.HierarchicalStreamWriter#setValue(java.lang.String)
+ */
+ public void setValue(String text) {
+ Object currentItem = items.get(items.size() - 1);
+
+ if((currentItem instanceof String || containsNonNumeric(text)) && !(currentItem
instanceof Number)) {
+ writer.write(" = \"" + text + "\"");
+ } else if(currentItem instanceof Number) {
+ Class<? extends Object> itemClass = currentItem.getClass();
+ if(itemClass.getPackage() == Integer.class.getPackage()) {
+ writer.write(" = " + text + itemClass.getSimpleName().charAt(0));
+ } else {
+ writer.write(" = " + text);
+ }
+ } else {
+ writer.write(" = " + text);
+ }
+
+ writer.write("\n");
+ writeNL = false;
+ }
+
+ /* (non-Javadoc)
+ * @see com.thoughtworks.xstream.io.HierarchicalStreamWriter#endNode()
+ */
+ public void endNode() {
+ indent--;
+ }
+
+ /* (non-Javadoc)
+ * @see
com.thoughtworks.xstream.io.HierarchicalStreamWriter#startNode(java.lang.String)
+ */
+ public void startNode(String name) {
+ }
+
+ /* (non-Javadoc)
+ * @see com.thoughtworks.xstream.io.HierarchicalStreamWriter#flush()
+ */
+ public void flush() {
+ writer.flush();
+ }
+
+ private void writeIndent(int count) {
+ for(int i = 0; i < count; i++) {
+ writer.write(" "); //$NON-NLS-1$
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see
com.thoughtworks.xstream.io.HierarchicalStreamWriter#addAttribute(java.lang.String,
java.lang.String)
+ */
+ public void addAttribute(String name, String value) {
+ }
+
+ /* (non-Javadoc)
+ * @see com.thoughtworks.xstream.io.HierarchicalStreamWriter#close()
+ */
+ public void close() {
+ }
+
+ private boolean containsNonNumeric(String text) {
+ for(int i = 0; i < text.length(); i++) {
+ if(!Character.isDigit(text.charAt(i))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private void assertNotNull(Object object, String name) {
+ if(object == null) {
+ throw new IllegalArgumentException("ObjectSerializer argument '" + name
+ "' is null.");
+ }
+ }
+}
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/XPathMarshaller.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/XPathMarshaller.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/launch/serialize/XPathMarshaller.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,52 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2009, JBoss Inc.
+ */
+package org.jboss.tools.smooks.launch.serialize;
+
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.ConverterLookup;
+import com.thoughtworks.xstream.core.ReferenceByXPathMarshaller;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.Mapper;
+
+/**
+ * XStream object marshaller.
+ * <p/>
+ * Provides access to the current object/item being processed.
+ *
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class XPathMarshaller extends ReferenceByXPathMarshaller {
+
+ private ObjectSerializer objectSerializer;
+
+ public XPathMarshaller(HierarchicalStreamWriter writer, ConverterLookup converterLookup,
Mapper mapper, int mode, ObjectSerializer objectSerializer) {
+ super(writer, converterLookup, mapper, mode);
+ this.objectSerializer = objectSerializer;
+ }
+
+ /* (non-Javadoc)
+ * @see
com.thoughtworks.xstream.core.AbstractReferenceMarshaller#convert(java.lang.Object,
com.thoughtworks.xstream.converters.Converter)
+ */
+ @Override
+ public void convert(Object item, Converter converter) {
+ objectSerializer.startNode(item);
+ super.convert(item, converter);
+ }
+}
Added: trunk/smooks/tests/org.jboss.tools.smooks.ui.test/META-INF/MANIFEST.MF
===================================================================
--- trunk/smooks/tests/org.jboss.tools.smooks.ui.test/META-INF/MANIFEST.MF
(rev 0)
+++ trunk/smooks/tests/org.jboss.tools.smooks.ui.test/META-INF/MANIFEST.MF 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,15 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %Bundle-Name.0
+Bundle-SymbolicName: org.jboss.tools.smooks.ui.test
+Bundle-Version: 1.0.0
+Bundle-ClassPath: lib/xmlunit-1.1.jar,
+ .,
+ lib/xstream-1.2.2.jar
+Bundle-Vendor: %Bundle-Vendor.0
+Bundle-Localization: plugin
+Require-Bundle: org.junit,
+ org.jboss.tools.smooks.runtime,
+ org.jboss.tools.smooks.ui
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Bundle-ActivationPolicy: lazy
Added: trunk/smooks/tests/org.jboss.tools.smooks.ui.test/build.properties
===================================================================
--- trunk/smooks/tests/org.jboss.tools.smooks.ui.test/build.properties
(rev 0)
+++ trunk/smooks/tests/org.jboss.tools.smooks.ui.test/build.properties 2009-12-19 10:26:20
UTC (rev 19484)
@@ -0,0 +1,8 @@
+output.. = bin/
+bin.includes = META-INF/,\
+ plugin.properties,\
+ projects/,\
+ lib/xmlunit-1.1.jar,\
+ .,\
+ lib/xstream-1.2.2.jar
+source.. = src/
Added: trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xmlunit-1.1.jar
===================================================================
(Binary files differ)
Property changes on:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xmlunit-1.1.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xstream-1.2.2.jar
===================================================================
(Binary files differ)
Property changes on:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/lib/xstream-1.2.2.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: trunk/smooks/tests/org.jboss.tools.smooks.ui.test/plugin.properties
===================================================================
--- trunk/smooks/tests/org.jboss.tools.smooks.ui.test/plugin.properties
(rev 0)
+++ trunk/smooks/tests/org.jboss.tools.smooks.ui.test/plugin.properties 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,3 @@
+#Properties file for org.jboss.tools.smooks.test
+Bundle-Vendor.0 = JBoss by Red Hat
+Bundle-Name.0 = Smooks Tools Templating Test Plug-in
\ No newline at end of file
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Header.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Header.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Header.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,51 @@
+/*
+ Milyn - Copyright (C) 2006
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License (version 2.1) as published by the Free Software
+ Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ See the GNU Lesser General Public License for more details:
+
http://www.gnu.org/licenses/lgpl.txt
+*/
+package org.jboss.tools.smooks.launch.serialize;
+
+import java.util.Date;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
+ */
+public class Header {
+ private Date date;
+ private Long customerNumber = 188276L;
+ private String customerName = "Mr. Fox";
+
+ public Date getDate() {
+ return date;
+ }
+
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+ public Long getCustomerNumber() {
+ return customerNumber;
+ }
+
+ public void setCustomerNumber(Long customerNumber) {
+ this.customerNumber = customerNumber;
+ }
+
+ public String getCustomerName() {
+ return customerName;
+ }
+
+ public void setCustomerName(String customerName) {
+ this.customerName = customerName;
+ }
+}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializerText.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializerText.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/ObjectSerializerText.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,68 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2009, JBoss Inc.
+ */
+package org.jboss.tools.smooks.launch.serialize;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.milyn.payload.JavaResult;
+import org.xml.sax.SAXException;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ *
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class ObjectSerializerText extends TestCase {
+
+ public void test() throws IOException, SAXException {
+ Map<String, Object> beanContext = new HashMap<String, Object>();
+ Order order = new Order();
+ List<OrderItem> items = order.getOrderItems();
+
+ beanContext.put("order", order);
+ beanContext.put("header", order.getHeader());
+ beanContext.put("orderItems", items);
+
+ items.add(new OrderItem().setProductId(55126).setQuantity(6).setPrice(65.98));
+ items.add(new OrderItem().setProductId(23423).setQuantity(3).setPrice(8.42));
+ items.add(new OrderItem().setProductId(45645).setQuantity(7).setPrice(999.00));
+
+ beanContext.put("orderItem", items.get(items.size() - 1));
+
+ JavaResult javaResult = new JavaResult(beanContext);
+
+ Collection<ObjectSerializer> rootBeans = ObjectSerializer.serialize(javaResult);
+
+ // ObjectSerializer should work out that there's just 1 root
+ // bean i.e. the "order" bean...
+ assertEquals(1, rootBeans.size());
+ for(ObjectSerializer serializer : rootBeans) {
+ //System.out.println(serializer.getSerializedForm());
+ TestUtil.assertEquals(serializer.getSerializedForm(), "expected-01.txt",
getClass());
+ assertEquals("[header, orderItems, orderItem]",
serializer.getReferencedBeans().toString());
+ }
+ }
+}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Order.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Order.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/Order.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,43 @@
+/*
+ Milyn - Copyright (C) 2006
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License (version 2.1) as published by the Free Software
+ Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ See the GNU Lesser General Public License for more details:
+
http://www.gnu.org/licenses/lgpl.txt
+*/
+package org.jboss.tools.smooks.launch.serialize;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
+ */
+public class Order {
+ private Header header = new Header();
+ private List<OrderItem> orderItems = new ArrayList<OrderItem>();
+
+ public Header getHeader() {
+ return header;
+ }
+
+ public void setHeader(Header header) {
+ this.header = header;
+ }
+
+ public List<OrderItem> getOrderItems() {
+ return orderItems;
+ }
+
+ public void setOrderItems(List<OrderItem> orderItems) {
+ this.orderItems = orderItems;
+ }
+}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/OrderItem.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/OrderItem.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/OrderItem.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,52 @@
+/*
+ Milyn - Copyright (C) 2006
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License (version 2.1) as published by the Free Software
+ Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ See the GNU Lesser General Public License for more details:
+
http://www.gnu.org/licenses/lgpl.txt
+*/
+package org.jboss.tools.smooks.launch.serialize;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
+ */
+public class OrderItem {
+ private long productId;
+ private Integer quantity;
+ private double price;
+
+ public long getProductId() {
+ return productId;
+ }
+
+ public OrderItem setProductId(long productId) {
+ this.productId = productId;
+ return this;
+ }
+
+ public Integer getQuantity() {
+ return quantity;
+ }
+
+ public OrderItem setQuantity(Integer quantity) {
+ this.quantity = quantity;
+ return this;
+ }
+
+ public double getPrice() {
+ return price;
+ }
+
+ public OrderItem setPrice(double price) {
+ this.price = price;
+ return this;
+ }
+}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/TestUtil.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/TestUtil.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/TestUtil.java 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,69 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.tools.smooks.launch.serialize;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import junit.framework.TestCase;
+
+import org.custommonkey.xmlunit.XMLAssert;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.milyn.io.StreamUtils;
+import org.milyn.xml.XmlUtil;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public abstract class TestUtil {
+
+ public static void assertXMLEquals(String xmlText, String xmlFileOnCP, Class refClass)
throws IOException, SAXException {
+ //System.out.println(xmlText);
+ XMLUnit.setIgnoreWhitespace( true );
+ XMLAssert.assertXMLEqual(new
InputStreamReader(refClass.getResourceAsStream(xmlFileOnCP)), new
StringReader(xmlText));
+ }
+
+ public static void assertEquals(String text, String fileOnCP, Class refClass) throws
IOException, SAXException {
+ //System.out.println(text);
+ String fileContents = readFileContents(fileOnCP, refClass);
+ TestCase.assertEquals(normalizeCRLF(fileContents.trim()),
normalizeCRLF(text.trim()));
+ }
+
+ public static String readFileContents(String fileOnCP, Class refClass) throws
IOException {
+ return StreamUtils.readStreamAsString(refClass.getResourceAsStream(fileOnCP));
+ }
+
+ public static String normalizeCRLF(String text) {
+ text = text.replace("\r\n", "\n");
+ text = text.replace("\n\r", "\n");
+ text = text.replace("\r", "\n");
+ return text;
+ }
+
+ public static Document toDOM(String theTemplate) throws ParserConfigurationException,
IOException, SAXException {
+ return XmlUtil.parseStream(new ByteArrayInputStream(theTemplate.getBytes()));
+ }
+}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/expected-01.txt
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/expected-01.txt
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.ui.test/src/org/jboss/tools/smooks/launch/serialize/expected-01.txt 2009-12-19
10:26:20 UTC (rev 19484)
@@ -0,0 +1,17 @@
+> order (beanId = order)
+ > header (beanId = header)
+ > customerNumber = 188276L
+ > customerName = "Mr. Fox"
+ > orderItems (beanId = orderItems)
+ > org.jboss.tools.smooks.launch.serialize.OrderItem (beanId = orderItem)
+ > productId = 55126L
+ > quantity = 6I
+ > price = 65.98D
+ > org.jboss.tools.smooks.launch.serialize.OrderItem (beanId = orderItem)
+ > productId = 23423L
+ > quantity = 3I
+ > price = 8.42D
+ > org.jboss.tools.smooks.launch.serialize.OrderItem (beanId = orderItem)
+ > productId = 45645L
+ > quantity = 7I
+ > price = 999.0D
\ No newline at end of file