Refactored logo htmlize management.

This commit is contained in:
Christian P. MOMON 2020-10-23 03:59:44 +02:00
parent 45debe8fc1
commit 0f26fff93e
14 changed files with 224 additions and 14 deletions

View file

@ -24,5 +24,6 @@
<classpathentry kind="lib" path="lib/commons-lang3-3.11.jar" sourcepath="lib/commons-lang3-3.11-sources.jar"/> <classpathentry kind="lib" path="lib/commons-lang3-3.11.jar" sourcepath="lib/commons-lang3-3.11-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-text-1.9.jar" sourcepath="lib/commons-text-1.9-sources.jar"/> <classpathentry kind="lib" path="lib/commons-text-1.9.jar" sourcepath="lib/commons-text-1.9-sources.jar"/>
<classpathentry kind="lib" path="lib/catgenerator-0.1.2.jar" sourcepath="lib/catgenerator-0.1.2-sources.zip"/> <classpathentry kind="lib" path="lib/catgenerator-0.1.2.jar" sourcepath="lib/catgenerator-0.1.2-sources.zip"/>
<classpathentry kind="lib" path="lib/tika-core-1.24.1.jar" sourcepath="lib/tika-core-1.24.1-sources.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

BIN
lib/tika-core-1.24.1.jar Normal file

Binary file not shown.

View file

@ -164,6 +164,7 @@ public class Factory
PathProperties properties = PathPropertyUtils.load(federationFile); PathProperties properties = PathPropertyUtils.load(federationFile);
result = new Federation(properties); result = new Federation(properties);
result.setInputFile(federationFile); result.setInputFile(federationFile);
result.setLogoFileName(result.getTechnicalName() + "-logo" + StringUtils.defaultIfBlank(cache.getExtension(result.getLogoURL()), ".png"));
PathProperties subs = result.getByPrefix("subs"); PathProperties subs = result.getByPrefix("subs");
for (PathProperty property : subs) for (PathProperty property : subs)
@ -236,6 +237,7 @@ public class Factory
result = new Organization(properties); result = new Organization(properties);
result.setInputFile(inputFile); result.setInputFile(inputFile);
result.setInputURL(inputURL); result.setInputURL(inputURL);
result.setLogoFileName(result.getTechnicalName() + "-logo" + StringUtils.defaultIfBlank(cache.getExtension(result.getLogoURL()), ".png"));
PathProperties subs = result.getByPrefix("subs"); PathProperties subs = result.getByPrefix("subs");
for (PathProperty property : subs) for (PathProperty property : subs)
@ -245,6 +247,7 @@ public class Factory
URL serviceInputURL = new URL(property.getValue()); URL serviceInputURL = new URL(property.getValue());
Service service = loadService(serviceInputURL, cache); Service service = loadService(serviceInputURL, cache);
service.setOrganization(result); service.setOrganization(result);
result.setLogoFileName(result.getTechnicalName() + "-" + result.getLogoFileName());
result.getServices().add(service); result.getServices().add(service);
} }
} }
@ -269,6 +272,7 @@ public class Factory
result = new Service(properties); result = new Service(properties);
result.setInputFile(inputFile); result.setInputFile(inputFile);
result.setInputURL(inputURL); result.setInputURL(inputURL);
result.setLogoFileName(result.getTechnicalName() + "-logo" + StringUtils.defaultIfBlank(cache.getExtension(result.getLogoURL()), ".png"));
// //
return result; return result;

View file

@ -36,6 +36,7 @@ public class Federation extends PathPropertyList
private static final long serialVersionUID = -8970835291634661580L; private static final long serialVersionUID = -8970835291634661580L;
private Organizations organizations; private Organizations organizations;
private File inputFile; private File inputFile;
private String logoFileName;
/** /**
* Instantiates a new federation. * Instantiates a new federation.
@ -63,6 +64,7 @@ public class Federation extends PathPropertyList
else else
{ {
this.organizations = new Organizations(); this.organizations = new Organizations();
} }
} }
@ -166,6 +168,11 @@ public class Federation extends PathPropertyList
return result; return result;
} }
public String getLogoFileName()
{
return this.logoFileName;
}
/** /**
* Gets the logo URL. * Gets the logo URL.
* *
@ -352,8 +359,19 @@ public class Federation extends PathPropertyList
return result; return result;
} }
/**
* Sets the input file.
*
* @param inputFile
* the new input file
*/
public void setInputFile(final File inputFile) public void setInputFile(final File inputFile)
{ {
this.inputFile = inputFile; this.inputFile = inputFile;
} }
public void setLogoFileName(final String logoFileName)
{
this.logoFileName = logoFileName;
}
} }

View file

