View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.support.handlers;
14  
15  import com.eviware.soapui.impl.rest.support.MediaTypeHandler;
16  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
17  import com.eviware.soapui.support.StringUtils;
18  import com.eviware.soapui.support.xml.XmlUtils;
19  import org.w3c.dom.Document;
20  import org.w3c.tidy.Tidy;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  
26  public class HtmlMediaTypeHandler implements MediaTypeHandler
27  {
28     public boolean canHandle( String contentType )
29     {
30        return contentType != null && contentType.equals( "text/html" );
31     }
32  
33     public String createXmlRepresentation( HttpResponse response )
34     {
35        String content = response == null ? null : response.getContentAsString();
36        if( !StringUtils.hasContent( content ) )
37           return null;
38  
39        try
40        {
41           Tidy tidy = new Tidy();
42           tidy.setXmlOut( true );
43           tidy.setShowWarnings( false );
44           tidy.setErrout( new PrintWriter( new StringWriter() ) );
45  //         tidy.setQuiet(true);
46           tidy.setNumEntities( true );
47           tidy.setQuoteNbsp( true );
48  
49           Document document = tidy.parseDOM( new ByteArrayInputStream( content.getBytes() ), null );
50           StringWriter writer = new StringWriter();
51           XmlUtils.serializePretty( document, writer );
52           return writer.toString();
53        }
54        catch( Throwable e )
55        {
56           e.printStackTrace();
57        }
58        return null;
59     }
60  }