KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xpath > visitor > impl > ExpressionWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xpath.visitor.impl;
21
22
23 import java.util.Iterator JavaDoc;
24
25 import org.netbeans.modules.xml.xpath.LocationStep;
26 import org.netbeans.modules.xml.xpath.XPathCoreFunction;
27 import org.netbeans.modules.xml.xpath.XPathCoreOperation;
28 import org.netbeans.modules.xml.xpath.XPathExpression;
29 import org.netbeans.modules.xml.xpath.XPathExpressionPath;
30 import org.netbeans.modules.xml.xpath.XPathExtensionFunction;
31 import org.netbeans.modules.xml.xpath.XPathLocationPath;
32 import org.netbeans.modules.xml.xpath.XPathNumericLiteral;
33 import org.netbeans.modules.xml.xpath.XPathOperationOrFuntion;
34 import org.netbeans.modules.xml.xpath.XPathPredicateExpression;
35 import org.netbeans.modules.xml.xpath.XPathStringLiteral;
36 import org.netbeans.modules.xml.xpath.XPathVariableReference;
37 import org.netbeans.modules.xml.xpath.visitor.AbstractXPathVisitor;
38 import org.netbeans.modules.xml.xpath.visitor.XPathVisitor;
39
40
41 /**
42  * Implements the XPathVisitor interface to generate a string representation
43  * of an expression.
44  *
45  * @author sbyn
46  * @version $Revision: 1.4 $
47  */

