KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > parser > PrintVisitor


1 /*
2  * This file belongs to the XQuark distribution. Copyright (C) 2004 XQuark Group.
3  *
4  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7  *
8  * You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it at http://www.gnu.org/licenses/lgpl.html
9  *
10  * For more information on this software, see http://www.xquark.org.
11  */

12
13 package org.xquark.xquery.parser;
14
15 import java.util.*;
16
17 import org.xquark.util.NamespaceContextStack;
18 import org.xquark.xpath.Axis;
19 import org.xquark.xpath.NodeKind;
20 import org.xquark.xquery.parser.util.Constants;
21
22 public class PrintVisitor extends DefaultParserVisitor {
23
24     private StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
25
26     private PrintVisitor pvisitor = null;
27
28     private int indent = 0;
29
30     private boolean doLineFeed = false;
31
32     private boolean doImports = false;
33
34     // private boolean doExpressions = true;
35
private boolean doNodeAccessor = false;
36
37     private boolean doPrefix = false;
38
39     private NamespaceContextStack declarations = null;
40
41     // PrintVisitor(NamespaceContextStack declarations) {
42
// this.declarations = declarations;
43
// }
44

45     // public void reset(boolean doLineFeed) {
46
// this.doLineFeed = doLineFeed;
47
// buf.setLength(0);
48
// }
49
// public void reset(boolean doLineFeed, boolean doImports) {
50
// this.doLineFeed = doLineFeed;
51
// this.doImports = doImports;
52
// buf.setLength(0);
53
// }
54

55     public void reset(boolean doLineFeed, boolean doImports, boolean doNodeAccessor, boolean doPrefix) {
56         this.doLineFeed = doLineFeed;
57         this.doImports = doImports;
58         this.doNodeAccessor = doNodeAccessor;
59         this.doPrefix = doPrefix;
60         this.declarations = null;
61         buf.setLength(0);
62     }
63
64     public void setNamespaceContextStack(NamespaceContextStack context) {
65         declarations = context;
66     }
67
68     // public void reset(boolean doLineFeed, boolean doImports, boolean doExpressions) {
69
// this.doLineFeed = doLineFeed;
70
// this.doImports = doImports;
71
// this.doExpressions = doExpressions;
72
// buf.setLength(0);
73
// }
74

75     public String JavaDoc getString() {
76         return buf.toString();
77     }
78
79     private void lineFeed() {
80         if (doLineFeed) {
81             buf.append('\n');
82             for (int i = 0; i < indent; i++) {
83                 buf.append('\t');
84             }
85         }
86     }
87
88     private void doBefore(XQueryExpression arg) {
89         if (arg.getParenthesis())
90             buf.append("(");
91     }
92     private void doAfter(XQueryExpression arg) {
93         if (arg.getParenthesis())
94             buf.append(")");
95     }
96
97     private String JavaDoc foreSee(XQueryExpression arg) throws XQueryException {
98         if (pvisitor == null)
99             pvisitor = new PrintVisitor();
100         pvisitor.reset(doLineFeed, doImports, doNodeAccessor, doPrefix);
101         pvisitor.setNamespaceContextStack(declarations);
102         arg.accept(pvisitor);
103         return pvisitor.getString();
104     }
105
106     // public void visit(AggregateFunctionCall arg) throws XQueryException;
107
public void visit(AttributeValuePair arg) throws XQueryException {
108
109         if (arg.isXmlns()) {
110             buf.append("xmlns");
111             if (((ValueString) arg.getAttributeName()).getStringValue().length() != 0) {
112                 buf.append(":");
113                 buf.append(((ValueString) arg.getAttributeName()).getValue());
114             }
115             buf.append("=");
116             arg.getExpression2().accept(this);
117         } else if (arg.isComputed()) {
118             buf.append("attribute ");
119             if (!(arg.getExpression1() instanceof QName))
120                 buf.append("{ ");
121             arg.getExpression1().accept(this);
122             if (!(arg.getExpression1() instanceof QName))
123                 buf.append(" }");
124             buf.append(" { ");
125             arg.getExpression2().accept(this);
126             buf.append(" }");
127         } else {
128             arg.getExpression1().accept(this);
129             String JavaDoc expr2 = foreSee(arg.getExpression2());
130             if (!(expr2.charAt(0) == '\"')) {
131                 buf.append("=\"");
132                 if (!(arg.getExpression2() instanceof Value))
133                     buf.append("{");
134                 buf.append(expr2);
135                 if (!(arg.getExpression2() instanceof Value))
136                     buf.append("}");
137                 buf.append("\"");
138             } else {
139                 buf.append("=");
140                 if (!(arg.getExpression2() instanceof Value))
141                     buf.append("{");
142                 buf.append(expr2);
143                 if (!(arg.getExpression2() instanceof Value))
144                     buf.append("}");
145             }
146         }
147
148     }
149
150     public void visit(BinOpANDExpression arg) throws XQueryException {
151
152         doBefore(arg);
153         arg.getExpression1().accept(this);
154         buf.append(Constants.NODELISTOPERATORSSTRINGS[Constants.AND_NODELISTOPERATOR]);
155         arg.getExpression2().accept(this);
156         doAfter(arg);
157     }
158
159     public void visit(BinOpORExpression arg) throws XQueryException {
160
161         doBefore(arg);
162         arg.getExpression1().accept(this);
163         buf.append(Constants.NODELISTOPERATORSSTRINGS[Constants.OR_NODELISTOPERATOR]);
164         arg.getExpression2().accept(this);
165         doAfter(arg);
166    }
167
168     public void visit(CaseClauseExpression arg) throws XQueryException {
169
170         buf.append("case ");
171         if (arg.getSequenceType() != null)
172             arg.getSequenceType().accept(this);
173         if (arg.getVariable() != null)
174             arg.getVariable().accept(this);
175         buf.append(" return ");
176         arg.getReturnExpression().accept(this);
177
178     }
179
180     public void visit(CastTreatExpression arg) throws XQueryException {
181
182         buf.append(Constants.TYPINGSTRINGS[arg.getOperator()]);
183         buf.append(" as ");
184         arg.getSequenceType().accept(this);
185         buf.append("(");
186         arg.getExpression().accept(this);
187         buf.append(")");
188
189     }
190
191     public void visit(CData arg) throws XQueryException {
192
193         buf.append("<![CDATA[ ");
194         buf.append(arg.getCData());
195         buf.append(" ]]>");
196
197     }
198
199     public void visit(ComputedNamespace arg) throws XQueryException {
200
201         buf.append("namespace ");
202         buf.append(arg.getNcname());
203         buf.append(" { ");
204         arg.getExpression().accept(this);
205         buf.append(" }");
206
207     }
208
209     public void visit(ComputedText arg) throws XQueryException {
210
211         buf.append("text {");
212         arg.getExpression().accept(this);
213         buf.append(" }");
214
215     }
216
217     public void visit(Document arg) throws XQueryException {
218
219         buf.append("document { ");
220         arg.getExpression().accept(this);
221         buf.append(" }");
222
223     }
224
225     public void visit(Element arg) throws XQueryException {
226         HashMap map = arg.getLocalPrefixes();
227         if (declarations != null && map != null) {
228             declarations.pushContext();
229             for (Iterator it = map.keySet().iterator(); it.hasNext();) {
230                 String JavaDoc prefix = (String JavaDoc) it.next();
231                 declarations.declarePrefix(prefix, (String JavaDoc) map.get(prefix));
232             }
233         }
234         if (arg.isComputed()) {
235             buf.append("element ");
236             if (!(arg.getStartTag() instanceof QName))
237                 buf.append("{ ");
238             arg.getStartTag().accept(this);
239             if (!(arg.getStartTag() instanceof QName))
240                 buf.append(" }");
241             buf.append(" {");
242             ArrayList attributes = arg.getAttributes();
243             if (attributes != null)
244                 for (int i = 0; i < attributes.size(); i++) {
245                     buf.append(" ");
246                     ((AttributeValuePair) attributes.get(i)).accept(this);
247                     if (i < attributes.size() - 1)
248                         buf.append(",");
249                 }
250             ArrayList subExpressions = arg.getSubExpressions();
251             if (subExpressions != null)
252                 for (int j = 0; j < subExpressions.size(); j++) {
253                     if (j == 0 && attributes != null)
254                         buf.append(",");
255                     buf.append(" ");
256                     ((XQueryExpression) subExpressions.get(j)).accept(this);
257                     if (j < subExpressions.size() - 1)
258                         buf.append(",");
259                 }
260             buf.append(" }");
261         } else {
262             buf.append("<");
263             arg.getStartTag().accept(this);
264             ArrayList attributes = arg.getAttributes();
265             if (attributes != null)
266                 for (int i = 0; i < attributes.size(); i++) {
267                     buf.append(" ");
268                     ((AttributeValuePair) attributes.get(i)).accept(this);
269                 }
270             buf.append(">");
271             ArrayList subExpressions = arg.getSubExpressions();
272             if (subExpressions != null) {
273                 indent++;
274                 for (int j = 0; j < subExpressions.size(); j++) {
275                     lineFeed();
276                     XQueryExpression exprj = (XQueryExpression) subExpressions.get(j);
277                     if (!(exprj instanceof ValueText) && !(exprj instanceof Element))
278                         buf.append("{");
279                     ((XQueryExpression) subExpressions.get(j)).accept(this);
280                     if (!(exprj instanceof ValueText) && !(exprj instanceof Element))
281                         buf.append("}");
282                 }
283                 indent--;
284                 lineFeed();
285             }
286             buf.append("</");
287             arg.getStartTag().accept(this);
288             buf.append(">");
289         }
290         if (declarations != null && map != null)
291             declarations.popContext();
292     }
293
294     public void visit(ElementTest arg) throws XQueryException {
295
296         buf.append("element(");
297         if (arg.getSchemaContextPath() != null)
298             arg.getSchemaContextPath().accept(this);
299         else if (arg.getDeclQName() != null) {
300             arg.getDeclQName().accept(this);
301             if (arg.getTypeQName() != null) {
302                 buf.append(",");
303                 arg.getTypeQName().accept(this);
304                 if (arg.getNillable())
305                     buf.append(" nillable");
306             }
307         }
308         buf.append(')');
309
310     }
311
312     //public void visit(ExternalVariable arg) throws XQueryException {
313

314     public void visit(FLWRExpression arg) throws XQueryException {
315         if (arg.getParentExpression() != null && !arg.getParentExpression().isEmpty()) {
316             indent++;
317             lineFeed();
318         }
319
320         ArrayList variables = arg.getVariables();
321         if (variables != null) {
322             for (int i = 0; i < variables.size(); i++) {
323                 if (i != 0)
324                     lineFeed();
325                 Variable vari = (Variable) variables.get(i);
326                 int bindingtype = vari.getBindingType();
327                 switch (bindingtype) {
328                 case Constants.FOR_BINDINGTYPE:
329                     buf.append(" for ");
330                     vari.accept(this);
331                     buf.append(" in ");
332                     break;
333                 case Constants.LET_BINDINGTYPE:
334                     buf.append(" let ");
335                     vari.accept(this);
336                     buf.append(" := ");
337                     break;
338                 case Constants.OUTERFOR_BINDINGTYPE:
339                     buf.append(" outerfor ");
340                     vari.accept(this);
341                     buf.append(" in ");
342                     break;
343                 case Constants.OUTERLET_BINDINGTYPE:
344                     buf.append(" outerlet ");
345                     vari.accept(this);
346                     buf.append(" := ");
347                 }
348                 if (vari.getPositionVar() != null) {
349                     buf.append(" at ");
350                     vari.getPositionVar().accept(this);
351                 }
352                 if (vari.getExpression() != null)
353                     vari.getExpression().accept(this);
354             }
355         }
356         if (arg.getWhereClause() != null) {
357             lineFeed();
358             buf.append(" where ");
359             arg.getWhereClause().accept(this);
360         }
361         if (arg.getOrderBy() != null) {
362             lineFeed();
363             buf.append(" order by ");
364             for (int i = 0; i < arg.getOrderBy().size(); i++) {
365                 XQueryExpression expri = (XQueryExpression) arg.getOrderBy().get(i);
366                 if (i != 0)
367                     buf.append(",");
368                 expri.accept(this);
369                 if (expri.getOrder(0) != Constants.NOTHING) {
370                     buf.append(" ");
371                     buf.append(Constants.ORDERSSTRINGS[expri.getOrder(0)]);
372                 }
373                 if (expri.getOrder(1) != Constants.NOTHING) {
374                     buf.append(" empty ");
375                     buf.append(Constants.ORDERSSTRINGS[expri.getOrder(1)]);
376                 }
377             }
378         }
379         if (arg.getReturnClause() != null) {
380             lineFeed();
381             buf.append(" return ");
382             indent++;
383             lineFeed();
384             arg.getReturnClause().accept(this);
385             indent--;
386             lineFeed();
387         }
388
389         if (arg.getParentExpression() != null && !arg.getParentExpression().isEmpty()) {
390             indent--;
391         }
392     }
393
394     public void visit(FunctionCall arg) throws XQueryException {
395
396         arg.getFuncName().accept(this);
397         buf.append("(");
398         ArrayList arguments = arg.getArguments();
399         if (arguments != null) {
400             for (int i = 0; i < arguments.size(); i++) {
401                 if (i != 0)
402                     buf.append(",");
403                 ((XQueryExpression) arguments.get(i)).accept(this);
404             }
405         }
406         buf.append(")");
407
408     }
409
410     public void visit(FunctionDeclaration arg) throws XQueryException {
411
412         buf.append("declare function ");
413         arg.getFuncName().accept(this);
414         buf.append(" (");
415         if (arg.getParameters() != null) {
416             for (int i = 0; i < arg.getParameters().size(); i++) {
417                 if (i != 0)
418                     buf.append(",");
419                 Variable vari = (Variable) arg.getParameters().get(i);
420                 vari.accept(this);
421                 if (vari.getTypeDeclaration() != null) {
422                     buf.append(" as ");
423                     vari.getTypeDeclaration().accept(this);
424                 }
425             }
426         }
427         buf.append(")");
428         if (arg.getReturnType() != null) {
429             buf.append(" as ");
430             arg.getReturnType().accept(this);
431         }
432         if (arg.getExpressions() != null) {
433             buf.append(" { ");
434             for (int i = 0; i < arg.getExpressions().size(); i++) {
435                 if (i != 0)
436                     buf.append(" , ");
437                 ((XQueryExpression) arg.getExpressions().get(i)).accept(this);
438             }
439             buf.append(" } ");
440         } else {
441             buf.append(" external");
442         }
443
444     }
445
446     public void visit(ImportModule arg) throws XQueryException {
447
448         buf.append("import module ");
449         if (arg.getPrefix() != null) {
450             buf.append("namespace ");
451             buf.append(arg.getPrefix());
452             buf.append(" = ");
453         }
454         buf.append("\"");
455         buf.append(arg.getName());
456         buf.append("\"");
457         if (arg.getLocation() != null) {
458             buf.append(" at \"");
459             buf.append(arg.getLocation());
460             buf.append("\"");
461         }
462         buf.append(";");
463
464     }
465
466     public void visit(ImportSchema arg) throws XQueryException {
467
468         buf.append("import schema ");
469         if (arg.getHasPrefix()) {
470             if (arg.getPrefix() == null)
471                 buf.append("default element namespace ");
472             else {
473                 buf.append("namespace ");
474                 buf.append(arg.getPrefix());
475                 buf.append(" = ");
476             }
477         }
478         buf.append("\"");
479         buf.append(arg.getName());
480         buf.append("\"");
481         if (arg.getLocation() != null) {
482             buf.append(" at \"");
483             buf.append(arg.getLocation());
484             buf.append("\"");
485         }
486         buf.append(";");
487
488     }
489
490     // public void visit(InputFunctionCall arg) throws XQueryException {
491
public void visit(InstanceOfExpression arg) throws XQueryException {
492
493         arg.getExpression().accept(this);
494         buf.append(" instanceof ");
495         arg.getSequenceType().accept(this);
496
497     }
498
499     // public void visit(InternalFunctionCall arg) throws XQueryException;
500
public void visit(ITEExpression arg) throws XQueryException {
501
502         buf.append(" if ( ");
503         arg.getIfExpression().accept(this);
504         buf.append(" ) then ");
505         arg.getThenExpression().accept(this);
506         buf.append(" else ");
507         arg.getElseExpression().accept(this);
508
509     }
510
511     public void visit(ItemType arg) throws XQueryException {
512
513         if (arg.getExpression() != null)
514             arg.getExpression().accept(this);
515         else
516             buf.append("item()");
517
518     }
519
520     // public void visit(LibraryFunctionCall arg) throws XQueryException;
521
public void visit(ListOpAFTERExpression arg) throws XQueryException {
522
523         arg.getExpression1().accept(this);
524         buf.append(" after ");
525         arg.getExpression2().accept(this);
526
527     }
528
529     public void visit(ListOpArithExpression arg) throws XQueryException {
530
531         arg.getExpression1().accept(this);
532         buf.append(" ");
533         buf.append(Constants.ARITHMETICSSTRINGS[arg.getOperator()]);
534         buf.append(" ");
535         arg.getExpression2().accept(this);
536
537     }
538
539     public void visit(ListOpBEFOREExpression arg) throws XQueryException {
540
541         arg.getExpression1().accept(this);
542         buf.append(" before ");
543         arg.getExpression2().accept(this);
544
545     }
546
547     public void visit(ListOpCompExpression arg) throws XQueryException {
548
549         arg.getExpression1().accept(this);
550         buf.append(" ");
551         buf.append(Constants.COMPOPSSTRINGS[arg.getOperator()]);
552         buf.append(" ");
553         arg.getExpression2().accept(this);
554
555     }
556
557     public void visit(ListOpEXCEPTExpression arg) throws XQueryException {
558
559         arg.getExpression1().accept(this);
560         buf.append(" except ");
561         arg.getExpression2().accept(this);
562
563     }
564
565     public void visit(ListOpINTERSECTExpression arg) throws XQueryException {
566
567         arg.getExpression1().accept(this);
568         buf.append(" intersect ");
569         arg.getExpression2().accept(this);
570
571     }
572
573     public void visit(ListOpUNIONExpression arg) throws XQueryException {
574
575         arg.getExpression1().accept(this);
576         buf.append(" union ");
577         arg.getExpression2().accept(this);
578
579     }
580
581     public void visit(LocatedExpression arg) throws XQueryException {
582         for (int i = 0; i < arg.getSteps().size(); i++) {
583             ((Step) arg.getSteps().get(i)).accept(this);
584         }
585         if (doNodeAccessor) {
586             int nodeAccessor = arg.getNodeAccessor();
587             switch (nodeAccessor) {
588             case Step.CHILD_TEXT:
589                 buf.append("/child::text()");
590                 break;
591             case Step.CHILD_NODE:
592                 buf.append("/child::node()");
593                 break;
594             case Step.SELF_TEXT:
595                 buf.append("/self::text()");
596                 break;
597             case Step.SELF_NODE:
598                 buf.append("/self::node()");
599                 break;
600             }
601         }
602     }
603
604     public void visit(NodeTest arg) throws XQueryException {
605
606         buf.append(NodeKind.NODEKINDSTRINGS[arg.getKind()]);
607         buf.append('(');
608         if (arg.getArg() != null)
609             arg.getArg().accept(this);
610         buf.append(')');
611
612     }
613
614     // public void visit(PrimitiveFunctionCall arg) throws XQueryException;
615
public void visit(QName arg) throws XQueryException {
616         String JavaDoc prefix = null;
617         if (arg.getPrefixName() != null && arg.getPrefixName().equals("*"))
618             prefix = arg.getPrefixName();
619         if (doPrefix) {
620             if (prefix == null && arg.getNameSpace() != null) {
621                 if (declarations != null)
622                     prefix = declarations.getPrefix(arg.getNameSpace());
623                 else if (arg.getNamespaceContext() != null)
624                     prefix = arg.getNamespaceContext().getPrefix(arg.getNameSpace());
625                 if (prefix != null)
626                     arg.setPrefixName(prefix);
627             }
628         }
629         if (prefix != null) {
630             if (prefix.length() != 0) {
631                 buf.append(prefix);
632                 buf.append(":");
633             }
634         } else {
635             if (arg.getNameSpace() != null) {
636                 buf.append("{");
637                 buf.append(arg.getNameSpace());
638                 buf.append("}");
639             }
640         }
641         buf.append(arg.getLocalName());
642
643         // if (arg.getNameSpace() != null) {
644
// buf.append("{");
645
// buf.append(arg.getNameSpace());
646
// buf.append("}");
647
// }
648
// buf.append(arg.getVarName().getLocalName());
649

650         // if (arg.getNameSpace() != null && declarations != null) {
651
// String prefix = declarations.getPrefix(arg.getNameSpace());
652
// if (prefix != null && prefix.length() != 0) {
653
// buf.append(prefix);
654
// buf.append(":");
655
// } else {
656
// if (arg.getPrefixName() != null && (!arg.getPrefixName().equals("*") || !arg.getLocalName().equals("*"))) {
657
// buf.append(arg.getPrefixName());
658
// buf.append(":");
659
// }
660
// }
661
// } else {
662
// if (arg.getPrefixName() != null && (!arg.getPrefixName().equals("*") || !arg.getLocalName().equals("*"))) {
663
// buf.append(arg.getPrefixName());
664
// buf.append(":");
665
// }
666
// }
667
// buf.append(arg.getLocalName());
668
}
669
670     public void visit(QuantifiedExpression arg) throws XQueryException {
671
672         buf.append(Constants.QUANTIFIERSSTRINGS[arg.getKind()]);
673         buf.append(" ");
674         for (int i = 0; i < arg.getVariables().size(); i++) {
675             if (i != 0)
676                 buf.append(" , ");
677             Variable vari = (Variable) arg.getVariables().get(i);
678             vari.accept(this);
679             buf.append(" in ");
680             buf.append(vari.getExpression());
681         }
682         buf.append(" satisfies ");
683         arg.getExpression().accept(this);
684
685     }
686
687     public void visit(RangeExpression arg) throws XQueryException {
688
689         arg.getExpression1().accept(this);
690         buf.append(" to ");
691         arg.getExpression2().accept(this);
692
693     }
694
695     public void visit(SchemaContextPath arg) throws XQueryException {
696
697         for (int i = 0; i < arg.getSteps().size(); i++)
698             ((SchemaContextStep) arg.getSteps().get(i)).accept(this);
699
700     }
701
702     public void visit(SchemaContextStep arg) throws XQueryException {
703
704         if (arg.getAxis() == Constants.WITHAXIS)
705             buf.append('/');
706         else if (arg.getAxis() == Constants.WITHTYPE)
707             buf.append("type(");
708         arg.getExpression().accept(this);
709         if (arg.getAxis() == Constants.WITHTYPE)
710             buf.append(')');
711
712     }
713
714     public void visit(SequenceType arg) throws XQueryException {
715
716         if (arg.getItemType() != null) {
717             arg.getItemType().accept(this);
718             if (arg.getOccurrence() != -1)
719                 buf.append(Constants.OCCURRENCE_INDICATORS[arg.getOccurrence()]);
720         } else
721             buf.append("empty()");
722
723     }
724
725     public void visit(SortedExpression arg) throws XQueryException {
726
727         arg.getExpression().accept(this);
728         if (arg.getStable())
729             buf.append(" stable");
730         buf.append(" sortby ( ");
731         for (int j = 0; j < arg.getSortClauses().size(); j++) {
732             XQueryExpression expri = (XQueryExpression) arg.getSortClauses().get(j);
733             if (j != 0)
734                 buf.append(",");
735             expri.accept(this);
736             if (expri.getOrder(0) != Constants.NOTHING) {
737                 buf.append(" ");
738                 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(0)]);
739             }
740             if (expri.getOrder(1) != Constants.NOTHING) {
741                 buf.append(" empty ");
742                 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(1)]);
743             }
744         }
745         buf.append(" )");
746
747     }
748
749     public void visit(Step arg) throws XQueryException {
750
751         if (arg.hasSeparator())
752             buf.append('/');
753         buf.append(Axis.AXISSTRINGS[arg.getAxis()]);
754         arg.getExpression().accept(this);
755         if (arg.getPredicates() != null)
756             for (int i = 0; i < arg.getPredicates().size(); i++) {
757                 buf.append("[");
758                 ((XQueryExpression) arg.getPredicates().get(i)).accept(this);
759                 buf.append("]");
760             }
761
762     }
763
764     public void visit(TypeSwitchExpression arg) throws XQueryException {
765
766         buf.append("typeswitch (");
767         arg.getExpression1().accept(this);
768         buf.append(") ");
769         for (Iterator it = arg.getCases().values().iterator(); it.hasNext();) {
770             ((CaseClauseExpression) it.next()).accept(this);
771         }
772         buf.append(" default ");
773         if (arg.getDefaultVariable() != null)
774             arg.getDefaultVariable().accept(this);
775         arg.getExpression2().accept(this);
776
777     }
778
779     public void visit(UnOpMinusExpression arg) throws XQueryException {
780
781         if (arg.getMinus())
782             buf.append("- ");
783         else
784             buf.append("+ ");
785         arg.getExpression().accept(this);
786
787     }
788
789     public void visit(ValidateExpression arg) throws XQueryException {
790
791         buf.append("validate");
792         if (arg.getValidationType() != Constants.NOTHING) {
793             buf.append(" ");
794             buf.append(Constants.VALIDATIONSTRINGS[arg.getValidationType()]);
795         }
796         if (arg.getScopeType() != Constants.NOTHING) {
797             buf.append(" ");
798             buf.append(Constants.SCOPE_STRINGS[arg.getScopeType()]);
799         }
800         if (arg.getSchemaContext() != null) {
801             buf.append(" ");
802             arg.getSchemaContext().accept(this);
803         }
804         buf.append(" { ");
805         arg.getExpression().accept(this);
806         buf.append(" }");
807
808     }
809
810     public void visit(Value arg) throws XQueryException {
811
812         buf.append(arg.getValue());
813
814     }
815
816     // public void visit(ValueBoolean arg) throws XQueryException;
817
// public void visit(ValueDecimal arg) throws XQueryException;
818
// public void visit(ValueDouble arg) throws XQueryException;
819
// public void visit(ValueFloat arg) throws XQueryException;
820
// public void visit(ValueInteger arg) throws XQueryException;
821
public void visit(ValueString arg) throws XQueryException {
822
823         buf.append("\"");
824         buf.append(arg.getValue());
825         buf.append("\"");
826
827     }
828
829     // public void visit(ValueParameter arg) throws XQueryException;
830
// public void visit(ValueText arg) throws XQueryException;
831
public void visit(Variable arg) throws XQueryException {
832
833         buf.append("$");
834         arg.getVarName().accept(this);
835         // if (arg.getVarName().getNameSpace() != null) {
836
// buf.append("{");
837
// buf.append(arg.getVarName().getNameSpace());
838
// buf.append("}");
839
// }
840
// buf.append(arg.getVarName().getLocalName());
841
}
842
843     public void visit(XMLComment arg) throws XQueryException {
844
845         if (arg.isComputed()) {
846             buf.append("comment { ");
847             arg.getExpression().accept(this);
848             buf.append(" }");
849         } else {
850             buf.append("<!-- ");
851             arg.getExpression().accept(this);
852             buf.append(" -->");
853         }
854
855     }
856
857     public void visit(XMLProcessingInstruction arg) throws XQueryException {
858
859         if (arg.isComputed()) {
860             buf.append("processing-instruction ");
861             arg.getExpression1().accept(this);
862             buf.append(" { ");
863             arg.getExpression2().accept(this);
864             buf.append(" }");
865         } else {
866             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
867             buf.append("<? ");
868             arg.getExpression1().accept(this);
869             buf.append(" ");
870             arg.getExpression2().accept(this);
871             buf.append(" ?>");
872         }
873
874     }
875
876     // public void visit(XQueryBinaryOperatorExpression arg) throws XQueryException;
877
// public void visit(XQueryBooleanOperatorExpression arg) throws XQueryException;
878
// public void visit(XQueryExpression arg) throws XQueryException {
879
public void visit(XQueryExpressionSequence arg) throws XQueryException {
880         doBefore(arg);
881         for (int j = 0; j < arg.getSubExpressions().size(); j++) {
882             if (j != 0)
883                 buf.append(" , ");
884             ((XQueryExpression) arg.getSubExpressions().get(j)).accept(this);
885         }
886         doAfter(arg);
887     }
888
889     // public void visit(XQueryListBinaryOperatorExpression arg) throws XQueryException;
890
// public void visit(XQueryListUnaryOperatorExpression arg) throws XQueryException;
891
// public void visit(XQueryUnaryOperatorExpression arg) throws XQueryException;
892
public void visit(XQueryModule arg) throws XQueryException {
893         if (arg.getVersion() != null) {
894             lineFeed();
895             buf.append("xquery version \"");
896             buf.append(arg.getVersion());
897             buf.append("\";");
898         }
899         if (arg.getPrefix() != null && arg.getNamespace() != null) {
900             lineFeed();
901             buf.append("module namespace ");
902             buf.append(arg.getPrefix());
903             buf.append(" = \"");
904             buf.append(arg.getNamespace());
905             buf.append("\"");
906         }
907         if (arg.getDeclarations() != null) {
908             lineFeed();
909             Collection declarationsPrefixes = arg.getDeclarations().getPrefixes();
910             Iterator prefixes = declarationsPrefixes.iterator();
911             while (prefixes.hasNext()) {
912                 String JavaDoc prefix = (String JavaDoc) prefixes.next();
913                 if ((prefix == null) || (prefix.trim().length() == 0)) {
914                     buf.append("declare default element namespace ");
915                     buf.append("=\"");
916                     buf.append((String JavaDoc) arg.getDeclarations().getNamespaceURI(prefix));
917                     buf.append("\";");
918                 } else if (prefix != "xml") {
919                     buf.append("declare namespace ");
920                     buf.append(prefix);
921                     buf.append("=\"");
922                     buf.append((String JavaDoc) arg.getDeclarations().getNamespaceURI(prefix));
923                     buf.append("\";");
924                 }
925             }
926         }
927         if (arg.getXmlSpace() != Constants.NOTHING) {
928             lineFeed();
929             buf.append("declare xmlspace ");
930             buf.append(Constants.XMLSPACESTRINGS[arg.getXmlSpace()]);
931         }
932         if (arg.getDefaultFunctionNamespace() != null) {
933             lineFeed();
934             buf.append("declare default function namespace = \"");
935             buf.append(arg.getDefaultFunctionNamespace());
936             buf.append("\";");
937         }
938         if (arg.getDefaultCollation() != null) {
939             lineFeed();
940             buf.append("declare default collation = \"");
941             buf.append(arg.getDefaultCollation());
942             buf.append("\";");
943         }
944         if (arg.getBaseURI() != null) {
945             lineFeed();
946             buf.append("declare base-uri = \"");
947             buf.append(arg.getBaseURI());
948             buf.append("\";");
949         }
950         if (arg.getSchemaList() != null) {
951             for (int i = 0; i < arg.getSchemaList().size(); i++) {
952                 lineFeed();
953                 ((ImportSchema) arg.getImportSchemas().get(arg.getSchemaList().get(i))).accept(this);
954             }
955         }
956         if (arg.getValidation() != Constants.NOTHING && arg.getValidation() != Constants.VALIDATION_PRESERVE) {
957             lineFeed();
958             buf.append("declare validation ");
959             buf.append(Constants.VALIDATIONSTRINGS[arg.getValidation()]);
960             buf.append(";");
961         }
962         if (doImports) {
963             if (arg.getModuleList() != null) {
964                 for (int i = 0; i < arg.getModuleList().size(); i++) {
965                     lineFeed();
966                     ((ImportModule) arg.getImportModules().get(arg.getModuleList().get(i))).accept(this);
967                 }
968             }
969         }
970         if (arg.getDeclList() != null) {
971             for (int i = 0; i < arg.getDeclList().size(); i++) {
972                 lineFeed();
973                 QName name = (QName) arg.getDeclList().get(i);
974                 HashMap map = null;
975                 if (arg.getVariables() != null) {
976                     map = (HashMap) arg.getVariables().get(name.getNameSpace());
977                     if (map != null) {
978                         Variable vari = (Variable) map.get(name.getLocalName());
979                         if (vari != null) {
980                             if (vari.getExpression() != null || doImports) {
981                                 buf.append("declare variable ");
982                                 vari.accept(this);
983                                 if (vari.getTypeDeclaration() != null) {
984                                     buf.append(" as ");
985                                     buf.append(vari.getTypeDeclaration());
986                                     buf.append("");
987                                 }
988                                 if (vari.getExpression() != null) {
989                                     buf.append(" {");
990                                     buf.append(vari.getExpression());
991                                     buf.append("};");
992                                 } else
993                                     buf.append(" external;");
994                             }
995                         }
996                         // } else {
997
// map = (HashMap) arg.getFunctions().get(name.getNameSpace());
998
// FunctionDeclaration funcdecl = (FunctionDeclaration) map.get(name.getLocalName());
999
// if (funcdecl != null)
1000
// funcdecl.accept(this);
1001
//
1002
}
1003                }
1004            }
1005        }
1006        // if (doExpressions) {
1007
if (arg.getExpressions() != null) {
1008            for (int i = 0; i < arg.getExpressions().size(); i++) {
1009                if (i != 0) {
1010                    buf.append(" ,");
1011                    lineFeed();
1012                }
1013                ((XQueryExpression) arg.getExpressions().get(i)).accept(this);
1014            }
1015        }
1016        // }
1017
}
1018    //public void visit(XQueryVoid arg) throws XQueryException {
1019
//
1020
// // primitive functions
1021
//
1022
// public void visit(FunctionABS arg) throws XQueryException ;
1023
// public void visit(FunctionAVG arg) throws XQueryException ;
1024
// public void visit(FunctionCEILING arg) throws XQueryException;
1025
// public void visit(FunctionCOLLECTION arg) throws XQueryException;
1026
// public void visit(FunctionCONCAT arg) throws XQueryException;
1027
// public void visit(FunctionCONTAINS arg) throws XQueryException;
1028
// public void visit(FunctionCOUNT arg) throws XQueryException;
1029
// public void visit(FunctionCURRENT_DATE arg) throws XQueryException;
1030
// public void visit(FunctionCURRENT_DATETIME arg) throws XQueryException;
1031
// public void visit(FunctionCURRENT_TIME arg) throws XQueryException;
1032
// public void visit(FunctionDATA arg) throws XQueryException;
1033
// public void visit(FunctionDATE arg) throws XQueryException;
1034
// public void visit(FunctionDATETIME arg) throws XQueryException;
1035
// public void visit(FunctionDECIMAL arg) throws XQueryException;
1036
// public void visit(FunctionDISTINCT_VALUES arg) throws XQueryException;
1037
// public void visit(FunctionDOC arg) throws XQueryException;
1038
// public void visit(FunctionDOUBLE arg) throws XQueryException;
1039
// public void visit(FunctionEMPTY arg) throws XQueryException;
1040
// public void visit(FunctionENDS_WITH arg) throws XQueryException;
1041
// public void visit(FunctionEXISTS arg) throws XQueryException;
1042
// public void visit(FunctionGET_MONTH_FROM_DATE arg) throws XQueryException;
1043
// public void visit(FunctionGET_YEAR_FROM_DATE arg) throws XQueryException;
1044
// public void visit(FunctionFALSE arg) throws XQueryException;
1045
// public void visit(FunctionFLOOR arg) throws XQueryException;
1046
// public void visit(FunctionINTEGER arg) throws XQueryException;
1047
// public void visit(FunctionLOCAL_NAME arg) throws XQueryException;
1048
// public void visit(FunctionLOWER_CASE arg) throws XQueryException;
1049
// public void visit(FunctionMATCHES arg) throws XQueryException;
1050
// public void visit(FunctionMAX arg) throws XQueryException;
1051
// public void visit(FunctionMIN arg) throws XQueryException;
1052
// public void visit(FunctionNAMESPACE_URI arg) throws XQueryException;
1053
// public void visit(FunctionNOT arg) throws XQueryException;
1054
// public void visit(FunctionNUMBER arg) throws XQueryException;
1055
// public void visit(FunctionROUND arg) throws XQueryException;
1056
// public void visit(FunctionSTARTS_WITH arg) throws XQueryException;
1057
// public void visit(FunctionSTRING arg) throws XQueryException;
1058
// public void visit(FunctionSTRING_LENGTH arg) throws XQueryException;
1059
// public void visit(FunctionSUBSTRING arg) throws XQueryException;
1060
// public void visit(FunctionSUM arg) throws XQueryException;
1061
// public void visit(FunctionTIME arg) throws XQueryException;
1062
// public void visit(FunctionTRUE arg) throws XQueryException;
1063
// public void visit(FunctionUPPER_CASE arg) throws XQueryException;
1064
// public void visit(FunctionPART arg) throws XQueryException;
1065
// public void visit(FunctionTUPLE arg) throws XQueryException;
1066
// public void visit(FunctionDEEP_EQUALS arg) throws XQueryException;
1067
}
Popular Tags