001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.primitive.regex;
028:
029: import org.databene.benerator.Generator;
030: import org.databene.benerator.sample.ConstantGenerator;
031: import org.databene.benerator.wrapper.ConvertingGenerator;
032: import org.databene.benerator.wrapper.AlternativeGenerator;
033: import org.databene.benerator.wrapper.UniqueAlternativeGenerator;
034: import org.databene.benerator.primitive.UniqueStringGenerator;
035: import org.databene.benerator.primitive.CharacterGenerator;
036: import org.databene.commons.converter.ToStringConverter;
037: import org.databene.regex.*;
038:
039: import java.util.Set;
040:
041: /**
042: * Creates generators for regex parts. @see RegexPart<br/>
043: * <br/>
044: * Created: 17.11.2007 16:30:09
045: */
046: class RegexPartGeneratorFactory {
047:
048: public static Generator<String> createRegexPartGenerator(
049: RegexPart part, int maxQuantity, boolean unique) {
050: SubPattern pattern = part.getPattern();
051: int min = part.getQuantifier().getMin();
052: int max = part.getQuantifier().getMax();
053: if (max == -1)
054: max = Math.max(min, maxQuantity);
055: if (pattern instanceof CharSetPattern)
056: return createCharSetPatternGenerator(pattern, min, max,
057: unique);
058: else if (pattern instanceof Group)
059: return createGroupGenerator((Group) pattern, min, max,
060: maxQuantity, unique);
061: else if (pattern instanceof AlternativePattern)
062: return createAlternativeGenerator(
063: (AlternativePattern) pattern, min, max,
064: maxQuantity, unique);
065: else
066: throw new UnsupportedOperationException(
067: "Unsupported RegexPart type: "
068: + part.getClass().getName());
069: }
070:
071: public static Generator<String>[] getRegexGenerators(
072: AlternativePattern alternatives, int maxPartLength,
073: boolean unique) {
074: /** creates an array of generators, of which each one will generate a pattern from the alternatives */
075: Regex[] regexes = alternatives.getPatterns();
076: RegexStringGenerator[] sources = new RegexStringGenerator[regexes.length];
077: for (int i = 0; i < regexes.length; i++)
078: sources[i] = new RegexStringGenerator(regexes[i],
079: maxPartLength, unique);
080: return sources;
081: }
082:
083: // private helpers -------------------------------------------------------------------------------------------------
084:
085: private static Generator<String> createAlternativeGenerator(
086: AlternativePattern pattern, int min, int max,
087: int maxQuantity, boolean unique) {
088: if (unique) {
089: if (min == max && min == 1)
090: return new UniqueAlternativeGenerator<String>(
091: String.class, getRegexGenerators(pattern,
092: maxQuantity, unique));
093: else
094: return new UniqueCompositeStringGenerator(pattern, min,
095: max, maxQuantity);
096: } else
097: return new NFoldCompositeStringGenerator(
098: new AlternativeGenerator<String>(String.class,
099: getRegexGenerators(pattern, maxQuantity,
100: unique)), min, max);
101: }
102:
103: private static Generator<String> createGroupGenerator(
104: Group pattern, int min, int max, int maxQuantity,
105: boolean unique) {
106: if (unique)
107: return new UniqueCompositeStringGenerator(pattern, min,
108: max, maxQuantity);
109: else
110: return new NFoldCompositeStringGenerator(
111: new RegexStringGenerator(pattern.getRegex(),
112: maxQuantity, unique), min, max);
113: }
114:
115: private static Generator<String> createCharSetPatternGenerator(
116: SubPattern pattern, int min, int max, boolean unique) {
117: Set<Character> charSet = ((CharSetPattern) pattern)
118: .getCharSet();
119: if (min == max && max == 1 && charSet.size() == 1)
120: return new ConstantGenerator<String>(String.valueOf(charSet
121: .iterator().next()));
122: if (unique) {
123: return new UniqueStringGenerator(min, max, charSet);
124: } else {
125: Generator<String> patternGenerator = new ConvertingGenerator<Character, String>(
126: new CharacterGenerator(((CharSetPattern) pattern)
127: .getCharSet()),
128: new ToStringConverter<Character>());
129: return new NFoldCompositeStringGenerator(patternGenerator,
130: min, max);
131: }
132: }
133: }
|