KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > install > TranslationHandler


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.install;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import org.xml.sax.helpers.*;
20 import org.xml.sax.*;
21
22 import org.compiere.util.*;
23
24 /**
25  * SAX Handler for parsing Translation
26  *
27  * @author Jorg Janke
28  * @version $Id: TranslationHandler.java,v 1.5 2003/10/04 03:52:36 jjanke Exp $
29  */

30 public class TranslationHandler extends DefaultHandler
31 {
32     /**
33      * Translation Handler
34      * @param AD_Client_ID only certain client if id >= 0
35      */

36     public TranslationHandler (int AD_Client_ID)
37     {
38         m_AD_Client_ID = AD_Client_ID;
39     } // TranslationHandler
40

41     /** Client */
42     private int m_AD_Client_ID = -1;
43     /** Language */
44     private String JavaDoc m_AD_Language = null;
45     /** Is Base Language */
46     private boolean m_isBaseLanguage = false;
47     /** Table */
48     private String JavaDoc m_TableName = null;
49     /** Update SQL */
50     private String JavaDoc m_updateSQL = null;
51     /** Current ID */
52     private String JavaDoc m_curID = null;
53     /** Current ColumnName */
54     private String JavaDoc m_curColumnName = null;
55     /** Current Value */
56     private StringBuffer JavaDoc m_curValue = null;
57     /** SQL */
58     private StringBuffer JavaDoc m_sql = null;
59
60     private Timestamp m_time = new Timestamp(System.currentTimeMillis());
61     private int m_updateCount = 0;
62
63     private Logger log = Logger.getCLogger(getClass());
64
65     /*************************************************************************/
66
67     /**
68      * Receive notification of the start of an element.
69      *
70      * @param uri namespace
71      * @param localName simple name
72      * @param qName qualified name
73      * @param attributes attributes
74      * @throws org.xml.sax.SAXException
75      */

76     public void startElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes)
77         throws org.xml.sax.SAXException JavaDoc
78     {
79     // Log.trace(Log.l6_Database, "TranslationHandler.startElement", qName); // + " - " + uri + " - " + localName);
80
if (qName.equals(Translation.XML_TAG))
81         {
82             m_AD_Language = attributes.getValue(Translation.XML_ATTRIBUTE_LANGUAGE);
83             m_isBaseLanguage = Language.isBaseLanguage(m_AD_Language);
84             m_TableName = attributes.getValue(Translation.XML_ATTRIBUTE_TABLE);
85             m_updateSQL = "UPDATE " + m_TableName;
86             if (!m_isBaseLanguage)
87                 m_updateSQL += "_Trl";
88             m_updateSQL += " SET ";
89             log.debug("AD_Language=" + m_AD_Language + ", Base=" + m_isBaseLanguage + ", TableName=" + m_TableName);
90         }
91         else if (qName.equals(Translation.XML_ROW_TAG))
92         {
93             m_curID = attributes.getValue(Translation.XML_ROW_ATTRIBUTE_ID);
94         // Log.trace(9, "ID=" + m_curID);
95
m_sql = new StringBuffer JavaDoc();
96         }
97         else if (qName.equals(Translation.XML_VALUE_TAG))
98         {
99             m_curColumnName = attributes.getValue(Translation.XML_VALUE_ATTRIBUTE_COLUMN);
100         // Log.trace(9, "ColumnName=" + m_curColName);
101
}
102         else
103             log.error ("startElement - UNKNOWN TAG: " + qName);
104         m_curValue = new StringBuffer JavaDoc();
105     } // startElement
106

107     /**
108      * Receive notification of character data inside an element.
109      *
110      * @param ch buffer
111      * @param start start
112      * @param length length
113      * @throws SAXException
114      */

115     public void characters (char ch[], int start, int length)
116         throws SAXException
117     {
118         m_curValue.append(ch, start, length);
119     // Log.trace(Log.l6_Database+1, "TranslationHandler.characters", m_curValue.toString());
120
} // characters
121

122     /**
123      * Receive notification of the end of an element.
124      * @param uri namespace
125      * @param localName simple name
126      * @param qName qualified name
127      * @throws SAXException
128      */

129     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
130         throws SAXException
131     {
132     // Log.trace(Log.l6_Database+1, "TranslationHandler.endElement", qName);
133
if (qName.equals(Translation.XML_TAG))
134         {
135         }
136         else if (qName.equals(Translation.XML_ROW_TAG))
137         {
138             // Set section
139
if (m_sql.length() > 0)
140                 m_sql.append(",");
141             m_sql.append("Updated=").append(DB.TO_DATE(m_time, false));
142             if (!m_isBaseLanguage)
143                 m_sql.append(",IsTranslated='Y'");
144             // Where section
145
m_sql.append(" WHERE ")
146                 .append(m_TableName).append("_ID=").append(m_curID);
147             if (!m_isBaseLanguage)
148                 m_sql.append(" AND AD_Language='").append(m_AD_Language).append("'");
149             if (m_AD_Client_ID >= 0)
150                 m_sql.append(" AND AD_Client_ID=").append(m_AD_Client_ID);
151             // Update section
152
m_sql.insert(0, m_updateSQL);
153
154             // Execute
155
int no = DB.executeUpdate(m_sql.toString());
156             if (no == 1)
157             {
158                 if (Log.isTraceLevel(10))
159                     log.debug(m_sql.toString());
160                 m_updateCount++;
161             }
162             else if (no == 0)
163                 log.warn ("Not Found - " + m_sql.toString());
164             else
165                 log.error ("Update Rows=" + no + " (Should be 1) - " + m_sql.toString());
166         }
167         else if (qName.equals(Translation.XML_VALUE_TAG))
168         {
169             if (m_sql.length() > 0)
170                 m_sql.append(",");
171             m_sql.append(m_curColumnName).append("=").append(DB.TO_STRING(m_curValue.toString()));
172         }
173     } // endElement
174

175
176     /**
177      * Get Number of updates
178      * @return update count
179      */

180     public int getUpdateCount()
181     {
182         return m_updateCount;
183     } // getUpdateCount
184

185 } // TranslationHandler
186
Popular Tags