All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.plantuml.acearth.PSystemXearth Maven / Gradle / Ivy

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PlantUML 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 General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 * 
 *
 */
package net.sourceforge.plantuml.acearth;

import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import ext.plantuml.com.ctreber.acearth.ACearth;
import ext.plantuml.com.ctreber.acearth.ConfigurationACearth;
import ext.plantuml.com.ctreber.acearth.plugins.markers.Marker;
import net.sourceforge.plantuml.AbstractPSystem;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.api.ImageDataSimple;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
import net.sourceforge.plantuml.core.UmlSource;

public class PSystemXearth extends AbstractPSystem {
	// ::remove folder when __CORE__
	// ::remove folder when __MIT__

	final private int width;
	final private int height;
	final private Map config;
	final private List markers;

	final private Collection enums = Arrays.asList("viewPositionType");
	final private Collection doubles = Arrays.asList("sunPosRelLat", "sunPosRelLong", "orbitPeriod",
			"orbitInclination", "viewPosLat", "viewPosLong", "starFrequency", "viewMagnification");
	final private Collection integers = Arrays.asList("daySideBrightness", "nightSideBrightness",
			"terminatorDiscontinuity", "gridDivision", "gridPixelDivision", "bigStars");
	final private Collection booleans = Arrays.asList("shadeP", "gridP", "starsP");

	public PSystemXearth(UmlSource source, int width, int height, Map config, List markers) {
		super(source);
		this.width = width;
		this.height = height;
		this.config = config;
		this.markers = markers;
	}

	@Override
	final protected ImageData exportDiagramNow(OutputStream os, int num, FileFormatOption fileFormat)
			throws IOException {
		synchronized (PSystemXearth.class) {
			final ACearth earth = new ACearth(markers);
			final ConfigurationACearth conf = earth.getConf();
			conf.setInt("imageWidth", width);
			conf.setInt("imageHeight", height);

			for (Map.Entry ent : config.entrySet()) {
				final String key = ent.getKey();
				final String value = ent.getValue();
				if (key.equalsIgnoreCase("GMT")) {
					final Date date = extractGmt(value);
					conf.setInt("fixedTime", (int) (date.getTime() / 1000L));
				} else if (enums.contains(key)) {
					conf.getMOEnum(key).set(value);
				} else if (doubles.contains(key)) {
					conf.setDouble(key, Double.parseDouble(value));
				} else if (integers.contains(key)) {
					conf.setInt(key, Integer.parseInt(value));
				} else if (booleans.contains(key)) {
					conf.setBoolean(key, value.equalsIgnoreCase("true"));
				} else {
					throw new UnsupportedOperationException(key);
				}
			}
			earth.exportPng(os);
			return new ImageDataSimple(width, height);
		}
	}

	private Date extractGmt(String s) {
		final SimpleDateFormat timeFormat;
		if (s.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
			timeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
		} else if (s.matches("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}")) {
			timeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.US);
		} else {
			throw new UnsupportedOperationException(s);
		}
		timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		try {
			return timeFormat.parse(s);
		} catch (ParseException e) {
			throw new UnsupportedOperationException(s);
		}

	}

	public DiagramDescription getDescription() {
		return new DiagramDescription("(XEarth)");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy