cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nico Verwer <>
Subject Re: cinclude a request to a sparql endpoint, character escaping
Date Fri, 21 Sep 2007 11:05:42 GMT
Nico Verwer wrote:
> I am looking at the same problem at the moment, and triied several 
> things (like the IncludeTransformer). My next attempt will be to build 
> a SPARQLTransformer. When it is ready, I'll let you know.
I think the following code works. Please try it, and let me know if it 

The Java code follows:
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.apache.cocoon.transformation;

import java.util.Map;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.source.SourceUtil;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceParameters;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

 *  * @cocoon.sitemap.component.documentation
 * This transformer triggers for the element <code>query</code> in the 
namespace "".
 * These elements must not be nested.
 * The <code>src</code> attribute contains the url which points to a 
SPARQL endpoint.
 * The optional <code>method</code> attribute contains the HTTP method 
for the request (default is GET).
 * Attributes in the "" namespace are 
used as request parameters (using the local name).
 * This allows for parameters such as 'format' or 'maxrows'.
 * The text of the content of the <code>query</code> element is passed 
as the value of the 'query' parameter.
 * The XML input to this transformer would look like:
 * <pre>
 *   <sparql:query xmlns:sparql=""
 *     method="POST" src=""
 *     sparql:maxrows="25" sparql:format="XML"
 *   >
 *   <![CDATA[
 *     PREFIX rdf: <>
 *     SELECT *
 *     FROM <>
 *     WHERE {
 *       ?person rdf:type <> .
 *     }
 *   ]]>
 *   </sparql:query>
 * </pre>
 * @author Nico Verwer
public class SparqlTransformer extends AbstractSAXTransformer {
  public static final String SPARQL_NAMESPACE_URI = 
  public static final String QUERY_ELEMENT = "query";
  public static final String METHOD_ATTR = "method";
  public static final String SRC_ATTR = "src";
  public static final String QUERY_PARAM = "query";
  private boolean inQuery;
  private String src;
  private String method;
  private SourceParameters requestParameters;

  public SparqlTransformer() {
    this.defaultNamespaceURI = SPARQL_NAMESPACE_URI;

  public void setup(SourceResolver resolver, Map objectModel, String src,
      Parameters params) throws ProcessingException, SAXException, 
IOException {
    super.setup(resolver, objectModel, src, params);
    inQuery = false;

  public void startTransformingElement(String uri, String name, String 
raw, Attributes attr)
      throws ProcessingException, IOException, SAXException {
    if (name.equals(QUERY_ELEMENT)) {
      if (inQuery) {
        throw new ProcessingException("Nested SPARQL queries are not 
      inQuery = true;
      src = attr.getValue(SRC_ATTR);
      method = attr.getValue(METHOD_ATTR);
      method = (method == null ? "GET" : method.toUpperCase());
      requestParameters = new SourceParameters();
      for (int i = 0; i < attr.getLength(); ++i) {
        if (attr.getURI(i).equals(SPARQL_NAMESPACE_URI)) {

  public void endTransformingElement(String uri, String name, String raw)
      throws ProcessingException, IOException, SAXException {
    if (name.equals(QUERY_ELEMENT)) {
      inQuery = false;
      String query = endTextRecording();
      requestParameters.setParameter(QUERY_PARAM, query);
      Parameters typeParameters = new Parameters();
      typeParameters.setParameter("method", method);
      Source source = SourceUtil.getSource(src, typeParameters, 
requestParameters, resolver);
      SourceUtil.toSAX(source, this.xmlConsumer);

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message