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