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.wadl.inference.schema.types;
14  
15  import org.apache.xmlbeans.SchemaTypeLoader;
16  import org.apache.xmlbeans.SchemaTypeSystem;
17  import org.apache.xmlbeans.XmlBeans;
18  import org.apache.xmlbeans.XmlException;
19  import org.apache.xmlbeans.XmlObject;
20  
21  import com.eviware.soapui.impl.wadl.inference.schema.Context;
22  import com.eviware.soapui.impl.wadl.inference.schema.Schema;
23  import com.eviware.soapui.impl.wadl.inference.schema.Type;
24  import com.eviware.soapui.inferredSchema.CustomTypeConfig;
25  
26  /***
27   * CustomType corresponds to any custom type given as a user-defined xsd type
28   * definition.
29   * 
30   * @author Dain Nilsson
31   */
32  public class CustomType implements Type
33  {
34  	private String xsd;
35  	private String name;
36  	private Schema schema;
37  
38  	public CustomType( String name, String xsd )
39  	{
40  		this.name = name;
41  		this.xsd = xsd;
42  	}
43  
44  	public CustomType( CustomTypeConfig xml, Schema schema )
45  	{
46  		this.schema = schema;
47  		name = xml.getName();
48  		xsd = xml.getXsd();
49  	}
50  
51  	public CustomTypeConfig save()
52  	{
53  		CustomTypeConfig xml = CustomTypeConfig.Factory.newInstance();
54  		xml.setName( name );
55  		xml.setXsd( xsd );
56  		return xml;
57  	}
58  
59  	public Type validate( Context context ) throws XmlException
60  	{
61  		String name = context.getCursor().getName().getLocalPart();
62  		SchemaTypeSystem sts = XmlBeans.compileXsd( new XmlObject[] { XmlObject.Factory
63  				.parse( "<schema xmlns=\"http://www.w3.org/2001/XMLSchema\"><element name=\"" + name + "\">" + xsd
64  						+ "</element></schema>" ) }, XmlBeans.getBuiltinTypeSystem(), null );
65  		SchemaTypeLoader stl = XmlBeans.typeLoaderUnion( new SchemaTypeLoader[] { sts, XmlBeans.getBuiltinTypeSystem() } );
66  		if( !stl.parse( context.getCursor().xmlText(), null, null ).validate() )
67  			throw new XmlException( "Element '" + name + "' does not validate for custom type!" );
68  		return this;
69  	}
70  
71  	@Override
72  	public String toString()
73  	{
74  		return xsd;
75  	}
76  
77  	public String getName()
78  	{
79  		return name;
80  	}
81  
82  	public Schema getSchema()
83  	{
84  		return schema;
85  	}
86  
87  	public void setSchema( Schema schema )
88  	{
89  		this.schema = schema;
90  	}
91  
92  }