Search in sources :

Example 26 with MatchResult

use of java.util.regex.MatchResult in project sukija by ahomansikka.

the class RegexCombinator method makeCombination.

public Set<String> makeCombination(CharSequence s) {
    Vector<String> v = new Vector<String>();
    Vector<Integer> index = new Vector<Integer>();
    int start = 0;
    boolean found = false;
    Matcher m = pattern.matcher(s);
    // 
    while (m.find()) {
        found = true;
        MatchResult r = m.toMatchResult();
        // 
        for (int i = 1; i <= r.groupCount(); i++) {
            if (r.start(i) >= 0) {
                // System.out.println ("makeCombination3 " + i + " " + s.subSequence (start, r.start(i)).toString());
                // Add chars between capturing groups.
                v.add(s.subSequence(start, r.start(i)).toString());
                // We are not interested where they are.
                index.add(-1);
                // Add matched capturing group.
                v.add(r.group(i));
                // And save their position.
                index.add(i - 1);
                start = r.end(i);
            }
        }
    }
    if (!found)
        return null;
    if (start < s.length()) {
        // Add end of the char sequence.
        v.add(s.subSequence(start, s.length()).toString());
        index.add(-1);
    }
    Vector<Vector<String>> vec = new Vector<Vector<String>>(v.size());
    // 
    for (int i = 0; i < v.size(); i++) {
        vec.add(new Vector<String>());
        vec.get(i).add(v.get(i));
    }
    // 
    for (int i = 0; i < v.size(); i++) {
        if (index.get(i) >= 0) {
            vec.get(i).add(data.get(index.get(i)).second);
        }
    }
    Vector<String> work = new Vector<String>();
    Set<String> result = new TreeSet<String>();
    getCombination(vec, 0, v.size(), work, result);
    /*
System.out.println ("A");
for (int i = 0; i < v.size(); i++) {
  System.out.print (i);
  for (int j = 0; j < vec.get(i).size(); j++) {
    System.out.print (" " + vec.get(i).get(j));
  }
  System.out.println ("");
}
System.out.println ("B");
Iterator<String> i = result.iterator();
while (i.hasNext()) {
  System.out.println (i.next());
}
System.out.println ("C");
*/
    return result;
}
Also used : Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) Vector(java.util.Vector) MatchResult(java.util.regex.MatchResult)

Example 27 with MatchResult

use of java.util.regex.MatchResult in project jena by apache.

the class ParameterizedSparqlString method toString.

/**
 * <p>
 * This method is where the actual work happens, the original command text
 * is always preserved and we just generated a temporary command string by
 * prepending the defined Base URI and namespace prefixes at the start of
 * the command and injecting the set parameters into a copy of that base
 * command string and return the resulting command.
 * </p>
 * <p>
 * This class makes no guarantees about the validity of the returned string
 * for use as a SPARQL Query or Update, for example if a variable parameter
 * was injected which was mentioned in the SELECT variables list you'd have
 * a syntax error when you try to parse the query. If you run into issues
 * like this try using a mixture of variable and positional parameters.
 * </p>
 *
 * @throws ARQException
 *             May be thrown if the code detects a SPARQL Injection
 *             vulnerability because of the interaction of the command
 *             string and the injected variables
 */
