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

juzu.impl.router.RouteEscaper Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * 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.GroupType;
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 RouteEscaper extends REVisitor { /** . */ private final char src; /** . */ private final char dst; RouteEscaper(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.Group expr) throws MalformedRouteException { if (expr.getType() == GroupType.CAPTURING_GROUP) { expr.setType(GroupType.NON_CAPTURING_GROUP); } super.visit(expr); } @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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy