KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > xml > DOM2DB


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// DOM2DB
15
//
16
// NK 25.07.2001
17
//
18

19 package org.jahia.utils.xml;
20
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23 import java.sql.Types JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.jahia.data.xml.JahiaXmlDocument;
29 import org.jahia.exceptions.JahiaException;
30 import org.jahia.utils.DBRowDataFilter;
31 import org.jahia.utils.JahiaConsole;
32 import org.jahia.utils.JahiaTools;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36
37 /**
38  * Tools used to load a DOM Document created using DB2DOM.
39  *
40  * @author Khue Nguyen
41  * @see org.jahia.tools.xml.DB2DOM
42  * @version 1.0
43  */

44 public class DOM2DB extends JahiaXmlDocument {
45     
46     private static final String JavaDoc CLASS_NAME = "DOM2DB";
47     
48     private static final String JavaDoc DATABASE_TAG = "database";
49     private static final String JavaDoc TABLE_TAG = "table";
50     private static final String JavaDoc ROW_TAG = "row";
51     private static final String JavaDoc COLUMN_TAG = "column";
52
53     private static final String JavaDoc ID_ATTRIB = "id";
54     private static final String JavaDoc NAME_ATTRIB = "name";
55     private static final String JavaDoc TYPE_ATTRIB = "type";
56     
57     
58     Vector JavaDoc mTables = null;
59     
60     
61     //--------------------------------------------------------------------------
62
/**
63      * Constructor
64      *
65      * @param (String) path, the full path to the application.xml file
66      */

67     public DOM2DB (String JavaDoc docPath) throws JahiaException
68     {
69       super(docPath);
70       extractDocumentData();
71     }
72
73
74     //--------------------------------------------------------------------------
75
/**
76      * Constructor using a gived parser
77      *
78      * @param (String) path, the full path to a xml file
79      * @param (Parser) parser, the parser to use
80      */

81     public DOM2DB (String JavaDoc docPath, org.xml.sax.helpers.ParserAdapter JavaDoc parser)
82     throws JahiaException {
83         super(docPath,parser);
84         extractDocumentData();
85     }
86
87
88     //--------------------------------------------------------------------------
89
/**
90      * Extracts data from the application.xml file. Build the JahiaWebAppsWarPackage object
91      * to store extracted data
92      */

93     public void extractDocumentData() throws JahiaException {
94
95         //JahiaConsole.println(CLASS_NAME+".extractDocumentData","started");
96

97         if (m_XMLDocument == null) {
98
99             throw new JahiaException( CLASS_NAME,
100                                     "The Document is null !",
101                                        JahiaException.ERROR_SEVERITY,
102                                        JahiaException.ERROR_SEVERITY);
103         }
104
105
106         if (!m_XMLDocument.hasChildNodes()) {
107
108             throw new JahiaException( CLASS_NAME,
109                                     "Main document node has no children",
110                                     JahiaException.ERROR_SEVERITY,
111                                     JahiaException.ERROR_SEVERITY);
112         }
113
114
115         Element JavaDoc docElNode = (Element JavaDoc) m_XMLDocument.getDocumentElement();
116
117         if (!docElNode.getNodeName().equalsIgnoreCase(DATABASE_TAG)) {
118
119             throw new JahiaException( "Invalid Document format",
120                                         "Tag <"+DATABASE_TAG+"> is not present as starting tag in file",
121                                         JahiaException.ERROR_SEVERITY,
122                                         JahiaException.ERROR_SEVERITY);
123         }
124
125
126         mTables = XMLParser.getChildNodes(docElNode,TABLE_TAG);
127     
128         //JahiaConsole.println(CLASS_NAME+".extractDocumentData","done");
129
}
130
131     //--------------------------------------------------------------------------
132
/**
133      * Returns an enumeration of all table names
134      *
135      * @return Vector the list of table names
136      */

137     public Vector JavaDoc getTableNames()
138     throws JahiaException {
139         Vector JavaDoc v = new Vector JavaDoc();
140         
141         int size = mTables.size();
142         for ( int i=0 ; i<size ; i++ ){
143             Element JavaDoc el = (Element JavaDoc)mTables.get(i);
144             v.add(XMLParser.getAttributeValue(el,NAME_ATTRIB));
145         }
146         return v;
147     }
148
149     //--------------------------------------------------------------------------
150
/**
151      * Returns an enumeration of the tables
152      *
153      * @return Enumeration the list of tables
154      */

155     public Enumeration JavaDoc getTables()
156     throws JahiaException {
157         return mTables.elements();
158     }
159
160     //--------------------------------------------------------------------------
161
/**
162      * Write the contents of a table in DB
163      *
164      * @param (Element) the Element table
165      * @param (Statement) the db statement to use to insert data in table
166      */

167     public static void sqlInsert(Element JavaDoc table, Statement JavaDoc stmt)
168     throws JahiaException,SQLException JavaDoc {
169         sqlInsert(table, stmt, null);
170     }
171     
172     //--------------------------------------------------------------------------
173
/**
174      * Write the contents of a table in DB
175      *
176      * @param (Element) the Element table
177      * @param (Statement) the db statement to use to insert data in table
178      * @param (DBRowDataFilter) an optional data filter. Give a null instance if you want no filtering
179      */

180     public static void sqlInsert(Element JavaDoc table, Statement JavaDoc stmt, DBRowDataFilter filter )
181     throws JahiaException,SQLException JavaDoc {
182
183     
184         String JavaDoc tableName = XMLParser.getAttributeValue(table,NAME_ATTRIB);
185         String JavaDoc insertQuery = "INSERT INTO " + tableName + " (";
186
187         JahiaConsole.println(CLASS_NAME+".sqlInsert"," table name=" + tableName );
188         
189         String JavaDoc colName = null;
190         String JavaDoc colVal = null;
191         int colType = 0;
192         
193         Vector JavaDoc rows = XMLParser.getChildNodes(table,ROW_TAG);
194         Vector JavaDoc cols = null;
195         Element JavaDoc r = null;
196         Element JavaDoc c = null;
197         int nbRows = rows.size();
198         int nbCols = 0;
199         boolean isText = false;
200         
201         StringBuffer JavaDoc buff = null;
202         StringBuffer JavaDoc valBuff = null;
203         StringBuffer JavaDoc colBuff = null;
204         
205         for ( int i=0 ; i<nbRows ; i++ ){
206
207             Hashtable JavaDoc vals = new Hashtable JavaDoc();
208
209             r = (Element JavaDoc)rows.get(i);
210             buff = new StringBuffer JavaDoc(insertQuery);
211             valBuff = new StringBuffer JavaDoc();
212             colBuff = new StringBuffer JavaDoc();
213             cols = XMLParser.getChildNodes(r,COLUMN_TAG);
214             nbCols = cols.size();
215             for ( int j=0 ; j<nbCols ; j++ ){
216                 isText = false;
217                 c = (Element JavaDoc)cols.get(j);
218                 colName = XMLParser.getAttributeValue(c,NAME_ATTRIB);
219                 colType = Integer.parseInt(XMLParser.getAttributeValue(c,TYPE_ATTRIB));
220
221                 //JahiaConsole.println(CLASS_NAME+".sqlInsert"," column node : name=" + colName );
222

223                 Node JavaDoc textNode = c.getFirstChild();
224                 
225                 if ( textNode == null ){
226                     colVal = "";
227                 } else if (textNode.getNodeType() == Node.TEXT_NODE) {
228                     colVal = textNode.getNodeValue();
229                 } else {
230                     throw new JahiaException(CLASS_NAME,
231                          "Value of column is not in correct format, should only be text",
232                          JahiaException.ERROR_SEVERITY,
233                          JahiaException.ERROR_SEVERITY);
234                 }
235
236                 vals.put(colName.toLowerCase(),colVal);
237                                 
238                 if( (colType == Types.VARCHAR)
239                     || (colType == Types.CHAR)
240                     || (colType == Types.BLOB)
241                     || (colType == Types.LONGVARCHAR) ) {
242                     isText = true;
243                 
244                 } else if ( colType == 8 ){
245                     Float JavaDoc f = new Float JavaDoc(colVal);
246                     colVal = (new Long JavaDoc(f.longValue())).toString();
247                 } else {
248                     try {
249                         int testConversion = Integer.parseInt(colVal);
250                     } catch (NumberFormatException JavaDoc nfe) {
251                         isText = true;
252                     }
253                 }
254                     
255                 // build columns list and values
256
colBuff.append(colName);
257                 if(isText) {
258                     valBuff.append("'");
259                 }
260                 valBuff.append(JahiaTools.quote(JahiaTools.text2XMLEntityRef(colVal,1)));
261                 if(isText) {
262                     valBuff.append("'");
263                 }
264
265                 if(j<(nbCols-1)) {
266                     valBuff.append(", ");
267                     colBuff.append(", ");
268                 }
269
270             }
271
272             buff.append(colBuff.toString().toLowerCase());
273             buff.append(") VALUES(");
274             buff.append(valBuff.toString());
275             buff.append(")");
276             
277             if ( filter == null || filter.inValue(vals) ){
278                 if ( valBuff.length()>0 ){
279                     stmt.executeUpdate(buff.toString());
280                 }
281             }
282             //JahiaConsole.println(CLASS_NAME+".sqlInsert",buff.toString());
283
}
284     }
285
286 }
287
Popular Tags