View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.agent.module.sensor;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.FileReader;
6   import java.io.FileWriter;
7   import java.io.IOException;
8   import java.io.PrintWriter;
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import java.util.Collections;
12  import java.util.Comparator;
13  import java.util.List;
14  
15  import com.thoughtworks.xstream.XStream;
16  import com.thoughtworks.xstream.annotations.XStreamAlias;
17  import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
18  import com.thoughtworks.xstream.annotations.XStreamImplicit;
19  import com.thoughtworks.xstream.io.xml.DomDriver;
20  
21  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
22  import cz.cuni.amis.utils.exception.PogamutIOException;
23  
24  @XStreamAlias("UT2004Map")
25  public class MapExport {
26  	
27  	private static final Comparator<NavPointExport> NAVPOINT_ID_COMPARATOR = new Comparator<NavPointExport>() {
28  
29  		@Override
30  		public int compare(NavPointExport o1, NavPointExport o2) {
31  			if (o1.Id == null) {
32  				if (o2.Id == null) return 0;
33  				return -1;
34  			} else {
35  				if (o2.Id == null) return 1;
36  				return o1.Id.compareTo(o2.Id);
37  			}
38  		}
39  	};
40  
41  	private static XStream xstream;
42  	
43  	public static XStream getXStream() {
44  		if (xstream != null) return xstream;
45  		
46  		xstream = new XStream(new DomDriver());
47  		xstream.autodetectAnnotations(true);
48  		xstream.alias(MapExport.class.getAnnotation(XStreamAlias.class).value(), MapExport.class);
49  		
50  		return xstream;
51  	}
52  	
53  	@XStreamAsAttribute
54  	public String name;
55  	
56  	@XStreamAsAttribute
57  	public long timestamp;
58  	
59  	@XStreamImplicit(itemFieldName="NavPoint")
60  	public List<NavPointExport> navPoints;
61  	
62  	public MapExport() {
63  	}
64  	
65  	public MapExport(String name, Collection<NavPoint> navPoints) {
66  		this.name = name;
67  		this.timestamp = System.currentTimeMillis();
68  		this.navPoints = new ArrayList<NavPointExport>(navPoints.size());
69  		for (NavPoint navPoint : navPoints) { 
70  			this.navPoints.add(new NavPointExport(navPoint));
71  		}
72  		Collections.sort(this.navPoints, NAVPOINT_ID_COMPARATOR);
73  	}
74  	
75  	public static MapExport loadXML(File xmlFile) {
76  		if (xmlFile == null) {
77  			throw new IllegalArgumentException("'xmlFile' can't be null!");
78  		}
79  		FileReader reader;
80  		try {
81  			reader = new FileReader(xmlFile);
82  		} catch (FileNotFoundException e1) {
83  			throw new RuntimeException("File " + xmlFile.getAbsolutePath() + " not found: " + e1.getMessage(), e1);
84  		}
85  		XStream xstream = getXStream();
86  		Object obj = xstream.fromXML(reader);
87  		try {
88  			reader.close();
89  		} catch (IOException e) {
90  		}
91  		if (obj == null || !(obj instanceof MapExport)) {
92  			throw new RuntimeException("file " + xmlFile.getAbsolutePath() + " doesn't contain a xml with MapExport");
93  		}
94  		return (MapExport)obj;
95  	}
96  	
97  	public void saveXML(File xmlFile) {		
98  		XStream xstream = getXStream();
99  		
100 		PrintWriter writer;
101 		
102 		try {
103 			writer = new PrintWriter(new FileWriter(xmlFile));
104 		} catch (IOException e) {
105 			throw new PogamutIOException("Failed to open file " + xmlFile.getAbsolutePath() + " for writing.", e);
106 		}
107 		
108 		xstream.toXML(this, writer);	
109 		
110 		writer.close();
111 	}
112 
113 }