/*
* Created on Feb 5, 2005
*
* Copyright 2005 CafeSip.org
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet.mapping;
import java.util.ArrayList;
import java.util.Iterator;
import org.cafesip.jiplet.JipletLogger;
import org.cafesip.jiplet.Pair;
import org.cafesip.jiplet.config.jip.AndOperation;
import org.cafesip.jiplet.config.jip.ContainsOperation;
import org.cafesip.jiplet.config.jip.EqualsOperation;
import org.cafesip.jiplet.config.jip.ExistsOperation;
import org.cafesip.jiplet.config.jip.Mapping;
import org.cafesip.jiplet.config.jip.MatchesOperation;
import org.cafesip.jiplet.config.jip.NotOperation;
import org.cafesip.jiplet.config.jip.OrOperation;
import org.cafesip.jiplet.config.jip.SubdomainOperation;
/**
* @author Amit Chatterjee
*
*/
public class MappingCompiler
{
private ArrayList compiledList;
private Object mappedObject;
private Mapping mapping;
/**
* A constructor for this class.
*
*
*/
public MappingCompiler()
{
super();
}
/**
* @return Returns the compiledList.
*/
protected ArrayList getCompiledList()
{
return compiledList;
}
/**
* @param compiledList
* The compiledList to set.
*/
public void setCompiledList(ArrayList map)
{
this.compiledList = map;
}
/**
* @return Returns the mappedObject.
*/
protected Object getMappedObject()
{
return mappedObject;
}
/**
* @param mappedObject
* The mappedObject to set.
*/
public void setMappedObject(Object mappedObject)
{
this.mappedObject = mappedObject;
}
/**
* @return Returns the mapping.
*/
protected Mapping getMapping()
{
return mapping;
}
/**
* @param mapping
* The mapping to set.
*/
public void setMapping(Mapping mapping)
{
this.mapping = mapping;
}
public String compile()
{
CriteriaMatch match = null;
if (mapping.getAnd() != null)
{
match = generateAndCriteria(mapping.getAnd());
if (match == null)
{
return null;
}
}
else if (mapping.getOr() != null)
{
match = generateOrCriteria(mapping.getOr());
if (match == null)
{
return null;
}
}
else if (mapping.getNot() != null)
{
match = generateNotCriteria(mapping.getNot());
if (match == null)
{
return null;
}
}
else if (mapping.getEquals() != null)
{
match = generateEqualsCriteria(mapping.getEquals());
if (match == null)
{
return null;
}
}
else if (mapping.getContains() != null)
{
match = generateContainsCriteria(mapping.getContains());
if (match == null)
{
return null;
}
}
else if (mapping.getExists() != null)
{
match = generateExistsCriteria(mapping.getExists());
if (match == null)
{
return null;
}
}
else if (mapping.getMatches() != null)
{
match = generateMatchesCriteria(mapping.getMatches());
if (match == null)
{
return null;
}
}
else if (mapping.getSubdomainOf() != null)
{
match = generateSubdomainCriteria(mapping.getSubdomainOf());
if (match == null)
{
return null;
}
}
else
{
return null;
}
compiledList.add(new Pair(match, mappedObject));
JipletLogger.debug("Mapped " + match.toExpression()
+ " to " + mappedObject);
return match.toExpression();
}
/**
* @param subdomainOf
* @return
*/
private CriteriaMatch generateSubdomainCriteria(SubdomainOperation op)
{
RequestCriterion cr = new RequestCriterion();
boolean ret = cr.setCriterion(op.getVar().trim(),
RequestCriterion.SUBDOMAIN,
op.getValue().trim());
if (ret == false)
{
return null;
}
return cr;
}
/**
* @param matches
* @return
*/
private CriteriaMatch generateMatchesCriteria(MatchesOperation op)
{
RequestCriterion cr = new RequestCriterion();
boolean ret = cr.setCriterion(op.getVar().trim(),
RequestCriterion.MATCHES,
op.getValue().trim());
if (ret == false)
{
return null;
}
return cr;
}
/**
* @param exists
* @return
*/
private CriteriaMatch generateExistsCriteria(ExistsOperation op)
{
RequestCriterion cr = new RequestCriterion();
boolean ret = cr.setCriterion(op.getVar().trim(), RequestCriterion.EXISTS, "");
if (ret == false)
{
return null;
}
return cr;
}
/**
* @param contains
* @return
*/
private CriteriaMatch generateContainsCriteria(ContainsOperation op)
{
RequestCriterion cr = new RequestCriterion();
int operator = -1;
if (op.getIgnoreCase().equals("true") == true)
{
operator = RequestCriterion.CONTAINSIGNORECASE;
}
else
{
operator = RequestCriterion.CONTAINS;
}
boolean ret = cr.setCriterion(op.getVar().trim(), operator, op
.getValue().trim());
if (ret == false)
{
return null;
}
return cr;
}
/**
* @param and
* @return
*/
private CriteriaMatch generateAndCriteria(AndOperation and)
{
AndCriterion criteria = new AndCriterion();
if (and.getAnd() != null)
{
Iterator iter = and.getAnd().iterator();
while (iter.hasNext() == true)
{
AndOperation op = (AndOperation) iter.next();
CriteriaMatch cr = generateAndCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getOr() != null)
{
Iterator iter = and.getOr().iterator();
while (iter.hasNext() == true)
{
OrOperation op = (OrOperation) iter.next();
CriteriaMatch cr = generateOrCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getNot() != null)
{
Iterator iter = and.getNot().iterator();
while (iter.hasNext() == true)
{
NotOperation op = (NotOperation) iter.next();
CriteriaMatch cr = generateNotCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getEquals() != null)
{
Iterator iter = and.getEquals().iterator();
while (iter.hasNext() == true)
{
EqualsOperation op = (EqualsOperation) iter.next();
CriteriaMatch cr = generateEqualsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getContains() != null)
{
Iterator iter = and.getContains().iterator();
while (iter.hasNext() == true)
{
ContainsOperation op = (ContainsOperation) iter.next();
CriteriaMatch cr = generateContainsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getExists() != null)
{
Iterator iter = and.getExists().iterator();
while (iter.hasNext() == true)
{
ExistsOperation op = (ExistsOperation) iter.next();
CriteriaMatch cr = generateExistsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getMatches() != null)
{
Iterator iter = and.getMatches().iterator();
while (iter.hasNext() == true)
{
MatchesOperation op = (MatchesOperation) iter.next();
CriteriaMatch cr = generateMatchesCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (and.getSubdomainOf() != null)
{
Iterator iter = and.getSubdomainOf().iterator();
while (iter.hasNext() == true)
{
SubdomainOperation op = (SubdomainOperation) iter.next();
CriteriaMatch cr = generateSubdomainCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
return criteria;
}
/**
* @param op
* @return
*/
private CriteriaMatch generateEqualsCriteria(EqualsOperation op)
{
RequestCriterion cr = new RequestCriterion();
int operator = -1;
if (op.getIgnoreCase().equals("true") == true)
{
operator = RequestCriterion.EQUALSIGNORECASE;
}
else
{
operator = RequestCriterion.EQUALS;
}
boolean ret = cr.setCriterion(op.getVar().trim(), operator, op
.getValue().trim());
if (ret == false)
{
return null;
}
return cr;
}
/**
* @param op
* @return
*/
private CriteriaMatch generateNotCriteria(NotOperation op)
{
NotCriterion not = new NotCriterion();
CriteriaMatch cr = null;
if (op.getAnd() != null)
{
cr = generateAndCriteria(op.getAnd());
}
else if (op.getOr() != null)
{
cr = generateOrCriteria(op.getOr());
}
else if (op.getNot() != null)
{
cr = generateNotCriteria(op.getNot());
}
else if (op.getEquals() != null)
{
cr = generateEqualsCriteria(op.getEquals());
}
else if (op.getContains() != null)
{
cr = generateContainsCriteria(op.getContains());
}
else if (op.getExists() != null)
{
cr = generateExistsCriteria(op.getExists());
}
else if (op.getMatches() != null)
{
cr = generateMatchesCriteria(op.getMatches());
}
else if (op.getSubdomainOf() != null)
{
cr = generateSubdomainCriteria(op.getSubdomainOf());
}
else
{
return null;
}
not.setCriteria(cr);
return not;
}
/**
* @param op
* @return
*/
private CriteriaMatch generateOrCriteria(OrOperation or)
{
OrCriterion criteria = new OrCriterion();
if (or.getAnd() != null)
{
Iterator iter = or.getAnd().iterator();
while (iter.hasNext() == true)
{
AndOperation op = (AndOperation) iter.next();
CriteriaMatch cr = generateAndCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getOr() != null)
{
Iterator iter = or.getOr().iterator();
while (iter.hasNext() == true)
{
OrOperation op = (OrOperation) iter.next();
CriteriaMatch cr = generateOrCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getNot() != null)
{
Iterator iter = or.getNot().iterator();
while (iter.hasNext() == true)
{
NotOperation op = (NotOperation) iter.next();
CriteriaMatch cr = generateNotCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getEquals() != null)
{
Iterator iter = or.getEquals().iterator();
while (iter.hasNext() == true)
{
EqualsOperation op = (EqualsOperation) iter.next();
CriteriaMatch cr = generateEqualsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getContains() != null)
{
Iterator iter = or.getContains().iterator();
while (iter.hasNext() == true)
{
ContainsOperation op = (ContainsOperation) iter.next();
CriteriaMatch cr = generateContainsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getExists() != null)
{
Iterator iter = or.getExists().iterator();
while (iter.hasNext() == true)
{
ExistsOperation op = (ExistsOperation) iter.next();
CriteriaMatch cr = generateExistsCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getMatches() != null)
{
Iterator iter = or.getMatches().iterator();
while (iter.hasNext() == true)
{
MatchesOperation op = (MatchesOperation) iter.next();
CriteriaMatch cr = generateMatchesCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
if (or.getSubdomainOf() != null)
{
Iterator iter = or.getSubdomainOf().iterator();
while (iter.hasNext() == true)
{
SubdomainOperation op = (SubdomainOperation) iter.next();
CriteriaMatch cr = generateSubdomainCriteria(op);
if (cr == null)
{
return null;
}
criteria.addCriterion(cr);
}
}
return criteria;
}
}