Model2Html.java

package org.linkedopenactors.rdfpub.usecases.overlapping;

import static org.eclipse.rdf4j.model.util.Values.iri;
import static org.eclipse.rdf4j.model.util.Values.literal;

import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.util.ModelBuilder;
import org.eclipse.rdf4j.model.vocabulary.RDF;

import de.naturzukunft.rdf4j.vocabulary.AS;

class Model2Html {

	public static void main(String args[]) {
		Model model = new ModelBuilder()
						.subject(iri("http://example.org/sample/8822"))
							.add(RDF.TYPE, AS.Object)
							.add(AS.name, literal("Manfred"))
							.add(AS.altitude, literal("10"))
							.add(AS.object, iri("http://example.org/sample/8833"))
						.subject(iri("http://example.org/sample/9933"))
							.add(RDF.TYPE, AS.Object)
							.add(AS.name, literal("Manuel"))
							.add(AS.altitude, literal("5"))
							.add(AS.object, iri("http://example.org/sample/8844"))							
						.build();
		System.out.println(convert(model));
	}
	
	public static String convert(Model model) {
		StringBuilder tables = new StringBuilder();
		Set<IRI> subjects = getSubjects(model);
		for (IRI subject : subjects) {
			tables.append(toTable(subject,  model));
		}
		return getHtmlTemplate(tables.toString());
	}
	
	private static String toTable(IRI subject, Model model) {		
		int line = 1;
		StringBuilder rows = new StringBuilder();
		for (Statement stmt : model) {
			rows.append(getRowTemplate(stmt.getPredicate().stringValue(), stmt.getObject().stringValue(), line%2==0));
			line++;			
		}
		return getTableTemplate(subject.stringValue(), rows.toString());
	}

	private static Set<IRI> getSubjects(Model model) {
		Model filtered = model.filter(null, RDF.TYPE, null);
		Set<IRI> subjects = filtered.stream().map(stmt->(IRI)stmt.getSubject()).collect(Collectors.toSet());
		return subjects;
	}

	private static String getHtmlTemplate(String tables) {
		return """
<style>
.styled-table {
    border-collapse: collapse;
    margin: 25px 0;
    font-size: 0.9em;
    font-family: sans-serif;
    min-width: 400px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
    }
.styled-table thead tr {
    background-color: #009879;
    color: #ffffff;
    text-align: left;
    }
.styled-table th,
.styled-table td {
    padding: 12px 15px;
    }
}
</style>
<html>
	<body>
		%s
	</body>
</html>
				""".formatted(tables);
	}

	private static String getTableTemplate(String subject, String rows) {
		return """
		<h1>%s</h1>
		<table class="styled-table">
		    <thead>
		        <tr>
		            <th>predicate</th>
		            <th>object</th>
		        </tr>
		    </thead>
		    <tbody>
		    %s
		    </tbody>
		</table>
				""".formatted(subject, rows);
	}

	private static String getRowTemplate(String predicate, String object, boolean active) {
		String startTag = "";
		if(active) {
			startTag += "<tr class=\"active-row\">";
		} else {
			startTag += "<tr>";
		}
			
		return """
		%s
			<td>%s</td>
			<td>%s</td>
		</tr>
				""".formatted(startTag, predicate, object);
	}
}