Coverage Report - net.sf.jmatchparser.template.engine.parameter.Tag
 
Classes in this File Line Coverage Branch Coverage Complexity
Tag
100%
52/52
100%
35/35
6,429
 
 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.parameter;
 19  
 
 20  
 import java.util.regex.Matcher;
 21  
 import java.util.regex.Pattern;
 22  
 
 23  
 import net.sf.jmatchparser.template.engine.ParserState;
 24  
 import net.sf.jmatchparser.util.PatternCache;
 25  
 
 26  1584
 abstract class Tag {
 27  
 
 28  
         boolean isParsingOnly() {
 29  1088
                 return false;
 30  
         }
 31  
 
 32  
         int getUsedGroupCount() {
 33  540
                 return 0;
 34  
         }
 35  
 
 36  
         String getRegex(ParserState state) {
 37  1257
                 return Pattern.quote(getString(state));
 38  
         }
 39  
 
 40  
         void applyMatch(ParserState state, String[] matches) {
 41  541
                 if (matches.length != 0)
 42  1
                         throw new RuntimeException();
 43  540
         }
 44  
 
 45  
         /**
 46  
          * Return whether both the {@link #getString(ParserState)} and
 47  
          * {@link #getRegex(ParserState)} method will always return the same value,
 48  
          * independent from the {@link ParserState}.
 49  
          */
 50  
         abstract boolean isStatic();
 51  
 
 52  
         abstract String getString(ParserState state);
 53  
 
 54  
         static Tag parseTag(String rawContent) {
 55  493
                 switch (rawContent.length() > 0 ? rawContent.charAt(0) : ' ') {
 56  
                 case '&':
 57  6
                         return new EntityWrapperTag(parseTag(rawContent.substring(1)));
 58  
                 case '=':
 59  40
                         return new InsertLocalTag(Parameter.parseRawParameter(rawContent.substring(1)));
 60  
                 case '^':
 61  18
                         return new CallbackFunctionTag(Parameter.parseRawParameters(rawContent.substring(1), 0, 255, 0));
 62  
                 case '\\':
 63  
                         char escaped;
 64  24
                         if (rawContent.length() == 2) {
 65  22
                                 switch (rawContent.charAt(1)) {
 66  
                                 case 'r':
 67  2
                                         escaped = '\r';
 68  2
                                         break;
 69  
                                 case 'n':
 70  4
                                         escaped = '\n';
 71  4
                                         break;
 72  
                                 case 'b':
 73  2
                                         escaped = '\b';
 74  2
                                         break;
 75  
                                 case 't':
 76  2
                                         escaped = '\t';
 77  2
                                         break;
 78  
                                 case '[':
 79  2
                                         escaped = '«';
 80  2
                                         break;
 81  
                                 case ']':
 82  2
                                         escaped = '»';
 83  2
                                         break;
 84  
                                 case '\\':
 85  2
                                         escaped = '\\';
 86  2
                                         break;
 87  
                                 case '(':
 88  2
                                         return new LiteralTag("<<");
 89  
                                 case ')':
 90  2
                                         return new LiteralTag(">>");
 91  
                                 default:
 92  2
                                         escaped = (char) Integer.parseInt(rawContent.substring(1), 16);
 93  2
                                         break;
 94  
                                 }
 95  
                         } else {
 96  2
                                 escaped = (char) Integer.parseInt(rawContent.substring(1), 16);
 97  
                         }
 98  20
                         return new LiteralTag("" + escaped);
 99  
                 case ',':
 100  283
                         if (rawContent.length() > 1)
 101  1
                                 throw new RuntimeException("Comma tag may not have any parameters");
 102  282
                         return new LiteralTag("\0");
 103  
                 case '?':
 104  5
                         return new OptionalTag(Parameter.parseRawParameter(rawContent.substring(1)));
 105  
                 default:
 106  
                         // case '/':
 107  
                         // case '@':
 108  117
                         Matcher m = PatternCache.compile("[/@+]?((?:[a-zA-Z][a-zA-Z0-9]*)?)(:.*)?").matcher(rawContent);
 109  117
                         if (!m.matches())
 110  1
                                 throw new RuntimeException("Unsupported tag: " + rawContent);
 111  116
                         String name = m.group(1);
 112  116
                         String regex = m.group(2);
 113  116
                         if (regex == null)
 114  36
                                 regex = ".*?";
 115  
                         else
 116  80
                                 regex = regex.substring(1);
 117  116
                         if (rawContent.startsWith("/")) {
 118  14
                                 return new TagParseTag(name, regex);
 119  102
                         } else if (rawContent.startsWith("@")) {
 120  4
                                 return new AttributeParseTag(name, regex);
 121  98
                         } else if (rawContent.startsWith("+")) {
 122  2
                                 return new LocalParseTag(name, regex, true);
 123  
                         } else {
 124  96
                                 return new LocalParseTag(name, regex, false);
 125  
                         }
 126  
                 }
 127  
         }
 128  
 }