KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > servlet > util > XQueryBeautifier


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

22
23 package org.xquark.servlet.util;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.StringReader JavaDoc;
28
29
30 public class XQueryBeautifier {
31   private static final String JavaDoc 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 JavaDoc 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 JavaDoc beautify(String JavaDoc s)
57     throws IOException JavaDoc
58   {
59     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new StringReader JavaDoc(s));
60     String JavaDoc line = reader.readLine();
61     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
62         
63     while(line != null)
64       {
65     //System.out.println("line="+line);
66
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 JavaDoc beautifyCommentLine(String JavaDoc line)
105   {
106     return beautifyIndentation(line+"\r\n");
107   }
108     
109     
110   String JavaDoc beautifyQueryLine(String JavaDoc line)
111   {
112     String JavaDoc 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 JavaDoc beautifyIndentation(String JavaDoc line)
125   {
126     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
127         
128     int i=0;
129     while(i<line.length())
130       {
131     if (line.charAt(i)==' ') sb.append("&nbsp;");
132     else if (line.charAt(i)=='\t') sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
133     else break;
134     i++;
135       }
136     return sb + line.substring(i) + "<br>";
137   }
138     
139   String JavaDoc getCommentStart()
140   {
141     return "<FONT color=\"#007000\">";
142   }
143     
144     
145   String JavaDoc getCommentEnd()
146   {
147     return "</FONT>";
148   }
149     
150     
151   /**
152    * Bind variable value. work with multiple occurences of the same variable
153    *
154    */

155   public String JavaDoc beautifyKeyword(String JavaDoc html, String JavaDoc var, String JavaDoc value)
156   {
157     String JavaDoc s = html;
158     int index = s.indexOf(var);
159     
160     //System.out.println("var="+var+" index="+index);
161

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     //System.out.println(s.substring(0, index)+value+s.substring(index+var.length(), s.length())+ " INDEX="+(index+value.length()));
165
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