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.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.schema.Context;
20  import com.eviware.soapui.impl.wadl.inference.schema.Schema;
21  import com.eviware.soapui.impl.wadl.inference.schema.Settings;
22  import com.eviware.soapui.impl.wadl.inference.schema.Type;
23  import com.eviware.soapui.impl.wadl.inference.schema.content.EmptyContent;
24  import com.eviware.soapui.impl.wadl.inference.support.TypeInferrer;
25  import com.eviware.soapui.inferredSchema.EmptyTypeConfig;
26  
27  /***
28   * EmptyRtpe corresponds to an instance of a type with no attributes, nor any
29   * content.
30   * 
31   * @author Dain Nilsson
32   */
33  public class EmptyType implements Type
34  {
35  	private Schema schema;
36  	private EmptyContent empty;
37  	private boolean completed = false;
38  
39  	public EmptyType( Schema schema )
40  	{
41  		this.schema = schema;
42  		empty = new EmptyContent( schema, false );
43  	}
44  
45  	public EmptyType( EmptyTypeConfig xml, Schema schema )
46  	{
47  		this.schema = schema;
48  		empty = new EmptyContent( schema, xml.getCompleted() );
49  		completed = xml.getCompleted();
50  	}
51  
52  	public EmptyTypeConfig save()
53  	{
54  		EmptyTypeConfig xml = EmptyTypeConfig.Factory.newInstance();
55  		xml.setCompleted( completed );
56  		return xml;
57  	}
58  
59  	public String getName()
60  	{
61  		return "empty_element";
62  	}
63  
64  	public Schema getSchema()
65  	{
66  		return schema;
67  	}
68  
69  	public void setSchema( Schema schema )
70  	{
71  		this.schema = schema;
72  	}
73  
74  	public Type validate( Context context ) throws XmlException
75  	{
76  		XmlCursor cursor = context.getCursor();
77  		if( !cursor.isAttr() && ( cursor.toFirstAttribute() || cursor.toFirstChild() ) )
78  		{
79  			// Element has attributes or children, must be complexType
80  			ComplexType newType = new ComplexType( schema, context.getName(), completed );
81  			newType.setContent( empty );
82  			return newType;
83  		}
84  		cursor.toFirstContentToken();
85  		if( empty.validate( context ) != empty )
86  		{
87  			// Element has simple content, must be simpleType
88  			String value = cursor.getTextValue();
89  			XmlAnySimpleType simpleType;
90  			if( completed )
91  				simpleType = TypeInferrer.getBlankType();
92  			else
93  				simpleType = TypeInferrer.inferSimpleType( value );
94  			// return
95  			// context.getSchemaSystem().getType(simpleType.schemaType().getName());
96  			return new SimpleType( schema, simpleType, completed );
97  		}
98  		completed = true;
99  		return this;
100 	}
101 
102 	public String toString()
103 	{
104 		String xsdns = schema.getPrefixForNamespace( Settings.xsdns );
105 		StringBuilder s = new StringBuilder( "<" + xsdns + ":complexType name=\"" + getName() + "\"/>" );
106 		return s.toString();
107 	}
108 
109 }