@ -38,6 +38,7 @@ public class Organization extends PathPropertyList
private Services services; private Services services;
private File inputFile; private File inputFile;
private URL inputURL; private URL inputURL;
private String logoFileName;
/** /**
* Instantiates a new organization. * Instantiates a new organization.
@ -208,6 +209,21 @@ public class Organization extends PathPropertyList
return result; return result;
} }
/**
* Gets the logo file name.
*
* @return the logo file name
*/
public String getLogoFileName()
{
String result;
result = this.logoFileName;
//
return result;
}
/** /**
* Gets the logo URL. * Gets the logo URL.
* *
@ -491,4 +507,9 @@ public class Organization extends PathPropertyList
{ {
this.inputURL = inputURL; this.inputURL = inputURL;
} }
public void setLogoFileName(final String logoFileName)
{
this.logoFileName = logoFileName;
}
} }

View file

@ -25,6 +25,7 @@ import java.time.LocalDateTime;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RegExUtils; import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -56,8 +57,8 @@ public class Service extends PathPropertyList
private Organization organization; private Organization organization;
private File inputFile; private File inputFile;
private URL inputURL; private URL inputURL;
private String logoFileName;
/** /**
* Instantiates a new service. * Instantiates a new service.
@ -188,6 +189,11 @@ public class Service extends PathPropertyList
return result; return result;
} }
public String getLogoFileName()
{
return this.logoFileName;
}
/** /**
* Gets the logo URL. * Gets the logo URL.
* *
@ -293,6 +299,12 @@ public class Service extends PathPropertyList
result = get("service.name"); result = get("service.name");
if (StringUtils.isBlank(result))
{
String seed = get("crawl.url");
result = DigestUtils.md5Hex(seed).substring(0, 8);
}
// //
return result; return result;
} }
@ -653,6 +665,11 @@ public class Service extends PathPropertyList
this.inputURL = inputURL; this.inputURL = inputURL;
} }
public void setLogoFileName(final String logoFileName)
{
this.logoFileName = logoFileName;
}
public void setOrganization(final Organization organization) public void setOrganization(final Organization organization)
{ {
this.organization = organization; this.organization = organization;

View file

@ -40,6 +40,10 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.apache.tika.Tika;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -119,6 +123,34 @@ public class StatoolInfosUtils
return result; return result;
} }
/**
* Find extension.
*
* @param file
* the file
* @return the string
*/
public static String findExtension(final File file)
{
String result;
try
{
Tika tika = new Tika();
String mimeTypeLabel = tika.detect(file);
MimeType mimeType = MimeTypes.getDefaultMimeTypes().forName(mimeTypeLabel);
result = mimeType.getExtension();
}
catch (IOException | MimeTypeException exception)
{
exception.printStackTrace();
result = "";
}
//
return result;
}
/** /**
* Generate cat logo. * Generate cat logo.
* *

View file

@ -111,6 +111,31 @@ public class CrawlCache
} }
} }
/**
* Gets the extension.
*
* @param url
* the url
* @return the extension
*/
public String getExtension(final URL url)
{
String result;
File file = restoreFile(url);
if (file == null)
{
result = null;
}
else
{
result = StatoolInfosUtils.findExtension(file);
}
//
return result;
}
/** /**
* Restore file. * Restore file.
* *
@ -124,7 +149,7 @@ public class CrawlCache
if (StringUtils.isBlank(key)) if (StringUtils.isBlank(key))
{ {
throw new IllegalArgumentException("Null parameter."); result = null;
} }
else else
{ {
@ -150,7 +175,14 @@ public class CrawlCache
{ {
File result; File result;
result = restoreFile(url.toString()); if (url == null)
{
result = null;
}
else
{
result = restoreFile(url.toString());
}
// //
return result; return result;
@ -417,7 +449,7 @@ public class CrawlCache
try try
{ {
if ((url == null) || (!StringUtils.startsWith(url.getProtocol(), "http"))) if ((url == null) || (!StringUtils.startsWithIgnoreCase(url.getProtocol(), "http")))
{ {
result = null; result = null;
} }

View file

@ -100,7 +100,7 @@ public class Crawler
cache.store(input.get("federation.name"), configuration.getCrawlInputFile()); cache.store(input.get("federation.name"), configuration.getCrawlInputFile());
cache.storeQuietly(input.getURL("federation.logo")); cache.storeQuietly(input.getURL("federation.logo"));
} }
else else if (configuration.isOrganization())
{ {
cache.store(input.get("organization.name"), configuration.getCrawlInputFile()); cache.store(input.get("organization.name"), configuration.getCrawlInputFile());
cache.storeQuietly(input.getURL("organization.logo")); cache.storeQuietly(input.getURL("organization.logo"));

View file

@ -57,7 +57,7 @@ public class FederationPage
TagDataManager data = new TagDataManager(); TagDataManager data = new TagDataManager();
data.setAttribute("federationRawButton", "href", federation.getTechnicalName() + ".properties"); data.setAttribute("federationRawButton", "href", federation.getTechnicalName() + ".properties");
data.setAttribute("federationLogo", "src", federation.getTechnicalName() + "-logo.png"); data.setAttribute("federationLogo", "src", federation.getLogoFileName());
data.setEscapedContent("federationName", federation.getName()); data.setEscapedContent("federationName", federation.getName());
data.setEscapedContent("federationDescription", federation.getDescription()); data.setEscapedContent("federationDescription", federation.getDescription());
@ -106,7 +106,7 @@ public class FederationPage
for (Organization organization : federation.getOrganizations().sortByReverseServiceCount()) for (Organization organization : federation.getOrganizations().sortByReverseServiceCount())
{ {
data.setAttribute("organizationListLine", index, "organizationListLineNameLink", "href", organization.getTechnicalName() + ".xhtml"); data.setAttribute("organizationListLine", index, "organizationListLineNameLink", "href", organization.getTechnicalName() + ".xhtml");
data.setAttribute("organizationListLine", index, "organizationListLineLogo", "src", organization.getTechnicalName() + "-logo.png"); data.setAttribute("organizationListLine", index, "organizationListLineLogo", "src", organization.getLogoFileName());
data.setAttribute("organizationListLine", index, "organizationListLineLogo", "alt", organization.getName()); data.setAttribute("organizationListLine", index, "organizationListLineLogo", "alt", organization.getName());
data.setEscapedContent("organizationListLine", index, "organizationListLineNameValue", organization.getName()); data.setEscapedContent("organizationListLine", index, "organizationListLineNameValue", organization.getName());

View file

@ -27,6 +27,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.catgenerator.core.BirdGenerator;
import fr.devinsy.catgenerator.core.CatGenerator;
import fr.devinsy.statoolinfos.checker.PropertyChecker; import fr.devinsy.statoolinfos.checker.PropertyChecker;
import fr.devinsy.statoolinfos.checker.PropertyChecks; import fr.devinsy.statoolinfos.checker.PropertyChecks;
import fr.devinsy.statoolinfos.core.Categories; import fr.devinsy.statoolinfos.core.Categories;
@ -315,7 +317,7 @@ public class Htmlizer
// Manage the logo file. // Manage the logo file.
logger.info("Htmlize federation logo."); logger.info("Htmlize federation logo.");
cache.restoreLogoTo(federation.getLogoURL(), new File(htmlizeDirectory, federation.getTechnicalName() + "-logo.png"), federation.getTechnicalName(), DefaultLogoGenerator.CAT); htmlizeFederationLogo(federation, cache, htmlizeDirectory);
logger.info("Htmlize federation properties files."); logger.info("Htmlize federation properties files.");
FileUtils.copyFile(federation.getInputFile(), new File(htmlizeDirectory, federation.getTechnicalName() + ".properties")); FileUtils.copyFile(federation.getInputFile(), new File(htmlizeDirectory, federation.getTechnicalName() + ".properties"));
@ -333,7 +335,7 @@ public class Htmlizer
{ {
// Manage the logo file. // Manage the logo file.
logger.info("Htmlize organization logo: {}.", organization.getName()); logger.info("Htmlize organization logo: {}.", organization.getName());
cache.restoreLogoTo(organization.getLogoURL(), new File(htmlizeDirectory, organization.getTechnicalName() + "-logo.png"), organization.getTechnicalName(), DefaultLogoGenerator.CAT); htmlizeOrganizationLogo(organization, cache, htmlizeDirectory);
logger.info("Htmlize organization properties file: {}.", organization.getName()); logger.info("Htmlize organization properties file: {}.", organization.getName());
FileUtils.copyFile(organization.getInputFile(), new File(htmlizeDirectory, organization.getTechnicalName() + ".properties")); FileUtils.copyFile(organization.getInputFile(), new File(htmlizeDirectory, organization.getTechnicalName() + ".properties"));
@ -347,8 +349,7 @@ public class Htmlizer
{ {
// Manage the logo file. // Manage the logo file.
logger.info("Htmlize service logo: {}.", service.getName()); logger.info("Htmlize service logo: {}.", service.getName());
cache.restoreLogoTo(service.getLogoURL(), new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + "-logo.png"), service.getTechnicalName(), htmlizeServiceLogo(service, cache, htmlizeDirectory);
DefaultLogoGenerator.BIRD);
logger.info("Htmlize service properties file: {}.", service.getName()); logger.info("Htmlize service properties file: {}.", service.getName());
FileUtils.copyFile(service.getInputFile(), FileUtils.copyFile(service.getInputFile(),
new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".properties")); new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".properties"));
@ -470,6 +471,36 @@ public class Htmlizer
} }
} }
/**
* Htmlize federation logo.
*
* @param federation
* the federation
* @param cache
* the cache
* @param htmlizeDirectory
* the htmlize directory
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private static void htmlizeFederationLogo(final Federation federation, final CrawlCache cache, final File htmlizeDirectory) throws IOException
{
logger.info("Htmlize federation logo.");
File target = new File(htmlizeDirectory, federation.getLogoFileName());
File logoFile = cache.restoreFile(federation.getLogoURL());
if (logoFile == null)
{
logger.info("CatGeneratoring cat avatar: {}", target.getAbsoluteFile());
CatGenerator.buildAvatarTo(federation.getTechnicalName(), target);
}
else
{
FileUtils.copyFile(logoFile, target);
}
}
/** /**
* Htmlize organisation. * Htmlize organisation.
* *
@ -512,4 +543,58 @@ public class Htmlizer
FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8); FileUtils.write(new File(htmlizeDirectory, organization.getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8);
} }
} }
/**
* Htmlize organization logo.
*
* @param organization
* the organization
* @param cache
* the cache
* @param htmlizeDirectory
* the htmlize directory
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private static void htmlizeOrganizationLogo(final Organization organization, final CrawlCache cache, final File htmlizeDirectory) throws IOException
{
logger.info("Htmlize organization logo.");
File target = new File(htmlizeDirectory, organization.getLogoFileName());
File logoFile = cache.restoreFile(organization.getLogoURL());
if (logoFile == null)
{
logger.info("CatGeneratoring cat avatar: {}", target.getAbsoluteFile());
CatGenerator.buildAvatarTo(organization.getTechnicalName(), target);
}
else
{
FileUtils.copyFile(logoFile, target);
}
}
/**
* @param service
* @param cache
* @param htmlizeDirectory
* @throws IOException
*/
private static void htmlizeServiceLogo(final Service service, final CrawlCache cache, final File htmlizeDirectory) throws IOException
{
logger.info("Htmlize organization logo.");
File target = new File(htmlizeDirectory, service.getLogoFileName());
File logoFile = cache.restoreFile(service.getLogoURL());
if (logoFile == null)
{
logger.info("BirdGeneratoring cat avatar: {}", target.getAbsoluteFile());
BirdGenerator.buildAvatarTo(service.getTechnicalName(), target);
}
else
{
FileUtils.copyFile(logoFile, target);
}
}
} }

