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.content;
14  
15  import org.apache.xmlbeans.XmlCursor;
16  import org.apache.xmlbeans.XmlException;
17  
18  import com.eviware.soapui.impl.wadl.inference.schema.Content;
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.support.TypeInferrer;
22  import com.eviware.soapui.inferredSchema.EmptyContentConfig;
23  
24  /***
25   * EmptyContent may not have any content, be it simpe or complex.
26   * 
27   * @author Dain Nilsson
28   */
29  public class EmptyContent implements Content
30  {
31  	private Schema schema;
32  	private boolean completed = false;
33  
34  	public EmptyContent( Schema schema, boolean completed )
35  	{
36  		this.schema = schema;
37  		this.completed = completed;
38  	}
39  
40  	public EmptyContent( EmptyContentConfig xml, Schema schema )
41  	{
42  		this.schema = schema;
43  		completed = xml.getCompleted();
44  	}
45  
46  	public EmptyContentConfig save()
47  	{
48  		EmptyContentConfig xml = EmptyContentConfig.Factory.newInstance();
49  		xml.setCompleted( completed );
50  		return xml;
51  	}
52  
53  	public String toString( String attrs )
54  	{
55  		return attrs;
56  	}
57  
58  	public Content validate( Context context ) throws XmlException
59  	{
60  		XmlCursor cursor = context.getCursor();
61  		cursor.push();
62  		if( cursor.toParent() && cursor.toFirstChild() )
63  		{
64  			// Element has children
65  			cursor.pop();
66  			return new SequenceContent( schema, completed );
67  		}
68  		else if( cursor.pop() && !cursor.isEnd() )
69  		{
70  			// Element has simple content
71  			if( completed )
72  				return new SimpleContent( schema, TypeInferrer.getBlankType() );
73  			else
74  				return new SimpleContent( schema, cursor.getTextValue() );
75  		}
76  		completed = true;
77  		return this;
78  	}
79  
80  }