jaxp/test/javax/xml/jaxp/functional/org/xml/sax/ptests/MyAttrCHandler.java
changeset 28740 3ebd3353fda5
parent 28737 ca4b6a6e5cc8
parent 28739 9cfbed0fed2d
child 28741 1f10b1bd612d
equal deleted inserted replaced
28737:ca4b6a6e5cc8 28740:3ebd3353fda5
     1 /*
       
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package org.xml.sax.ptests;
       
    24 
       
    25 import java.io.BufferedWriter;
       
    26 import java.io.FileWriter;
       
    27 import java.io.IOException;
       
    28 import org.xml.sax.Attributes;
       
    29 import org.xml.sax.SAXException;
       
    30 import org.xml.sax.helpers.DefaultHandler;
       
    31 
       
    32 /**
       
    33  * Simple attributes handler.
       
    34  */
       
    35 public class MyAttrCHandler extends DefaultHandler {
       
    36     /**
       
    37      * FileWriter to write string to output file.
       
    38      */
       
    39     private final BufferedWriter bWriter;
       
    40 
       
    41     /**
       
    42      * Initiate FileWriter
       
    43      * @param fileName output file name.
       
    44      * @throws IOException
       
    45      */
       
    46     public MyAttrCHandler(String fileName) throws IOException {
       
    47         bWriter = new BufferedWriter(new FileWriter(fileName));
       
    48     }
       
    49 
       
    50     /**
       
    51      * Write element content before start access every element.
       
    52      * @throws org.xml.sax.SAXException
       
    53      */
       
    54     @Override
       
    55     public void startElement(String uri, String localName,
       
    56                 String qName, Attributes attributes) throws SAXException {
       
    57         try {
       
    58             String string = "uri <" + uri + "> localName <" + localName +
       
    59                         "> qName <" + qName + ">";
       
    60 
       
    61             bWriter.write( string, 0, string.length());
       
    62             bWriter.newLine();
       
    63 
       
    64             int length = attributes.getLength();
       
    65             string = "length: " + length;
       
    66 
       
    67             bWriter.write( string, 0, string.length());
       
    68             bWriter.newLine();
       
    69 
       
    70             for (int ind=0; ind < length ; ind++) {
       
    71                 string = "For index = " + ind + "\n";
       
    72                 string += "getLocalName <" + attributes.getLocalName(ind)
       
    73                                 +">" + "\n";
       
    74                 string += "getQName <" + attributes.getQName(ind) +">" + "\n";
       
    75                 string += "getType <" + attributes.getType(ind) +">" + "\n";
       
    76                 string += "getURI <" + attributes.getURI(ind) +">" + "\n";
       
    77                 string += "getValue <" + attributes.getValue(ind) +">" + "\n";
       
    78 
       
    79                 bWriter.write( string, 0, string.length());
       
    80                 bWriter.newLine();
       
    81 
       
    82                 String gotLocalName = attributes.getLocalName(ind);
       
    83                 String gotQName = attributes.getQName(ind);
       
    84                 String gotURI = attributes.getURI(ind);
       
    85 
       
    86                 string ="Using localName, qname and uri pertaining to index = "
       
    87                                 + ind;
       
    88                 bWriter.write( string, 0, string.length());
       
    89                 bWriter.newLine();
       
    90 
       
    91                 string = "getIndex(qName) <" + attributes.getIndex(gotQName)
       
    92                                 +">" + "\n";
       
    93                 string += "getIndex(uri, localName) <" +
       
    94                         attributes.getIndex(gotURI, gotLocalName) +">" + "\n";
       
    95 
       
    96                 string += "getType(qName) <" +
       
    97                         attributes.getType(gotQName) +">" + "\n";
       
    98                 string += "getType(uri, localName) <" +
       
    99                         attributes.getType(gotURI, gotLocalName) +">" + "\n";
       
   100 
       
   101                 string += "getValue(qName) <" +
       
   102                         attributes.getValue(gotQName) +">" + "\n";
       
   103                 string += "getValue(uri, localName) <" +
       
   104                         attributes.getValue(gotURI, gotLocalName) +">" + "\n";
       
   105 
       
   106                 bWriter.write( string, 0, string.length());
       
   107                 bWriter.newLine();
       
   108             }
       
   109             bWriter.newLine();
       
   110         } catch(IOException ex){
       
   111             throw new SAXException(ex);
       
   112         }
       
   113     }
       
   114 
       
   115     /**
       
   116      * Flush the stream and close the file.
       
   117      * @throws IOException when writing or closing file failed.
       
   118      */
       
   119     public void flushAndClose() throws IOException {
       
   120         bWriter.flush();
       
   121         bWriter.close();
       
   122     }
       
   123 }