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.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.apache.xmlbeans.XmlCursor;
21
22 import com.eviware.soapui.impl.wadl.inference.ConflictHandler;
23
24 /***
25 * An object that holds information about a validation in-progress, such as the
26 * cursor of the content to be validated., etc.
27 *
28 * @author Dain Nilsson
29 */
30 public class Context
31 {
32 private ConflictHandler handler;
33 private XmlCursor cursor;
34 private SchemaSystem system;
35 private List<String> path;
36 private List<List<String>> stack;
37 private Map<String, String> attributes;
38
39 /***
40 * Creates a new Context object.
41 *
42 * @param system
43 * The SchemaSystem holding the namespaces to be used for
44 * validation.
45 * @param handler
46 * The ConflictHandler to use whenever a validation error occurs to
47 * decide upon which action to take.
48 * @param cursor
49 * An XmlCursor pointing to the beginning of the XML content to
50 * validate.
51 */
52 public Context( SchemaSystem system, ConflictHandler handler, XmlCursor cursor )
53 {
54 this.system = system;
55 this.handler = handler;
56 this.cursor = cursor;
57 path = new ArrayList<String>();
58 stack = new ArrayList<List<String>>();
59 attributes = new HashMap<String, String>();
60 }
61
62 /***
63 * Getter for the contained ConflictHandler.
64 *
65 * @return Returns the ConflictHandler used for validation.
66 */
67 public ConflictHandler getHandler()
68 {
69 return handler;
70 }
71
72 /***
73 * Get a name to use for a Complex Type at the current path. Names are
74 * derived from paths, using the locality from the Settings class.
75 *
76 * @return A name to be used for the Complex Type at the current location.
77 */
78 public String getName()
79 {
80 String path = getPath().replace( "/", "_" );
81 int parts = Settings.locality;
82 int i = path.length();
83 while( parts > 0 && i > 0 )
84 {
85 i-- ;
86 if( path.charAt( i ) == '_' )
87 parts-- ;
88 }
89 if( parts > 0 )
90 return path;
91 return path.substring( i + 1 );
92 }
93
94 /***
95 * Get a stored attribute.
96 *
97 * @param key
98 * The key of the attribute to get.
99 * @return Returns the value of the attribute, if it exists. An empty string
100 * is returned if not.
101 */
102 public String getAttribute( String key )
103 {
104 if( attributes.containsKey( key ) )
105 return attributes.get( key );
106 return "";
107 }
108
109 /***
110 * Store an attribute.
111 *
112 * @param key
113 * The name of the attribute to store.
114 * @param value
115 * The value to store.
116 */
117 public void putAttribute( String key, String value )
118 {
119 attributes.put( key, value );
120 }
121
122 /***
123 * Delete a stored attribute.
124 *
125 * @param key
126 * The name of the attribute to delete.
127 */
128 public void clearAttribute( String key )
129 {
130 attributes.remove( key );
131 }
132
133 /***
134 * Get the path currently at.
135 *
136 * @return Returns the current path, elements are separated by slash.
137 */
138 public String getPath()
139 {
140 StringBuilder s = new StringBuilder();
141 for( String item : path )
142 s.append( "/" + item );
143 return s.toString();
144 }
145
146 /***
147 * Push the current path to an internal stack, and start with an empty path.
148 */
149 public void pushPath()
150 {
151 stack.add( path );
152 path = new ArrayList<String>();
153 }
154
155 /***
156 * Pop a previously pushed path from the internal stack, overwriting whatever
157 * is currently in the path.
158 */
159 public void popPath()
160 {
161 int last = stack.size() - 1;
162 if( last >= 0 )
163 {
164 path = stack.get( last );
165 stack.remove( last );
166 }
167 }
168
169 /***
170 * Append an element to the end of the current path.
171 *
172 * @param item
173 * The name of the element to trascend into.
174 */
175 public void cd( String item )
176 {
177 path.add( item );
178 }
179
180 /***
181 * Move up one level, removing the last element from the path.
182 */
183 public void up()
184 {
185 if( path.size() > 0 )
186 path.remove( path.size() - 1 );
187 }
188
189 /***
190 * Get the internal cursor pointing to the current position of the XML
191 * content to be validated.
192 *
193 * @return Returns an XmlCursor.
194 */
195 public XmlCursor getCursor()
196 {
197 return cursor;
198 }
199
200 /***
201 * Get the SchemaSystem currently used for validation.
202 *
203 * @return Returns a SchemaSystem.
204 */
205 public SchemaSystem getSchemaSystem()
206 {
207 return system;
208 }
209 }