@Override
public String toString() {
    String command = this.cmd.toString();
    Pattern p;
    // Go ahead and inject Variable Parameters
    SerializationContext context = new SerializationContext(this.prefixes);
    context.setBaseIRI(this.baseUri);
    for (String var : this.params.keySet()) {
        Node n = this.params.get(var);
        if (n == null) {
            continue;
        }
        this.validateSafeToInject(command, var, n);
        p = Pattern.compile("([?$]" + var + ")([^\\w]|$)");
        command = p.matcher(command).replaceAll(Matcher.quoteReplacement(this.stringForNode(n, context)) + "$2");
    }
    // Inject Values Parameters
    command = applyValues(command, context);
    // Then inject Positional Parameters
    // To do this we need to find the ? we will replace
    p = Pattern.compile("(\\?)[\\s;,.]");
    int index = -1;
    int adj = 0;
    Matcher matcher = p.matcher(command);
    while (matcher.find()) {
        index++;
        MatchResult posMatch = matcher.toMatchResult();
        Node n = this.positionalParams.get(index);
        if (n == null)
            continue;
        this.validateSafeToInject(command, index, posMatch.start(1) + adj, n);
        String nodeStr = this.stringForNode(n, context);
        command = command.substring(0, posMatch.start() + adj) + nodeStr + command.substring(posMatch.start() + adj + 1);
        // Because we are using a matcher over the string state prior to
        // starting replacements we need to
        // track the offset adjustments to make
        adj += nodeStr.length() - 1;
    }
    // Build the final command string
    StringBuilder finalCmd = new StringBuilder();
    // Add BASE declaration
    if (this.baseUri != null) {
        finalCmd.append("BASE ");
        finalCmd.append(FmtUtils.stringForURI(this.baseUri, null, null));
        finalCmd.append('\n');
    }
    for (String prefix : this.prefixes.getNsPrefixMap().keySet()) {
        finalCmd.append("PREFIX ");
        finalCmd.append(prefix);
        finalCmd.append(": ");
        finalCmd.append(FmtUtils.stringForURI(this.prefixes.getNsPrefixURI(prefix), null, null));
        finalCmd.append('\n');
    }
    finalCmd.append(command);
    return finalCmd.toString();
}
Also used : SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) MatchResult(java.util.regex.MatchResult)

Example 28 with MatchResult

use of java.util.regex.MatchResult in project uPortal by Jasig.

the class JsonLayoutPlaceholderEventSourceTest method testGenerateCharacterEventsNull.

@Test(expected = NullPointerException.class)
public void testGenerateCharacterEventsNull() {
    MatchResult result = null;
    jsonLayoutPlaceholderEventSource.generateCharacterEvents(req, result, null);
}
Also used : MatchResult(java.util.regex.MatchResult) Test(org.junit.Test)

Example 29 with MatchResult

use of java.util.regex.MatchResult in project uPortal by Jasig.

the class ChunkingEventReader method chunkString.

/**
 * Breaks up the String into a List of CharacterEvents based on the configured Map of Patterns
 * to CharacterEventSources
 */
private void chunkString(final List<CharacterEvent> characterEvents, final CharSequence buffer, int patternIndex) {
    // Iterate over the chunking patterns
    for (; patternIndex < this.chunkingPatterns.length; patternIndex++) {
        final Pattern pattern = this.chunkingPatterns[patternIndex];
        final Matcher matcher = pattern.matcher(buffer);
        if (matcher.find()) {
            final CharacterEventSource eventSource = this.chunkingPatternEventSources.get(pattern);
            int prevMatchEnd = 0;
            do {
                // Add all of the text up to the match as a new chunk, use subSequence to avoid
                // extra string alloc
                this.chunkString(characterEvents, buffer.subSequence(prevMatchEnd, matcher.start()), patternIndex + 1);
                // Get the generated CharacterEvents for the match
                final MatchResult matchResult = matcher.toMatchResult();
                eventSource.generateCharacterEvents(this.request, matchResult, characterEvents);
                prevMatchEnd = matcher.end();
            } while (matcher.find());
            // Add any remaining text from the original CharacterDataEvent
            if (prevMatchEnd < buffer.length()) {
                this.chunkString(characterEvents, buffer.subSequence(prevMatchEnd, buffer.length()), patternIndex + 1);
            }
            return;
        }
    }
    // Buffer didn't match anything, just append the string data
    // de-duplication of the event string data
    final String eventString = buffer.toString();
    characterEvents.add(CharacterDataEventImpl.create(eventString));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CharacterEventSource(org.apereo.portal.character.stream.CharacterEventSource) MatchResult(java.util.regex.MatchResult)

Example 30 with MatchResult

use of java.util.regex.MatchResult in project ow by vtst.

the class EasyScanner method match.

/**
 * Match the pattern against the stream. The pattern <b>must</b> start with
 * the BOF pattern (\A). In case the stream does not match, the function
 * returns and EasyMatchResult which contains a null MatchResult.
 */
public EasyMatchResult match(Pattern pattern) {
    prepareToScan();
    MatchResult m = null;
    // beginning of the pattern)
    if (scanner.findWithinHorizon(pattern, 0) != null) {
        m = scanner.match();
    }
    EasyMatchResult r = new EasyMatchResult(input, m);
    seekInput(m);
    return r;
}
Also used : MatchResult(java.util.regex.MatchResult)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1