juzu.impl.router.CharEscapeTransformation Maven / Gradle / Ivy
/*
* Copyright 2013 eXo Platform SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package juzu.impl.router;
import juzu.impl.router.regex.RENode;
import juzu.impl.router.regex.REVisitor;
/**
* The route escaper transformer a regular expression with the following rules:
*
* - substitute any char occurence of the source s by the destination d
* - replace the any by the negated destination character [^]
* - append &&[^s] to any top character class
*
* A few examples with / replaced by _:
*
* - / becomes _
- . becomes [^/]
* - [a/] becomes [a_&[^/]]
- [,-1] becomes [,-.0-1_&&[^/]]
*
*
* @author Julien Viet
* @version $Revision$
*/
class CharEscapeTransformation extends REVisitor {
/** . */
private final char src;
/** . */
private final char dst;
public CharEscapeTransformation(char src, char dst) {
this.src = src;
this.dst = dst;
}
@Override
protected void visit(RENode.Char expr) throws MalformedRouteException {
if (expr.getValue() == src) {
expr.setValue(dst);
}
}
@Override
protected void visit(RENode.Any expr) throws MalformedRouteException {
RENode.CharacterClass repl = new RENode.CharacterClass(new RENode.CharacterClassExpr.Not(new RENode.CharacterClassExpr.Char('/')));
repl.setQuantifier(expr.getQuantifier());
expr.replaceBy(repl);
}
@Override
protected void visit(RENode.CharacterClass expr) throws MalformedRouteException {
RENode.CharacterClassExpr ccExpr = expr.getExpr();
ccExpr = ccExpr.replace(src, dst);
// RENode.CharacterClassExpr.And ccRepl = new RENode.CharacterClassExpr.And(null, new RENode.CharacterClassExpr.Not(new RENode.CharacterClassExpr.Char('/')));
// ccExpr.replaceBy(ccRepl);
// ccRepl.setLeft(ccExpr);
}
}