1 22 23 package org.xquark.servlet.util; 24 25 import java.io.BufferedReader ; 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 29 30 public class XQueryBeautifier { 31 private static final String keywords[] = 32 { 33 "for ", 34 "in ", 35 "let ", 36 "where ", 37 "return", 38 "order by ", 39 "and ", 40 "or ", 41 "union ", 42 "| ", 43 "namespace ", 44 "some ", 45 "every ", 46 "satisfies " 47 }; 48 private static final String builtin[] = 49 { 50 "collection", "doc", "exists", "empty", "not", "avg", "count", "sum", "min", "max", "contains", 51 "starts-with", "ends-with", "dateTime", "date", "current-dateTime", "substring", "concat", "distinct-values" 52 }; 53 54 boolean incomment = false; 55 56 public String beautify(String s) 57 throws IOException 58 { 59 BufferedReader reader = new BufferedReader (new StringReader (s)); 60 String line = reader.readLine(); 61 StringBuffer sb = new StringBuffer (); 62 63 while(line != null) 64 { 65 if(line.indexOf("(:") != -1) 67 { 68 if(incomment==true) 69 sb.append(beautifyCommentLine(line)); 70 else 71 { 72 incomment = true; 73 sb.append(getCommentStart()); 74 sb.append(beautifyCommentLine(line)); 75 if(line.indexOf(":)") != -1) { 76 incomment = false; 77 sb.append(getCommentEnd()); 78 } 79 } 80 } 81 82 else if(line.indexOf(":)") != -1) 83 { 84 incomment = false; 85 sb.append(beautifyCommentLine(line)); 86 sb.append(getCommentEnd()); 87 } 88 89 else if(incomment==true) 90 { 91 sb.append(beautifyCommentLine(line)); 92 } 93 94 else 95 { 96 sb.append(beautifyQueryLine(line)); 97 } 98 line = reader.readLine(); 99 } 100 return sb.toString(); 101 } 102 103 104 String beautifyCommentLine(String line) 105 { 106 return beautifyIndentation(line+"\r\n"); 107 } 108 109 110 String beautifyQueryLine(String line) 111 { 112 String xquery = beautifyIndentation(line); 113 for(int i=0; i<keywords.length; i++) 114 { 115 xquery = beautifyKeyword(xquery, keywords[i], "<FONT COLOR=202080><B>"+keywords[i]+"</B></FONT>"); 116 } 117 for(int i=0; i<builtin.length; i++) { 118 xquery = beautifyKeyword(xquery, builtin[i]+"(", "<FONT COLOR=red>"+builtin[i]+"</FONT>"+"("); 119 } 120 return xquery +"\r\n"; 121 } 122 123 124 String beautifyIndentation(String line) 125 { 126 StringBuffer sb = new StringBuffer (); 127 128 int i=0; 129 while(i<line.length()) 130 { 131 if (line.charAt(i)==' ') sb.append(" "); 132 else if (line.charAt(i)=='\t') sb.append(" "); 133 else break; 134 i++; 135 } 136 return sb + line.substring(i) + "<br>"; 137 } 138 139 String getCommentStart() 140 { 141 return "<FONT color=\"#007000\">"; 142 } 143 144 145 String getCommentEnd() 146 { 147 return "</FONT>"; 148 } 149 150 151 155 public String beautifyKeyword(String html, String var, String value) 156 { 157 String s = html; 158 int index = s.indexOf(var); 159 160 162 while(index!=-1) { 163 if (index == 0 || s.charAt(index-1) == ' ' || s.charAt(index-1) == ';' || s.charAt(index-1) == '>' || s.charAt(index-1) == '{' || s.charAt(index-1) == '(') { 164 s = s.substring(0, index)+value+s.substring(index+var.length(), s.length()); 166 } 167 index = s.indexOf(var, index+value.length()); 168 } 169 170 return s; 171 } 172 173 } 174 175 | Popular Tags |