it.uniroma2.art.sheet2rdf.pearl.PearlBuilder Maven / Gradle / Ivy
package it.uniroma2.art.sheet2rdf.pearl;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class PearlBuilder {
private Map prefixMapping;
private String rule;
private List nodeSection;
private List graphSection;
public PearlBuilder(Map prefixMapping, String annotationType,
List nodeSection, List graphSection){
this.prefixMapping = prefixMapping;
this.rule = "rule "+ annotationType +" id:row";
this.nodeSection = nodeSection;
this.graphSection = graphSection;
}
/**
* Serializes the pearl code.
* @return
*/
public String serialize(){
int indent = 0;//increases right after { opening, decreases right before } closing.
String tabs = getTabs(indent);
String pearl = "";
//serialize prefix namespace mapping
Set prefs = prefixMapping.keySet();
for (String pref : prefs){
pearl += tabs + "prefix "+pref+": <"+prefixMapping.get(pref)+">\n";
}
pearl += tabs + "\n";
//serialize rule
pearl += tabs + rule + " {\n";
indent++;
tabs = getTabs(indent);
//serialize node section
pearl += tabs + "nodes = {\n";
indent++;
tabs = getTabs(indent);
for (NodePearlElement n : nodeSection){
pearl += n.serialize(tabs, prefixMapping);
}
indent--;
tabs = getTabs(indent);
pearl += tabs + "}\n";//closing node section
//serialize graph section
pearl += tabs + "graph = {\n";
indent++;
tabs = getTabs(indent);
for (GraphPearlElement g : graphSection){
pearl += g.serialize(tabs, prefixMapping) + "\n";
}
indent--;
tabs = getTabs(indent);
pearl += tabs + "}\n";//closing graph section
indent--;
tabs = getTabs(indent);
pearl += tabs + "}\n";//closing rule
return pearl;
}
private String getTabs(int indent){
String tabs = "";
for (int i = 0; i < indent; ++i)
tabs += "\t";
return tabs;
}
}