All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.util.test.DumpUtils Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Stats
 * Copyright (C) 2011-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile/
 * SCM: http://java.net/hg/bluebill-server~stats-src
 *
 **********************************************************************************************************************/
package it.tidalwave.util.test;

import javax.annotation.Nonnull;
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import lombok.Cleanup;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static lombok.AccessLevel.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j @NoArgsConstructor(access=PRIVATE)
public final class DumpUtils // TODO: move class to TFT testutils 
  {
    public static final String APPLICATION_XML = "application/xml";
    
    public static final String APPLICATION_JSON = "application/json";

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    public static void dump (final @Nonnull File file, final @Nonnull List objects) 
      throws IOException
      {
        file.getParentFile().mkdirs();
        final @Cleanup PrintWriter pw = new PrintWriter(file);
        
        for (final Object object : objects)
          {
            pw.println(object);  
          }
        
        pw.close();
      }    
    
    /*******************************************************************************************************************
     *
     * Dump a document in a formatted fashion, useful for comparations in tests.
     * 
     * @param  file      the file to dump the document to
     * @param  mimeType  the mime type of the document (e.g. 'application/xml' or 'application/json')
     * @param  string    the document contents
     * 
     ******************************************************************************************************************/
    public static void dumpFormatted (final @Nonnull File file, final String mimeType, final String string) 
      throws Exception
      {
        file.getParentFile().mkdirs();
        
        if (mimeType.equals(APPLICATION_XML))
          {
            dumpXml(file, string);
            return;
          }
        
        OutputStream os = new FileOutputStream(file);
        
        if (mimeType.equals(APPLICATION_JSON))
          { 
            os = new JsonPrettyPrinterStream(os);  
          }
        
        final @Cleanup PrintWriter pw = new PrintWriter(os);
        pw.print(string);
        pw.close();
      }
    
    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    private static void dumpXml (final @Nonnull File file, final String string) 
      throws Exception
      {
        final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        final DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
        final LSSerializer writer = impl.createLSSerializer();
        writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
        final LSOutput output = impl.createLSOutput();
        final @Cleanup OutputStream os = new FileOutputStream(file);
        output.setByteStream(os);
        writer.write(parseXmlFile(string), output);
        os.close();
//        final Transformer t = TransformerFactory.newInstance().newTransformer();
//        t.setOutputProperty(OutputKeys.METHOD, "xml");
//        t.setOutputProperty(OutputKeys.INDENT, "true");
//        t.transform(new DOMSource(parseXmlFile(string)), new StreamResult(file));      
      }
    
    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    @Nonnull
    private static Document parseXmlFile (final @Nonnull String in) 
      throws ParserConfigurationException, SAXException, IOException
      {
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy