Coverage Report - net.sf.jmatchparser.template.engine.operation.MatchXMLOperation
 
Classes in this File Line Coverage Branch Coverage Complexity
MatchXMLOperation
84%
33/39
54%
18/33
12
 
 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 java.util.ArrayList;
 21  
 import java.util.List;
 22  
 
 23  
 import net.sf.jmatchparser.template.engine.ParserState;
 24  
 
 25  
 public class MatchXMLOperation extends Operation {
 26  
 
 27  
         private final boolean checkOnly;
 28  
 
 29  
         public MatchXMLOperation(String templatePosition, boolean checkOnly) {
 30  6
                 super(templatePosition);
 31  6
                 this.checkOnly = checkOnly;
 32  6
         }
 33  
 
 34  
         @Override
 35  
         public OperationResult execute(ParserState state) {
 36  6
                 boolean success = true;
 37  6
                 int consumed = 0;
 38  6
                 CharSequence rest = state.getRest();
 39  6
                 if (rest == null) {
 40  0
                         success = false;
 41  
                 } else {
 42  6
                         List<String> stack = new ArrayList<String>();
 43  13
                         for (String line : rest.toString().split("\n")) {
 44  13
                                 boolean continuedLine = false;
 45  13
                                 consumed += line.length() + 1;
 46  13
                                 switch (line.charAt(0)) {
 47  
                                 case '<':
 48  6
                                         if (!line.endsWith(">"))
 49  0
                                                 throw new RuntimeException("Invalid XML line: " + line);
 50  6
                                         if (line.startsWith("</")) {
 51  
                                                 // end tag must match start tag
 52  3
                                                 String tagName = line.substring(2, line.length() - 1);
 53  3
                                                 if (stack.size() == 0) {
 54  0
                                                         success = false;
 55  
                                                 } else {
 56  3
                                                         String lastTag = stack.remove(stack.size() - 1);
 57  3
                                                         if (!lastTag.equals(tagName)) {
 58  0
                                                                 success = false;
 59  
                                                         }
 60  
                                                 }
 61  3
                                         } else {
 62  
                                                 // add start tag
 63  3
                                                 int pos = line.indexOf(" ");
 64  3
                                                 if (pos == -1)
 65  3
                                                         pos = line.length() - 1;
 66  3
                                                 stack.add(line.substring(1, pos));
 67  
                                         }
 68  3
                                         break;
 69  
                                 case '+':
 70  2
                                         continuedLine = true;
 71  2
                                         break;
 72  
                                 case '!': // comment
 73  
                                 case '[': // CDATA
 74  
                                 case '-': // text
 75  
                                 case '?': // processing instruction
 76  5
                                         break;
 77  
                                 default:
 78  0
                                         throw new RuntimeException("Invalid XML line: " + line);
 79  
                                 }
 80  13
                                 if (!success || (!continuedLine && stack.size() == 0)) {
 81  6
                                         break;
 82  
                                 }
 83  
                         }
 84  
                 }
 85  
 
 86  6
                 if (success || !checkOnly) {
 87  6
                         state.discardCharacters(consumed);
 88  
                 }
 89  6
                 if (success)
 90  6
                         return OperationResult.CONTINUE;
 91  
                 else
 92  0
                         return OperationResult.FAIL;
 93  
         }
 94  
 }