48 public class ExpressionWriter extends AbstractXPathVisitor {
49     
50     /** The string buffer. */
51     private StringBuffer JavaDoc mBuffer;
52     
53     
54     /** Constructor. */
55     public ExpressionWriter() {
56         mBuffer = new StringBuffer JavaDoc();
57     }
58     
59     
60     /**
61      * Gets the string representation of the expression.
62      * @return the string representation
63      */

64     public String JavaDoc getString() {
65         return mBuffer.toString();
66     }
67     
68
69     /**
70      * Visits an location step.
71      * @param locationStep to visit
72      */

73     public void visit(LocationStep locationStep) {
74         mBuffer.append('/');
75     
76         mBuffer.append(locationStep.getString());
77         XPathExpression[] predicates = locationStep.getPredicates();
78         if (predicates != null) {
79             for (int j = 0, length = predicates.length; j < length; j++) {
80                 mBuffer.append('[');
81                 mBuffer.append(predicates[j].getExpressionString());
82                 mBuffer.append(']');
83             }
84         }
85         
86     }
87
88     public void visit(XPathVariableReference vReference) {
89         mBuffer.append("$" + vReference.getVariableName());
90     }
91     
92     /**
93      * Visits a string literal.
94      * @param stringLiteral to visit
95      */

96     public void visit(XPathStringLiteral stringLiteral) {
97         
98         // quotes in literal strings for xpath 1.0 basically work like this:
99
// 1. we can either quote strings with single or double quotes
100
// 2. quote the string with single quotes if the string contains double quotes
101
// i.e. 'the "correct" way', 'the 'incorrect' way'
102
// 3. quote the string with double quotes if the string contains single quotes
103
// i.e. "the 'correct' way", "the "incorrect" way"
104
// - josh
105

106         String JavaDoc literal = stringLiteral.getValue();
107         boolean isStringQuoted = false;
108         if (literal.length() >= 2) {
109             if (literal.startsWith("'") && literal.endsWith("'")) {
110                 isStringQuoted = true;
111             } else if (literal.startsWith("\"") && literal.endsWith("\"")) {
112                 isStringQuoted = true;
113             }
114         }
115         
116         if (isStringQuoted) {
117             // if literal is already quoted, do not quote the literal
118
mBuffer.append(literal);
119         } else {
120             if (literal.indexOf("'") >= 0) {
121                 // string contains a single-quote,
122
// it must be quoted with double-quotes
123
mBuffer.append("\"");
124                 mBuffer.append(literal);
125                 mBuffer.append("\"");
126             } else {
127                 // quote the string with single-quotes by default
128
mBuffer.append("'");
129                 mBuffer.append(literal);
130                 mBuffer.append("'");
131             }
132         }
133         
134     }
135     
136     
137     /**
138      * Visits a numeric literal.
139      * @param numericLiteral to visit
140      */

141     public void visit(XPathNumericLiteral numericLiteral) {
142         mBuffer.append(numericLiteral.getValue().toString());
143     }
144     
145     
146     /**
147      * Visits a location path.
148      * @param locationPath to visit
149      */

150     public void visit(XPathLocationPath locationPath) {
151         LocationStep[] steps = locationPath.getSteps();
152         if (locationPath.getAbsolute()) {
153             mBuffer.append('/');
154         }
155         for (int i = 0; i < steps.length; i++) {
156             if (i != 0) {
157                 mBuffer.append('/');
158             }
159             mBuffer.append(steps[i].getString());
160             XPathExpression[] predicates = steps[i].getPredicates();
161             if (predicates != null) {
162                 for (int j = 0, length = predicates.length; j < length; j++) {
163                     mBuffer.append('[');
164                     mBuffer.append(predicates[j].getExpressionString());
165                     mBuffer.append(']');
166                 }
167             }
168         }
169         
170        }
171     
172     
173
174     /**
175      * Visits a expression path.
176      * @param locationPath to visit
177      */

178     public void visit(XPathExpressionPath expressionPath) {
179         XPathExpression rootExpression = expressionPath.getRootExpression();
180         if(rootExpression != null) {
181             mBuffer.append(rootExpression.getExpressionString());
182         }
183         
184         LocationStep[] steps = expressionPath.getSteps();
185         
186         for (int i = 0; i < steps.length; i++) {
187             mBuffer.append('/');
188             
189             mBuffer.append(steps[i].getString());
190             XPathExpression[] predicates = steps[i].getPredicates();
191             if (predicates != null) {
192                 for (int j = 0, length = predicates.length; j < length; j++) {
193                     mBuffer.append('[');
194                     mBuffer.append(predicates[j].getExpressionString());
195                     mBuffer.append(']');
196                 }
197             }
198         }
199     }
200     
201     /**
202      * Visits a core operation.
203      * @param coreOperation to visit
204      */

205     public void visit(XPathCoreOperation coreOperation) {
206         if (XPathCoreOperation.OP_NEGATIVE == coreOperation.getOperator()) {
207             mBuffer.append(coreOperation.getSign());
208             if(coreOperation.getChildCount() > 0) {
209                 mBuffer.append(coreOperation.getChild(0).getExpressionString());
210             }
211         } else {
212             mBuffer.append(" ( ");
213             if(coreOperation.getChildCount() > 0) {
214                 mBuffer.append(coreOperation.getChild(0).getExpressionString());
215             }
216             mBuffer.append(' ');
217             mBuffer.append(coreOperation.getSign());
218             mBuffer.append(' ');
219             if(coreOperation.getChildCount() > 1) {
220                 mBuffer.append(coreOperation.getChild(1).getExpressionString());
221             }
222             mBuffer.append(" ) ");
223         }
224          
225     }
226     
227     
228     /**
229      * Visits a core function.
230      * @param coreFunction to visit
231      */

232     public void visit(XPathCoreFunction coreFunction) {
233         mBuffer.append(coreFunction.getName());
234         mBuffer.append('(');
235         for (Iterator JavaDoc iter = coreFunction.getChildren().iterator();
236                 iter.hasNext();) {
237             XPathExpression expr = (XPathExpression) iter.next();
238             mBuffer.append(expr.getExpressionString());
239             if (iter.hasNext()) {
240                 mBuffer.append(", ");
241             }
242         }
243         mBuffer.append(')');
244     }
245     
246     
247     /**
248      * Visits an extension function.
249      * @param extensionFunction to visit
250      */

251     public void visit(XPathExtensionFunction extensionFunction) {
252         mBuffer.append(extensionFunction.getName());
253         mBuffer.append('(');
254         for (Iterator JavaDoc iter = extensionFunction.getChildren().iterator();
255                 iter.hasNext();) {
256             XPathExpression expr = (XPathExpression) iter.next();
257             mBuffer.append(expr.getExpressionString());
258             if (iter.hasNext()) {
259                 mBuffer.append(", ");
260             }
261         }
262         mBuffer.append(')');
263         
264     }
265     
266     
267
268 }
269
Popular Tags