| 1 |
|
package org.jaxen.util; |
| 2 |
|
|
| 3 |
|
import java.io.PrintWriter; |
| 4 |
|
import java.util.Iterator; |
| 5 |
|
import java.util.List; |
| 6 |
|
|
| 7 |
|
import org.jaxen.expr.AdditiveExpr; |
| 8 |
|
import org.jaxen.expr.AllNodeStep; |
| 9 |
|
import org.jaxen.expr.CommentNodeStep; |
| 10 |
|
import org.jaxen.expr.EqualityExpr; |
| 11 |
|
import org.jaxen.expr.Expr; |
| 12 |
|
import org.jaxen.expr.FilterExpr; |
| 13 |
|
import org.jaxen.expr.FunctionCallExpr; |
| 14 |
|
import org.jaxen.expr.LiteralExpr; |
| 15 |
|
import org.jaxen.expr.LocationPath; |
| 16 |
|
import org.jaxen.expr.LogicalExpr; |
| 17 |
|
import org.jaxen.expr.MultiplicativeExpr; |
| 18 |
|
import org.jaxen.expr.NameStep; |
| 19 |
|
import org.jaxen.expr.NumberExpr; |
| 20 |
|
import org.jaxen.expr.PathExpr; |
| 21 |
|
import org.jaxen.expr.Predicate; |
| 22 |
|
import org.jaxen.expr.ProcessingInstructionNodeStep; |
| 23 |
|
import org.jaxen.expr.RelationalExpr; |
| 24 |
|
import org.jaxen.expr.Step; |
| 25 |
|
import org.jaxen.expr.TextNodeStep; |
| 26 |
|
import org.jaxen.expr.UnaryExpr; |
| 27 |
|
import org.jaxen.expr.UnionExpr; |
| 28 |
|
import org.jaxen.expr.VariableReferenceExpr; |
| 29 |
|
import org.jaxen.expr.Visitor; |
| 30 |
|
|
| 31 |
|
public class XPath2XMLVisitor implements Visitor { |
| 32 |
|
protected PrintWriter printer; |
| 33 |
|
protected int tabIndex; |
| 34 |
|
|
| 35 |
0 |
public XPath2XMLVisitor() { |
| 36 |
0 |
this.printer = new PrintWriter(System.out); |
| 37 |
0 |
} |
| 38 |
|
|
| 39 |
0 |
public XPath2XMLVisitor(PrintWriter printer) { |
| 40 |
0 |
this.printer = printer; |
| 41 |
0 |
} |
| 42 |
|
|
| 43 |
|
public void visit(PathExpr expr) { |
| 44 |
0 |
printLn("<PathExpr>"); |
| 45 |
0 |
if (expr.getFilterExpr() != null){ |
| 46 |
0 |
expr.getFilterExpr().accept(this); |
| 47 |
|
} |
| 48 |
0 |
if (expr.getLocationPath() != null){ |
| 49 |
0 |
expr.getLocationPath().accept(this); |
| 50 |
|
} |
| 51 |
0 |
printLn("</PathExpr>"); |
| 52 |
0 |
} |
| 53 |
|
public void visit(LocationPath expr) { |
| 54 |
0 |
printLn("<LocationPath absolute=\"" + expr.isAbsolute() + "\">"); |
| 55 |
0 |
Iterator steps = expr.getSteps().iterator(); |
| 56 |
|
|
| 57 |
0 |
while (steps.hasNext()){ |
| 58 |
0 |
Step step = (Step)steps.next(); |
| 59 |
0 |
step.accept(this); |
| 60 |
|
} |
| 61 |
0 |
printLn("</LocationPath>"); |
| 62 |
0 |
} |
| 63 |
|
public void visit(LogicalExpr expr) { |
| 64 |
0 |
printLn("<LogicalExpr operator=\""+ expr.getOperator() + "\">"); |
| 65 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 66 |
0 |
printLn("</LogicalExpr>"); |
| 67 |
0 |
} |
| 68 |
|
|
| 69 |
|
void printLhsRhs(Expr lhs, Expr rhs){ |
| 70 |
0 |
tabIndex++; |
| 71 |
0 |
printLn("<lhsExpr>"); |
| 72 |
0 |
lhs.accept(this); |
| 73 |
0 |
printLn("</lhsExpr>"); |
| 74 |
0 |
printLn("<rhsExpr>"); |
| 75 |
0 |
rhs.accept(this); |
| 76 |
0 |
printLn("</rhsExpr>"); |
| 77 |
0 |
tabIndex--; |
| 78 |
0 |
} |
| 79 |
|
public void visit(EqualityExpr expr) { |
| 80 |
0 |
printLn("<EqualityExpr operator=\""+ expr.getOperator() + "\">"); |
| 81 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 82 |
0 |
printLn("</EqualityExpr>"); |
| 83 |
0 |
} |
| 84 |
|
public void visit(FilterExpr expr) { |
| 85 |
0 |
printLn("<FilterExpr>"); |
| 86 |
0 |
tabIndex++; |
| 87 |
0 |
if (expr.getExpr() != null){ |
| 88 |
0 |
expr.getExpr().accept(this); |
| 89 |
|
} |
| 90 |
0 |
Iterator iter = expr.getPredicates().iterator(); |
| 91 |
0 |
while (iter.hasNext()){ |
| 92 |
0 |
((Predicate)iter.next()).getExpr().accept(this); |
| 93 |
|
} |
| 94 |
0 |
tabIndex--; |
| 95 |
0 |
printLn("</FilterExpr>"); |
| 96 |
0 |
} |
| 97 |
|
public void visit(RelationalExpr expr) { |
| 98 |
0 |
printLn("<RelationalExpr operator=\""+ expr.getOperator() + "\">"); |
| 99 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 100 |
0 |
printLn("</RelationalExpr>"); |
| 101 |
0 |
} |
| 102 |
|
public void visit(AdditiveExpr expr) { |
| 103 |
0 |
printLn("<AdditiveExpr operator=\""+ expr.getOperator() + "\">"); |
| 104 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 105 |
0 |
printLn("</AdditiveExpr>"); |
| 106 |
0 |
} |
| 107 |
|
public void visit(MultiplicativeExpr expr) { |
| 108 |
0 |
printLn("<MultiplicativeExpr operator=\""+ expr.getOperator() + "\">"); |
| 109 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 110 |
0 |
printLn("</MultiplicativeExpr>"); |
| 111 |
0 |
} |
| 112 |
|
public void visit(UnaryExpr expr) { |
| 113 |
0 |
printLn("<UnaryExpr>"); |
| 114 |
0 |
expr.getExpr().accept(this); |
| 115 |
0 |
printLn("</UnaryExpr>"); |
| 116 |
0 |
} |
| 117 |
|
public void visit(UnionExpr expr) { |
| 118 |
0 |
printLn("<UnionExpr>"); |
| 119 |
0 |
printLhsRhs(expr.getLHS(), expr.getRHS()); |
| 120 |
0 |
printLn("</UnionExpr>"); |
| 121 |
0 |
} |
| 122 |
|
public void visit(NumberExpr expr) { |
| 123 |
0 |
printLn("<NumberExpr>"); |
| 124 |
0 |
printLn("</NumberExpr>"); |
| 125 |
0 |
} |
| 126 |
|
public void visit(LiteralExpr expr) { |
| 127 |
0 |
printLn("<LiteralExpr literal=\"" + expr.getLiteral() + "\"/>"); |
| 128 |
0 |
} |
| 129 |
|
|
| 130 |
|
public void visit(VariableReferenceExpr expr) { |
| 131 |
0 |
printLn("<VariableReferenceExpr name=\"" + expr.getVariableName() + "\"/>"); |
| 132 |
0 |
} |
| 133 |
|
public void visit(FunctionCallExpr expr){ |
| 134 |
0 |
printLn("<FunctionCallExpr prefix=\"" + expr.getPrefix() + |
| 135 |
|
"\" functionName=\"" + expr.getFunctionName() + "\">"); |
| 136 |
|
|
| 137 |
0 |
Iterator iterator = expr.getParameters().iterator(); |
| 138 |
0 |
tabIndex++; |
| 139 |
0 |
printLn("<Args>"); |
| 140 |
0 |
while (iterator.hasNext()){ |
| 141 |
0 |
((Expr)iterator.next()).accept(this); |
| 142 |
|
} |
| 143 |
0 |
printLn("</Args>"); |
| 144 |
0 |
tabIndex--; |
| 145 |
0 |
printLn("</FunctionCallExpr>"); |
| 146 |
0 |
} |
| 147 |
|
|
| 148 |
|
public void visit(NameStep step){ |
| 149 |
0 |
printLn("<NameStep prefix=\"" + step.getPrefix()+ |
| 150 |
|
"\" localName=\"" + step.getLocalName() + "\">"); |
| 151 |
0 |
Iterator iter = step.getPredicates().iterator(); |
| 152 |
0 |
tabIndex++; |
| 153 |
0 |
while(iter.hasNext()){ |
| 154 |
0 |
Predicate predicate = (Predicate)iter.next(); |
| 155 |
0 |
predicate.accept(this); |
| 156 |
|
} |
| 157 |
0 |
tabIndex--; |
| 158 |
0 |
printLn("</NameStep>"); |
| 159 |
0 |
} |
| 160 |
|
public void visit(ProcessingInstructionNodeStep step){ |
| 161 |
0 |
printLn("<ProcessingInstructionNodeStep name=\"" + step.getName() + |
| 162 |
|
"\" axis=\"" + step.getAxis() + ">"); |
| 163 |
|
|
| 164 |
0 |
tabIndex++; |
| 165 |
0 |
handlePredicates(step.getPredicates()); |
| 166 |
0 |
tabIndex--; |
| 167 |
0 |
printLn("</ProcessingInstructionNodeStep>"); |
| 168 |
0 |
} |
| 169 |
|
public void visit(AllNodeStep step){ |
| 170 |
0 |
printLn("<AllNodeStep>"); |
| 171 |
0 |
tabIndex++; |
| 172 |
0 |
handlePredicates(step.getPredicates()); |
| 173 |
0 |
tabIndex--; |
| 174 |
0 |
printLn("</AllNodeStep>"); |
| 175 |
0 |
} |
| 176 |
|
public void visit(TextNodeStep step){ |
| 177 |
0 |
printLn("<TextNodeStep>"); |
| 178 |
0 |
tabIndex++; |
| 179 |
0 |
handlePredicates(step.getPredicates()); |
| 180 |
0 |
tabIndex--; |
| 181 |
0 |
printLn("</TextNodeStep>"); |
| 182 |
0 |
} |
| 183 |
|
public void visit(CommentNodeStep step){ |
| 184 |
0 |
printLn("<CommentNodeStep>"); |
| 185 |
0 |
tabIndex++; |
| 186 |
0 |
handlePredicates(step.getPredicates()); |
| 187 |
0 |
tabIndex--; |
| 188 |
0 |
printLn("</CommentNodeStep>"); |
| 189 |
0 |
} |
| 190 |
|
public void visit(Predicate predicate){ |
| 191 |
0 |
printLn("<Predicate>"); |
| 192 |
0 |
tabIndex++; |
| 193 |
0 |
predicate.getExpr().accept(this); |
| 194 |
0 |
tabIndex--; |
| 195 |
0 |
printLn("</Predicate>"); |
| 196 |
0 |
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
protected void printLn(String str){ |
| 200 |
0 |
StringBuffer buffer = new StringBuffer(); |
| 201 |
0 |
for (int i = 0; i < tabIndex; i++) { |
| 202 |
0 |
buffer.append("\t"); |
| 203 |
|
} |
| 204 |
0 |
buffer.append(str); |
| 205 |
|
|
| 206 |
0 |
printer.println(buffer.toString()); |
| 207 |
0 |
} |
| 208 |
|
|
| 209 |
|
protected void handlePredicates(List predicates){ |
| 210 |
0 |
if (predicates != null){ |
| 211 |
0 |
Iterator iter = predicates.iterator(); |
| 212 |
0 |
while(iter.hasNext()){ |
| 213 |
0 |
((Predicate)iter.next()).accept(this); |
| 214 |
|
} |
| 215 |
|
} |
| 216 |
0 |
} |
| 217 |
|
} |