KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > PrintDataHandler


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.print;
15
16 import java.util.*;
17
18 import org.xml.sax.helpers.*;
19 import org.xml.sax.*;
20
21 /**
22  * SAX Handler for parsing PrintData
23  *
24  * @author Jorg Janke
25  * @version $Id: PrintDataHandler.java,v 1.7 2003/04/24 06:15:05 jjanke Exp $
26  */

27 public class PrintDataHandler extends DefaultHandler
28 {
29     /**
30      * Constructor
31      * @param ctx context
32      */

33     public PrintDataHandler(Properties ctx)
34     {
35         m_ctx = ctx;
36     } // PrintDataHandler
37

38     /** Context */
39     private Properties m_ctx = null;
40     /** Final Structure */
41     private PrintData m_pd = null;
42
43     /** Current Active Element Name */
44     private String JavaDoc m_curPDEname = null;
45     /** Current Active Element Value */
46     private StringBuffer JavaDoc m_curPDEvalue = null;
47     /** Current Active Print Data */
48     private PrintData m_curPD = null;
49
50     /**
51      * Get PrintData
52      * @return PrintData
53      */

54     public PrintData getPrintData()
55     {
56         return m_pd;
57     } // getPrintData
58

59     /*************************************************************************/
60
61     /**
62      * Receive notification of the start of an element.
63      *
64      * @param uri namespace
65      * @param localName simple name
66      * @param qName qualified name
67      * @param attributes attributes
68      * @throws org.xml.sax.SAXException
69      */

70     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes)
71         throws org.xml.sax.SAXException JavaDoc
72     {
73         if (qName.equals(PrintData.XML_TAG))
74         {
75             String JavaDoc name = attributes.getValue(PrintData.XML_ATTRIBUTE_NAME);
76             if (m_pd == null)
77             {
78                 m_pd = new PrintData(m_ctx, name);
79                 push(m_pd);
80             }
81             else
82             {
83                 PrintData temp = new PrintData(m_ctx, name);
84                 m_curPD.addNode(temp);
85                 push(temp);
86             }
87         }
88         else if (qName.equals(PrintData.XML_ROW_TAG))
89         {
90             m_curPD.addRow(false, 0);
91         }
92         else if (qName.equals(PrintDataElement.XML_TAG))
93         {
94             m_curPDEname = attributes.getValue(PrintDataElement.XML_ATTRIBUTE_NAME);
95             m_curPDEvalue = new StringBuffer JavaDoc();
96         }
97     } // startElement
98

99     /**
100      * Receive notification of character data inside an element.
101      *
102      * @param ch buffer
103      * @param start start
104      * @param length length
105      * @throws SAXException
106      */

107     public void characters (char ch[], int start, int length)
108         throws SAXException
109     {
110         m_curPDEvalue.append(ch, start, length);
111     } // characters
112

113     /**
114      * Receive notification of the end of an element.
115      * @param uri namespace
116      * @param localName simple name
117      * @param qName qualified name
118      * @throws SAXException
119      */

120     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
121         throws SAXException
122     {
123         if (qName.equals(PrintData.XML_TAG))
124         {
125             pop();
126         }
127         else if (qName.equals(PrintDataElement.XML_TAG))
128         {
129             m_curPD.addNode(new PrintDataElement(m_curPDEname, m_curPDEvalue.toString(),0));
130         }
131     } // endElement
132

133     /*************************************************************************/
134
135     /** Stack */
136     private ArrayList m_stack = new ArrayList();
137
138     /**
139      * Push new PD on Stack and set m_cutPD
140      * @param newPD new PD
141      */

142     private void push (PrintData newPD)
143     {
144         // add
145
m_stack.add(newPD);
146         m_curPD = newPD;
147     } // push
148

149     /**
150      * Pop last PD from Stack and set m_cutPD
151      */

152     private void pop ()
153     {
154         // remove last
155
if (m_stack.size() > 0)
156             m_stack.remove(m_stack.size()-1);
157         // get previous
158
if (m_stack.size() > 0)
159             m_curPD = (PrintData)m_stack.get(m_stack.size()-1);
160     } // pop
161

162 } // PrintDataHandler
163
Popular Tags