Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SideEffectOperation |
|
| 3.3333333333333335;3,333 |
1 | /* | |
2 | * Copyright (c) 2006 - 2011 Michael Schierl | |
3 | * All rights reserved. | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | package net.sf.jmatchparser.template.engine.operation; | |
19 | ||
20 | import net.sf.jmatchparser.template.engine.ParserState; | |
21 | ||
22 | // these operations have a side effect, | |
23 | // and can only be called if there are no other | |
24 | // queued states | |
25 | public abstract class SideEffectOperation extends Operation { | |
26 | ||
27 | protected SideEffectOperation(String templatePosition) { | |
28 | 21 | super(templatePosition); |
29 | 21 | } |
30 | ||
31 | @Override | |
32 | public final OperationResult execute(ParserState state) { | |
33 | // note that this operation can be run a lot of times before | |
34 | // all states are away, because the other states may fork a lot | |
35 | // before all of them die | |
36 | 30 | if (state.getParser().getSideEffectState() == state) |
37 | 1 | state.getParser().setSideEffectState(null); |
38 | 30 | if (state.getParser().hasQueuedStates()) { |
39 | 3 | if (state.getParser().getSideEffectState() != null) { |
40 | // another operation is waiting as well; so set | |
41 | // waitingSideEffectOperation to this so that | |
42 | // the other one will fail as well | |
43 | 1 | state.getParser().setSideEffectState(state); |
44 | 1 | return OperationResult.FAIL_BLOCKED; |
45 | } else { | |
46 | 2 | state.getParser().setSideEffectState(state); |
47 | // put this state to the end and wait for the others to die | |
48 | 2 | state.setNextInstruction(state.getNextInstruction() - 1); |
49 | 2 | state.getParser().enqueueState(state); |
50 | 2 | return OperationResult.DIE; |
51 | } | |
52 | } else { | |
53 | 27 | if (state.getParser().getSideEffectState() != null) { |
54 | 1 | return OperationResult.FAIL_BLOCKED; |
55 | } | |
56 | // optimize the parser state | |
57 | 26 | state.preprocessXmlActions(); |
58 | 26 | return executeWithSideEffects(state); |
59 | } | |
60 | } | |
61 | ||
62 | public abstract OperationResult executeWithSideEffects(ParserState state); | |
63 | } |