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