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

juzu.impl.router.regex.RERenderer 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.regex;

import java.io.IOException;

/**
 * Renders a {@link RENode} to its pattern representation.
 *
 * @author Julien Viet
 * @version $Revision$
 */
public class RERenderer extends REVisitor {

  public static  A render(RENode re, A appendable) throws IOException, NullPointerException {
    if (re == null) {
      throw new NullPointerException("No null disjunction accepted");
    }
    if (appendable == null) {
      throw new NullPointerException("No null appendable accepted");
    }

    //
    RERenderer renderer = new RERenderer(appendable);

    //
    re.accept(renderer);

    //
    return appendable;
  }

  /** . */
  private final Appendable appendable;

  public RERenderer(Appendable appendable) {
    this.appendable = appendable;
  }

  protected void visit(RENode.Disjunction disjunction) throws IOException, NullPointerException {
    RENode.Alternative alternative = disjunction.getAlternative();
    if (alternative != null) {
      alternative.accept(this);
    }
    if (disjunction.hasAlternative() && disjunction.hasNext()) {
      appendable.append('|');
    }
    RENode.Disjunction next = disjunction.getNext();
    if (next != null) {
      next.accept(this);
    }
  }

  protected void visit(RENode.Alternative alternative) throws IOException, NullPointerException {
    alternative.getExpr().accept(this);
    RENode.Alternative next = alternative.getNext();
    if (next != null) {
      visit(next);
    }
  }

  protected void visit(RENode.Assertion.Begin expr) throws IOException {
    appendable.append('^');
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.Assertion.End expr) throws IOException {
    appendable.append('$');
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.Group expr) throws IOException {
    appendable.append(expr.getType().getOpen());
    this.visit(expr.getDisjunction());
    appendable.append(expr.getType().getClose());
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.Char expr) throws IOException {
    Literal.escapeTo(expr.getValue(), appendable);
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.Any expr) throws IOException {
    appendable.append('.');
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.CharacterClass expr) throws IOException {
    appendable.append("[");
    expr.getExpr().accept(this);
    appendable.append("]");
    if (expr.getQuantifier() != null) {
      expr.getQuantifier().toString(appendable);
    }
  }

  protected void visit(RENode.CharacterClassExpr.Not expr) throws IOException {
    boolean needBrace = false;
    for (RENode current = expr.getParent();current != null;current = current.getParent()) {
      if (current instanceof RENode.CharacterClassExpr.Or) {
        needBrace = true;
        break;
      }
      else if (current instanceof RENode.CharacterClassExpr.And) {
        needBrace = true;
        break;
      }
      else if (current instanceof RENode.CharacterClassExpr.Not) {
        needBrace = true;
        break;
      }
    }
    if (needBrace) {
      appendable.append("[");
    }
    appendable.append("^");
    expr.getNegated().accept(this);
    if (needBrace) {
      appendable.append(']');
    }
  }

  protected void visit(RENode.CharacterClassExpr.Or expr) throws IOException {
    expr.getLeft().accept(this);
    expr.getRight().accept(this);
  }

  protected void visit(RENode.CharacterClassExpr.And expr) throws IOException {
    expr.getLeft().accept(this);
    appendable.append("&&");
    expr.getRight().accept(this);
  }

  protected void visit(RENode.CharacterClassExpr.Range expr) throws IOException {
    visit(expr.getFrom());
    appendable.append('-');
    visit(expr.getTo());
  }

  protected void visit(RENode.CharacterClassExpr.Char expr) throws IOException {
    Literal.escapeTo(expr.getValue(), appendable);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy