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.content;
14  
15  import javax.xml.namespace.QName;
16  
17  import org.apache.xmlbeans.XmlAnySimpleType;
18  import org.apache.xmlbeans.XmlCursor;
19  import org.apache.xmlbeans.XmlException;
20  
21  import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
22  import com.eviware.soapui.impl.wadl.inference.schema.Content;
23  import com.eviware.soapui.impl.wadl.inference.schema.Context;
24  import com.eviware.soapui.impl.wadl.inference.schema.Schema;
25  import com.eviware.soapui.impl.wadl.inference.schema.Settings;
26  import com.eviware.soapui.impl.wadl.inference.support.TypeInferrer;
27  import com.eviware.soapui.inferredSchema.SimpleContentConfig;
28  
29  /***
30   * SimpleContent may contain xs:simpleContent.
31   * 
32   * @author Dain Nilsson
33   */
34  public class SimpleContent implements Content
35  {
36  	private Schema schema;
37  	private XmlAnySimpleType simpleType;
38  
39  	public SimpleContent( Schema schema, String initialValue )
40  	{
41  		this.schema = schema;
42  		simpleType = TypeInferrer.inferSimpleType( initialValue );
43  	}
44  
45  	public SimpleContent( Schema schema, XmlAnySimpleType initialType )
46  	{
47  		this.schema = schema;
48  		simpleType = initialType;
49  	}
50  
51  	public SimpleContent( SimpleContentConfig xml, Schema schema )
52  	{
53  		this.schema = schema;
54  		simpleType = TypeInferrer.getType( xml.getTypeName() );
55  	}
56  
57  	public SimpleContentConfig save()
58  	{
59  		SimpleContentConfig xml = SimpleContentConfig.Factory.newInstance();
60  		xml.setTypeName( simpleType.schemaType().getName().getLocalPart() );
61  		return xml;
62  	}
63  
64  	public Content validate( Context context ) throws XmlException
65  	{
66  		XmlCursor cursor = context.getCursor();
67  		String value = "";
68  		if( cursor.isStart() )
69  			throw new XmlException( "Unsupported!" );
70  		if( !cursor.isEnd() )
71  			value = cursor.getTextValue();
72  		if( !TypeInferrer.validateSimpleType( value, simpleType ) )
73  		{
74  			XmlAnySimpleType newSimpleType = TypeInferrer.expandTypeForValue( value, simpleType );
75  			if( context.getHandler().callback( ConflictHandler.Event.MODIFICATION, ConflictHandler.Type.TYPE,
76  					new QName( schema.getNamespace(), context.getAttribute( "typeName" ) ), context.getPath(),
77  					"Illegal value." ) )
78  			{
79  				simpleType = newSimpleType;
80  			}
81  			else
82  				throw new XmlException( "Illegal content!" );
83  		}
84  		return this;
85  	}
86  
87  	public String toString( String attrs )
88  	{
89  		if( simpleType == null )
90  			return attrs;
91  		String xsdns = schema.getPrefixForNamespace( Settings.xsdns );
92  		StringBuilder s = new StringBuilder( "<" + xsdns + ":simpleContent><" + xsdns + ":extension base=\"" + xsdns
93  				+ ":" + simpleType.schemaType().getName().getLocalPart() + "\">" );
94  		s.append( attrs );
95  		s.append( "</" + xsdns + ":extension></" + xsdns + ":simpleContent>" );
96  		return s.toString();
97  	}
98  
99  }