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