View Javadoc

1   package com.eviware.soapui.impl.wadl.inference.support;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.apache.xmlbeans.SchemaTypeSystem;
10  import org.apache.xmlbeans.XmlBeans;
11  import org.apache.xmlbeans.XmlException;
12  import org.apache.xmlbeans.XmlObject;
13  
14  import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
15  import com.eviware.soapui.impl.wadl.inference.InferredSchema;
16  import com.eviware.soapui.impl.wadl.inference.schema.SchemaSystem;
17  import com.eviware.soapui.inferredSchema.SchemaSetConfig;
18  
19  /*
20   *  soapUI, copyright (C) 2004-2009 eviware.com 
21   *
22   *  soapUI is free software; you can redistribute it and/or modify it under the 
23   *  terms of version 2.1 of the GNU Lesser General Public License as published by 
24   *  the Free Software Foundation.
25   *
26   *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
27   *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
28   *  See the GNU Lesser General Public License for more details at gnu.org.
29   */
30  
31  public class InferredSchemaImpl implements InferredSchema
32  {
33  	private SchemaSystem ss;
34  
35  	public InferredSchemaImpl()
36  	{
37  		ss = new SchemaSystem();
38  	}
39  
40  	public InferredSchemaImpl( InputStream is ) throws XmlException, IOException
41  	{
42  		ss = new SchemaSystem( SchemaSetConfig.Factory.parse( is ) );
43  	}
44  
45  	public String[] getNamespaces()
46  	{
47  		return ss.getNamespaces().toArray( new String[0] );
48  	}
49  
50  	public SchemaTypeSystem getSchemaTypeSystem()
51  	{
52  		return getSchemaTypeSystem( XmlBeans.getBuiltinTypeSystem() );
53  	}
54  
55  	public SchemaTypeSystem getSchemaTypeSystem( SchemaTypeSystem sts )
56  	{
57  		List<XmlObject> schemas = new ArrayList<XmlObject>();
58  		try
59  		{
60  			for( String namespace : getNamespaces() )
61  			{
62  				schemas.add( XmlObject.Factory.parse( getXsdForNamespace( namespace ).toString() ) );
63  			}
64  			return XmlBeans.compileXsd( schemas.toArray( new XmlObject[0] ), sts, null );
65  		}
66  		catch( XmlException e )
67  		{
68  			e.printStackTrace();
69  			return null;
70  		}
71  	}
72  
73  	public String getXsdForNamespace( String namespace )
74  	{
75  		return ss.getSchemaForNamespace( namespace ).toString();
76  	}
77  
78  	public void learningValidate( XmlObject xml, ConflictHandler handler ) throws XmlException
79  	{
80  		ss.validate( xml, handler );
81  	}
82  
83  	public void processValidXml( XmlObject xml ) throws XmlException
84  	{
85  		ss.validate( xml, new AllowAll() );
86  	}
87  
88  	public void save( OutputStream os ) throws IOException
89  	{
90  		SchemaSetConfig xml = SchemaSetConfig.Factory.newInstance();
91  		ss.save( xml );
92  		xml.save( os );
93  	}
94  
95  	public boolean validate( XmlObject xml )
96  	{
97  		try
98  		{
99  			ss.validate( xml, new DenyAll() );
100 			return true;
101 		}
102 		catch( XmlException e )
103 		{
104 			return false;
105 		}
106 	}
107 
108 	public void deleteNamespace( String ns )
109 	{
110 		ss.deleteNamespace( ns );
111 	}
112 
113 }