Code example for Logger

Methods: entering, exiting, isLoggable, logp

0
     * @throws IOException Thrown if an IO error occurs. 
     */ 
    public static void toXml(InputStream JSONStream, OutputStream XMLStream, boolean verbose)
    throws IOException 
    { 
        if (logger.isLoggable(Level.FINER)) {
            logger.entering(className, "toXml(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", "Parsing the JSON and a DOM builder.");
            } 
 
            try { 
                //Get the JSON from the stream. 
                JSONObject jObject = new JSONObject(JSONStream);
 
                //Create a new document 
 
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbf.newDocumentBuilder();
                Document doc = dBuilder.newDocument();
 
                if (logger.isLoggable(Level.FINEST)) {
                    logger.logp(Level.FINEST, className, "transform", "Parsing the JSON content to XML");
                } 
 
                convertJSONObject(doc, doc.getDocumentElement(), jObject, "jsonObject");
 
                //Serialize it. 
                TransformerFactory tfactory = TransformerFactory.newInstance();
                Transformer serializer  = null;
                if (verbose) {
                    serializer = tfactory.newTransformer(new StreamSource( new StringReader(styleSheet) ));;
                } else { 
                    serializer = tfactory.newTransformer();
                } 
                Properties oprops = new Properties();
                oprops.put(OutputKeys.METHOD, "xml");
                oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
                oprops.put(OutputKeys.VERSION, "1.0");
                oprops.put(OutputKeys.INDENT, "true");
                serializer.setOutputProperties(oprops);
                serializer.transform(new DOMSource(doc), new StreamResult(XMLStream));
 
            } catch (Exception ex) {
                IOException iox = new IOException("Problem during conversion");
                iox.initCause(ex);
                throw iox;
            } 
        } 
 
        if (logger.isLoggable(Level.FINER)) {
            logger.exiting(className, "toXml(InputStream, OutputStream)");
        } 
    } 
 
    /** 
     * Method to take an input stream to an JSON document and return a String of the XML format.   
     * Note that the JSONStream 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 toXml(JSONStream,false) 
     * 
     * @param JSONStream The InputStream to an JSON document to transform to XML. 
     * @return A string of the JSON representation of the XML file 
     *  
     * @throws IOException Thrown if an IOError occurs. 
     */ 
    public static String toXml(InputStream JSONStream)
    throws IOException 
    { 
        return toXml(JSONStream,false);
    } 
 
 
    /** 
     * Method to take an input stream to an JSON document and return a String of the XML format.  Note that the JSONStream 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 JSON document to transform to XML. 
     * @param verbose Boolean flag denoting whther or not to write the XML in verbose (formatted), or compact form (no whitespace) 
     * @return A string of the JSON representation of the XML file 
     *  
     * @throws IOException Thrown if an IOError occurs. 
     */ 
    public static String toXml(InputStream JSONStream, boolean verbose)
    throws IOException 
    { 
        if (logger.isLoggable(Level.FINER)) {
            logger.exiting(className, "toXml(InputStream, boolean)");
        } 
 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String result              = null;
 
        try { 
            toXml(JSONStream,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, "toXml(InputStream, boolean)");
        } 
 
        return result;
    }