01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.parser.sparql;
07:
08: import java.util.List;
09:
10: import org.openrdf.model.URI;
11: import org.openrdf.model.impl.URIImpl;
12: import org.openrdf.query.Dataset;
13: import org.openrdf.query.MalformedQueryException;
14: import org.openrdf.query.impl.DatasetImpl;
15: import org.openrdf.query.parser.sparql.ast.ASTDatasetClause;
16: import org.openrdf.query.parser.sparql.ast.ASTIRI;
17: import org.openrdf.query.parser.sparql.ast.ASTQueryContainer;
18:
19: /**
20: * Extracts a SPARQL {@link #Dataset} from an ASTQueryContainer, if one is
21: * contained.
22: *
23: * @author Simon Schenk
24: * @author Arjohn Kampman
25: */
26: public class DatasetDeclProcessor {
27:
28: /**
29: * Extracts a SPARQL {@link #Dataset} from an ASTQueryContainer, if one is
30: * contained. Returns null otherwise.
31: *
32: * @param qc
33: * The query model to resolve relative URIs in.
34: * @throws MalformedQueryException
35: * If DatasetClause does not contain a valid URI.
36: */
37: public static Dataset process(ASTQueryContainer qc)
38: throws MalformedQueryException {
39: DatasetImpl dataset = null;
40:
41: List<ASTDatasetClause> datasetClauses = qc.getQuery()
42: .getDatasetClauseList();
43:
44: if (!datasetClauses.isEmpty()) {
45: dataset = new DatasetImpl();
46:
47: for (ASTDatasetClause dc : datasetClauses) {
48: ASTIRI astIri = dc.jjtGetChild(ASTIRI.class);
49:
50: try {
51: URI uri = new URIImpl(astIri.getValue());
52: if (dc.isNamed()) {
53: dataset.addNamedGraph(uri);
54: } else {
55: dataset.addDefaultGraph(uri);
56: }
57: } catch (IllegalArgumentException e) {
58: throw new MalformedQueryException(e.getMessage(), e);
59: }
60: }
61: }
62:
63: return dataset;
64: }
65: }
|