View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.dbom.utils;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.util.ArrayList;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.log4j.LogManager;
27  import org.apache.log4j.Logger;
28  
29  import com.explosion.utilities.GeneralConstants;
30  
31  /***
32   * @author Steve.Cowx
33   * This formatter has a LONG way to go.  this is not well implemented an should be done with a 
34   * proper lexer.
35   */
36  public class SQLFormatter 
37  {
38      private static Logger log = LogManager.getLogger(SQLFormatter.class);
39      
40  	public static String format(String SQLToFormat) throws Exception
41  	 {
42  		String data = SQLToFormat;
43  		ArrayList matches = new ArrayList();
44  		ArrayList replacements = new ArrayList();
45  		
46  		matches.add("\t");
47  		replacements.add(GeneralConstants.LS + " ");
48  		
49  		matches.add("  ");
50  		replacements.add(GeneralConstants.LS + " ");
51  		
52  		matches.add(GeneralConstants.LS);
53  		replacements.add("");
54  		
55  		matches.add(",( {0,})");
56  		replacements.add("," + GeneralConstants.LS + "\t");
57  		
58  		matches.add("//(");
59  		replacements.add(GeneralConstants.LS + "//(" + GeneralConstants.LS);
60  		
61  		matches.add("//)");
62  		replacements.add(GeneralConstants.LS + "//)" + GeneralConstants.LS);
63  		
64  		matches.add("and");
65  		replacements.add(GeneralConstants.LS + "\tand");
66  		
67  		matches.add("with");
68  		replacements.add(GeneralConstants.LS + "with");
69  		
70  		matches.add("left");
71  		replacements.add(GeneralConstants.LS + "left");
72  		
73  		matches.add("right");
74  		replacements.add(GeneralConstants.LS + "right");
75  		
76  		matches.add("where");
77  		replacements.add(GeneralConstants.LS + "where");
78  				
79  		matches.add("from");
80  		replacements.add(GeneralConstants.LS + "from");
81  		
82  		matches.add(GeneralConstants.LS+"{2,}");
83  		replacements.add("");
84  		
85  		for (int i=0;i<matches.size();i++)
86  		{
87  			String toFind = (String) matches.get(i);
88  			String toInsert = (String) replacements.get(i);
89  			Pattern pattern = Pattern.compile(toFind,Pattern.CASE_INSENSITIVE);
90  			Matcher matcher = pattern.matcher(data);
91  			if (matcher != null)
92  			{
93  			  data = matcher.replaceAll(toInsert);
94  			}
95  		}
96   	
97  		return data;
98  	 } 
99   
100 }
101