Package com.puppetlabs.geppetto.pp.util

Source Code of com.puppetlabs.geppetto.pp.util.TextExpressionHelper

/**
* Copyright (c) 2013 Puppet Labs, Inc. and other contributors, as listed below.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*   Puppet Labs
*/
package com.puppetlabs.geppetto.pp.util;

import java.util.ArrayList;
import java.util.List;

import com.puppetlabs.geppetto.pp.DoubleQuotedString;
import com.puppetlabs.geppetto.pp.Expression;
import com.puppetlabs.geppetto.pp.ExpressionTE;
import com.puppetlabs.geppetto.pp.LiteralName;
import com.puppetlabs.geppetto.pp.LiteralNameOrReference;
import com.puppetlabs.geppetto.pp.TextExpression;
import com.puppetlabs.geppetto.pp.VariableExpression;
import com.puppetlabs.geppetto.pp.VariableTE;
import com.puppetlabs.geppetto.pp.VerbatimTE;
import org.eclipse.emf.common.util.EList;

/**
* Helps with TextExpressions in interpolated Strings.
*
*/
public class TextExpressionHelper {

  public static String getNonInterpolated(DoubleQuotedString dqString) {
    if(hasInterpolation(dqString))
      return null;
    // if there is no interpolation, there is by definition only one StringData
    // ignore the fact that a static string could be interpolated since this is an
    // esoteric case.
    // TODO: Handle this esoteric case "abc${"xyz"}"
    EList<TextExpression> parts = dqString.getStringPart();
    if(parts.size() < 1)
      return "";
    if(parts.size() == 1) {
      return ((VerbatimTE) parts.get(0)).getText();
    }
    // impossible can not have more than one verbatim part and not be interpolated
    throw new IllegalArgumentException("The given dqString lied about not being interpolated");
  }

  public static boolean hasInterpolation(DoubleQuotedString dqString) {
    for(TextExpression stringPart : dqString.getStringPart())
      if(!(stringPart instanceof VerbatimTE))
        return true;
    return false;

  }

  /**
   * Returns a list of trivially interpolated variables; i.e. ${name}, $name, or ${$name}but not variables embedded in more
   * complex expression e.g. ${$a + $b}.
   *
   * @param dqString
   * @return
   */
  public static List<String> interpolatedVariables(DoubleQuotedString dqString) {
    List<String> result = new ArrayList<String>();
    for(TextExpression te : dqString.getStringPart()) {
      if(te instanceof VariableTE) {
        result.add(((VariableTE) te).getVarName());
      }
      else if(te instanceof ExpressionTE) {
        Expression e = ((ExpressionTE) te).getExpression();
        if(e instanceof LiteralNameOrReference)
          result.add(((LiteralNameOrReference) e).getValue());
        else if(e instanceof LiteralName)
          result.add(((LiteralName) e).getValue());
        else if(e instanceof VariableExpression)
          result.add(((VariableExpression) e).getVarName());
      }
    }
    return result;
  }

  public static int interpolationCount(DoubleQuotedString dqString) {
    int count = 0;
    for(TextExpression te : dqString.getStringPart()) {
      if(te instanceof VerbatimTE)
        continue;
      count++;
    }
    return count;

  }

  public static int segmentCount(DoubleQuotedString dqString) {
    return dqString.getStringPart().size();
  }

  public String doubleQuote(String s) {
    return '"' + s + '"';
  }

}
TOP

Related Classes of com.puppetlabs.geppetto.pp.util.TextExpressionHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.