Code example for Logger

Methods: entering, exiting, isLoggable, logp

0
     * 
     * @throws SAXException Thrown if a parse error occurs. 
     * @throws IOException Thrown if an IO error occurs. 
     */ 
    public static void toJson(InputStream XMLStream, OutputStream JSONStream, boolean verbose) throws SAXException, IOException {
        if (logger.isLoggable(Level.FINER)) {
            logger.entering(className, "toJson(InputStream, OutputStream)");
        } 
 
        if (XMLStream == null) {
            throw new NullPointerException("XMLStream cannot be null");
        } else if (JSONStream == null) {
            throw new NullPointerException("JSONStream cannot be null");
        } else { 
 
            if (logger.isLoggable(Level.FINEST)) {
                logger.logp(Level.FINEST, className, "transform", "Fetching a SAX parser for use with JSONSAXHandler");
            } 
 
            try { 
                /** 
                 * Get a parser. 
                 */ 
                SAXParserFactory factory = SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                SAXParser sParser = factory.newSAXParser();
                XMLReader parser = sParser.getXMLReader();
                JSONSAXHandler jsonHandler = new JSONSAXHandler(JSONStream, verbose);
                parser.setContentHandler(jsonHandler);
                parser.setErrorHandler(jsonHandler);
                InputSource source = new InputSource(new BufferedInputStream(XMLStream));
 
                if (logger.isLoggable(Level.FINEST)) {
                    logger.logp(Level.FINEST, className, "transform", "Parsing the XML content to JSON");
                } 
 
                /**  
                 * Parse it. 
                 */ 
                source.setEncoding("UTF-8");
                parser.parse(source);                 
                jsonHandler.flushBuffer();
            } catch (javax.xml.parsers.ParserConfigurationException pce) {
                throw new SAXException("Could not get a parser: " + pce.toString());
            } 
        } 
 
        if (logger.isLoggable(Level.FINER)) {
            logger.exiting(className, "toJson(InputStream, OutputStream)");
        } 
    } 
 
    /** 
     * Method to take an input stream to an XML document and return a String of the JSON format.   
     * Note that the xmlStream is not closed when read is complete.  This is left up to the caller, who may wish to do more with it.   
     * This is the same as toJson(xmlStream,false) 
     * 
     * @param xmlStream The InputStream to an XML document to transform to JSON. 
     * @return A string of the JSON representation of the XML file 
     *  
     * @throws SAXException Thrown if an error occurs during parse. 
     * @throws IOException Thrown if an IOError occurs. 
     */ 
    public static String toJson(InputStream xmlStream) throws SAXException, IOException {
        return toJson(xmlStream,false);
    } 
 
 
    /** 
     * Method to take an input stream to an XML document and return a String of the JSON format.  Note that the xmlStream is not closed when read is complete.  This is left up to the caller, who may wish to do more with it. 
     * @param xmlStream The InputStream to an XML document to transform to JSON. 
     * @param verbose Boolean flag denoting whther or not to write the JSON in verbose (formatted), or compact form (no whitespace) 
     * @return A string of the JSON representation of the XML file 
     *  
     * @throws SAXException Thrown if an error occurs during parse. 
     * @throws IOException Thrown if an IOError occurs. 
     */                                                                     
    public static String toJson(InputStream xmlStream, boolean verbose)  throws SAXException, IOException {
        if (logger.isLoggable(Level.FINER)) {
            logger.exiting(className, "toJson(InputStream, boolean)");
        } 
 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String result              = null;
 
        try { 
            toJson(xmlStream,baos,verbose);
            result = baos.toString("UTF-8");
            baos.close();
        } catch (UnsupportedEncodingException uec) {
            IOException iox = new IOException(uec.toString());
            iox.initCause(uec);
            throw iox;
        } 
 
        if (logger.isLoggable(Level.FINER)) {
            logger.exiting(className, "toJson(InputStream, boolean)");
        } 
 
        return result;
    }