merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / uno / SAXEventPrinter.java
blobd841926bdd13043dcbf240963050bc92b9e5fd71
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package com.sun.star.xml.security.uno;
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;
37 /* uno classes */
38 import com.sun.star.xml.sax.XDocumentHandler;
39 import com.sun.star.xml.sax.XAttributeList;
42 * The SAXEventPrinter class is used to print out received
43 * SAX event stream.
45 class SAXEventPrinter implements XDocumentHandler
48 * how many spaces as the indent of line
50 private int m_nIndent;
53 * whether a NEW LINE character need to be appended to
54 * each line
56 private boolean m_bIsFormatted;
59 * the output stream to write
61 private FileOutputStream m_fileOutputStream;
63 SAXEventPrinter(FileOutputStream fileOutputStream, boolean isFormatted)
65 m_nIndent = 0;
66 m_fileOutputStream = fileOutputStream;
67 m_bIsFormatted = isFormatted;
70 protected static void outputIndent(int m_nIndent, FileOutputStream fileOutputStream)
71 throws IOException
73 for (int i=0; i<m_nIndent; ++i)
75 fileOutputStream.write(" ".getBytes());
80 * displays the tree of a Node.
82 protected static void display(Node node, int indent, FileOutputStream fileOutputStream, boolean isFormatted)
83 throws IOException
85 if (node != null)
87 int type = node.getNodeType();
88 String message;
89 NodeList children;
90 int i, length;
92 switch (type)
94 case Node.DOCUMENT_NODE:
95 children = node.getChildNodes();
96 length = children.getLength();
97 for (i=0; i<length; ++i)
99 display(children.item(i), indent+2, fileOutputStream, isFormatted);
102 break;
104 case Node.ELEMENT_NODE:
105 message = new String("<"+node.getNodeName());
106 NamedNodeMap attrs = node.getAttributes();
108 length = attrs.getLength();
109 for (i=0; i<length; ++i)
111 Attr attr = (Attr)attrs.item(i);
112 message += " "+attr.getNodeName()+"=\""+attr.getNodeValue()+"\"";
115 message += ">";
117 if (isFormatted)
119 outputIndent(indent, fileOutputStream);
122 fileOutputStream.write(message.getBytes("UTF-8"));
124 if (isFormatted)
126 fileOutputStream.write("\n".getBytes());
129 children = node.getChildNodes();
130 length = children.getLength();
131 for (i=0; i<length; ++i)
133 display(children.item(i), indent+2, fileOutputStream, isFormatted);
136 if (isFormatted)
138 outputIndent(indent, fileOutputStream);
141 fileOutputStream.write("</".getBytes());
142 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
143 fileOutputStream.write(">".getBytes());
145 if (isFormatted)
147 fileOutputStream.write("\n".getBytes());
150 break;
152 case Node.TEXT_NODE:
153 message = node.getNodeValue();
154 if (message != null )
156 if (isFormatted)
158 outputIndent(indent, fileOutputStream);
161 fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
163 if (isFormatted)
165 fileOutputStream.write("\n".getBytes());
168 break;
170 case Node.PROCESSING_INSTRUCTION_NODE:
171 if (isFormatted)
173 outputIndent(indent, fileOutputStream);
176 fileOutputStream.write("<?".getBytes());
177 fileOutputStream.write(node.getNodeName().getBytes("UTF-8"));
178 fileOutputStream.write(node.getNodeValue().getBytes("UTF-8"));
179 fileOutputStream.write("?>".getBytes());
181 if (isFormatted)
183 fileOutputStream.write("\n".getBytes());
186 break;
187 default:
188 break;
194 * XDocumentHandler
196 public void startDocument ()
200 public void endDocument()
204 public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
208 String message;
210 message = new String("<"+str);
211 if (xattribs !=null)
213 int length = xattribs.getLength();
214 for (short i=0; i<length; ++i)
216 message += " "+xattribs.getNameByIndex(i)+"=\""+xattribs.getValueByIndex(i)+"\"";
219 message += ">";
221 if (m_bIsFormatted)
223 outputIndent(m_nIndent, m_fileOutputStream);
226 m_fileOutputStream.write(message.getBytes("UTF-8"));
228 if (m_bIsFormatted)
230 m_fileOutputStream.write("\n".getBytes());
233 m_nIndent += 2;
235 catch (IOException e)
237 e.printStackTrace();
241 public void endElement(String str)
245 m_nIndent -= 2;
246 if (m_bIsFormatted)
248 outputIndent(m_nIndent, m_fileOutputStream);
251 m_fileOutputStream.write("</".getBytes());
252 m_fileOutputStream.write(str.getBytes("UTF-8"));
253 m_fileOutputStream.write(">".getBytes());
255 if (m_bIsFormatted)
257 m_fileOutputStream.write("\n".getBytes());
260 catch (IOException e)
262 e.printStackTrace();
266 public void characters(String str)
270 if (m_bIsFormatted)
272 outputIndent(m_nIndent, m_fileOutputStream);
275 m_fileOutputStream.write(str.getBytes("UTF-8"));
277 if (m_bIsFormatted)
279 m_fileOutputStream.write("\n".getBytes());
282 catch (IOException e)
284 e.printStackTrace();
288 public void ignorableWhitespace(String str)
292 public void processingInstruction(String aTarget, String aData)
296 if (m_bIsFormatted)
298 outputIndent(m_nIndent, m_fileOutputStream);
301 m_fileOutputStream.write("<?".getBytes());
302 m_fileOutputStream.write(aTarget.getBytes("UTF-8"));
303 m_fileOutputStream.write("?>".getBytes());
305 if (m_bIsFormatted)
307 m_fileOutputStream.write("\n".getBytes());
310 catch (IOException e)
312 e.printStackTrace();
316 public void setDocumentLocator (com.sun.star.xml.sax.XLocator xLocator )
317 throws com.sun.star.xml.sax.SAXException