TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/XmlConditionVisitor.java
Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002     Simple distributed database engine
00003     Copyright (C) 2012  Sylvain Hallé
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  -------------------------------------------------------------------------*/
00018 package ca.uqac.dim.turtledb;
00019 
00020 import java.util.Stack;
00021 import org.w3c.dom.*;
00022 
00023 public class XmlConditionVisitor extends ConditionVisitor
00024 {
00025   protected Stack<Node> m_parts;
00026   protected Document m_doc;
00027 
00028   public XmlConditionVisitor(Document doc)
00029   {
00030     super();
00031     m_doc = doc;
00032     m_parts = new Stack<Node>();
00033   }
00034   
00035   @Override
00036   public void visit(LogicalAnd c)
00037   {
00038     Node n = m_doc.createElement("and");
00039     visitNAry(n, c);
00040   }
00041   
00042   protected void visitNAry(Node operator, NAryCondition c)
00043   {
00044     int len = c.getArity();
00045     for (int i = 0; i < len; i++)
00046     {
00047       Node op = m_parts.pop();
00048       operator.appendChild(op);
00049     }
00050     Node out = m_doc.createElement("condition");
00051     out.appendChild(operator);
00052     m_parts.push(out);
00053   }
00054 
00055   @Override
00056   public void visit(LogicalOr c)
00057   {
00058     Node n = m_doc.createElement("or");
00059     visitNAry(n, c);
00060   }
00061 
00062   @Override
00063   public void visit(Equality e)
00064   {
00065     Node n = m_doc.createElement("equals");
00066     Node n_left = createLiteralNode(e.m_left);
00067     Node n_right = createLiteralNode(e.m_right);
00068     n.appendChild(n_left);
00069     n.appendChild(n_right);
00070     Node n_c = m_doc.createElement("condition");
00071     n_c.appendChild(n);
00072     m_parts.push(n_c);
00073   }
00074   
00075   public Node getCondition()
00076   {
00077     Node out = m_parts.peek();
00078     return out;
00079   }
00080   
00081   public Node getNode()
00082   {
00083     Node out = m_parts.peek();
00084     return out;
00085   }
00086 
00087   protected Node createLiteralNode(Literal l)
00088   {
00089     Node n = null;
00090     if (l instanceof Attribute)
00091     {
00092       Attribute a = (Attribute) l;
00093       n = m_doc.createElement("attribute");
00094       Node n_n = m_doc.createElement("name");
00095       n_n.setTextContent(a.getName());
00096       n.appendChild(n_n);
00097       Node n_t = m_doc.createElement("table");
00098       n_t.setTextContent(a.getTableName());
00099       n.appendChild(n_t);
00100     }
00101     if (l instanceof Value)
00102       n = m_doc.createElement("value");
00103     n.setTextContent(l.toString());
00104     return n;
00105   }
00106 
00107 }