View Javadoc

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