View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: AttributeManagerConverter.java
13   * Created: 2007-00-00
14   * Author: awos
15   * $Id: AttributeManagerConverter.java 3020 2009-07-17 14:41:19Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.backward.resources.xml;
19  
20  import java.util.HashMap;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.eclipse.osgi.util.NLS;
27  
28  import pl.edu.agh.cast.Activator;
29  import pl.edu.agh.cast.model.attributes.Attribute;
30  import pl.edu.agh.cast.model.attributes.AttributeManager;
31  import pl.edu.agh.cast.model.attributes.ConnectionGroupAttributeManager;
32  import pl.edu.agh.cast.model.attributes.DiagramAttributeManager;
33  import pl.edu.agh.cast.model.attributes.LegendAttributeManager;
34  import pl.edu.agh.cast.model.attributes.NodeAttributeManager;
35  
36  import com.thoughtworks.xstream.converters.Converter;
37  import com.thoughtworks.xstream.converters.MarshallingContext;
38  import com.thoughtworks.xstream.converters.UnmarshallingContext;
39  import com.thoughtworks.xstream.io.HierarchicalStreamReader;
40  import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
41  
42  /**
43   * A XStream {@link Converter} for serializing and deserializing {@link AttributeManager}s.
44   *
45   * @author AGH CAST Team
46   */
47  public class AttributeManagerConverter implements Converter {
48  
49  	private static final String ATTRIBUTE_NODE_NAME = "attribute";
50  
51  	private static final String TYPE_ATTRIBUTE_NAME = "type";
52  
53  	private static Map<Class<? extends AttributeManager>, String> knownManagers;
54  	static {
55  		knownManagers = new HashMap<Class<? extends AttributeManager>, String>();
56  		knownManagers.put(DiagramAttributeManager.class, "diagram");
57  		knownManagers.put(NodeAttributeManager.class, "node");
58  		knownManagers.put(ConnectionGroupAttributeManager.class, "connectionGroup");
59  		knownManagers.put(LegendAttributeManager.class, "legend");
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 *
65  	 * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
66  	 *      com.thoughtworks.xstream.io.HierarchicalStreamWriter,
67  	 *      com.thoughtworks.xstream.converters.MarshallingContext)
68  	 */
69  	public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
70  		AttributeManager manager = (AttributeManager)object;
71  		writer.addAttribute(TYPE_ATTRIBUTE_NAME, knownManagers.get(object.getClass()));
72  		for (Attribute a : manager.getAttributes()) {
73  			writer.startNode(ATTRIBUTE_NODE_NAME);
74  			context.convertAnother(a);
75  			writer.endNode();
76  		}
77  	}
78  
79  	/**
80  	 * {@inheritDoc}
81  	 *
82  	 * @see com.thoughtworks.xstream.converters.Converter
83  	 *      #unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
84  	 *      com.thoughtworks.xstream.converters.UnmarshallingContext)
85  	 */
86  	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
87  		String type = reader.getAttribute(TYPE_ATTRIBUTE_NAME);
88  		AttributeManager ab = null;
89  		for (Entry<Class<? extends AttributeManager>, String> entry : knownManagers.entrySet()) {
90  			if (entry.getValue().equals(type)) {
91  				try {
92  					ab = entry.getKey().newInstance();
93  				} catch (Exception e) {
94  					String str = NLS.bind("Cannot instantiate attribute manager of class {0}, type {1}", entry.getKey()
95  					        .getCanonicalName(), type);
96  					Activator.getLogger().error(str, e);
97  				}
98  			}
99  		}
100 		if (ab != null) {
101 			List<Attribute> attributes = new LinkedList<Attribute>();
102 			while (reader.hasMoreChildren()) {
103 				reader.moveDown();
104 				// other tags are ignored
105 				if (ATTRIBUTE_NODE_NAME.equals(reader.getNodeName())) {
106 					attributes.add((Attribute)context.convertAnother(ab, Attribute.class));
107 				}
108 				reader.moveUp();
109 			}
110 			ab.init(attributes);
111 		}
112 		return ab;
113 	}
114 
115 	/**
116 	 * {@inheritDoc}
117 	 *
118 	 * @see com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java.lang.Class)
119 	 */
120 	@SuppressWarnings("unchecked")
121 	public boolean canConvert(Class arg0) {
122 		return knownManagers.containsKey(arg0);
123 	}
124 
125 }