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);
StringBuilder pearl = new StringBuilder();
//serialize prefix namespace mapping
Set prefs = prefixMapping.keySet();
for (String pref : prefs){
pearl.append(tabs + "prefix "+pref+": <"+prefixMapping.get(pref)+">\n");
}
pearl.append(tabs + "\n");
//serialize rule
pearl.append(tabs + rule + " {\n");
indent++;
tabs = getTabs(indent);
//serialize node section
pearl.append(tabs + "nodes = {\n");
indent++;
tabs = getTabs(indent);
for (NodePearlElement n : nodeSection){
pearl.append(n.serialize(tabs, prefixMapping));
}
indent--;
tabs = getTabs(indent);
pearl.append(tabs + "}\n");//closing node section
//serialize graph section
pearl.append(tabs + "graph = {\n");
indent++;
tabs = getTabs(indent);
for (GraphPearlElement g : graphSection){
pearl.append(g.serialize(tabs, prefixMapping) + "\n");
}
indent--;
tabs = getTabs(indent);
pearl.append(tabs + "}\n");//closing graph section
indent--;
tabs = getTabs(indent);
pearl.append(tabs + "}\n");//closing rule
return pearl.toString();
}
private String getTabs(int indent){
String tabs = "";
for (int i = 0; i < indent; ++i)
tabs += "\t";
return tabs;
}
}