-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDFWriter.java
108 lines (81 loc) · 3.6 KB
/
RDFWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Write to an RDF file
Ruth Helfinstein
July 2012
changes:
12.09.24 add a "#" in writeAttributeDataResource!
12.09.18 when writing properties, allow different "ranges" (types) besides String.
when writing attribute data, write other types besides string (references to properties)
12.08.18 if a property/class has no superclass listed, make it a subclass of Thing
(tried to do this before, but was checking for empty string incorrectly, oops)
*/
public class RDFWriter {
// for now this just writes very simple RDF to the file specified, or to the stdout
// but I'm doing it as a class to abstract it away from reading from the csv
// instance variables
private Out out;
public final String STRING_RANGE = "http://www.w3.org/2001/XMLSchema#string";
// constructor
public RDFWriter() {
// no params = write to stdout
out = new Out();
}
public RDFWriter(String s) {
// file name
out = new Out(s);
}
public void endRDF() {
// write whatever goes at the botom of the rdf file
// and close the file.
out.println("</rdf:RDF>");
out.close();
}
public void writePropertyTag(String attributeName, String className, String rangeType) {
// allow for rangeTypes other than "String". If rangeType is empty, use "#string"
// have to put a "#" before a primitive type but not in front of a "schema" type.
// grr. Maybe just check if the string has a # and add one to the beginning if not.
out.printf( "<rdf:Property rdf:ID=\"%s\">\n", attributeName);
out.printf( " <rdfs:domain rdf:resource=\"#%s\"/>\n", className);
if (rangeType.length() == 0) rangeType = STRING_RANGE;
else if (!rangeType.contains("#")) rangeType = "#" + rangeType;
out.printf(" <rdfs:range rdf:resource=\"%s\"/>\n", rangeType);
out.println("</rdf:Property>");
out.println(" ");
}
public void writeClassInfo(String className, String superclass) {
out.printf("<rdf:Description rdf:ID=\"%s\">\n", className);
out.println(" <rdf:type rdf:resource=\"http://www.w3.org/2000/01/rdf-schema#Class\"/>");
if (superclass.length() == 0) superclass = "Thing";
out.printf(" <rdfs:subClassOf rdf:resource=\"#%s\"/>", superclass);
out.println("\n</rdf:Description>");
out.println(" ");
}
public void startInstance(String id, String className) {
// start a new instance, writes the opening rdf:description tag, and the rdf:type tag
out.printf("<rdf:Description rdf:ID=\"%s\">\n", id);
out.printf(" <rdf:type rdf:resource=\"%s\"/>\n", className);
}
public void endInstance() {
out.printf("</rdf:Description>\n");
out.println(" ");
}
public void writeAttributeData(String attName, String attData) {
// <sample:price>14.00</sample:price>
out.printf(" <sample:%s>%s</sample:%s>\n", attName, attData, attName);
}
public void writeAttributeDataResource(String attName, String attData) {
// <sample:fromcountry rdf:resource="Thailand" />
out.printf(" <sample:%s rdf:resource=\"#%s\"/>\n", attName, attData);
}
public void startRDF() {
// write whatever goes at the top of the rdf file
// open the rdf tag, and write header info
out.println("<rdf:RDF");
out.println(" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
out.println(" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"");
out.println(" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"");
out.println(" xmlns:sample=\"http://www.test.nql.org/schemas/sample#\">");
out.println("<owl:Ontology rdf:about=\"http://www.w3.org/2000/01/rdf-schema#\"/>");
out.println(" ");
// classes and property descriptions will be written by the caller
}
}