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
;
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
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
56 private boolean m_bIsFormatted
;
59 * the output stream to write
61 private FileOutputStream m_fileOutputStream
;
63 SAXEventPrinter(FileOutputStream fileOutputStream
, boolean isFormatted
)
66 m_fileOutputStream
= fileOutputStream
;
67 m_bIsFormatted
= isFormatted
;
70 protected static void outputIndent(int m_nIndent
, FileOutputStream fileOutputStream
)
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
)
87 int type
= node
.getNodeType();
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
);
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()+"\"";
119 outputIndent(indent
, fileOutputStream
);
122 fileOutputStream
.write(message
.getBytes("UTF-8"));
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
);
138 outputIndent(indent
, fileOutputStream
);
141 fileOutputStream
.write("</".getBytes());
142 fileOutputStream
.write(node
.getNodeName().getBytes("UTF-8"));
143 fileOutputStream
.write(">".getBytes());
147 fileOutputStream
.write("\n".getBytes());
153 message
= node
.getNodeValue();
154 if (message
!= null )
158 outputIndent(indent
, fileOutputStream
);
161 fileOutputStream
.write(node
.getNodeValue().getBytes("UTF-8"));
165 fileOutputStream
.write("\n".getBytes());
170 case Node
.PROCESSING_INSTRUCTION_NODE
:
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());
183 fileOutputStream
.write("\n".getBytes());
196 public void startDocument ()
200 public void endDocument()
204 public void startElement (String str
, com
.sun
.star
.xml
.sax
.XAttributeList xattribs
)
210 message
= new String("<"+str
);
213 int length
= xattribs
.getLength();
214 for (short i
=0; i
<length
; ++i
)
216 message
+= " "+xattribs
.getNameByIndex(i
)+"=\""+xattribs
.getValueByIndex(i
)+"\"";
223 outputIndent(m_nIndent
, m_fileOutputStream
);
226 m_fileOutputStream
.write(message
.getBytes("UTF-8"));
230 m_fileOutputStream
.write("\n".getBytes());
235 catch (IOException e
)
241 public void endElement(String str
)
248 outputIndent(m_nIndent
, m_fileOutputStream
);
251 m_fileOutputStream
.write("</".getBytes());
252 m_fileOutputStream
.write(str
.getBytes("UTF-8"));
253 m_fileOutputStream
.write(">".getBytes());
257 m_fileOutputStream
.write("\n".getBytes());
260 catch (IOException e
)
266 public void characters(String str
)
272 outputIndent(m_nIndent
, m_fileOutputStream
);
275 m_fileOutputStream
.write(str
.getBytes("UTF-8"));
279 m_fileOutputStream
.write("\n".getBytes());
282 catch (IOException e
)
288 public void ignorableWhitespace(String str
)
292 public void processingInstruction(String aTarget
, String aData
)
298 outputIndent(m_nIndent
, m_fileOutputStream
);
301 m_fileOutputStream
.write("<?".getBytes());
302 m_fileOutputStream
.write(aTarget
.getBytes("UTF-8"));
303 m_fileOutputStream
.write("?>".getBytes());
307 m_fileOutputStream
.write("\n".getBytes());
310 catch (IOException e
)
316 public void setDocumentLocator (com
.sun
.star
.xml
.sax
.XLocator xLocator
)
317 throws com
.sun
.star
.xml
.sax
.SAXException