Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* (c) Copyright 2007-2009 by Volker Bergmann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted under the terms of the
* GNU General Public License.
*
* For redistributing this software or a derivative work under a license other
* than the GPL-compatible Free Software License as defined by the Free
* Software Foundation or approved by OSI, you must first obtain a commercial
* license to this software product from Volker Bergmann.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
* REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
* HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.databene.regex;
import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.databene.commons.Assert;
import org.databene.commons.CharSet;
import org.databene.commons.CollectionUtil;
import org.databene.commons.LocaleUtil;
import org.databene.regex.antlr.RegexLexer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Parses a regular expression into an object model.
*
* Created: 18.08.2006 19:10:42
* @since 0.1
* @author Volker Bergmann
*/
public class RegexParser {
private static final Logger LOGGER = LoggerFactory.getLogger(RegexParser.class);
private Locale locale;
// constructors ----------------------------------------------------------------------------------------------------
public RegexParser() {
this(LocaleUtil.getFallbackLocale());
}
public RegexParser(Locale locale) {
this.locale = locale;
}
// interface -------------------------------------------------------------------------------------------------------
public Object parseRegex(String pattern) throws ParseException {
if (pattern == null)
return null;
if (pattern.length() == 0)
return "";
try {
RegexLexer lex = new RegexLexer(new ANTLRReaderStream(new StringReader(pattern)));
CommonTokenStream tokens = new CommonTokenStream(lex);
org.databene.regex.antlr.RegexParser parser = new org.databene.regex.antlr.RegexParser(tokens);
org.databene.regex.antlr.RegexParser.expression_return r = parser.expression();
if (parser.getNumberOfSyntaxErrors() > 0)
throw new ParseException("Illegal regex: " + pattern, -1);
if (r != null) {
CommonTree tree = (CommonTree) r.getTree();
if (LOGGER.isDebugEnabled())
LOGGER.debug("parsed " + pattern + " to " + tree.toStringTree());
return convertNode(tree);
} else
return null;
} catch (RuntimeException e) {
if (e.getCause() instanceof RecognitionException)
throw mapToParseException((RecognitionException) e.getCause());
else
throw e;
} catch (IOException e) {
throw new IllegalStateException("Encountered illegal state in regex parsing", e);
} catch (RecognitionException e) {
throw mapToParseException(e);
}
}
public Object parseSingleChar(String pattern) throws ParseException {
if (pattern == null)
return null;
if (pattern.length() == 0)
return "";
try {
RegexLexer lex = new RegexLexer(new ANTLRReaderStream(new StringReader(pattern)));
CommonTokenStream tokens = new CommonTokenStream(lex);
org.databene.regex.antlr.RegexParser parser = new org.databene.regex.antlr.RegexParser(tokens);
org.databene.regex.antlr.RegexParser.singlechar_return r = parser.singlechar();
if (parser.getNumberOfSyntaxErrors() > 0)
throw new ParseException("Illegal regex: " + pattern, -1);
if (r != null) {
CommonTree tree = (CommonTree) r.getTree();
if (LOGGER.isDebugEnabled())
LOGGER.debug("parsed " + pattern + " to " + tree.toStringTree());
return convertNode(tree);
} else
return null;
} catch (RuntimeException e) {
if (e.getCause() instanceof RecognitionException)
throw mapToParseException((RecognitionException) e.getCause());
else
throw e;
} catch (IOException e) {
throw new IllegalStateException("Encountered illegal state in regex parsing", e);
} catch (RecognitionException e) {
throw mapToParseException(e);
}
}
public static CharSet toCharSet(Object o) {
if (o instanceof CharSet)
return (CharSet) o;
else if (o instanceof Character)
return new CharSet(CollectionUtil.toSet((Character) o));
else if (o instanceof CustomCharClass)
return ((CustomCharClass) o).getCharSet();
else
throw new IllegalArgumentException("Not a supported character regex type: " + o.getClass());
}
public static Set toSet(Object regex) {
return toCharSet(regex).getSet();
}
private ParseException mapToParseException(RecognitionException e) {
return new ParseException("Error parsing regular expression: " + e.getMessage(), e.charPositionInLine);
}
private Object convertNode(CommonTree node) throws ParseException {
if (node == null)
return null;
if (node.getToken() == null)
return "";
switch (node.getType()) {
case RegexLexer.CHOICE: return convertChoice(node);
case RegexLexer.GROUP: return convertGroup(node);
case RegexLexer.SEQUENCE: return convertSequence(node);
case RegexLexer.FACTOR: return convertFactor(node);
case RegexLexer.SIMPLEQUANTIFIER: return convertSimpleQuantifier(node);
case RegexLexer.CLASS: return convertClass(node);
case RegexLexer.RANGE: return convertRange(node);
case RegexLexer.ALPHANUM: return convertAlphanum(node);
case RegexLexer.SPECIALCHARACTER: return convertAlphanum(node);
case RegexLexer.ESCAPEDCHARACTER: return convertEscaped(node);
case RegexLexer.NONTYPEABLECHARACTER: return convertNonTypeable(node);
case RegexLexer.OCTALCHAR: return convertOctal(node);
case RegexLexer.HEXCHAR: return convertHexChar(node);
case RegexLexer.CODEDCHAR: return convertCodedChar(node);
case RegexLexer.PREDEFINEDCLASS: return convertPredefClass(node);
default: throw new ParseException("Unknown token type: " + node.getToken(), node.getCharPositionInLine());
}
}
@SuppressWarnings("unchecked")
private Group convertGroup(CommonTree node) throws ParseException {
List childNodes = node.getChildren();
Assert.equals(1, childNodes.size(), "Group is expected to have exactly one child node");
return new Group(convertNode(childNodes.get(0)));
}
@SuppressWarnings("unchecked")
private Choice convertChoice(CommonTree node) throws ParseException {
List childNodes = node.getChildren();
List