Coverage Report - net.sf.jmatchparser.template.engine.ParserState
 
Classes in this File Line Coverage Branch Coverage Complexity
ParserState
92%
85/92
74%
26/35
2,227
 
 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;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.BitSet;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.w3c.dom.Element;
 27  
 
 28  
 public class ParserState {
 29  
         private ParserImpl parser;
 30  
         private int nextInstruction;
 31  
         private int offset;
 32  808
         private Map<String, String> locals = new HashMap<String, String>();
 33  808
         private List<String[]> xmlActions = new ArrayList<String[]>();
 34  
         private final List<String[]> preprocessedXmlActions;
 35  
         private final BitSet coveredOperations;
 36  
         private CallStackFrame callStack;
 37  
 
 38  262
         public ParserState(ParserImpl parser) {
 39  262
                 this.parser = parser;
 40  262
                 nextInstruction = 0;
 41  262
                 offset = 0;
 42  262
                 preprocessedXmlActions = new ArrayList<String[]>();
 43  262
                 if (parser.getTemplate().getCoveredStatementsTracingSize() != 0) {
 44  1
                         coveredOperations = new BitSet(parser.getTemplate().getCoveredStatementsTracingSize());
 45  
                 } else {
 46  261
                         coveredOperations = null;
 47  
                 }
 48  262
         }
 49  
 
 50  546
         public ParserState(ParserState toCopy, int nextInstruction) {
 51  546
                 xmlActions = new ArrayList<String[]>(toCopy.xmlActions);
 52  546
                 preprocessedXmlActions = toCopy.preprocessedXmlActions; // we can share
 53  
                                                                                                                                 // them
 54  546
                 parser = toCopy.parser;
 55  546
                 this.nextInstruction = nextInstruction;
 56  546
                 offset = toCopy.offset;
 57  546
                 locals.putAll(toCopy.locals);
 58  546
                 callStack = toCopy.callStack;
 59  546
                 if (toCopy.coveredOperations == null) {
 60  545
                         coveredOperations = null;
 61  
                 } else {
 62  1
                         coveredOperations = (BitSet) toCopy.coveredOperations.clone();
 63  
                 }
 64  546
         }
 65  
 
 66  
         public void applyXMLActions(Element target) {
 67  198
                 checkNoQueuedStates();
 68  198
                 if (parser != null && parser.getBuffer().length() > 0)
 69  0
                         throw new IllegalStateException();
 70  198
                 Element current = target;
 71  198
                 preprocessXmlActions();
 72  198
                 for (String[] elem : preprocessedXmlActions) {
 73  48
                         switch (elem[0].charAt(0)) {
 74  
                         case '/':
 75  15
                                 current = (Element) current.getParentNode();
 76  15
                                 break;
 77  
                         case '<':
 78  15
                                 Element e = current.getOwnerDocument().createElement(elem[1]);
 79  15
                                 current.appendChild(e);
 80  15
                                 current = e;
 81  15
                                 break;
 82  
                         case 'T':
 83  14
                                 current.appendChild(current.getOwnerDocument().createTextNode(elem[1]));
 84  14
                                 break;
 85  
                         case '@':
 86  4
                                 current.setAttribute(elem[1], elem[2]);
 87  4
                                 break;
 88  
                         default:
 89  0
                                 throw new RuntimeException();
 90  
                         }
 91  
                 }
 92  198
                 if (current != target)
 93  0
                         throw new IllegalStateException("Incorrect nesting of XML actions");
 94  198
                 xmlActions.clear();
 95  198
         }
 96  
 
 97  
         public void appendXmlAction(String... action) {
 98  48
                 xmlActions.add(action);
 99  48
         }
 100  
 
 101  
         public String getLocal(String varname) {
 102  723
                 return locals.get(varname);
 103  
         }
 104  
 
 105  
         public Map<String, String> getLocals() {
 106  68
                 return new HashMap<String, String>(locals);
 107  
         }
 108  
 
 109  
         public void setLocal(String varname, String value) {
 110  912
                 locals.put(varname, value);
 111  912
                 if (value == null)
 112  7
                         locals.remove(varname);
 113  912
         }
 114  
 
 115  
         public int getNextInstructionAndIncrement() {
 116  3949
                 if (coveredOperations != null)
 117  8
                         coveredOperations.set(nextInstruction);
 118  3949
                 return nextInstruction++;
 119  
         }
 120  
 
 121  
         public int getNextInstruction() {
 122  2
                 return nextInstruction;
 123  
         }
 124  
 
 125  
         public void setNextInstruction(int nextInstruction) {
 126  1094
                 this.nextInstruction = nextInstruction;
 127  1094
         }
 128  
 
 129  
         public int getOffset() {
 130  1426
                 return offset;
 131  
         }
 132  
 
 133  
         public ParserImpl getParser() {
 134  867
                 return parser;
 135  
         }
 136  
 
 137  
         public CharSequence getRest() {
 138  1070
                 return parser.getFile().subSequence(offset, parser.getFile().length());
 139  
         }
 140  
 
 141  
         public CharSequence getFirstLine() {
 142  1053
                 CharSequence rest = getRest();
 143  5681
                 for (int i = 0; i < rest.length(); i++) {
 144  5678
                         if (rest.charAt(i) == '\n') {
 145  1050
                                 return rest.subSequence(0, i);
 146  
                         }
 147  
                 }
 148  3
                 return null;
 149  
         }
 150  
 
 151  
         public void discardCharacters(int count) {
 152  666
                 offset += count;
 153  666
         }
 154  
 
 155  
         public void write(String text) {
 156  24
                 parser.writeBuffer(text);
 157  24
         }
 158  
 
 159  
         public void startNextPass() {
 160  10
                 checkNoQueuedStates();
 161  10
                 if (xmlActions.size() != 0)
 162  0
                         throw new IllegalStateException();
 163  10
                 if (preprocessedXmlActions.size() != 0)
 164  0
                         throw new IllegalStateException();
 165  10
                 if (offset != parser.getFile().length())
 166  0
                         throw new IllegalStateException();
 167  10
                 offset = 0;
 168  10
                 parser.setFile(this, parser.getBuffer());
 169  10
         }
 170  
 
 171  
         private void checkNoQueuedStates() {
 172  432
                 if (parser != null && parser.hasQueuedStates())
 173  0
                         throw new IllegalStateException();
 174  432
         }
 175  
 
 176  
         public void preprocessXmlActions() {
 177  224
                 checkNoQueuedStates();
 178  224
                 preprocessedXmlActions.addAll(xmlActions);
 179  224
                 xmlActions.clear();
 180  224
         }
 181  
 
 182  
         public BitSet getCoveredOperations() {
 183  165
                 return coveredOperations;
 184  
         }
 185  
 
 186  
         public CallStackFrame getCallStack() {
 187  698
                 return callStack;
 188  
         }
 189  
 
 190  
         public void setCallStack(CallStackFrame callStack) {
 191  534
                 this.callStack = callStack;
 192  534
         }
 193  
 }