All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.util.NamedVariablePatternMatcher Maven / Gradle / Ivy

Go to download

XWork is an command-pattern framework that is used to power WebWork as well as other applications. XWork provides an Inversion of Control container, a powerful expression language, data type conversion, validation, and pluggable configuration.

There is a newer version: 2.1.3
Show newest version
/*
 * Copyright (c) 2002-2006 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.xwork2.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * An implementation of a pattern matcher that uses simple named wildcards.  The named wildcards are defined using the
 * {VARIABLE_NAME} syntax and will match any characters that aren't '/'.  Internally, the pattern is
 * converted into a regular expression where the named wildcard will be translated into ([^/]+) so that
 * at least one character must match in order for the wildcard to be matched successfully.  Matched values will be
 * available in the variable map, indexed by the name they were given in the pattern.
 *
 * 

For example, the following patterns will be processed as so: *

* * * * * * * * * * * * * * * * * * *
PatternExampleVariable Map Contents
/animals/{animal} * /animals/dog{animal -> dog}
/animals/{animal}/tag/No{id} * /animals/dog/tag/No23{animal -> dog, id -> 23}
/{language} * /en{language -> en}
* *

* Excaping hasn't been implemented since the intended use of these patterns will be in matching URLs. *

* * @Since 2.1 */ public class NamedVariablePatternMatcher implements PatternMatcher { public boolean isLiteral(String pattern) { return (pattern == null || pattern.indexOf('{') == -1); } /** * Compiles the pattern. * * @param data The pattern, must not be null or empty * @return The compiled pattern, null if the pattern was null or empty */ public CompiledPattern compilePattern(String data) { StringBuilder regex = new StringBuilder(); if (data != null && data.length() > 0) { List varNames = new ArrayList(); StringBuilder varName = null; for (int x=0; x map, String data, CompiledPattern expr) { if (data != null && data.length() > 0) { Matcher matcher = expr.getPattern().matcher(data); if (matcher.matches()) { for (int x=0; x variableNames; public CompiledPattern(Pattern pattern, List variableNames) { this.pattern = pattern; this.variableNames = variableNames; } public Pattern getPattern() { return pattern; } public List getVariableNames() { return variableNames; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy