statoolinfosweb/src/fr/devinsy/statoolinfos/io/JSONFile.java

190 lines
5.3 KiB
Java
Raw Normal View History

2021-01-23 20:06:42 +01:00
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple service statistics tool.
*
* StatoolInfos is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* StatoolInfos is 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with StatoolInfos. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.core.Federation;
import fr.devinsy.statoolinfos.core.Organization;
import fr.devinsy.statoolinfos.core.Organizations;
import fr.devinsy.statoolinfos.core.Service;
import fr.devinsy.statoolinfos.core.Services;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class JSONFile.
*/
public class JSONFile
{
private static final Logger logger = LoggerFactory.getLogger(JSONFile.class);
public static final String DEFAULT_CHARSET_NAME = "UTF-8";
/**
* Escape.
*
* @param source
* the source
* @return the string
*/
public static String escapeJSON(final String source)
{
String result;
result = StringEscapeUtils.escapeJson(source);
//
return result;
}
/**
* Save.
*
* @param file
* the file
* @param federation
* the federation
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Federation federation) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"federation\" : ");
lines.addAll(PathPropertyUtils.toJSON(federation));
lines.removeLast();
lines.appendln(",");
lines.appendln("\"organizations\" : [");
for (Organization organization : federation.getOrganizations())
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.removeLast();
lines.appendln(",");
lines.append("\"services\" : [");
for (Service service : organization.getServices())
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.appendln(",");
}
if (!organization.getServices().isEmpty())
{
lines.removeLast(2);
}
lines.appendln();
lines.appendln("]");
lines.append("}");
lines.appendln(",");
}
if (!federation.getOrganizations().isEmpty())
{
lines.removeLast(2);
lines.appendln();
}
lines.appendln("]");
lines.appendln("}");
lines.appendln("}");
StringsUtils.writeToFile(file, lines);
}
/**
* Save.
*
* @param file
* the file
* @param organizations
* the organizations
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Organizations organizations) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"organizations\" : [");
for (Organization organization : organizations)
{
lines.addAll(PathPropertyUtils.toJSON(organization));
lines.append(",");
}
if (!organizations.isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
StringsUtils.writeToFile(file, lines);
}
/**
* Save.
*
* @param file
* the file
* @param services
* the services
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
public static void save(final File file, final Services services) throws UnsupportedEncodingException, FileNotFoundException
{
StringList lines = new StringList();
lines.appendln("{ \"services\" : [");
for (Service service : services)
{
lines.addAll(PathPropertyUtils.toJSON(service));
lines.append(",");
}
if (!services.isEmpty())
{
lines.removeLast();
}
lines.appendln("] }");
StringsUtils.writeToFile(file, lines);
}
}