1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SAXEventPrinter.java,v $
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
;
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
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
59 private boolean m_bIsFormatted
;
62 * the output stream to write
64 private FileOutputStream m_fileOutputStream
;
66 SAXEventPrinter(FileOutputStream fileOutputStream
, boolean isFormatted
)
69 m_fileOutputStream
= fileOutputStream
;
70 m_bIsFormatted
= isFormatted
;
73 protected static void outputIndent(int m_nIndent
, FileOutputStream fileOutputStream
)
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
)
90 int type
= node
.getNodeType();
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
);
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()+"\"";
122 outputIndent(indent
, fileOutputStream
);
125 fileOutputStream
.write(message
.getBytes("UTF-8"));
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
);
141 outputIndent(indent
, fileOutputStream
);
144 fileOutputStream
.write("</".getBytes());
145 fileOutputStream
.write(node
.getNodeName().getBytes("UTF-8"));
146 fileOutputStream
.write(">".getBytes());
150 fileOutputStream
.write("\n".getBytes());
156 message
= node
.getNodeValue();
157 if (message
!= null )
161 outputIndent(indent
, fileOutputStream
);
164 fileOutputStream
.write(node
.getNodeValue().getBytes("UTF-8"));
168 fileOutputStream
.write("\n".getBytes());
173 case Node
.PROCESSING_INSTRUCTION_NODE
:
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());
186 fileOutputStream
.write("\n".getBytes());
199 public void startDocument ()
203 public void endDocument()
207 public void startElement (String str
, com
.sun
.star
.xml
.sax
.XAttributeList xattribs
)
213 message
= new String("<"+str
);
216 int length
= xattribs
.getLength();
217 for (short i
=0; i
<length
; ++i
)
219 message
+= " "+xattribs
.getNameByIndex(i
)+"=\""+xattribs
.getValueByIndex(i
)+"\"";
226 outputIndent(m_nIndent
, m_fileOutputStream
);
229 m_fileOutputStream
.write(message
.getBytes("UTF-8"));
233 m_fileOutputStream
.write("\n".getBytes());
238 catch (IOException e
)
244 public void endElement(String str
)
251 outputIndent(m_nIndent
, m_fileOutputStream
);
254 m_fileOutputStream
.write("</".getBytes());
255 m_fileOutputStream
.write(str
.getBytes("UTF-8"));
256 m_fileOutputStream
.write(">".getBytes());
260 m_fileOutputStream
.write("\n".getBytes());
263 catch (IOException e
)
269 public void characters(String str
)
275 outputIndent(m_nIndent
, m_fileOutputStream
);
278 m_fileOutputStream
.write(str
.getBytes("UTF-8"));
282 m_fileOutputStream
.write("\n".getBytes());
285 catch (IOException e
)
291 public void ignorableWhitespace(String str
)
295 public void processingInstruction(String aTarget
, String aData
)
301 outputIndent(m_nIndent
, m_fileOutputStream
);
304 m_fileOutputStream
.write("<?".getBytes());
305 m_fileOutputStream
.write(aTarget
.getBytes("UTF-8"));
306 m_fileOutputStream
.write("?>".getBytes());
310 m_fileOutputStream
.write("\n".getBytes());
313 catch (IOException e
)
319 public void setDocumentLocator (com
.sun
.star
.xml
.sax
.XLocator xLocator
)
320 throws com
.sun
.star
.xml
.sax
.SAXException