1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wadl.inference.schema;
14
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17 import java.util.Set;
18
19 import javax.xml.namespace.QName;
20
21 import org.apache.xmlbeans.XmlCursor;
22 import org.apache.xmlbeans.XmlException;
23 import org.apache.xmlbeans.XmlObject;
24
25 import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
26 import com.eviware.soapui.inferredSchema.SchemaConfig;
27 import com.eviware.soapui.inferredSchema.SchemaSetConfig;
28
29 /***
30 * Represents a set of namespaces with inferred schemas.
31 *
32 * @author Dain Nilsson
33 */
34 public class SchemaSystem
35 {
36 Map<String, Schema> schemas;
37
38 /***
39 * Constructs a new SchemaSystem object.
40 */
41 public SchemaSystem()
42 {
43 schemas = new LinkedHashMap<String, Schema>();
44 }
45
46 /***
47 * Constructs a SchemaSystem object using previously saved data.
48 *
49 * @param xml
50 * The XmlObject to which data has previously been saved.
51 */
52 public SchemaSystem( SchemaSetConfig xml )
53 {
54 schemas = new LinkedHashMap<String, Schema>();
55 for( SchemaConfig item : xml.getSchemaList() )
56 schemas.put( item.getNamespace(), new Schema( item, this ) );
57 }
58
59 /***
60 * Saves the SchemaSystem to an XmlObject.
61 *
62 * @param xml
63 * A blank XmlObject to save to.
64 */
65 public void save( SchemaSetConfig xml )
66 {
67 for( Schema item : schemas.values() )
68 item.save( xml.addNewSchema() );
69 }
70
71 /***
72 * Create a blank new Schema under this SchemaSystem for a given namespace.
73 *
74 * @param namespace
75 * The namespace for which to create a Schema.
76 * @return The newly created Schema.
77 */
78 public Schema newSchema( String namespace )
79 {
80 Schema schema = new Schema( namespace, this );
81 schemas.put( namespace, schema );
82 return schema;
83 }
84
85 /***
86 * Returns the matching Schema for the given namespace.
87 *
88 * @param namespace
89 * A namespace that already exists within the SchemaSystem.
90 * @return Returns the Schema corresponding to the given namespace if one
91 * exists. Otherwise returns null.
92 */
93 public Schema getSchemaForNamespace( String namespace )
94 {
95 return schemas.get( namespace );
96 }
97
98 /***
99 * Get an existing Type by its QName.
100 *
101 * @param qname
102 * A QName containing the namespace URI of the schema in which the
103 * Type exists, and also the name of the type.
104 * @return Returns the Type, if one is found. Otherwise returns null.
105 */
106 public Type getType( QName qname )
107 {
108 return getSchemaForNamespace( qname.getNamespaceURI() ).getType( qname.getLocalPart() );
109 }
110
111 /***
112 * Validate an XmlObject against the contained inferred schemas. Upon
113 * validation errors, the ConflictHandler is used to determine if a schema
114 * should be adjusted, or if validation should fail.
115 *
116 * @param xmlo
117 * An XmlObject containing the document to be validated.
118 * @param handler
119 * A ConflictHandler to use on validation errors.
120 * @throws XmlException
121 * On unresolvable validation error.
122 */
123 public void validate( XmlObject xmlo, ConflictHandler handler ) throws XmlException
124 {
125 XmlCursor cursor = xmlo.newCursor();
126 cursor.toFirstChild();
127 Schema s = getSchemaForNamespace( cursor.getName().getNamespaceURI() );
128 boolean created = false;
129 if( s == null )
130 {
131 s = newSchema( cursor.getName().getNamespaceURI() );
132 created = true;
133 }
134 Context context = new Context( this, handler, cursor );
135 try
136 {
137 s.validate( context );
138 }
139 catch( XmlException e )
140 {
141 if( created )
142 schemas.remove( s.getNamespace() );
143 throw e;
144 }
145 }
146
147 /***
148 * Get a list of contained namespaces.
149 *
150 * @return Returns the contained namespaces, as a Set.
151 */
152 public Set<String> getNamespaces()
153 {
154 return schemas.keySet();
155 }
156
157 public void deleteNamespace( String ns )
158 {
159 schemas.remove( ns );
160 }
161
162 }