View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.wsdl.actions.iface;
14  
15  import java.io.File;
16  import java.io.FileReader;
17  import java.io.FileWriter;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.xml.transform.Transformer;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.transform.stream.StreamResult;
24  import javax.xml.transform.stream.StreamSource;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.impl.support.definition.export.WsdlDefinitionExporter;
28  import com.eviware.soapui.impl.wsdl.WsdlInterface;
29  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
30  import com.eviware.soapui.model.settings.Settings;
31  import com.eviware.soapui.support.Tools;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
34  import com.eviware.x.form.XFormDialog;
35  import com.eviware.x.form.support.ADialogBuilder;
36  import com.eviware.x.form.support.AField;
37  import com.eviware.x.form.support.AForm;
38  import com.eviware.x.form.support.AField.AFieldType;
39  
40  public class CreateWsdlDocumentationAction extends AbstractSoapUIAction<WsdlInterface>
41  {
42  	public static final String SOAPUI_ACTION_ID = "CreateWsdlDocumentationAction";
43  
44  	private static final String REPORT_DIRECTORY_SETTING = CreateWsdlDocumentationAction.class.getSimpleName()
45  			+ "@report-directory";
46  	private XFormDialog dialog;
47  	private static Map<String, Transformer> transformers;
48  
49  	public CreateWsdlDocumentationAction()
50  	{
51  		super( "CreateWsdlDocumentationAction", "Generate Documentation",
52  				"Generate simple HTML Documentation for this WSDL" );
53  	}
54  
55  	public void perform( WsdlInterface target, Object param )
56  	{
57  		try
58  		{
59  			if( dialog == null )
60  			{
61  				dialog = ADialogBuilder.buildDialog( Form.class );
62  			}
63  
64  			Settings settings = target.getSettings();
65  			dialog.setValue( Form.OUTPUT_FOLDER, settings.getString( REPORT_DIRECTORY_SETTING, "" ) );
66  
67  			if( !dialog.show() )
68  			{
69  				return;
70  			}
71  
72  			settings.setString( REPORT_DIRECTORY_SETTING, dialog.getValue( Form.OUTPUT_FOLDER ) );
73  
74  			final File reportDirectory = new File( settings.getString( REPORT_DIRECTORY_SETTING, "" ) );
75  			String reportDirAbsolutePath = reportDirectory.getAbsolutePath();
76  			String filename = reportDirAbsolutePath + File.separatorChar + "report.xml";
77  			String reportUrl = transform( target, reportDirAbsolutePath, filename );
78  			Tools.openURL( reportUrl );
79  		}
80  		catch( Exception e )
81  		{
82  			UISupport.showErrorMessage( e );
83  		}
84  	}
85  
86  	private static String transform( WsdlInterface target, String reportDirAbsolutePath, String filename )
87  			throws Exception
88  	{
89  		if( transformers == null )
90  		{
91  			initTransformers();
92  		}
93  
94  		Transformer transformer = transformers.get( "WSDL" );
95  		if( transformer == null )
96  		{
97  			throw new Exception( "Missing transformer for format [WSDL]" );
98  		}
99  
100 		transformer.setParameter( "output.dir", reportDirAbsolutePath );
101 
102 		String reportFile = reportDirAbsolutePath + File.separatorChar + "wsdl-report.html";
103 		StreamResult result = new StreamResult( new FileWriter( reportFile ) );
104 
105 		WsdlDefinitionExporter exporter = new WsdlDefinitionExporter( target );
106 		String infile = exporter.export( reportDirAbsolutePath );
107 
108 		transformer.transform( new StreamSource( new FileReader( infile ) ), result );
109 
110 		String reportUrl = new File( reportFile ).toURI().toURL().toString();
111 		return reportUrl;
112 	}
113 
114 	protected static void initTransformers() throws Exception
115 	{
116 		transformers = new HashMap<String, Transformer>();
117 		TransformerFactory xformFactory = TransformerFactory.newInstance();
118 
119 		Transformer transformer = xformFactory.newTransformer( new StreamSource( SoapUI.class
120 				.getResourceAsStream( "/com/eviware/soapui/resources/doc/wsdl-viewer.xsl" )
121 
122 		// new File( "C://dev//wsdl-viewer-1.3//wsdl-viewer.xsl")
123 				) );
124 		transformers.put( "WSDL", transformer );
125 	}
126 
127 	@AForm( description = "Creates an HTML-Report for the interface WSDL", name = "Create Documentation", helpUrl = HelpUrls.CREATEWADLDOC_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
128 	public interface Form
129 	{
130 		@AField( name = "Output Folder", description = "The folder where to create the report", type = AFieldType.FOLDER )
131 		public final static String OUTPUT_FOLDER = "Output Folder";
132 	}
133 }