package aimax.osm.writer; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Collection; import java.util.HashSet; import java.util.logging.Level; import java.util.logging.Logger; import aimax.osm.data.BoundingBox; import aimax.osm.data.OsmMap; import aimax.osm.data.entities.EntityAttribute; import aimax.osm.data.entities.MapNode; import aimax.osm.data.entities.MapWay; import aimax.osm.reader.OsmRuntimeException; /** * Writes a map to file using the standard osm XML format. * @author Ruediger Lunde */ public class OsmWriter implements MapWriter { private static Logger LOG = Logger.getLogger("aimax.osm"); /** * Writes all data from mapData to file. */ public void writeMap(File file, OsmMap map, BoundingBox bb) { try { FileOutputStream fs = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter (new BufferedOutputStream(fs), "UTF-8"); writeMap(writer, map, bb); } catch (FileNotFoundException fnfe) { LOG.warning("File does not exist "+file); } catch (UnsupportedEncodingException fnfe) { LOG.warning("UTF-8 encoding not supported, sorry."); } } /** * Writes all data from mapData to a stream. */ public void writeMap(OutputStreamWriter writer, OsmMap map, BoundingBox bb) { try { StringBuffer text = new StringBuffer(); text.append("\n"); text.append("\n"); text.append("\n"); writer.write(text.toString()); HashSet nodeHash = new HashSet(); Collection ways = map.getWays(bb); for (MapWay way : ways) for (MapNode node : way.getNodes()) if (!nodeHash.contains(node)) { writeNode(writer, node); nodeHash.add(node); } for (MapNode poi : map.getPois(bb)) if (!nodeHash.contains(poi)) { writeNode(writer, poi); nodeHash.add(poi); } for (MapWay way : ways) writeWay(writer, way); writer.write("\n"); } catch (IOException e) { throw new OsmRuntimeException("Unable to write XML output to file.", e); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { LOG.log(Level.SEVERE, "Unable to close output stream.", e); } } } } public String[] fileFormatDescriptions() { return new String[] {"OSM File (osm)"}; } public String[] fileFormatExtensions() { return new String[] {"osm"}; } protected void writeNode(OutputStreamWriter writer, MapNode node) throws IOException { StringBuffer text = new StringBuffer(); text.append("\n"); } else { text.append("\">\n"); addTags(text, node.getName(), node.getAttributes()); text.append("\n"); } writer.append(text.toString()); } /**/ protected void writeWay(OutputStreamWriter writer, MapWay way) throws IOException { StringBuffer text = new StringBuffer(); text.append("\n"); for (MapNode node : way.getNodes()) { text.append(" \n"); } addTags(text, way.getName(), way.getAttributes()); text.append("\n"); writer.append(text.toString()); } /* */ protected void addTags(StringBuffer text, String name, EntityAttribute[] atts) { if (name != null) { text.append(" \n"); } for (EntityAttribute att : atts) { text.append(" \n"); } } /** * & is replaced by & * < is replaced by < * > is replaced by > * " is replaced by " */ protected String convertToXML(String text) { text = text.replace("&", "&"); text = text.replace("<", "<"); text = text.replace(">", ">"); text = text.replace("\"", """); return text; } }