update dev300-m58
[ooovba.git] / xmlsecurity / tools / uno / SAXEventPrinter.java
blob2868762d55a496cb87889eef515426fe3e65b157
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SAXEventPrinter.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.xml.security.uno;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NamedNodeMap;
35 import org.w3c.dom.Attr;
36 import org.w3c.dom.NodeList;
37 import java.io.IOException;
38 import java.io.FileOutputStream;
40 /* uno classes */
41 import com.sun.star.xml.sax.XDocumentHandler;
42 import com.sun.star.xml.sax.XAttributeList;
45 * The SAXEventPrinter class is used to print out received
46 * SAX event stream.
48 class SAXEventPrinter implements XDocumentHandler
51 * how many spaces as the indent of line
53 private int m_nIndent;
56 * whether a NEW LINE character need to be appended to
57 * each line
59 private boolean m_bIsFormatted;
62 * the output stream to write
64 private FileOutputStream m_fileOutputStream;
66 SAXEventPrinter(FileOutputStream fileOutputStream, boolean isFormatted)
68 m_nIndent = 0;
69 m_fileOutputStream = fileOutputStream;
70 m_bIsFormatted = isFormatted;
73 protected static void outputIndent(int m_nIndent, FileOutputStream fileOutputStream)
74 throws IOException
76 for (int i=0; i<m_nIndent; ++i)
78 fileOutputStream.write(" ".getBytes());
83 * displays the tree of a Node.
85 protected static void display(Node node, int indent, FileOutputStream fileOutputStream, boolean isFormatted)
86 throws IOException
88 if (node != null)
90 int type = node.getNodeType();
91 String message;
92 NodeList children;
93 int i, length;
95 switch (type)
97 case Node.DOCUMENT_NODE:
98 children = node.getChildNodes();
99 length = children.getLength();
100 for (i=0; i<length; ++i)
102 display(children.item(i), indent+2, fileOutputStream, isFormatted);
105 break;
107 case Node.ELEMENT_NODE:
108 message = new String("<"+node.getNodeName());
109 NamedNodeMap attrs = node.getAttributes();
111 length = attrs.getLength();
112 for (i=0; i<length; ++i)
114 Attr attr = (Attr)attrs.item(i);
115 message += " "+attr.getNodeName()+"=\""+attr.getNodeValue()+"\"";
118 message += ">";
120 if (isFormatted)
122 outputIndent(indent, fileOutputStream);
125 fileOutputStream.write(message.getBytes("UTF-8"));
127 if (isFormatted)
129 fileOutputStream.write("\n".getBytes());
132 children = node.getChildNodes();
133 length = children.getLength();
134 for (i=0; i<length; ++i)
136 display(children.item(i), indent+2, fileOutputStream, isFormatted);
139 if (isFormatted)
141 outputIndent(indent, fileOutputStream);
144 fileOutputStream.write("</".getBytes());
145 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
146 fileOutputStream.write(">".getBytes());
148 if (isFormatted)
150 fileOutputStream.write("\n".getBytes());
153 break;
155 case Node.TEXT_NODE:
156 message = node.getNodeValue();
157 if (message != null )
159 if (isFormatted)
161 outputIndent(indent, fileOutputStream);
164 fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
166 if (isFormatted)
168 fileOutputStream.write("\n".getBytes());
171 break;
173 case Node.PROCESSING_INSTRUCTION_NODE:
174 if (isFormatted)
176 outputIndent(indent, fileOutputStream);
179 fileOutputStream.write("<?".getBytes());
180 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
181 fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
182 fileOutputStream.write("?>".getBytes());
184 if (isFormatted)
186 fileOutputStream.write("\n".getBytes());
189 break;
190 default:
191 break;
197 * XDocumentHandler
199 public void startDocument ()
203 public void endDocument()
207 public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
211 String message;
213 message = new String("<"+str);
214 if (xattribs !=null)
216 int length = xattribs.getLength();
217 for (short i=0; i<length; ++i)
219 message += " "+xattribs.getNameByIndex(i)+"=\""+xattribs.getValueByIndex(i)+"\"";
222 message += ">";
224 if (m_bIsFormatted)
226 outputIndent(m_nIndent, m_fileOutputStream);
229 m_fileOutputStream.write(message.getBytes("UTF-8"));
231 if (m_bIsFormatted)
233 m_fileOutputStream.write("\n".getBytes());
236 m_nIndent += 2;
238 catch (IOException e)
240 e.printStackTrace();
244 public void endElement(String str)
248 m_nIndent -= 2;
249 if (m_bIsFormatted)
251 outputIndent(m_nIndent, m_fileOutputStream);
254 m_fileOutputStream.write("</".getBytes());
255 m_fileOutputStream.write(str.getBytes("UTF-8"));
256 m_fileOutputStream.write(">".getBytes());
258 if (m_bIsFormatted)
260 m_fileOutputStream.write("\n".getBytes());
263 catch (IOException e)
265 e.printStackTrace();
269 public void characters(String str)
273 if (m_bIsFormatted)
275 outputIndent(m_nIndent, m_fileOutputStream);
278 m_fileOutputStream.write(str.getBytes("UTF-8"));
280 if (m_bIsFormatted)
282 m_fileOutputStream.write("\n".getBytes());
285 catch (IOException e)
287 e.printStackTrace();
291 public void ignorableWhitespace(String str)
295 public void processingInstruction(String aTarget, String aData)
299 if (m_bIsFormatted)
301 outputIndent(m_nIndent, m_fileOutputStream);
304 m_fileOutputStream.write("<?".getBytes());
305 m_fileOutputStream.write(aTarget.getBytes("UTF-8"));
306 m_fileOutputStream.write("?>".getBytes());
308 if (m_bIsFormatted)
310 m_fileOutputStream.write("\n".getBytes());
313 catch (IOException e)
315 e.printStackTrace();
319 public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
320 throws com.sun.star.xml.sax.SAXException