xref: /trunk/main/xmlsecurity/tools/uno/SAXEventPrinter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Attr;
33 import org.w3c.dom.NodeList;
34 import java.io.IOException;
35 import java.io.FileOutputStream;
36 
37 /* uno classes */
38 import com.sun.star.xml.sax.XDocumentHandler;
39 import com.sun.star.xml.sax.XAttributeList;
40 
41 /*
42  * The SAXEventPrinter class is used to print out received
43  * SAX event stream.
44  */
45 class SAXEventPrinter implements XDocumentHandler
46 {
47     /*
48      * how many spaces as the indent of line
49      */
50     private int m_nIndent;
51 
52     /*
53      * whether a NEW LINE character need to be appended to
54      * each line
55      */
56     private boolean m_bIsFormatted;
57 
58     /*
59      * the output stream to write
60      */
61     private FileOutputStream m_fileOutputStream;
62 
63     SAXEventPrinter(FileOutputStream fileOutputStream, boolean isFormatted)
64     {
65         m_nIndent = 0;
66         m_fileOutputStream = fileOutputStream;
67         m_bIsFormatted = isFormatted;
68     }
69 
70     protected static void outputIndent(int m_nIndent, FileOutputStream fileOutputStream)
71         throws IOException
72     {
73         for (int i=0; i<m_nIndent; ++i)
74         {
75             fileOutputStream.write(" ".getBytes());
76         }
77     }
78 
79     /*
80      * displays the tree of a Node.
81      */
82     protected static void display(Node node, int indent, FileOutputStream fileOutputStream, boolean isFormatted)
83         throws IOException
84     {
85         if (node != null)
86         {
87             int type = node.getNodeType();
88             String message;
89             NodeList children;
90             int i, length;
91 
92             switch (type)
93             {
94             case Node.DOCUMENT_NODE:
95                 children = node.getChildNodes();
96                 length = children.getLength();
97                 for (i=0; i<length; ++i)
98                 {
99                     display(children.item(i), indent+2, fileOutputStream, isFormatted);
100                 }
101 
102                 break;
103 
104             case Node.ELEMENT_NODE:
105                 message = new String("<"+node.getNodeName());
106                 NamedNodeMap attrs = node.getAttributes();
107 
108                 length = attrs.getLength();
109                 for (i=0; i<length; ++i)
110                 {
111                     Attr attr = (Attr)attrs.item(i);
112                     message += " "+attr.getNodeName()+"=\""+attr.getNodeValue()+"\"";
113                 }
114 
115                 message += ">";
116 
117                 if (isFormatted)
118                 {
119                     outputIndent(indent, fileOutputStream);
120                 }
121 
122                 fileOutputStream.write(message.getBytes("UTF-8"));
123 
124                 if (isFormatted)
125                 {
126                     fileOutputStream.write("\n".getBytes());
127                 }
128 
129                 children = node.getChildNodes();
130                 length = children.getLength();
131                 for (i=0; i<length; ++i)
132                 {
133                     display(children.item(i), indent+2, fileOutputStream, isFormatted);
134                 }
135 
136                 if (isFormatted)
137                 {
138                     outputIndent(indent, fileOutputStream);
139                 }
140 
141                 fileOutputStream.write("</".getBytes());
142                 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
143                 fileOutputStream.write(">".getBytes());
144 
145                 if (isFormatted)
146                 {
147                     fileOutputStream.write("\n".getBytes());
148                 }
149 
150                 break;
151 
152             case Node.TEXT_NODE:
153                 message = node.getNodeValue();
154                 if (message != null )
155                 {
156                     if (isFormatted)
157                     {
158                         outputIndent(indent, fileOutputStream);
159                     }
160 
161                     fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
162 
163                     if (isFormatted)
164                     {
165                         fileOutputStream.write("\n".getBytes());
166                     }
167                 }
168                 break;
169 
170             case Node.PROCESSING_INSTRUCTION_NODE:
171                 if (isFormatted)
172                 {
173                     outputIndent(indent, fileOutputStream);
174                 }
175 
176                 fileOutputStream.write("<?".getBytes());
177                 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
178                 fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
179                 fileOutputStream.write("?>".getBytes());
180 
181                 if (isFormatted)
182                 {
183                     fileOutputStream.write("\n".getBytes());
184                 }
185 
186                 break;
187             default:
188                 break;
189             }
190         }
191     }
192 
193     /*
194      * XDocumentHandler
195      */
196     public void  startDocument ()
197     {
198     }
199 
200     public void endDocument()
201     {
202     }
203 
204     public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
205     {
206         try
207         {
208             String message;
209 
210             message = new String("<"+str);
211             if (xattribs !=null)
212             {
213                 int length = xattribs.getLength();
214                 for (short i=0; i<length; ++i)
215                 {
216                     message += " "+xattribs.getNameByIndex(i)+"=\""+xattribs.getValueByIndex(i)+"\"";
217                 }
218             }
219             message += ">";
220 
221             if (m_bIsFormatted)
222             {
223                 outputIndent(m_nIndent, m_fileOutputStream);
224             }
225 
226             m_fileOutputStream.write(message.getBytes("UTF-8"));
227 
228             if (m_bIsFormatted)
229             {
230                 m_fileOutputStream.write("\n".getBytes());
231             }
232 
233             m_nIndent += 2;
234         }
235         catch (IOException e)
236         {
237             e.printStackTrace();
238         }
239     }
240 
241     public void endElement(String str)
242     {
243         try
244         {
245             m_nIndent -= 2;
246             if (m_bIsFormatted)
247             {
248                 outputIndent(m_nIndent, m_fileOutputStream);
249             }
250 
251             m_fileOutputStream.write("</".getBytes());
252             m_fileOutputStream.write(str.getBytes("UTF-8"));
253             m_fileOutputStream.write(">".getBytes());
254 
255             if (m_bIsFormatted)
256             {
257                 m_fileOutputStream.write("\n".getBytes());
258             }
259         }
260         catch (IOException e)
261         {
262             e.printStackTrace();
263         }
264     }
265 
266     public void characters(String str)
267     {
268         try
269         {
270             if (m_bIsFormatted)
271             {
272                 outputIndent(m_nIndent, m_fileOutputStream);
273             }
274 
275             m_fileOutputStream.write(str.getBytes("UTF-8"));
276 
277             if (m_bIsFormatted)
278             {
279                 m_fileOutputStream.write("\n".getBytes());
280             }
281         }
282         catch (IOException e)
283         {
284             e.printStackTrace();
285         }
286     }
287 
288     public void ignorableWhitespace(String str)
289     {
290     }
291 
292     public void processingInstruction(String aTarget, String aData)
293     {
294         try
295         {
296             if (m_bIsFormatted)
297             {
298                 outputIndent(m_nIndent, m_fileOutputStream);
299             }
300 
301             m_fileOutputStream.write("<?".getBytes());
302             m_fileOutputStream.write(aTarget.getBytes("UTF-8"));
303             m_fileOutputStream.write("?>".getBytes());
304 
305             if (m_bIsFormatted)
306             {
307                 m_fileOutputStream.write("\n".getBytes());
308             }
309         }
310         catch (IOException e)
311         {
312             e.printStackTrace();
313         }
314     }
315 
316     public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
317         throws com.sun.star.xml.sax.SAXException
318     {
319     }
320 }
321