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;
14  
15  import javax.xml.namespace.QName;
16  
17  import org.apache.xmlbeans.XmlException;
18  
19  import com.eviware.soapui.impl.wadl.inference.schema.particles.AttributeParticle;
20  import com.eviware.soapui.impl.wadl.inference.schema.particles.ElementParticle;
21  import com.eviware.soapui.impl.wadl.inference.schema.particles.ReferenceParticle;
22  import com.eviware.soapui.inferredSchema.AttributeParticleConfig;
23  import com.eviware.soapui.inferredSchema.ElementParticleConfig;
24  import com.eviware.soapui.inferredSchema.ParticleConfig;
25  import com.eviware.soapui.inferredSchema.ReferenceParticleConfig;
26  
27  /***
28   * An attribute or element in the schema. Has a name, a type, and zero or more
29   * attributes.
30   * 
31   * @author Dain Nilsson
32   */
33  public interface Particle
34  {
35  
36  	/***
37  	 * Get the QName of this Particle.
38  	 * 
39  	 * @return The QName describing the particles name and namespace.
40  	 */
41  	public QName getName();
42  
43  	/***
44  	 * Get the ParticleType of the Particle, that is, attribute or element.
45  	 * 
46  	 * @return Returns the type of particle this is.
47  	 */
48  	public ParticleType getPType();
49  
50  	/***
51  	 * Get the Type of the element or attribute that is described by this
52  	 * particle.
53  	 * 
54  	 * @return Returns the Type that corresponds to the particle.
55  	 */
56  	public Type getType();
57  
58  	/***
59  	 * Set the Type of the element or attribute that is described by this
60  	 * particle.
61  	 * 
62  	 * @param type
63  	 *           The Type to set.
64  	 */
65  	public void setType( Type type );
66  
67  	/***
68  	 * Get the attribute value that corresponds to the given name.
69  	 * 
70  	 * @param key
71  	 *           The name of the attribute to get the value for.
72  	 * @return Returns the value for the attribute.
73  	 */
74  	public String getAttribute( String key );
75  
76  	/***
77  	 * Set an attribute.
78  	 * 
79  	 * @param key
80  	 *           The name of the attribute to set.
81  	 * @param value
82  	 *           The value to set.
83  	 */
84  	public void setAttribute( String key, String value );
85  
86  	/***
87  	 * Validates an XML document contained in a given Context object, at the
88  	 * position specified by the cursor contained in same Context object.
89  	 * 
90  	 * @param context
91  	 *           A Context object containing the XML data to be validated, and
92  	 *           other needed contextual variables.
93  	 * @throws XmlException
94  	 *            On unresolvable validation error.
95  	 */
96  	public void validate( Context context ) throws XmlException;
97  
98  	public ParticleConfig save();
99  
100 	/***
101 	 * An enum representing one of two particle types, element or attribute.
102 	 * 
103 	 * @author Dain Nilsson
104 	 */
105 	public enum ParticleType
106 	{
107 		ATTRIBUTE( "attribute" ), ELEMENT( "element" );
108 		private final String name;
109 
110 		ParticleType( String name )
111 		{
112 			this.name = name;
113 		}
114 
115 		public String toString()
116 		{
117 			return name;
118 		}
119 	};
120 
121 	/***
122 	 * A static factory class for creating new instances.
123 	 * 
124 	 * @author Dain Nilsson
125 	 */
126 	public class Factory
127 	{
128 
129 		/***
130 		 * Create a blank new Particle representing an xs:element.
131 		 * 
132 		 * @param schema
133 		 *           The Schema in which the element will live.
134 		 * @param name
135 		 *           A name to give the newly created element.
136 		 * @return Returns the newly created particle.
137 		 */
138 		public static Particle newElementInstance( Schema schema, String name )
139 		{
140 			return new ElementParticle( schema, name );
141 		}
142 
143 		/***
144 		 * Create a blank new Particle representing an xs:attribute.
145 		 * 
146 		 * @param schema
147 		 *           The Schema in which the attribute will live.
148 		 * @param name
149 		 *           A name to give the newly created attribute.
150 		 * @return Returns the newly created particle.
151 		 */
152 		public static Particle newAttributeInstance( Schema schema, String name )
153 		{
154 			return new AttributeParticle( schema, name );
155 		}
156 
157 		/***
158 		 * Create a blank new Particle representing a reference to an element or
159 		 * attribute within a separate namespace.
160 		 * 
161 		 * @param schema
162 		 *           The Schema in which the reference will live.
163 		 * @param reference
164 		 *           The Particle to create a reference to.
165 		 * @return Returns the newly created particle.
166 		 */
167 		public static Particle newReferenceInstance( Schema schema, Particle reference )
168 		{
169 			return new ReferenceParticle( schema, reference );
170 		}
171 
172 		/***
173 		 * Constructs a Particle object using previously saved data.
174 		 * 
175 		 * @param xml
176 		 *           XmlObject to which data has previously been saved.
177 		 * @param schema
178 		 *           The Schema in which to place the newly constructed Particle.
179 		 * @return Returns the newly constructed Particle.
180 		 */
181 		public static Particle parse( ParticleConfig xml, Schema schema )
182 		{
183 			if( xml instanceof AttributeParticleConfig )
184 				return new AttributeParticle( ( AttributeParticleConfig )xml, schema );
185 			if( xml instanceof ElementParticleConfig )
186 				return new ElementParticle( ( ElementParticleConfig )xml, schema );
187 			if( xml instanceof ReferenceParticleConfig )
188 				return new ReferenceParticle( ( ReferenceParticleConfig )xml, schema );
189 			return null;
190 		}
191 	}
192 }