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