Coverage Report - net.sf.jmatchparser.template.engine.template.MatchTemplateImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MatchTemplateImpl
95%
41/43
92%
13/14
1,588
 
 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.template;
 19  
 
 20  
 import java.io.BufferedReader;
 21  
 import java.io.IOException;
 22  
 import java.util.ArrayList;
 23  
 import java.util.BitSet;
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import net.sf.jmatchparser.template.ChainedFormatter;
 28  
 import net.sf.jmatchparser.template.Formatter;
 29  
 import net.sf.jmatchparser.template.MatchTemplate.MatchTemplateResolver;
 30  
 import net.sf.jmatchparser.template.engine.formatter.CSVFormatter;
 31  
 import net.sf.jmatchparser.template.engine.formatter.JTidyFormatter;
 32  
 import net.sf.jmatchparser.template.engine.formatter.TrimFormatter;
 33  
 import net.sf.jmatchparser.template.engine.formatter.XMLFormatter;
 34  
 import net.sf.jmatchparser.template.engine.operation.Label;
 35  
 import net.sf.jmatchparser.template.engine.operation.NopOperation;
 36  
 import net.sf.jmatchparser.template.engine.operation.Operation;
 37  
 
 38  
 public abstract class MatchTemplateImpl {
 39  
 
 40  
         private String currentTemplatePosition;
 41  203
         private final List<Operation> operations = new ArrayList<Operation>();
 42  203
         private BitSet coveredOperations = null;
 43  203
         private final Map<String, Formatter> formatters = new HashMap<String, Formatter>();
 44  203
         private final Map<String, Label> defLabels = new HashMap<String, Label>();
 45  203
         protected final List<String> includedTemplateFileNames = new ArrayList<String>();
 46  
 
 47  203
         protected MatchTemplateImpl(Formatter[] formatters) throws IOException {
 48  203
                 addFormatters(new Formatter[] {
 49  
                                 new TrimFormatter(false),
 50  
                                 new TrimFormatter(true),
 51  
                                 new XMLFormatter(),
 52  
                                 new JTidyFormatter(false),
 53  
                                 new JTidyFormatter(true),
 54  
                                 new CSVFormatter() });
 55  203
                 addFormatters(new Formatter[] { new ChainedFormatter("html", getFormatter("jtidy+cleanspace"), getFormatter("xml")) });
 56  203
                 addFormatters(formatters);
 57  203
         }
 58  
 
 59  
         private void addFormatters(Formatter[] fs) {
 60  2030
                 for (Formatter f : fs) {
 61  1421
                         formatters.put(f.getName(), f);
 62  
                 }
 63  609
         }
 64  
 
 65  
         public Formatter getFormatter(String name) {
 66  416
                 return formatters.get(name.toLowerCase().trim());
 67  
         }
 68  
 
 69  
         public int getNextOperationIndex() {
 70  653
                 return operations.size();
 71  
         }
 72  
 
 73  
         public void appendOperation(Operation op) {
 74  1487
                 operations.add(op);
 75  1487
         }
 76  
 
 77  
         public Operation getOperation(int ip) {
 78  3949
                 return operations.get(ip);
 79  
         }
 80  
 
 81  
         public void enableCoveredStatementsTracing() {
 82  1
                 coveredOperations = new BitSet(operations.size());
 83  1
         }
 84  
 
 85  
         public int getCoveredStatementsTracingSize() {
 86  263
                 return coveredOperations == null ? 0 : operations.size();
 87  
         }
 88  
 
 89  
         public void addCoveredOperations(BitSet operations) {
 90  1
                 coveredOperations.or(operations);
 91  1
         }
 92  
 
 93  
         public List<String> getUncoveredStatements() {
 94  2
                 if (coveredOperations == null)
 95  0
                         throw new IllegalStateException("Covered statements tracing is not enabled");
 96  2
                 List<String> result = new ArrayList<String>();
 97  18
                 for (int i = 0; i < operations.size(); i++) {
 98  16
                         if (!coveredOperations.get(i)) {
 99  8
                                 String tpos = operations.get(i).getTemplatePosition();
 100  8
                                 if (!result.contains(tpos))
 101  6
                                         result.add(tpos);
 102  
                         }
 103  
                 }
 104  2
                 return result;
 105  
         }
 106  
         
 107  
         public List<String> getIncludedTemplateFileNames() {
 108  0
                 return includedTemplateFileNames;
 109  
         }
 110  
 
 111  
         public void nopOperation(int position) {
 112  128
                 Operation op = operations.get(position);
 113  128
                 operations.set(position, new NopOperation(op.getTemplatePosition()));
 114  128
         }
 115  
 
 116  
         public static MatchTemplateImpl createTemplateImpl(String matchLanguage, MatchTemplateResolver resolver, String templateName, BufferedReader br, Formatter[] formatters) throws IOException {
 117  130
                 if (matchLanguage.equals("plain")) {
 118  129
                         return new PlainMatchTemplateImpl(resolver, templateName, br, formatters);
 119  
                 } else {
 120  1
                         throw new IllegalArgumentException("Unsupported match template language!");
 121  
                 }
 122  
         }
 123  
 
 124  
         public String getCurrentTemplatePosition() {
 125  1614
                 return currentTemplatePosition;
 126  
         }
 127  
 
 128  
         protected void setCurrentTemplatePosition(String currentTemplatePosition) {
 129  1193
                 this.currentTemplatePosition = currentTemplatePosition;
 130  1193
         }
 131  
 
 132  
         public abstract String[] getSpecialTags();
 133  
 
 134  
         public Map<String, Label> getDefLabels() {
 135  16
                 return defLabels;
 136  
         }
 137  
 }