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.particles;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.xmlbeans.XmlException;
21  
22  import com.eviware.soapui.impl.wadl.inference.schema.Context;
23  import com.eviware.soapui.impl.wadl.inference.schema.Particle;
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.schema.Type;
27  import com.eviware.soapui.inferredSchema.MapEntryConfig;
28  import com.eviware.soapui.inferredSchema.ReferenceParticleConfig;
29  
30  /***
31   * A ReferenceParticle is a reference to a particle in another namespace. It may
32   * be either an xs:element or an xs:attribute.
33   * 
34   * @author Dain Nilsson
35   */
36  public class ReferenceParticle implements Particle
37  {
38  	private Schema schema;
39  	private Particle reference;
40  	private QName referenceQName;
41  	private Map<String, String> attributes;
42  
43  	public ReferenceParticle( Schema schema, Particle reference )
44  	{
45  		this.schema = schema;
46  		this.reference = reference;
47  		referenceQName = reference.getName();
48  		attributes = new HashMap<String, String>();
49  	}
50  
51  	public ReferenceParticle( ReferenceParticleConfig xml, Schema schema )
52  	{
53  		this.schema = schema;
54  		referenceQName = xml.getReference();
55  		attributes = new HashMap<String, String>();
56  		for( MapEntryConfig entry : xml.getAttributeList() )
57  		{
58  			attributes.put( entry.getKey(), entry.getValue() );
59  		}
60  	}
61  
62  	public ReferenceParticleConfig save()
63  	{
64  		ReferenceParticleConfig xml = ReferenceParticleConfig.Factory.newInstance();
65  		xml.setReference( referenceQName );
66  		for( Map.Entry<String, String> entry : attributes.entrySet() )
67  		{
68  			MapEntryConfig mapEntry = xml.addNewAttribute();
69  			mapEntry.setKey( entry.getKey() );
70  			mapEntry.setValue( entry.getValue() );
71  		}
72  		return xml;
73  	}
74  
75  	private Particle getReference()
76  	{
77  		if( reference == null )
78  		{
79  			reference = schema.getSystem().getSchemaForNamespace( referenceQName.getNamespaceURI() ).getParticle(
80  					referenceQName.getLocalPart() );
81  		}
82  		return reference;
83  	}
84  
85  	public QName getName()
86  	{
87  		return referenceQName;
88  	}
89  
90  	public String getAttribute( String key )
91  	{
92  		String value = attributes.get( key );
93  		if( ( key.equals( "minOccurs" ) || key.equals( "maxOccurs" ) ) && value == null )
94  			value = "1";
95  		return value;
96  	}
97  
98  	public void setAttribute( String key, String value )
99  	{
100 		attributes.put( key, value );
101 	}
102 
103 	public Type getType()
104 	{
105 		return null;
106 	}
107 
108 	public void setType( Type type )
109 	{
110 	}
111 
112 	public void validate( Context context ) throws XmlException
113 	{
114 		context.pushPath();
115 		getReference().validate( context );
116 		context.popPath();
117 	}
118 
119 	@Override
120 	public String toString()
121 	{
122 		StringBuilder s = new StringBuilder( "<" + schema.getPrefixForNamespace( Settings.xsdns ) + ":"
123 				+ getReference().getPType() + " ref=\"" + schema.getPrefixForNamespace( referenceQName.getNamespaceURI() )
124 				+ ":" + referenceQName.getLocalPart() + "\"" );
125 		for( Map.Entry<String, String> entry : attributes.entrySet() )
126 			s.append( " " + entry.getKey() + "=\"" + entry.getValue() + "\"" );
127 		s.append( "/>" );
128 		return s.toString();
129 	}
130 
131 	public Particle.ParticleType getPType()
132 	{
133 		return getReference().getPType();
134 	}
135 
136 }