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

org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools.Filter2 Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;

import java.io.IOException;

import java.util.Vector;
import java.util.HashMap;

/**
 * Renames local variable names to short ones
 * @version $Name:  $ ($Revision: 1538667 $) $Date: 2013-11-04 09:37:51 -0700 (Mon, 04 Nov 2013) $
 */
public class Filter2 implements TokenReader
{

  public Filter2(TokenReader in)
  {
    _in = in;
    Runnable runner = new Runnable()
      {
        public void run()
        {
          try
          {
            //ystem.out.println("Compressor: start:"+Thread.currentThread());
            _run();
            //ystem.out.println("Comressor: end:"+Thread.currentThread());
          }
          catch (InterruptedException e)
          {
            e.printStackTrace();
          }
        }
      };
    new Thread(runner).start();
  }

  /**
   * @see TokenReader#read()
   */
  public Token read() throws IOException, InterruptedException
  {
    return _buffer.read();
  }

  private void _run() throws InterruptedException
  {
    try
    {
      Token cur = _in.read();
      for(;cur!=null;)
      {
        _process(cur);
        cur = _in.read();
      }
    }
    catch (IOException e)
    {
      _buffer.write(e);
    }
    catch (TokenException e)
    {
      e.printStackTrace();
      _buffer.write(new IOException("Error parsing line:"+
                                    e.getToken().lineNumber));
    }
    catch (RuntimeException e)
    {
      e.printStackTrace();
      _buffer.write(new IOException());
    }
    _buffer.close();
  }

  /**
   * renames local variable names to short names.
   * First pass. sets a flag if this function uses the JS eval method.
   */
  private void _process(Token cur) throws InterruptedException
  {
    if ((cur.code == Token.LEFT_BRACE) && (cur.ch == '{'))
      _openCurly++;
    else if ((cur.code == Token.RIGHT_BRACE) && (cur.ch == '}'))
      _openCurly--;


    switch(_state)
    {
    case ROOT_MODE :
      if ((cur.code==Token.RESERVED) && (cur.string.equals("function")))
      {
        _state = FUNCTION_PARAM_MODE;
        _function.clear();
        _isFunctionUsingEval = false;
        _function.add(cur);
      }
      else _buffer.write(cur);
      break;
    case FUNCTION_PARAM_MODE :
      _function.add(cur);
      if ((cur.code==Token.LEFT_BRACE) && (cur.ch=='{'))
      {
        _state = FUNCTION_BODY_MODE;
        _beginFunction = _openCurly;
      }
      break;
    case FUNCTION_BODY_MODE :
      _function.add(cur);
      if (_openCurly<_beginFunction)
      {
        _state = ROOT_MODE;
        if (_isFunctionUsingEval) _writeTokens(_function);
        else
        {
          for(int i=0,sz=_function.size(); ioldToken then the oldToken is returned.
   * @param oldToken the token to substitute for.
   */
  private Token _substForToken(Token oldToken)
  {
    String oldName = oldToken.string;
    String newName = (String) _localVarMap.get(oldName);
    if (newName==null)
    {
      if (_nameGen.isInUse(oldName))
        throw new TokenException(oldToken,
                                 "Conflict with global var:"+oldName);
      return oldToken;
    }
    Token tok = new Token(oldToken.code, oldToken.lineNumber, newName);
    return tok;
  }

  private class NameGen
  {
    private int _i = 0;
    private char _ch = 'a';
    private final StringBuffer _sb = new StringBuffer();

    /**
     * @return a new unique short name
     */
    public String getNext()
    {
      _sb.setLength(0);
      return _sb.append(_ch).append(_i++).toString();
    }

    public void reset()
    {
      _i = 0;
    }

    /**
     * @return true if varName has already been returned by
     *   the method getNext()
     * @see #getNext()
     */
    public boolean isInUse(String varName)
    {
      int sz=varName.length();
      if ((varName.charAt(0)!=_ch) || (sz<=1)) return false;

      for(int i=1; i= _i) return false;
      else return true;
    }
  }

  private int _state = ROOT_MODE;
  private int _openCurly = 0;
  private int _beginFunction = 0;
  private boolean _isFunctionUsingEval = false;

  private final TokenReader _in;
  private final NameGen _nameGen = new NameGen();
  private final HashMap _localVarMap = new HashMap();
  private final TokenBuffer _buffer = new TokenBuffer();
  private final Vector _function = new Vector();

  private static final int ROOT_MODE =           0;
  private static final int FUNCTION_MODE =       1;
  private static final int FUNCTION_PARAM_MODE = 2;
  private static final int FUNCTION_BODY_MODE =  3;
  private static final int VAR_DEF_MODE =        4;
  private static final int PERIOD_MODE =         5;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy