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.XmlAnySimpleType;
16  import org.apache.xmlbeans.XmlCursor;
17  import org.apache.xmlbeans.XmlException;
18  
19  import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
20  import com.eviware.soapui.impl.wadl.inference.schema.Context;
21  import com.eviware.soapui.impl.wadl.inference.schema.Schema;
22  import com.eviware.soapui.impl.wadl.inference.schema.Settings;
23  import com.eviware.soapui.impl.wadl.inference.schema.Type;
24  import com.eviware.soapui.impl.wadl.inference.support.TypeInferrer;
25  import com.eviware.soapui.inferredSchema.SimpleTypeConfig;
26  
27  /***
28   * SimpleType corresponds to an instance of a xs:simpleType. Each element or
29   * attribute with a value of a specific xs:simpleType should have its own
30   * instance of SimpleType.
31   * 
32   * @author Dain Nilsson
33   */
34  public class SimpleType implements Type
35  {
36  	private XmlAnySimpleType simpleType;
37  	private Schema schema;
38  	private boolean completed = true;
39  
40  	public SimpleType( Schema schema, XmlAnySimpleType simpleType, boolean completed )
41  	{
42  		this.schema = schema;
43  		this.simpleType = simpleType;
44  		this.completed = completed;
45  	}
46  
47  	public SimpleType( SimpleTypeConfig xml, Schema schema )
48  	{
49  		this.schema = schema;
50  		completed = xml.getCompleted();
51  		simpleType = TypeInferrer.getType( xml.getTypeName() );
52  	}
53  
54  	public SimpleTypeConfig save()
55  	{
56  		SimpleTypeConfig xml = SimpleTypeConfig.Factory.newInstance();
57  		xml.setCompleted( completed );
58  		xml.setTypeName( simpleType.schemaType().getName().getLocalPart() );
59  		return xml;
60  	}
61  
62  	public Type validate( Context context ) throws XmlException
63  	{
64  		XmlCursor cursor = context.getCursor();
65  		if( !cursor.isAttr() && ( cursor.toFirstAttribute() || cursor.toFirstChild() ) )
66  		{
67  			// Element with complex content
68  			return new ComplexType( schema, context.getName(), completed );
69  		}
70  		else if( !context.getAttribute( "nil" ).equals( "true" ) )
71  		{
72  			String value = "";
73  			cursor.toFirstContentToken();
74  			if( !cursor.isEnd() )
75  				value = cursor.getTextValue();
76  			if( TypeInferrer.validateSimpleType( value, simpleType ) )
77  				return this;
78  			XmlAnySimpleType newType = TypeInferrer.expandTypeForValue( value, simpleType );
79  			if( cursor.getName() == null )
80  				cursor.toParent();
81  			if( context.getHandler().callback( ConflictHandler.Event.MODIFICATION, ConflictHandler.Type.TYPE,
82  					cursor.getName(), context.getPath(), "Illegal content '" + value + "'" ) )
83  			{
84  				// Switch to other simpleType.
85  				// return
86  				// context.getSchemaSystem().getType(newType.schemaType().getName());
87  				simpleType = newType;
88  			}
89  			else
90  				throw new XmlException( "Invalid value!" );
91  		}
92  		return this;
93  	}
94  
95  	@Override
96  	public String toString()
97  	{
98  		return "";
99  	}
100 
101 	public String getName()
102 	{
103 		return schema.getPrefixForNamespace( Settings.xsdns ) + ":" + simpleType.schemaType().getName().getLocalPart();
104 	}
105 
106 	public Schema getSchema()
107 	{
108 		return schema;
109 	}
110 
111 	public void setSchema( Schema schema )
112 	{
113 		this.schema = schema;
114 	}
115 
116 }