12
12
import javax .servlet .http .HttpServletResponse ;
13
13
14
14
import org .linkeddatafragments .config .ConfigReader ;
15
- import org .rdfhdt . hdt . hdt . HDT ;
16
- import org .rdfhdt . hdt . hdt . HDTManager ;
17
- import org .rdfhdt . hdtjena . HDTGraph ;
15
+ import org .linkeddatafragments . datasource . BasicLinkedDataFragment ;
16
+ import org .linkeddatafragments . datasource . DataSource ;
17
+ import org .linkeddatafragments . datasource . HdtDataSource ;
18
18
19
+ import com .hp .hpl .jena .datatypes .TypeMapper ;
19
20
import com .hp .hpl .jena .rdf .model .Model ;
20
- import com .hp .hpl .jena .rdf .model .ModelFactory ;
21
21
import com .hp .hpl .jena .rdf .model .Property ;
22
22
import com .hp .hpl .jena .rdf .model .RDFNode ;
23
23
import com .hp .hpl .jena .rdf .model .Resource ;
24
- import com .hp .hpl .jena .rdf .model .StmtIterator ;
24
+ import com .hp .hpl .jena .rdf .model .ResourceFactory ;
25
25
26
26
/**
27
27
* Servlet that responds with a Basic Linked Data Fragment.
28
28
* @author Ruben Verborgh
29
29
*/
30
30
public class BasicLdfServlet extends HttpServlet {
31
31
private final static long serialVersionUID = 1L ;
32
- private final static int TRIPLES_PER_PAGE = 100 ;
33
- private final static Pattern STRINGPATTERN = Pattern . compile ( "^ \" (.*) \" (?: \\ ^ \\ ^<(.*)>|@(.*))?$" );
32
+ private final static Pattern STRINGPATTERN = Pattern . compile ( "^ \" (.*) \" (?:@(.*)| \\ ^ \\ ^<(.*)>)?$" ) ;
33
+ private final static TypeMapper types = TypeMapper . getInstance ( );
34
34
35
35
private ConfigReader config ;
36
- private HashMap <String , Model > dataSources = new HashMap <String , Model >();
36
+ private HashMap <String , DataSource > dataSources = new HashMap <String , DataSource >();
37
37
38
38
@ Override
39
39
public void init (ServletConfig servletConfig ) throws ServletException {
40
40
try {
41
41
config = new ConfigReader (servletConfig .getInitParameter ("configFile" ));
42
- for (Entry <String , String > dataSource : config .getDataSources ().entrySet ()) {
43
- final HDT hdt = HDTManager .mapIndexedHDT (dataSource .getValue (), null );
44
- final Model model = ModelFactory .createModelForGraph (new HDTGraph (hdt ));
45
- dataSources .put (dataSource .getKey (), model );
46
- }
42
+ for (Entry <String , String > dataSource : config .getDataSources ().entrySet ())
43
+ dataSources .put (dataSource .getKey (), new HdtDataSource (dataSource .getValue ()));
47
44
}
48
45
catch (Exception e ) {
49
46
throw new ServletException (e );
@@ -56,23 +53,19 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
56
53
// find the data source
57
54
final String path = request .getRequestURI ().substring (request .getContextPath ().length ());
58
55
final String dataSourceName = path .substring (1 );
59
- final Model dataSource = dataSources .get (dataSourceName );
56
+ final DataSource dataSource = dataSources .get (dataSourceName );
60
57
if (dataSource == null )
61
58
throw new Exception ("data source not found" );
62
59
63
- // create the output model
64
- final Model output = ModelFactory .createDefaultModel ();
65
- output .setNsPrefixes (config .getPrefixes ());
66
-
67
- // parse the subject, predicate, and object parameters
68
- final Resource subject = parseAsResource (request .getParameter ("subject" ), output );
69
- final Property predicate = parseAsProperty (request .getParameter ("predicate" ), output );
70
- final RDFNode object = parseAsNode (request .getParameter ("object" ), output );
60
+ // query the fragment
61
+ final Resource subject = parseAsResource (request .getParameter ("subject" ));
62
+ final Property predicate = parseAsProperty (request .getParameter ("predicate" ));
63
+ final RDFNode object = parseAsNode (request .getParameter ("object" ));
64
+ final BasicLinkedDataFragment fragment = dataSource .getFragment (subject , predicate , object );
71
65
72
- // add all statements with the given parameters to the output model
73
- final StmtIterator statements = dataSource .listStatements (subject , predicate , object );
74
- for (int i = 0 ; i < TRIPLES_PER_PAGE && statements .hasNext (); i ++)
75
- output .add (statements .next ());
66
+ // fill the output model
67
+ final Model output = fragment .getTriples ();
68
+ output .setNsPrefixes (config .getPrefixes ());
76
69
77
70
// serialize the output as Turtle
78
71
response .setHeader ("Server" , "Linked Data Fragments Server" );
@@ -87,32 +80,29 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
87
80
/**
88
81
* Parses the given value as an RDF resource.
89
82
* @param value the value
90
- * @param model the model
91
83
* @return the parsed value, or null if unspecified
92
84
*/
93
- private Resource parseAsResource (String value , Model model ) {
94
- final RDFNode subject = parseAsNode (value , model );
85
+ private Resource parseAsResource (String value ) {
86
+ final RDFNode subject = parseAsNode (value );
95
87
return subject instanceof Resource ? (Resource )subject : null ;
96
88
}
97
89
98
90
/**
99
91
* Parses the given value as an RDF property.
100
92
* @param value the value
101
- * @param model the model
102
93
* @return the parsed value, or null if unspecified
103
94
*/
104
- private Property parseAsProperty (String value , Model model ) {
105
- final RDFNode predicate = parseAsNode (value , model );
106
- return predicate instanceof Resource ? model .createProperty (((Resource )predicate ).getURI ()) : null ;
95
+ private Property parseAsProperty (String value ) {
96
+ final RDFNode predicate = parseAsNode (value );
97
+ return predicate instanceof Resource ? ResourceFactory .createProperty (((Resource )predicate ).getURI ()) : null ;
107
98
}
108
99
109
100
/**
110
101
* Parses the given value as an RDF node.
111
102
* @param value the value
112
- * @param model the model
113
103
* @return the parsed value, or null if unspecified
114
104
*/
115
- private RDFNode parseAsNode (String value , Model model ) {
105
+ private RDFNode parseAsNode (String value ) {
116
106
// nothing or empty indicates an unknown
117
107
if (value == null || value .length () == 0 )
118
108
return null ;
@@ -125,24 +115,24 @@ private RDFNode parseAsNode(String value, Model model) {
125
115
return null ;
126
116
// angular brackets indicate a URI
127
117
case '<' :
128
- return model .createResource (value .substring (1 , value .length () - 1 ));
118
+ return ResourceFactory .createResource (value .substring (1 , value .length () - 1 ));
129
119
// quotes indicate a string
130
120
case '"' :
131
121
final Matcher matcher = STRINGPATTERN .matcher (value );
132
122
if (matcher .matches ()) {
133
123
final String body = matcher .group (1 );
134
- final String type = matcher .group (2 );
135
- final String lang = matcher .group (3 );
136
- if (type != null )
137
- return model .createTypedLiteral (body , type );
124
+ final String lang = matcher .group (2 );
125
+ final String type = matcher .group (3 );
138
126
if (lang != null )
139
- return model .createLiteral (body , lang );
140
- return model .createLiteral (body );
127
+ return ResourceFactory .createLangLiteral (body , lang );
128
+ if (type != null )
129
+ return ResourceFactory .createTypedLiteral (body , types .getSafeTypeByName (type ));
130
+ return ResourceFactory .createPlainLiteral (body );
141
131
}
142
132
return null ;
143
133
// assume it's a URI without angular brackets
144
134
default :
145
- return model .createResource (value );
135
+ return ResourceFactory .createResource (value );
146
136
}
147
137
}
148
138
}
0 commit comments