Refactored PathProperties.

This commit is contained in:
Christian P. MOMON 2020-09-17 02:28:37 +02:00
parent 1d49fd1871
commit 3de7de0ef1
9 changed files with 79 additions and 47 deletions

View file

@ -28,6 +28,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyList;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils; import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
@ -133,9 +134,9 @@ public class CrawlCache
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public PathPropertyList restoreProperties(final URL url) throws IOException public PathProperties restoreProperties(final URL url) throws IOException
{ {
PathPropertyList result; PathProperties result;
if (url == null) if (url == null)
{ {
@ -189,7 +190,7 @@ public class CrawlCache
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public File storeProperties(final URL url, final PathPropertyList properties) throws IOException public File storeProperties(final URL url, final PathProperties properties) throws IOException
{ {
File result; File result;

View file

@ -26,8 +26,8 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathProperty; import fr.devinsy.statoolinfos.properties.PathProperty;
import fr.devinsy.statoolinfos.properties.PathPropertyList;
/** /**
* The Class PathProperty. * The Class PathProperty.
@ -50,7 +50,7 @@ public class Factory
* @throws StatoolInfosException * @throws StatoolInfosException
* @throws IOException * @throws IOException
*/ */
public static Federation loadFederation(final PathPropertyList properties) throws StatoolInfosException, IOException public static Federation loadFederation(final PathProperties properties) throws StatoolInfosException, IOException
{ {
Federation result; Federation result;
@ -61,12 +61,12 @@ public class Factory
result = new Federation(properties); result = new Federation(properties);
PathPropertyList subs = result.getByPrefix("subs"); PathProperties subs = result.getByPrefix("subs");
for (PathProperty property : subs) for (PathProperty property : subs)
{ {
if (StringUtils.startsWith(property.getValue(), "http")) if (StringUtils.startsWith(property.getValue(), "http"))
{ {
PathPropertyList subProperties = cache.restoreProperties(new URL(property.getValue())); PathProperties subProperties = cache.restoreProperties(new URL(property.getValue()));
Organization organization = loadOrganization(subProperties, cache); Organization organization = loadOrganization(subProperties, cache);
result.getOrganizations().add(organization); result.getOrganizations().add(organization);
@ -84,7 +84,7 @@ public class Factory
* @throws StatoolInfosException * @throws StatoolInfosException
* @throws IOException * @throws IOException
*/ */
public static Organization loadOrganization(final PathPropertyList properties) throws StatoolInfosException, IOException public static Organization loadOrganization(final PathProperties properties) throws StatoolInfosException, IOException
{ {
Organization result; Organization result;
@ -109,18 +109,18 @@ public class Factory
* @return the organization * @return the organization
* @throws IOException * @throws IOException
*/ */
public static Organization loadOrganization(final PathPropertyList properties, final CrawlCache cache) throws IOException public static Organization loadOrganization(final PathProperties properties, final CrawlCache cache) throws IOException
{ {
Organization result; Organization result;
result = new Organization(properties); result = new Organization(properties);
PathPropertyList section = result.getByPrefix("subs"); PathProperties section = result.getByPrefix("subs");
for (PathProperty property : section) for (PathProperty property : section)
{ {
if (StringUtils.startsWith(property.getValue(), "http")) if (StringUtils.startsWith(property.getValue(), "http"))
{ {
PathPropertyList subProperties = cache.restoreProperties(new URL(property.getValue())); PathProperties subProperties = cache.restoreProperties(new URL(property.getValue()));
Service service = loadService(subProperties); Service service = loadService(subProperties);
result.getServices().add(service); result.getServices().add(service);
@ -136,7 +136,7 @@ public class Factory
* *
* @return the service * @return the service
*/ */
public static Service loadService(final PathPropertyList properties) public static Service loadService(final PathProperties properties)
{ {
Service result; Service result;

View file

@ -20,6 +20,7 @@ package fr.devinsy.statoolinfos.core;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyList;
/** /**
@ -45,7 +46,7 @@ public class Federation extends PathPropertyList
* @param properties * @param properties
* the properties * the properties
*/ */
public Federation(final PathPropertyList properties) public Federation(final PathProperties properties)
{ {
super(properties); super(properties);

View file

@ -20,6 +20,7 @@ package fr.devinsy.statoolinfos.core;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyList;
/** /**
@ -45,7 +46,7 @@ public class Organization extends PathPropertyList
* @param properties * @param properties
* the properties * the properties
*/ */
public Organization(final PathPropertyList properties) public Organization(final PathProperties properties)
{ {
super(properties); super(properties);

View file

@ -18,6 +18,7 @@
*/ */
package fr.devinsy.statoolinfos.core; package fr.devinsy.statoolinfos.core;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyList;
/** /**
@ -41,7 +42,7 @@ public class Service extends PathPropertyList
* @param properties * @param properties
* the properties * the properties
*/ */
public Service(final PathPropertyList properties) public Service(final PathProperties properties)
{ {
super(properties); super(properties);
} }

View file

@ -28,6 +28,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.htmlize.Htmlizer; import fr.devinsy.statoolinfos.htmlize.Htmlizer;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathProperty; import fr.devinsy.statoolinfos.properties.PathProperty;
import fr.devinsy.statoolinfos.properties.PathPropertyList; import fr.devinsy.statoolinfos.properties.PathPropertyList;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils; import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
@ -52,7 +53,7 @@ public class StatoolInfos
public static void build(final File input) throws StatoolInfosException, IOException public static void build(final File input) throws StatoolInfosException, IOException
{ {
logger.info("Build {}", input.getAbsolutePath()); logger.info("Build {}", input.getAbsolutePath());
PathPropertyList inputProperties = PathPropertyUtils.load(input); PathProperties inputProperties = PathPropertyUtils.load(input);
String buildDirectoryName = inputProperties.get("conf.build.directory"); String buildDirectoryName = inputProperties.get("conf.build.directory");
if (StringUtils.isBlank(buildDirectoryName)) if (StringUtils.isBlank(buildDirectoryName))
@ -66,26 +67,25 @@ public class StatoolInfos
if (targetDirectory.exists()) if (targetDirectory.exists())
{ {
// Load configuration file. // Load configuration file.
File targetFile = new File(targetDirectory, input.getName());
// PathProperties targetProperties = new PathPropertyList();
PathPropertyList targetProperties = new PathPropertyList();
// Add generator paths. // Add generator paths.
PathPropertyList fileSection = new PathPropertyList(); PathProperties fileSection = new PathPropertyList();
fileSection.put("file.class", inputProperties.get("conf.class")); fileSection.put("file.class", inputProperties.get("conf.class"));
fileSection.put("file.generator", "StatoolInfos"); fileSection.put("file.generator", "StatoolInfos");
fileSection.put("file.datetime", LocalDateTime.now().toString()); fileSection.put("file.datetime", LocalDateTime.now().toString());
fileSection.put("file.protocol", inputProperties.get("conf.protocol")); fileSection.put("file.protocol", inputProperties.get("conf.protocol"));
targetProperties.addAll(fileSection); targetProperties.add(fileSection);
// //
targetProperties.addAll(inputProperties); targetProperties.add(inputProperties);
// Clear configuration paths. // Clear configuration paths.
targetProperties.removeSection("conf"); targetProperties.removeSection("conf");
// Save target file. // Save target file.
File targetFile = new File(targetDirectory, input.getName());
PathPropertyUtils.save(targetFile, targetProperties); PathPropertyUtils.save(targetFile, targetProperties);
} }
else else
@ -107,7 +107,7 @@ public class StatoolInfos
*/ */
public static void clear(final File input) throws StatoolInfosException, IOException public static void clear(final File input) throws StatoolInfosException, IOException
{ {
PathPropertyList inputProperties = PathPropertyUtils.load(input); PathProperties inputProperties = PathPropertyUtils.load(input);
{ {
String crawlCacheName = inputProperties.get("conf.crawl.cache"); String crawlCacheName = inputProperties.get("conf.crawl.cache");
@ -166,13 +166,13 @@ public class StatoolInfos
*/ */
public static void crawl(final File input) throws StatoolInfosException, IOException public static void crawl(final File input) throws StatoolInfosException, IOException
{ {
PathPropertyList configuration = PathPropertyUtils.load(input); PathProperties configuration = PathPropertyUtils.load(input);
String crawlCachePath = configuration.get("conf.crawl.cache"); String crawlCachePath = configuration.get("conf.crawl.cache");
logger.info("Cache setting: {}", configuration.get("conf.crawl.cache")); logger.info("Cache setting: {}", configuration.get("conf.crawl.cache"));
CrawlCache cache = new CrawlCache(new File(crawlCachePath)); CrawlCache cache = new CrawlCache(new File(crawlCachePath));
PathPropertyList subs = configuration.getByPrefix("subs"); PathProperties subs = configuration.getByPrefix("subs");
for (PathProperty property : subs) for (PathProperty property : subs)
{ {
URL url = new URL(property.getValue()); URL url = new URL(property.getValue());
@ -197,13 +197,13 @@ public class StatoolInfos
logger.info("Crawling " + url); logger.info("Crawling " + url);
File file = cache.store(url); File file = cache.store(url);
PathPropertyList properties = PathPropertyUtils.load(file); PathProperties properties = PathPropertyUtils.load(file);
PathPropertyList crawlSection = new PathPropertyList(); PathProperties crawlSection = new PathPropertyList();
crawlSection.put("crawl.crawler", "StatoolInfos"); crawlSection.put("crawl.crawler", "StatoolInfos");
crawlSection.put("crawl.datetime", LocalDateTime.now().toString()); crawlSection.put("crawl.datetime", LocalDateTime.now().toString());
crawlSection.put("crawl.url", url.toString()); crawlSection.put("crawl.url", url.toString());
properties.addAll(crawlSection); properties.add(crawlSection);
cache.storeProperties(url, properties); cache.storeProperties(url, properties);
cache.storeQuietly(properties.getURL("federation.logo")); cache.storeQuietly(properties.getURL("federation.logo"));
@ -214,7 +214,7 @@ public class StatoolInfos
cache.storeQuietly(properties.getURL("service.logo.url")); cache.storeQuietly(properties.getURL("service.logo.url"));
// //
PathPropertyList subs = properties.getByPrefix("subs"); PathProperties subs = properties.getByPrefix("subs");
for (PathProperty property : subs) for (PathProperty property : subs)
{ {
URL subUrl = new URL(property.getValue()); URL subUrl = new URL(property.getValue());
@ -234,7 +234,7 @@ public class StatoolInfos
*/ */
public static void htmlize(final File input) throws StatoolInfosException, IOException public static void htmlize(final File input) throws StatoolInfosException, IOException
{ {
PathPropertyList properties = PathPropertyUtils.load(input); PathProperties properties = PathPropertyUtils.load(input);
String className = properties.get("conf.class"); String className = properties.get("conf.class");
if (StringUtils.equals(className, "federation")) if (StringUtils.equals(className, "federation"))

View file

@ -20,6 +20,7 @@ package fr.devinsy.statoolinfos.properties;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Iterator;
import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringSet; import fr.devinsy.strings.StringSet;
@ -27,8 +28,12 @@ import fr.devinsy.strings.StringSet;
/** /**
* The Interface PathProperties. * The Interface PathProperties.
*/ */
public interface PathProperties public interface PathProperties extends Iterable<PathProperty>
{ {
void add(PathProperties source);
boolean add(PathProperty property);
/** /**
* Gets the. * Gets the.
* *
@ -45,7 +50,7 @@ public interface PathProperties
* the prefix * the prefix
* @return the by prefix * @return the by prefix
*/ */
public PathProperties getByPrefix(String prefix); PathProperties getByPrefix(String prefix);
/** /**
* Gets the keys. * Gets the keys.
@ -84,7 +89,15 @@ public interface PathProperties
* the path * the path
* @return the url * @return the url
*/ */
public URL getURL(String path) throws MalformedURLException; URL getURL(String path) throws MalformedURLException;
/**
* Iterator.
*
* @return the iterator
*/
@Override
Iterator<PathProperty> iterator();
/** /**
* Put. * Put.

View file

@ -63,7 +63,7 @@ public class PathPropertyList extends ArrayList<PathProperty> implements PathPro
* @param source * @param source
* the source * the source
*/ */
public PathPropertyList(final PathPropertyList source) public PathPropertyList(final PathProperties source)
{ {
super(); super();
if (source != null) if (source != null)
@ -77,6 +77,21 @@ public class PathPropertyList extends ArrayList<PathProperty> implements PathPro
} }
} }
/**
* Adds the.
*
* @param source
* the source
*/
@Override
public void add(final PathProperties source)
{
for (PathProperty property : source)
{
add(property);
}
}
/** /**
* Gets the. * Gets the.
* *
@ -468,7 +483,7 @@ public class PathPropertyList extends ArrayList<PathProperty> implements PathPro
{ {
result.add("# [" + StringUtils.capitalize(prefix) + "]"); result.add("# [" + StringUtils.capitalize(prefix) + "]");
PathPropertyList section = getByPrefix(prefix); PathProperties section = getByPrefix(prefix);
result.addAll(section.toStringList()); result.addAll(section.toStringList());
result.add(""); result.add("");

View file

@ -78,9 +78,9 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static PathPropertyList load(final File file) throws IOException public static PathProperties load(final File file) throws IOException
{ {
PathPropertyList result; PathProperties result;
result = load(file, DEFAULT_CHARSET_NAME); result = load(file, DEFAULT_CHARSET_NAME);
@ -99,9 +99,9 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static PathPropertyList load(final File file, final String charsetName) throws IOException public static PathProperties load(final File file, final String charsetName) throws IOException
{ {
PathPropertyList result; PathProperties result;
result = new PathPropertyList(); result = new PathPropertyList();
@ -129,9 +129,9 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static PathPropertyList load(final URL url) throws IOException public static PathProperties load(final URL url) throws IOException
{ {
PathPropertyList result; PathProperties result;
StringList lines = StringsUtils.load(url); StringList lines = StringsUtils.load(url);
@ -150,9 +150,9 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static PathPropertyList read(final BufferedReader in) throws IOException public static PathProperties read(final BufferedReader in) throws IOException
{ {
PathPropertyList result; PathProperties result;
result = new PathPropertyList(); result = new PathPropertyList();
@ -184,9 +184,9 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static PathPropertyList read(final StringList source) throws IOException public static PathProperties read(final StringList source) throws IOException
{ {
PathPropertyList result; PathProperties result;
result = new PathPropertyList(); result = new PathPropertyList();
@ -277,7 +277,7 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static void save(final File file, final PathPropertyList source) throws IOException public static void save(final File file, final PathProperties source) throws IOException
{ {
PrintWriter out = null; PrintWriter out = null;
try try
@ -327,7 +327,7 @@ public class PathPropertyUtils
* @throws IOException * @throws IOException
* Signals that an I/O exception has occurred. * Signals that an I/O exception has occurred.
*/ */
public static void write(final PrintWriter out, final PathPropertyList source) throws IOException public static void write(final PrintWriter out, final PathProperties source) throws IOException
{ {
if (source != null) if (source != null)
{ {