1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package tsukuba_bunko.peko.scenario; |
20 |
|
|
21 |
|
import java.io.Serializable; |
22 |
|
|
23 |
|
import java.util.List; |
24 |
|
import java.util.Map; |
25 |
|
import java.util.Set; |
26 |
|
|
27 |
|
import tsukuba_bunko.peko.Logger; |
28 |
|
|
29 |
|
import tsukuba_bunko.peko.session.Session; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public class SceneContext implements Serializable { |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
private static final long serialVersionUID = 1698890995605002291L; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
0 |
protected String _sceneName = null; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
0 |
protected String _sceneTitle = null; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
0 |
protected Map _properties = null; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
0 |
protected NextSceneMapping _nextSceneMapping = null; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
0 |
transient protected Node _current = null; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
0 |
protected Node _lastCommittedNode = null; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
0 |
protected Set _sceneScopeFlags = null; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
0 |
transient protected Session _session = null; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
0 |
transient protected SceneProcessor _processor = null; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
public SceneContext( String sceneName, Session session, SceneProcessor processor ) |
97 |
0 |
{ |
98 |
0 |
_sceneName = sceneName; |
99 |
0 |
_session = session; |
100 |
0 |
_processor = processor; |
101 |
|
|
102 |
0 |
SceneContext previous = session.getSceneContext(); |
103 |
0 |
if( previous != null ) { |
104 |
0 |
_lastCommittedNode = previous.getLastCommittedNode(); |
105 |
0 |
_properties = previous._properties; |
106 |
0 |
_nextSceneMapping = previous.getNextSceneMapping(); |
107 |
0 |
_sceneScopeFlags = previous._sceneScopeFlags; |
108 |
0 |
} |
109 |
|
else { |
110 |
0 |
_nextSceneMapping = new NextSceneMapping(); |
111 |
0 |
_sceneScopeFlags = new java.util.HashSet(); |
112 |
|
} |
113 |
0 |
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
public String getSceneName() |
121 |
|
{ |
122 |
0 |
return _sceneName; |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
public void setSceneTitle( String title ) |
130 |
|
{ |
131 |
0 |
_sceneTitle = title; |
132 |
0 |
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
public String getSceneTitle() |
139 |
|
{ |
140 |
0 |
return _sceneTitle; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
public Session getSession() |
148 |
|
{ |
149 |
0 |
return _session; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
public SceneProcessor getSceneProcessor() |
157 |
|
{ |
158 |
0 |
return _processor; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
public void setProperty( String name, String value ) |
167 |
|
{ |
168 |
0 |
if( _properties == null ) { |
169 |
0 |
_properties = new java.util.HashMap( 17 ); |
170 |
|
} |
171 |
0 |
_properties.put( name, value ); |
172 |
0 |
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
public String getProperty( String name ) |
180 |
|
{ |
181 |
0 |
if( _properties == null ) { |
182 |
0 |
return null; |
183 |
|
} |
184 |
|
else { |
185 |
0 |
return (String)_properties.get( name ); |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
public void setNextSceneMapping( NextSceneMapping mapping ) |
194 |
|
{ |
195 |
0 |
_nextSceneMapping = mapping; |
196 |
0 |
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
public NextSceneMapping getNextSceneMapping() |
203 |
|
{ |
204 |
0 |
return _nextSceneMapping; |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
public String getNextSceneName() |
212 |
|
{ |
213 |
0 |
return _nextSceneMapping.getNextScene( this ); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
public void pushNode( String nodeName ) |
220 |
|
{ |
221 |
0 |
if( _current == null ) { |
222 |
0 |
_current = new Node( null, nodeName.intern(), 1 ); |
223 |
0 |
} |
224 |
|
else { |
225 |
0 |
int position = _current.countSibling(nodeName) + 1; |
226 |
0 |
Node child = new Node( _current, nodeName.intern(), position ); |
227 |
0 |
_current.addChildNode( child ); |
228 |
0 |
_current = child; |
229 |
|
} |
230 |
0 |
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
public void popNode() |
236 |
|
{ |
237 |
0 |
if( _current == null ) { |
238 |
0 |
Logger.error( "[scenario] BUG! invalid state of SceneLog." ); |
239 |
0 |
} |
240 |
|
else { |
241 |
0 |
_current.removeChildren(); |
242 |
0 |
_current = _current.getParentNode(); |
243 |
|
} |
244 |
0 |
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
public void saveCurrentNode() |
250 |
|
{ |
251 |
0 |
_lastCommittedNode = _current; |
252 |
0 |
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
public Node getLastCommittedNode() |
259 |
|
{ |
260 |
0 |
return _lastCommittedNode; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
public boolean isCurrentNode( Node node ) |
269 |
|
{ |
270 |
0 |
if( node == _current ) { |
271 |
0 |
return true; |
272 |
|
} |
273 |
0 |
else if( _current != null ) { |
274 |
0 |
return _current.equals(node); |
275 |
|
} |
276 |
|
else { |
277 |
0 |
return false; |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
public Node getCurrentNode() |
286 |
|
{ |
287 |
0 |
Node node = _current; |
288 |
0 |
if( node == null ) { |
289 |
0 |
return null; |
290 |
|
} |
291 |
|
else { |
292 |
0 |
Node copy = new Node( null, node._nodeName, node._position ); |
293 |
0 |
Node temp = copy; |
294 |
0 |
while( (node = node.getParentNode()) != null ) { |
295 |
0 |
temp._parent = new Node( null, node.getNodeName(), node.getPosition() ); |
296 |
0 |
temp = temp.getParentNode(); |
297 |
0 |
} |
298 |
0 |
return copy; |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
public String getCurrentPath() |
307 |
|
{ |
308 |
0 |
if( _current != null ) { |
309 |
0 |
return _current.getPath(); |
310 |
|
} |
311 |
|
else { |
312 |
0 |
return "/"; |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
public void declareFlag( String flagID, FlagScope scope ) |
324 |
|
{ |
325 |
0 |
if( flagID == null ) { |
326 |
0 |
throw new IllegalArgumentException(); |
327 |
|
} |
328 |
|
|
329 |
0 |
if( scope == FlagScope.SCENE ) { |
330 |
0 |
_sceneScopeFlags.add( flagID ); |
331 |
0 |
} |
332 |
0 |
else if( scope == FlagScope.SESSION ) { |
333 |
0 |
_session.declareSessionFlag( flagID ); |
334 |
0 |
} |
335 |
0 |
else if( scope == FlagScope.SYSTEM ) { |
336 |
0 |
_session.declareSystemFlag( flagID ); |
337 |
|
} |
338 |
0 |
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
public void undeclareFlag( String flagID, FlagScope scope ) |
347 |
|
{ |
348 |
0 |
if( flagID == null ) { |
349 |
0 |
throw new IllegalArgumentException(); |
350 |
|
} |
351 |
|
|
352 |
0 |
if( scope == FlagScope.SCENE ) { |
353 |
0 |
_sceneScopeFlags.remove( flagID ); |
354 |
0 |
} |
355 |
0 |
else if( scope == FlagScope.SESSION ) { |
356 |
0 |
_session.undeclareSessionFlag( flagID ); |
357 |
0 |
} |
358 |
0 |
else if( scope == FlagScope.SYSTEM ) { |
359 |
0 |
_session.undeclareSystemFlag( flagID ); |
360 |
|
} |
361 |
0 |
} |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
public boolean isDeclaredFlag( String flagID ) |
368 |
|
{ |
369 |
0 |
if( _sceneScopeFlags.contains(flagID) ) { |
370 |
0 |
return true; |
371 |
|
} |
372 |
0 |
else if( _session.isDeclaredSessionFlag(flagID) ) { |
373 |
0 |
return true; |
374 |
|
} |
375 |
0 |
else if( _session.isDeclaredSystemFlag(flagID) ) { |
376 |
0 |
return true; |
377 |
|
} |
378 |
|
else { |
379 |
0 |
return false; |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
public boolean isDeclaredFlag( String flagID, FlagScope scope ) |
390 |
|
{ |
391 |
0 |
if( scope == FlagScope.SCENE ) { |
392 |
0 |
return _sceneScopeFlags.contains( flagID ); |
393 |
|
} |
394 |
0 |
else if( scope == FlagScope.SESSION ) { |
395 |
0 |
return _session.isDeclaredSessionFlag( flagID ); |
396 |
|
} |
397 |
0 |
else if( scope == FlagScope.SYSTEM ) { |
398 |
0 |
return _session.isDeclaredSystemFlag( flagID ); |
399 |
|
} |
400 |
|
else { |
401 |
0 |
return false; |
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
public static class Node implements Serializable { |
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
private static final long serialVersionUID = 7590190847826566241L; |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
protected Node _parent = null; |
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
protected String _nodeName = null; |
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
protected int _position = 0; |
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
protected List _children = null; |
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
public Node( Node parent, String nodeName, int position ) |
443 |
|
{ |
444 |
|
_parent = parent; |
445 |
|
_nodeName = nodeName.intern(); |
446 |
|
_position = position; |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
public Node getParentNode() |
454 |
|
{ |
455 |
|
return _parent; |
456 |
|
} |
457 |
|
|
458 |
|
|
459 |
|
|
460 |
|
|
461 |
|
|
462 |
|
public String getNodeName() |
463 |
|
{ |
464 |
|
return _nodeName; |
465 |
|
} |
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
public int getPosition() |
472 |
|
{ |
473 |
|
return _position; |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
public void addChildNode( Node child ) |
482 |
|
{ |
483 |
|
if( _children == null ) { |
484 |
|
_children = new java.util.ArrayList(); |
485 |
|
} |
486 |
|
_children.add( child ); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
|
492 |
|
|
493 |
|
|
494 |
|
public int countSibling( String nodeName ) |
495 |
|
{ |
496 |
|
if( _children == null ) { |
497 |
|
return 0; |
498 |
|
} |
499 |
|
else { |
500 |
|
String symbol = nodeName.intern(); |
501 |
|
List children = _children; |
502 |
|
int size = children.size(); |
503 |
|
Node node = null; |
504 |
|
int count = 0; |
505 |
|
for( int i = 0; i < size; ++i ) { |
506 |
|
node = (Node)children.get( i ); |
507 |
|
if( node._nodeName == symbol ) { |
508 |
|
count++; |
509 |
|
} |
510 |
|
} |
511 |
|
return count; |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
|
|
517 |
|
|
518 |
|
public void removeChildren() |
519 |
|
{ |
520 |
|
if( _children != null ) { |
521 |
|
List children = _children; |
522 |
|
int size = children.size(); |
523 |
|
for( int i = 0; i < size; ++i ) { |
524 |
|
((Node)children.get(i)).removeChildren(); |
525 |
|
} |
526 |
|
children.clear(); |
527 |
|
_children = null; |
528 |
|
} |
529 |
|
} |
530 |
|
|
531 |
|
|
532 |
|
|
533 |
|
|
534 |
|
|
535 |
|
public String getPath() |
536 |
|
{ |
537 |
|
StringBuffer sink = new StringBuffer(); |
538 |
|
Node node = this; |
539 |
|
while( node != null ) { |
540 |
|
sink.insert( 0, node ); |
541 |
|
node = node.getParentNode(); |
542 |
|
} |
543 |
|
return new String(sink); |
544 |
|
} |
545 |
|
|
546 |
|
public boolean equals( Object obj ) |
547 |
|
{ |
548 |
|
if( obj instanceof Node ) { |
549 |
|
Node target = (Node)obj; |
550 |
|
Node node = this; |
551 |
|
while( (node != null) && (target != class="keyword">null) ) { |
552 |
|
if( !node._nodeName.equals(target._nodeName) || (node._position != target._position) ) { |
553 |
|
return false; |
554 |
|
} |
555 |
|
node = node._parent; |
556 |
|
target = target._parent; |
557 |
|
} |
558 |
|
return ((node == null) && (target == class="keyword">null)); |
559 |
|
} |
560 |
|
else { |
561 |
|
return false; |
562 |
|
} |
563 |
|
} |
564 |
|
|
565 |
|
public String toString() |
566 |
|
{ |
567 |
|
return "/" + _nodeName + "[" + _position + "]"; |
568 |
|
} |
569 |
|
} |
570 |
|
} |