Coverage Report - net.sf.jmatchparser.template.MatchTemplate
 
Classes in this File Line Coverage Branch Coverage Complexity
MatchTemplate
87%
14/16
N/A
1
MatchTemplate$FileMatchTemplateResolver
100%
5/5
N/A
1
MatchTemplate$MatchTemplateResolver
N/A
N/A
1
MatchTemplate$ResourceMatchTemplateResolver
100%
6/6
N/A
1
 
 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;
 19  
 
 20  
 import java.io.BufferedReader;
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStreamReader;
 25  
 import java.util.List;
 26  
 import net.sf.jmatchparser.template.engine.template.MatchTemplateImpl;
 27  
 
 28  
 /**
 29  
  * A template, usually loaded from a file, that is used by the {@link Parser}
 30  
  * for matching.
 31  
  */
 32  
 public class MatchTemplate {
 33  
 
 34  
         private final MatchTemplateImpl impl;
 35  
 
 36  
         /**
 37  
          * Load a match template from a file.
 38  
          * 
 39  
          * @param filename
 40  
          *            Name of the file
 41  
          * @param encoding
 42  
          *            File encoding
 43  
          * @param formatters
 44  
          *            Additional formatters to use (optional)
 45  
          */
 46  
         public MatchTemplate(String filename, String encoding, Formatter... formatters) throws IOException {
 47  1
                 this(new File(filename), encoding, formatters);
 48  1
         }
 49  
 
 50  
         /**
 51  
          * Load a match template from a file.
 52  
          * 
 53  
          * @param file
 54  
          *            The file
 55  
          * @param encoding
 56  
          *            File encoding
 57  
          * @param formatters
 58  
          *            Additional formatters to use (optional)
 59  
          */
 60  
         public MatchTemplate(File file, String encoding, Formatter... formatters) throws IOException {
 61  1
                 this(new FileMatchTemplateResolver(file.getAbsoluteFile().getParentFile(), encoding), file.getName(), null, formatters);
 62  1
         }
 63  
 
 64  
         /**
 65  
          * Load a match template from a resource (a "file" on the class path).
 66  
          * 
 67  
          * @param clazz
 68  
          *            Class to use as a base for finding the resource
 69  
          * @param resourceName
 70  
          *            Name of the resource
 71  
          * @param encoding
 72  
          *            File encoding
 73  
          * @param formatters
 74  
          *            Additional formatters to use (optional)
 75  
          */
 76  
         public MatchTemplate(Class<?> clazz, String resourceName, String encoding, Formatter... formatters) throws IOException {
 77  1
                 this(new ResourceMatchTemplateResolver(clazz, resourceName.substring(0, resourceName.lastIndexOf("/") + 1), encoding), resourceName.substring(resourceName.lastIndexOf("/") + 1), null, formatters);
 78  0
         }
 79  
 
 80  
         /**
 81  
          * Load a match template from a custom reader.
 82  
          * 
 83  
          * @param resolver
 84  
          *            Match template resolver for resolving additional template
 85  
          *            names (<code>$INCLUDE</code>), optional
 86  
          * @param templateName
 87  
          *            Name of the template
 88  
          * @param br
 89  
          *            Reader to read the template from
 90  
          * @param formatters
 91  
          *            Additional formatters to use (optional)
 92  
          */
 93  
         public MatchTemplate(MatchTemplateResolver resolver, String templateName, BufferedReader br, Formatter... formatters) throws IOException {
 94  8
                 this("plain", resolver, templateName, br, formatters);
 95  7
         }
 96  
 
 97  
         /**
 98  
          * Load a match template from a custom reader using a custom match language.
 99  
          * 
 100  
          * @param matchLanguage
 101  
          *            The match language (currently only <code>"plain"</code> is
 102  
          *            supported)
 103  
          * @param resolver
 104  
          *            Match template resolver for resolving additional template
 105  
          *            names (<tt>$INCLUDE</tt>), optional
 106  
          * @param templateName
 107  
          *            Name of the template
 108  
          * @param br
 109  
          *            Reader to read the template from
 110  
          * @param formatters
 111  
          *            Additional formatters to use (optional)
 112  
          */
 113  129
         public MatchTemplate(String matchLanguage, MatchTemplateResolver resolver, String templateName, BufferedReader br, Formatter... formatters) throws IOException {
 114  129
                 this.impl = MatchTemplateImpl.createTemplateImpl(matchLanguage, resolver, templateName, br, formatters);
 115  114
         }
 116  
 
 117  
         /**
 118  
          * Enable tracing of covered statements. This can be used to find statements
 119  
          * in your match templates that are no longer needed.
 120  
          * 
 121  
          * @see #getUncoveredStatements()
 122  
          */
 123  
         public void enableCoveredStatementsTracing() {
 124  1
                 impl.enableCoveredStatementsTracing();
 125  1
         }
 126  
 
 127  
         /**
 128  
          * Get a list of uncovered statements. This only works if
 129  
          * {@link #enableCoveredStatementsTracing()} has been called before parsing.
 130  
          */
 131  
         public List<String> getUncoveredStatements() {
 132  2
                 return impl.getUncoveredStatements();
 133  
         }
 134  
         
 135  
         /**
 136  
          * Get a list of template file names that are used by this template. This
 137  
          * includes the script itself and (recursively) all scripts that have been
 138  
          * included by it, in the order they were included.
 139  
          */
 140  
         public List<String> getIncludedTemplateFileNames() {
 141  0
                 return impl.getIncludedTemplateFileNames();
 142  
         }
 143  
 
 144  
         MatchTemplateImpl getImpl() {
 145  114
                 return impl;
 146  
         }
 147  
 
 148  
         /**
 149  
          * Interface to resolve additional templates from <code>$INCLUDE</code>
 150  
          * statements.
 151  
          */
 152  
         public static interface MatchTemplateResolver {
 153  
 
 154  
                 /**
 155  
                  * Resolve a template.
 156  
                  * 
 157  
                  * @param templateName
 158  
                  *            Name of the template
 159  
                  * @return Buffered reader for the template
 160  
                  */
 161  
                 public BufferedReader resolve(String templateName) throws IOException;
 162  
         }
 163  
 
 164  
         private static class FileMatchTemplateResolver implements MatchTemplateResolver {
 165  
 
 166  
                 private final File directory;
 167  
                 private final String encoding;
 168  
 
 169  1
                 public FileMatchTemplateResolver(File directory, String encoding) {
 170  1
                         this.directory = directory;
 171  1
                         this.encoding = encoding;
 172  1
                 }
 173  
 
 174  
                 @Override
 175  
                 public BufferedReader resolve(String templateName) throws IOException {
 176  1
                         return new BufferedReader(new InputStreamReader(new FileInputStream(new File(directory, templateName)), encoding));
 177  
                 }
 178  
         }
 179  
 
 180  
         private static class ResourceMatchTemplateResolver implements MatchTemplateResolver {
 181  
 
 182  
                 private final Class<?> clazz;
 183  
                 private final String resourceBase;
 184  
                 private final String encoding;
 185  
 
 186  1
                 public ResourceMatchTemplateResolver(Class<?> clazz, String resourceBase, String encoding) {
 187  1
                         this.clazz = clazz;
 188  1
                         this.resourceBase = resourceBase;
 189  1
                         this.encoding = encoding;
 190  1
                 }
 191  
 
 192  
                 @Override
 193  
                 public BufferedReader resolve(String templateName) throws IOException {
 194  1
                         return new BufferedReader(new InputStreamReader(clazz.getResourceAsStream(resourceBase + templateName), encoding));
 195  
                 }
 196  
         }
 197  
 }