1 19 20 package org.netbeans.modules.xml.xpath.visitor.impl; 21 22 23 import java.util.Iterator ; 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 48 public class ExpressionWriter extends AbstractXPathVisitor { 49 50 51 private StringBuffer mBuffer; 52 53 54 55 public ExpressionWriter() { 56 mBuffer = new StringBuffer (); 57 } 58 59 60 64 public String getString() { 65 return mBuffer.toString(); 66 } 67 68 69 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 96 public void visit(XPathStringLiteral stringLiteral) { 97 98 106 String 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 mBuffer.append(literal); 119 } else { 120 if (literal.indexOf("'") >= 0) { 121 mBuffer.append("\""); 124 mBuffer.append(literal); 125 mBuffer.append("\""); 126 } else { 127 mBuffer.append("'"); 129 mBuffer.append(literal); 130 mBuffer.append("'"); 131 } 132 } 133 134 } 135 136 137 141 public void visit(XPathNumericLiteral numericLiteral) { 142 mBuffer.append(numericLiteral.getValue().toString()); 143 } 144 145 146 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 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 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 232 public void visit(XPathCoreFunction coreFunction) { 233 mBuffer.append(coreFunction.getName()); 234 mBuffer.append('('); 235 for (Iterator 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 251 public void visit(XPathExtensionFunction extensionFunction) { 252 mBuffer.append(extensionFunction.getName()); 253 mBuffer.append('('); 254 for (Iterator 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 |