001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.io.PrintWriter;
005import java.util.Objects;
006
007/**
008 * This factory is called by everyone who needs an OsmWriter object,
009 * instead of directly calling the OsmWriter constructor.
010 *
011 * This enables plugins to substitute the original OsmWriter with
012 * their own version, altering the way JOSM writes objects to the
013 * server, and to disk.
014 *
015 * @author Frederik Ramm
016 *
017 */
018public class OsmWriterFactory {
019
020    private static volatile OsmWriterFactory theFactory;
021
022    /**
023     * Creates new {@code OsmWriter}.
024     * @param out print writer
025     * @param osmConform if {@code true}, prevents modification attributes to be written to the common part
026     * @param version OSM API version (0.6)
027     * @return new {@code OsmWriter}
028     */
029    public static OsmWriter createOsmWriter(PrintWriter out, boolean osmConform, String version) {
030        // pre-set factory with this default implementation; can still be overwritten
031        // later. note that the default factory may already be used for constructing
032        // OsmWriters during the startup process.
033        if (theFactory == null) {
034            theFactory = new OsmWriterFactory();
035        }
036        return theFactory.createOsmWriterImpl(out, osmConform, version);
037    }
038
039    /**
040     * Sets the default factory.
041     * @param factory new default factory
042     * @since 11851
043     */
044    public static void setDefaultFactory(OsmWriterFactory factory) {
045        theFactory = Objects.requireNonNull(factory);
046    }
047
048    /**
049     * Creates new {@code OsmWriter}.
050     * @param out print writer
051     * @param osmConform if {@code true}, prevents modification attributes to be written to the common part
052     * @param version OSM API version (0.6)
053     * @return new {@code OsmWriter}
054     */
055    protected OsmWriter createOsmWriterImpl(PrintWriter out, boolean osmConform, String version) {
056        return new OsmWriter(out, osmConform, version);
057    }
058}