View Javadoc

1   //$Id: ConfigHelper.java,v 1.3 2007/06/13 12:58:27 kameleono Exp $
2   
3   package net.sourceforge.sql2java;
4   
5   import java.io.File;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import org.jdom.Document;
10  import org.jdom.Element;
11  import org.jdom.input.SAXBuilder;
12  import org.jdom.xpath.XPath;
13  
14  
15  public class ConfigHelper
16  {
17      static private Document doc = null;
18  
19      //init
20      static
21      {
22          try
23          {
24              String filename = "sql2java.xml";
25              if ((new File(filename)).isFile() == false)
26              {
27                  filename = "src/sql2java.xml";
28                  if ((new File(filename)).isFile() == false)
29                  {
30                      filename = "src/config/sql2java.xml";
31                  }
32              }
33              SAXBuilder builder = new SAXBuilder();
34              doc = builder.build(new File(filename));
35          }
36          catch(Exception e)
37          {
38              e.printStackTrace();
39          }
40      }
41  
42      static public String getTableProperty(String table, String propertyName)
43      {
44          return getXPathProperty("//table[@name='" + table.toLowerCase() + "']", propertyName.toLowerCase());
45      }
46  
47      static public String getColumnProperty(String table, String column, String propertyName)
48      {
49          return getXPathProperty("//table[@name='" + table.toLowerCase() + "']/column[@name='" + column.toLowerCase() + "']", propertyName);
50      }
51  
52      static public String getProperty(String propertyName)
53      {
54          return getXPathProperty("//sql2java", propertyName.toLowerCase());
55      }
56  
57      static public String getGlobalProperty(String propertyName)
58      {
59          return getProperty(propertyName);
60      }
61  
62      static public String getXPathProperty(String xPathQuery, String propertyName)
63      {
64          // sql2java.xml could not be found
65          if (doc == null)
66              return null ;
67  
68          String result = null;
69          try
70          {
71              XPath servletPath = XPath.newInstance(xPathQuery);
72              List nodes = servletPath.selectNodes(doc);
73              if (nodes == null)
74                  return null;
75              Iterator i = nodes.iterator();
76              if(i.hasNext())
77              {
78                  Element item = (Element) i.next();
79  
80                  // using the attribute
81                  if (item.getAttribute(propertyName) != null)
82                  {
83                      result = item.getAttribute(propertyName).getValue();
84                      if (result != null)
85                          return result ;
86                  }
87  
88                  // using child
89                  if (item.getChild(propertyName) == null)
90                  {
91                      return null;
92                  }
93                  else
94                  {
95                      result = item.getChild(propertyName).getTextTrim();
96                      return result;
97                  }
98              }
99          }
100         catch(Exception e)
101         {
102             e.printStackTrace();
103         }
104         finally
105         {
106 //            if (result != null)
107 //            System.out.println("xPathQuery = " + xPathQuery + " " + propertyName + " with result [" + result + "]");
108         }
109         return null;
110     }
111 }