Version 1.0

This commit is contained in:
Christian Torbidone 2024-08-01 19:24:58 +02:00
parent c9ef9cbd0c
commit fa70c16dbd
4 changed files with 94 additions and 7 deletions

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View file

@ -1,16 +1,17 @@
plugins {
kotlin("jvm") version "2.0.0"
id("maven-publish")
}
group = "org.thundernetwork"
version = "1.0-SNAPSHOT"
version = "1.0"
repositories {
maven("https://repository.thundernetwork.org/repository/maven-public/")
maven("https://repository.thundernetwork.org/repository/maven-public")
}
dependencies {
testImplementation(kotlin("test"))
// testImplementation(kotlin("test"))
}
tasks.test {
@ -19,3 +20,53 @@ tasks.test {
kotlin {
jvmToolchain(17)
}
publishing {
repositories {
maven {
url = uri("https://repository.thundernetwork.org/repository/maven-releases")
credentials {
username = System.getenv("REPOSITORY_USERNAME")
password = System.getenv("REPOSITORY_PASSWORD")
}
}
}
publications {
create<MavenPublication>("mavenJava") {
pom {
name = "Sitemap"
description = "A simply collection of utils for sitemap creation written in Kotlin"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "killerbossoriginal"
name = "KillerBossOriginal"
email = "killerbossoriginal@thundernetwork.org"
}
}
scm {
connection = "scm:git:git://github.com/ThunderNetworkRaD/sitemap.git"
developerConnection = "scm:git:ssh://github.com/ThunderNetworkRaD/sitemap.git"
url = "https://github.com/ThunderNetworkRaD/sitemap"
}
issueManagement {
system = "GitHub Issues"
url = "https://github.com/ThunderNetworkRaD/sitemap/issues"
}
ciManagement {
system = "GitHub Actions"
url = "https://github.com/ThunderNetworkRaD/sitemap/actions"
}
organization {
name = "ThunderNetwork"
url = "https://thundernetwork.org"
}
}
}
}
}

View file

@ -2,7 +2,7 @@ package org.thundernetwork.sitemap
import org.thundernetwork.sitemap.models.UrlEntry
fun main() {
fun test() {
val large = true // Imposta questo valore in base alle tue esigenze
val path = "sitemaps.xml" // Imposta il percorso dove salvare le sitemaps

View file

@ -10,10 +10,24 @@ import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
/**
* A class for generating sitemaps.
*
* @param large whether to generate a large sitemap
* @param path the path to the sitemap file
*/
class SitemapGenerator(private val large: Boolean, private val path: String) {
private val urlEntries = mutableListOf<UrlEntry>()
/**
* Loads a sitemap from the specified file path and parses it into a list of UrlEntry objects.
*
* @param filePath the path to the sitemap file
* @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the configuration requested
* @throws IOException if any I/O errors occur
* @throws SAXException if any parse errors occur
*/
fun loadSitemap(filePath: String) {
val docFactory = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val doc: Document = docFactory.parse(File(filePath))
@ -23,22 +37,35 @@ class SitemapGenerator(private val large: Boolean, private val path: String) {
val urlElement = urlNodes.item(i) as Element
val loc = urlElement.getElementsByTagName("loc").item(0).textContent
val lastmod = urlElement.getElementsByTagName("lastmod")?.item(0)?.textContent
val changefreq = urlElement.getElementsByTagName("changefreq")?.item(0)?.textContent
val priority = urlElement.getElementsByTagName("priority")?.item(0)?.textContent?.toDouble()
val lastmod = urlElement.getElementsByTagName("lastmod").item(0)?.textContent
val changefreq = urlElement.getElementsByTagName("changefreq").item(0)?.textContent
val priority = urlElement.getElementsByTagName("priority").item(0)?.textContent?.toDouble()
urlEntries.add(UrlEntry(loc, lastmod, changefreq, priority))
}
}
/**
* Adds a new UrlEntry to the sitemap.
*
* @param urlEntry the UrlEntry to add
*/
fun addUrl(urlEntry: UrlEntry) {
urlEntries.add(urlEntry)
}
/**
* Removes a UrlEntry from the sitemap.
*
* @param loc the location of the UrlEntry to remove
*/
fun removeUrl(loc: String) {
urlEntries.removeAll { it.loc == loc }
}
/**
* Generates the sitemap.
*/
fun generateSitemap() {
if (large && urlEntries.size > 10000) {
generateLargeSitemap()
@ -47,6 +74,11 @@ class SitemapGenerator(private val large: Boolean, private val path: String) {
}
}
/**
* Generates a single sitemap.
*
* @param filePath the path to the sitemap file
*/
private fun generateSingleSitemap(filePath: String) {
val docFactory = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val doc: Document = docFactory.newDocument()
@ -93,6 +125,9 @@ class SitemapGenerator(private val large: Boolean, private val path: String) {
transformer.transform(source, result)
}
/**
* Generates a large sitemap.
*/
private fun generateLargeSitemap() {
val dir = File(path)
if (!dir.exists()) {