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.rest.actions.service;
14  
15  import java.io.File;
16  import java.io.FileReader;
17  import java.io.FileWriter;
18  import java.net.URL;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.xml.transform.Source;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.URIResolver;
27  import javax.xml.transform.stream.StreamResult;
28  import javax.xml.transform.stream.StreamSource;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.impl.rest.RestService;
32  import com.eviware.soapui.impl.support.definition.export.WadlDefinitionExporter;
33  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
34  import com.eviware.soapui.impl.wsdl.support.PathUtils;
35  import com.eviware.soapui.model.settings.Settings;
36  import com.eviware.soapui.support.Tools;
37  import com.eviware.soapui.support.UISupport;
38  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
39  import com.eviware.x.form.XFormDialog;
40  import com.eviware.x.form.support.ADialogBuilder;
41  import com.eviware.x.form.support.AField;
42  import com.eviware.x.form.support.AForm;
43  import com.eviware.x.form.support.AField.AFieldType;
44  
45  public class CreateWadlDocumentationAction extends AbstractSoapUIAction<RestService>
46  {
47  	public static final String SOAPUI_ACTION_ID = "CreateWadlDocumentationAction";
48  
49  	private static final String REPORT_DIRECTORY_SETTING = CreateWadlDocumentationAction.class.getSimpleName()
50  			+ "@report-directory";
51  	private XFormDialog dialog;
52  	private static Map<String, Transformer> transformers;
53  
54  	public CreateWadlDocumentationAction()
55  	{
56  		super( "CreateWadlDocumentationAction", "Create Documentation",
57  				"Generate simple HTML Documentation for this WADL" );
58  	}
59  
60  	public void perform( RestService target, Object param )
61  	{
62  		try
63  		{
64  			if( dialog == null )
65  			{
66  				dialog = ADialogBuilder.buildDialog( Form.class );
67  			}
68  
69  			Settings settings = target.getSettings();
70  			dialog.setValue( Form.OUTPUT_FOLDER, settings.getString( REPORT_DIRECTORY_SETTING, "" ) );
71  
72  			if( !dialog.show() )
73  			{
74  				return;
75  			}
76  
77  			settings.setString( REPORT_DIRECTORY_SETTING, dialog.getValue( Form.OUTPUT_FOLDER ) );
78  
79  			final File reportDirectory = new File( settings.getString( REPORT_DIRECTORY_SETTING, "" ) );
80  			String reportDirAbsolutePath = reportDirectory.getAbsolutePath();
81  			String filename = reportDirAbsolutePath + File.separatorChar + "report.xml";
82  			String reportUrl = transform( target, reportDirAbsolutePath, filename );
83  			Tools.openURL( reportUrl );
84  		}
85  		catch( Exception e )
86  		{
87  			UISupport.showErrorMessage( e );
88  		}
89  	}
90  
91  	private static String transform( RestService target, final String reportDirAbsolutePath, String filename )
92  			throws Exception
93  	{
94  		if( transformers == null )
95  		{
96  			initTransformers();
97  		}
98  
99  		Transformer transformer = transformers.get( "WADL" );
100 		if( transformer == null )
101 		{
102 			throw new Exception( "Missing transformer for format [" + target + "]" );
103 		}
104 
105 		transformer.setParameter( "output.dir", reportDirAbsolutePath );
106 
107 		String reportFile = reportDirAbsolutePath + File.separatorChar + "wadl-report.html";
108 		StreamResult result = new StreamResult( new FileWriter( reportFile ) );
109 
110 		WadlDefinitionExporter exporter = new WadlDefinitionExporter( target );
111 		String infile = exporter.export( reportDirAbsolutePath );
112 
113 		transformer.setURIResolver( new FileUriResolver( reportDirAbsolutePath ) );
114 		transformer.transform( new StreamSource( new FileReader( infile ) ), result );
115 
116 		String reportUrl = new File( reportFile ).toURI().toURL().toString();
117 		return reportUrl;
118 	}
119 
120 	protected static void initTransformers() throws Exception
121 	{
122 		transformers = new HashMap<String, Transformer>();
123 		TransformerFactory xformFactory = new org.apache.xalan.processor.TransformerFactoryImpl();
124 
125 		transformers.put( "WADL", xformFactory.newTemplates(
126 				new StreamSource( SoapUI.class
127 						.getResourceAsStream( "/com/eviware/soapui/resources/doc/wadl_documentation.xsl" ) ) )
128 				.newTransformer() );
129 	}
130 
131 	@AForm( description = "Creates an HTML-Report for the current WADL", name = "Create Report", helpUrl = HelpUrls.CREATEWADLDOC_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
132 	public interface Form
133 	{
134 		@AField( name = "Output Folder", description = "The folder where to create the report", type = AFieldType.FOLDER )
135 		public final static String OUTPUT_FOLDER = "Output Folder";
136 	}
137 
138 	public static class FileUriResolver implements URIResolver
139 	{
140 		private final String basePath;
141 
142 		public FileUriResolver( String basePath )
143 		{
144 			this.basePath = basePath;
145 		}
146 
147 		public Source resolve( String href, String base ) throws TransformerException
148 		{
149 			try
150 			{
151 				if( PathUtils.isHttpPath( href ) )
152 					return new StreamSource( new URL( href ).openStream() );
153 
154 				File file = PathUtils.isAbsolutePath( href ) ? new File( href ) : new File( basePath, href );
155 				FileReader reader = new FileReader( file );
156 				return new StreamSource( reader );
157 			}
158 			catch( Exception e )
159 			{
160 				return null;
161 			}
162 		}
163 	}
164 }