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.panels.request.inspectors.schema;
14  
15  import java.beans.PropertyChangeListener;
16  import java.beans.PropertyChangeSupport;
17  import java.io.ByteArrayInputStream;
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import com.eviware.soapui.impl.rest.RestService;
25  import com.eviware.soapui.impl.wadl.inference.InferredSchema;
26  
27  /***
28   * @author Dain.Nilsson
29   * 
30   */
31  public class InferredSchemaManager
32  {
33  	private static Map<RestService, InferredSchema> schemas;
34  	private static Map<RestService, PropertyChangeSupport> propertyChangeSupports;
35  	private static Map<String, String> filenames;
36  	private static Map<String, String> rFilenames;
37  
38  	static
39  	{
40  		schemas = new HashMap<RestService, InferredSchema>();
41  		propertyChangeSupports = new HashMap<RestService, PropertyChangeSupport>();
42  		filenames = new HashMap<String, String>();
43  		rFilenames = new HashMap<String, String>();
44  	}
45  
46  	public static String filenameForNamespace( String namespace )
47  	{
48  		if( !filenames.containsKey( namespace ) )
49  		{
50  			filenames.put( namespace, generateFilename( namespace ) );
51  			rFilenames.put( filenames.get( namespace ), namespace );
52  		}
53  		return filenames.get( namespace );
54  	}
55  
56  	public static String namespaceForFilename( String filename )
57  	{
58  		if( !rFilenames.containsKey( filename ) )
59  		{
60  			for( InferredSchema is : schemas.values() )
61  			{
62  				for( String ns : is.getNamespaces() )
63  				{
64  					if( filenameForNamespace( ns ).equals( filename ) )
65  						return ns;
66  				}
67  			}
68  		}
69  		else
70  			return rFilenames.get( filename );
71  		return null;
72  	}
73  
74  	private static String generateFilename( String namespace )
75  	{
76  		if( namespace.equals( "" ) )
77  			return "unnamed.xsd";
78  		return namespace.replaceAll( "[^a-zA-Z0-9]", "" ) + ".xsd";
79  	}
80  
81  	public static InferredSchema getInferredSchema( RestService service )
82  	{
83  		if( !schemas.containsKey( service ) )
84  		{
85  			try
86  			{
87  				schemas.put( service, InferredSchema.Factory.parse( new ByteArrayInputStream( service.getInferredSchema()
88  						.getBytes() ) ) );
89  			}
90  			catch( Exception e )
91  			{
92  				schemas.put( service, InferredSchema.Factory.newInstance() );
93  			}
94  			propertyChangeSupports.put( service, new PropertyChangeSupport( schemas.get( service ) ) );
95  		}
96  		return schemas.get( service );
97  	}
98  
99  	public static void save( RestService service )
100 	{
101 		if( schemas.containsKey( service ) )
102 		{
103 			OutputStream out = new ByteArrayOutputStream();
104 			String old = service.getInferredSchema();
105 			try
106 			{
107 				schemas.get( service ).save( out );
108 				service.setInferredSchema( out.toString() );
109 			}
110 			catch( IOException e )
111 			{
112 				e.printStackTrace();
113 			}
114 			propertyChangeSupports.get( service ).firePropertyChange( "inferredSchema", old, out.toString() );
115 		}
116 	}
117 	
118 	public static void release( RestService service )
119 	{
120 		schemas.remove( service );
121 		propertyChangeSupports.remove( service );
122 	}
123 
124 	public static void delete( RestService service )
125 	{
126 		service.setInferredSchema( null );
127 		if( schemas.containsKey( service ) )
128 		{
129 			schemas.remove( service );
130 		}
131 		propertyChangeSupports.get( service ).firePropertyChange( "inferredSchema", service.getInferredSchema(), null );
132 	}
133 
134 	public static void addPropertyChangeListener( RestService service, PropertyChangeListener listener )
135 	{
136 		if( getInferredSchema( service ) != null )
137 			propertyChangeSupports.get( service ).addPropertyChangeListener( "inferredSchema", listener );
138 	}
139 
140 	public static void removePropertyChangeListener( RestService service, PropertyChangeListener listener )
141 	{
142 		propertyChangeSupports.get( service ).removePropertyChangeListener( "inferredSchema", listener );
143 	}
144 
145 	public static void deleteNamespace( RestService service, String ns )
146 	{
147 		getInferredSchema( service ).deleteNamespace( ns );
148 		save( service );
149 	}
150 }