Package com.hp.hpl.squirrelrdf.ldap

Source Code of com.hp.hpl.squirrelrdf.ldap.LdapQueryEngine$LdapPattern

/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/

package com.hp.hpl.squirrelrdf.ldap;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.engine.QueryIterator;
import com.hp.hpl.jena.query.engine1.ExecutionContext;
import com.hp.hpl.jena.query.engine1.Plan;
import com.hp.hpl.jena.query.engine1.PlanElement;
import com.hp.hpl.jena.query.engine1.PlanVisitor;
import com.hp.hpl.jena.query.engine1.PlanVisitorBase;
import com.hp.hpl.jena.query.engine1.PlanWalker;
import com.hp.hpl.jena.query.engine1.QueryEngine;
import com.hp.hpl.jena.query.engine1.compiler.PlanBasicPattern;
import com.hp.hpl.jena.query.engine1.compiler.PlanElementBase;
import com.hp.hpl.jena.query.engine1.compiler.PlanGroup;
import com.hp.hpl.jena.rdf.model.Model;

/**
* LdapQueryEngine
*
* @author pldms <damian.steer@hp.com>
*
* Query an LDAP source using SPARQL
*/
public class LdapQueryEngine extends QueryEngine
{
  final static Log log = LogFactory.getLog(LdapQueryEngine.class);

  private Model config;

  /**
   * @param Query
   *            A query
   * @param configModel
   *            An ldapmap configuration
   */
  public LdapQueryEngine(Query arg0, Model config)
  {
    super(arg0);
    this.config = config;
  }

  /*
   * walk through the plan, and replace the bits we want to handle.
   */
  protected PlanElement queryPlanHook(Plan plan, PlanElement planElt)
  {
    PlanWalker.walk(new LdapPlanVisitor(), planElt);
    return planElt;
  }

  /*
   * This does the pottering through the plan
   */
  class LdapPlanVisitor extends PlanVisitorBase
  {
    /*
     * We start replacing at PlanGroups, since here we can remove and add
     * bits
     */
    @SuppressWarnings("unchecked")
    public void visit(PlanGroup planElt)
    {
      Object[] elements = planElt.getPlanElements().toArray();

      for (int i = 0; i < elements.length; i++)
      {
        // Aha, something to replace...
        if (elements[i] instanceof PlanBasicPattern)
        {
          PlanBasicPattern pbp = (PlanBasicPattern) elements[i];
          planElt.getPlanElements().remove(i);
          // Warning supressed
          planElt.getPlanElements().add(i,
              new LdapPattern(pbp.getPlan(), pbp));
        }
      }
    }

  }

  /**
   * This class replaces PlanBasicPatterns (although we keep the original
   * around). SparqlLdap can answer these things.
   */
  class LdapPattern extends PlanElementBase
  {
    PlanBasicPattern pattern;
    LdapSparqlMap map;
   
    @SuppressWarnings("unchecked")
    protected LdapPattern(Plan p, PlanBasicPattern pattern)
    {
      super(p);
      this.pattern = pattern;
      this.map = new LdapSparqlMap(config);
      this.map.setTriples((List<Triple>) pattern.getPattern());
    }
   
    public void visit(PlanVisitor visitor)
    {
    }

    public QueryIterator build(QueryIterator arg0, ExecutionContext arg1)
    {
      return map.execute(arg0, arg1);
    }

  }

}

/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. 2. Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. 3. The name of the author may not
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ 
TOP

Related Classes of com.hp.hpl.squirrelrdf.ldap.LdapQueryEngine$LdapPattern

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.