View file

@ -57,7 +57,7 @@ public class OrganizationPage
data.setAttribute("organizationRawButton", "href", organization.getTechnicalName() + ".properties"); data.setAttribute("organizationRawButton", "href", organization.getTechnicalName() + ".properties");
data.setAttribute("organizationLogo", "src", organization.getTechnicalName() + "-logo.png"); data.setAttribute("organizationLogo", "src", organization.getLogoFileName());
data.setEscapedContent("organizationName", organization.get("organization.name")); data.setEscapedContent("organizationName", organization.get("organization.name"));
data.setEscapedContent("organizationURL", organization.getWebsite()); data.setEscapedContent("organizationURL", organization.getWebsite());

View file

@ -60,12 +60,12 @@ public class ServiceListView
int index = 0; int index = 0;
for (Service service : services.sortByName()) for (Service service : services.sortByName())
{ {
data.setAttribute("serviceListLine", index, "serviceListLineLogo", "src", service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + "-logo.png"); data.setAttribute("serviceListLine", index, "serviceListLineLogo", "src", service.getLogoFileName());
data.setEscapedContent("serviceListLine", index, "serviceListLineNameValue", service.getName()); data.setEscapedContent("serviceListLine", index, "serviceListLineNameValue", service.getName());
data.setAttribute("serviceListLine", index, "serviceListLineNameLink", "href", service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"); data.setAttribute("serviceListLine", index, "serviceListLineNameLink", "href", service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml");
data.setAttribute("serviceListLine", index, "serviceListLineOrganizationLink", "href", service.getOrganization().getTechnicalName() + ".xhtml"); data.setAttribute("serviceListLine", index, "serviceListLineOrganizationLink", "href", service.getOrganization().getTechnicalName() + ".xhtml");
data.setAttribute("serviceListLine", index, "serviceListLineOrganizationLogo", "src", service.getOrganization().getTechnicalName() + "-logo.png"); data.setAttribute("serviceListLine", index, "serviceListLineOrganizationLogo", "src", service.getOrganization().getLogoFileName());
data.setEscapedContent("serviceListLine", index, "serviceListLineOrganizationValue", service.getOrganization().getName()); data.setEscapedContent("serviceListLine", index, "serviceListLineOrganizationValue", service.getOrganization().getName());
data.setEscapedContent("serviceListLine", index, "serviceListLineUrlLink", service.getWebsite()); data.setEscapedContent("serviceListLine", index, "serviceListLineUrlLink", service.getWebsite());