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