1 22 23 package org.xquark.xquery.normalize; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.xquery.parser.*; 28 import org.xquark.xquery.typing.TypeVisitor; 29 30 public class AtomizeVisitor extends DefaultParserVisitor { 31 32 33 private XQueryExpression resultExpr = null; 34 private TypeVisitor typeVisitor = null; 35 private boolean isSpecial = false; 36 37 38 AtomizeVisitor(TypeVisitor typeVisitor) { 39 this.typeVisitor = typeVisitor; 40 } 41 42 public XQueryExpression getExpression() { 43 isSpecial = false; 44 return resultExpr; 45 } 46 47 public void isSpecial() { 48 isSpecial= true; 49 } 50 51 53 public void visit(AttributeValuePair arg) throws XQueryException { 54 arg.getExpression2().accept(this); 55 } 56 57 67 public void visit(Element arg) throws XQueryException { 68 if (isSpecial) { 69 resultExpr = new ValueInteger("1",arg.getParentModule()); 70 return; 71 } 72 ArrayList list = arg.getSubExpressions(); 73 if (list != null) { 74 if (list.size() == 1) 75 resultExpr = (XQueryExpression)list.get(0); 76 else 77 resultExpr = new XQueryExpressionSequence(list, arg.getParentModule()); 78 } else 79 resultExpr = null; 80 } 81 82 public void visit(FLWRExpression arg) throws XQueryException { 83 if (isSpecial) { 84 if (!(arg.getReturnClause() instanceof Value)) 85 arg.setReturnClause(new ValueInteger("1",arg.getParentModule())); 87 } else { 88 arg.getReturnClause().accept(this); 89 arg.setReturnClause(resultExpr); 90 } 91 resultExpr = arg; 92 } 93 94 139 public void visit(XQueryExpression arg) throws XQueryException { 140 resultExpr = arg; 141 } 142 143 public void visit(XQueryExpressionSequence arg) throws XQueryException { 144 ArrayList list = arg.getSubExpressions(); 145 if (list != null) { 146 if (list.size() == 1) { 147 ((XQueryExpression)list.get(0)).accept(this); 148 } else { 149 for (int i=0;i<list.size();i++) { 150 XQueryExpression expri = (XQueryExpression)list.get(i); 151 expri.accept(this); 152 if (resultExpr != null) 153 list.set(i,resultExpr); 154 else { 155 list.remove(i); 156 i--; 157 } 158 } 159 if (list.size() == 0) 160 resultExpr = null; 161 else if (list.size() == 1) 162 resultExpr = (XQueryExpression)list.get(0); 163 else 164 resultExpr = arg; 165 } 166 } 167 } 168 174 176 } 220 | Popular Tags |