View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.support.definition.export;
14  
15  import java.util.List;
16  
17  import net.java.dev.wadl.x2009.x02.ApplicationDocument;
18  import net.java.dev.wadl.x2009.x02.MethodDocument.Method;
19  import net.java.dev.wadl.x2009.x02.RepresentationDocument.Representation;
20  import net.java.dev.wadl.x2009.x02.ResourceDocument.Resource;
21  import net.java.dev.wadl.x2009.x02.ResourcesDocument.Resources;
22  import net.java.dev.wadl.x2009.x02.ResponseDocument.Response;
23  
24  import org.apache.xmlbeans.XmlObject;
25  import org.w3c.dom.Element;
26  
27  import com.eviware.soapui.impl.rest.RestService;
28  import com.eviware.soapui.impl.support.definition.InterfaceDefinition;
29  import com.eviware.soapui.impl.support.definition.InterfaceDefinitionPart;
30  import com.eviware.soapui.impl.wsdl.support.Constants;
31  
32  public class WadlDefinitionExporter extends AbstractDefinitionExporter<RestService>
33  {
34  	public WadlDefinitionExporter( InterfaceDefinition<RestService> definition )
35  	{
36  		super( definition );
37  	}
38  
39  	public WadlDefinitionExporter( RestService restService ) throws Exception
40  	{
41  		this( restService.getDefinitionContext().getInterfaceDefinition() );
42  	}
43  
44  	public String export( String folderName ) throws Exception
45  	{
46  		if( getDefinition().getInterface().isGenerated() )
47  			setDefinition( getDefinition().getInterface().getWadlContext().regenerateWadl() );
48  
49  		return super.export( folderName );
50  	}
51  
52  	protected String[] getLocationXPathsToReplace()
53  	{
54  		return new String[] { "declare namespace s='" + getDefinition().getInterface().getWadlVersion() + "' .//s:grammars/s:include/@href",
55  				"declare namespace s='http://www.w3.org/2001/XMLSchema' .//s:import/@schemaLocation",
56  				"declare namespace s='http://www.w3.org/2001/XMLSchema' .//s:include/@schemaLocation" };
57  	}
58  
59  	@Override
60  	protected void postProcessing( XmlObject obj, InterfaceDefinitionPart part )
61  	{
62  		if( part.getType().equals( Constants.WADL11_NS ) )
63  		{
64  			ApplicationDocument document = ( ApplicationDocument )obj;
65  			for( Resources resources : document.getApplication().getResourcesList() )
66  			{
67  				for( Resource resource : resources.getResourceList() )
68  				{
69  					for( Method method : resource.getMethodList() )
70  					{
71  						fixRepresentations( method.getRequest().getRepresentationList() );
72  						for( Response response : method.getResponseList() )
73  						{
74  							fixRepresentations( response.getRepresentationList() );
75  						}
76  					}
77  				}
78  			}
79  		}
80  	}
81  
82  	private void fixRepresentations( List<Representation> representationList )
83  	{
84  		for( Representation representation : representationList )
85  		{
86  			if( !( "text/xml".equals( representation.getMediaType() )
87  					|| "application/xml".equals( representation.getMediaType() ) ) && representation.isSetElement())
88  			{
89  				String prefix = representation.xgetElement().getDomNode().getNodeValue().split( ":" )[0];
90  				representation.unsetElement();
91  				((Element)representation.getDomNode()).removeAttribute( "xmlns:"+prefix );
92  			}
93  		}
94  	}
95  
96  }