incubator-adffaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mat...@apache.org
Subject svn commit: r425117 [14/14] - in /incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript: ./ javascript20parser/ javascriptcompiler/ obfuscator/ obfuscato...
Date Mon, 24 Jul 2006 16:58:51 GMT
Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Queue.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Queue.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Queue.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Queue.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,208 @@
+/*
+ * Copyright 1999-2002,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+import java.util.List;
+
+/**
+ * Implements a first-in-first-out (FIFO) queue. Typically, one thread will add
+ * elements to this queue, and another will remove elements from this queue.
+ * This class is thread safe.
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon (arjuna.wijeyekoon@oracle.com)
+ */
+public class Queue
+{
+  /**
+   * @param size the maximum size of this queue
+   */
+  public Queue(int size)
+  {
+    if (size<=0)
+      throw new IllegalArgumentException("size is nonpositive:"+size);
+    _buf = new Object[size];
+  }
+
+  /**
+   * @return the number of elements in this queue. This will never be larger
+   * than the {@link #Queue(int) maximum size} of this queue.
+   */
+  public final synchronized int size()
+  {
+    return _size;
+  }
+
+  /**
+   * @return true if the queue has been closed.
+   * @see #close()
+   */
+  public final synchronized boolean isClosed()
+  {
+    return _closed;
+  }
+
+  /**
+   * closes this queue. Any consequent {@link #add(Object)} method calls will
+   * fail.  All {@link #get()} operations will succeed until the queue is
+   * empty. This method may be called multiple times.
+   * @see #isClosed()
+   */
+  public synchronized void close()
+  {
+    _closed = true;
+    notifyAll();
+  }
+
+  /**
+   * @return true if the queue is full and a call to {@link #add(Object)}
+   * would block.
+   */
+  public final boolean isFull()
+  {
+    return (size() == _buf.length);
+  }
+
+  /**
+   * @return true if the queue is empty and a call to {@link #get()}
+   * would block.
+   */
+  public final boolean isEmpty()
+  {
+    return (size() == 0);
+  }
+
+  /**
+   * This method blocks until space is available in this queue.
+   * @param obj the Object to add to the end of this queue. null is permitted.
+   * @exception InterruptedException if the current thread is interrupted.
+   * @exception IllegalStateException if queue is closed.
+   * @see #close()
+   * @see #remove()
+   */
+  public synchronized void add(Object obj)
+    throws InterruptedException, IllegalStateException
+  {
+    for(;isFull() && (!isClosed());)
+    {
+      //ystem.out.println("waiting to add. size");
+      wait();
+    }
+    //ystem.out.println("adding. size:"+size());
+
+    _checkIsClosed();
+
+    _buf[_head] = obj;
+    _head = _incIndex(_head);
+    _size++;
+    // yes, we are waking up all threads, including those that are
+    // waiting to do an add. This may be inefficient.
+    // note that we must do notifyAll() and not notify()
+    notifyAll();
+  }
+
+  /**
+   * This method blocks until some element is added to this queue, or the queue
+   * is closed.
+   * @return removes and returns the Object at the front of this queue.
+   * null may be returned if null was added using {@link #add(Object)}
+   * @exception InterruptedException if the current thread is interrupted.
+   * @exception IllegalStateException if queue is closed.
+   * @see #close()
+   * @see #add(Object)
+   * @see #remove(LIst,int)
+   */
+  public synchronized Object remove()
+    throws InterruptedException, IllegalStateException
+  {
+    for(;isEmpty();)
+    {
+      _checkIsClosed();
+      wait();
+    }
+
+    Object res = _buf[_tail];
+    _buf[_tail] = null; // allow garbage collect
+    _tail = _incIndex(_tail);
+    _size--;
+
+    // yes, we are waking up all threads, including those that are
+    // waiting to do a remove. This may be inefficient.
+    // note that we must do notifyAll() and not notify()
+    notifyAll();
+    return res;
+  }
+
+  /**
+   * Removes multiple elements. This method will block until there is something
+   * to remove.
+   * @param collector all the elements removed from this queue are added to
+   * the end of this List.
+   * @param count the maximum number of elements to remove. If this is zero,
+   * then it defaults to the maximum size of this queue.
+   * @return the number of elements actually removed.
+   * @see #remove()
+   */
+  public synchronized int remove(List collector, int count)
+    throws InterruptedException, IllegalStateException
+  {
+    collector.add(remove());
+
+    int sz = size()+1;
+    if ((count == 0) || (count > sz))
+      count = sz;
+    else if (count < 0)
+      throw new IllegalArgumentException("count is negative");
+
+    int read = 1;
+    try
+    {
+      for(;read < count; read++)
+      {
+        collector.add(remove());
+      }
+    }
+    catch(IllegalStateException e)
+    {
+      // this should not happen unless the user has subclassed remove() and
+      // done something weird
+    }
+    catch(InterruptedException e)
+    {
+      // this should not happen unless the user has subclassed remove() and
+      // done something weird
+
+      // mark this thread as interrupted, so that it doesn't block again.
+      Thread.currentThread().interrupt();
+    }
+    return read;
+  }
+
+  private int _incIndex(int index)
+  {
+    index++;
+    return (index < _buf.length) ? index : 0;
+  }
+
+  private void _checkIsClosed()
+  {
+    if (isClosed())
+      throw new IllegalStateException("Queue has been closed");
+  }
+
+  private final Object[] _buf;
+  private boolean _closed = false;
+  private int _size = 0, _head = 0, _tail = 0;
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Queue.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Reducer.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Reducer.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Reducer.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Reducer.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2000-2001,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * Reduces JavaScript files by stripping comments and redundant whitespace
+ * and renaming local variable names to shorter ones.
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public class Reducer extends FileProcessor
+{
+  /**
+   * creates a new Reducer.
+   * @param whitespaceComments if true removes comments and extra whitespace
+   * @param localVars if true renames local variable names to shorter ones.
+   */
+  public Reducer(boolean whitespaceComments, boolean localVars)
+  {
+    super(".js", false);
+    _STRIP_WHITESPACE_COMMENTS = whitespaceComments;
+    _RENAME_LOCAL_VARIABLES = localVars;
+  }
+
+  public Reducer()
+  {
+    this(true, true);
+  }
+
+  /**
+   * reduces a JavaScript file.
+   * @param in the source to read from
+   * @param out the source to write the reduced form to
+   */
+  public void process(BufferedReader in, PrintWriter out)
+    throws IOException, InterruptedException
+  {
+    TokenReader tr = new Tokenizer(in);
+    if (_STRIP_WHITESPACE_COMMENTS) tr = new Filter1(tr);
+    if (_RENAME_LOCAL_VARIABLES) tr = new Filter2(tr);
+    Detokenizer detok = new Detokenizer(out);
+    for(;;)
+    {
+      Token tok = tr.read();
+      if (tok==null) break;
+      else detok.write(tok);
+    }
+  }
+
+  /**
+   * @see FileProcessor#processFile(File, File)
+   */
+  protected void processFile(File in, File out)
+    throws IOException, InterruptedException
+  {
+    BufferedReader reader = new BufferedReader(new FileReader(in));
+    PrintWriter writer =  new PrintWriter(new FileWriter(out));
+    process(reader, writer);
+    writer.close();
+    reader.close();
+  }
+
+  private static void _help()
+  {
+    String s;
+    s = "Reduces JavaScript source code\n" +
+      "Usage:\n" +
+      "java oracle.uix.tools.uix22.javascript.Reducer" +
+      " [-norename] [-whitespace] [-help] input output \n" +
+      " input/output can be either files or directories.\n" +
+      " Directories will be processed recursively.\n" +
+      " Only files with names that end with .js will be processed.\n" +
+      " -norename prevents renaming local variables to short ones\n" +
+      " -whitespace prevents removing comments and extra whitespace\n" +
+      " -help prints this message.";
+    System.out.println(s);
+  }
+
+  public static void main(String[] args)
+  {
+    boolean rename = true;
+    boolean space = true;
+
+    final int sz = args.length-2;
+
+    if (sz<0)
+    {
+      _help();
+      return;
+    }
+
+    for(int i=0; i<sz; i++)
+    {
+      String s = args[i];
+      if (s.equals("-help")) _help();
+      else if (s.equals("-norename")) rename = false;
+      else if (s.equals("-whitespace")) space = false;
+      else
+      {
+        System.out.println("Unknown option:"+s);
+        _help();
+        return;
+      }
+    }
+
+    File in = new File(args[sz]);
+    File out = new File(args[sz+1]);
+    Reducer reducer = new Reducer(space, rename);
+    reducer.process(in, out);
+  }
+
+  private final boolean _STRIP_WHITESPACE_COMMENTS;
+  private final boolean _RENAME_LOCAL_VARIABLES;
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Reducer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Token.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Token.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Token.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Token.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2000-2001,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+/**
+ * Tokens for JavaScript source code
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public class Token
+{
+
+  public Token(int code, int lineNumber)
+  {
+    this(code, lineNumber, (char) 0, (String) null);
+  }
+
+  public Token(int code, int lineNumber, char ch)
+  {
+    this(code, lineNumber, ch, (String) null);
+  }
+
+  public Token(int code, int lineNumber, String s)
+  {
+    this(code, lineNumber, (char) 0, s);
+  }
+
+  public Token(int code, int lineNumber, char ch, String s)
+  {
+    this.code=code;
+    this.lineNumber = lineNumber;
+    this.ch=ch;
+    string = s;
+  }
+
+  public String toString()
+  {
+    return "Token code:"+code+" line:"+lineNumber+
+      " char:"+ch+" string:"+string;
+  }
+
+  public final int code, lineNumber;
+  public final char ch;
+  public final String string;
+
+  public static final int EOF =                 0;
+  public static final int NEWLINE =             10;
+  public static final int WHITESPACE =          11;
+  public static final int PERIOD =              12;
+  public static final int SEMICOLON =           13;
+  public static final int QUOTED =              20;
+  public static final int NAME =                30;
+  public static final int NUMBER =              40;
+  public static final int COMMENT =             50;
+  public static final int REGULAR_EXP =         60;
+  public static final int REGULAR_EXP_MODIFIER =65;
+  public static final int CONTROL =             100;
+  public static final int LEFT_BRACE =          110;
+  public static final int RIGHT_BRACE =         120;
+  public static final int RESERVED =            200; //reserved words
+
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Token.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenBuffer.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenBuffer.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenBuffer.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenBuffer.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2000-2002,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+import java.io.IOException;
+
+/**
+ * A buffer to hold Token objects. Tokens can be read from and written to this
+ * buffer as if it were a queue. it is thread safe. Best if a single thread is
+ * reading and a single thread is writing.
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public class TokenBuffer extends Queue implements TokenReader
+{
+
+  /**
+   * @param bufferSize the maximum size of this buffer.
+   */
+  public TokenBuffer(int bufferSize)
+  {
+    super(bufferSize);
+  }
+
+  public TokenBuffer()
+  {
+    this(100);
+  }
+
+  /**
+   * reads a Token from this buffer. This method blocks until data is available
+   * @return null if there is no more data and this buffer has been closed.
+   * @see TokenReader
+   */
+  public synchronized Token read() throws IOException, InterruptedException
+  {
+    Token tok;
+    try
+    {
+      tok = (Token) super.remove();
+    }
+    catch (IllegalStateException e)
+    {
+      tok = null;
+    }
+
+    if (tok==_EXCEPTION_TOKEN)
+    {
+      throw _getException();
+    }
+    return tok;
+  }
+
+  /**
+   * This method blocks if the buffer is full.
+   * @param tok the token to write to this buffer
+   */
+  public synchronized void write(Token tok) throws InterruptedException
+  {
+    super.add(tok);
+  }
+
+  public synchronized void write(IOException e) throws InterruptedException
+  {
+    _setException(e);
+    write(_EXCEPTION_TOKEN);
+    close();
+  }
+
+  private synchronized void _setException(IOException e)
+  {
+    _exception = e;
+  }
+
+  private synchronized IOException _getException()
+  {
+    return _exception;
+  }
+
+  private IOException _exception = null;
+
+  private static final Token _EXCEPTION_TOKEN = new Token(-1, 0);
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenException.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenException.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenException.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenException.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2000-2001,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+/**
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public class TokenException extends RuntimeException
+{
+  private final Token _token;
+
+  public TokenException(Token token, String message)
+  {
+    super(message);
+    _token = token;
+  }
+
+  public Token getToken()
+  {
+    return _token;
+  }
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenReader.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenReader.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenReader.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenReader.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,421 @@
+/*
+ * Copyright 2000-2001,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+
+/**
+ * A Tokenizer for JavaScript source files.
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public class Tokenizer implements TokenReader
+{
+
+  /**
+   * @param in used to read data from the JS file
+   */
+  public Tokenizer(BufferedReader in)
+  {
+    _in = in;
+    Runnable runner = new Runnable()
+      {
+        public void run()
+        {
+          //ystem.out.println("Tokenizer: start:"+Thread.currentThread());
+          _run();
+          //ystem.out.println("Tokenizer: end:"+Thread.currentThread());
+        }
+      };
+    new Thread(runner).start();
+  }
+
+  /**
+   * reads a Token. blocks until a Token is available.
+   * @return null if the EOF is reached.
+   */
+  public Token read() throws IOException, InterruptedException
+  {
+    return _out.read();
+  }
+
+  private void _run()
+  {
+    try
+      {
+        _run2();
+      }
+    catch (Exception e)
+      {
+        System.out.println("Exception parsing line:"+_lineNumber);
+        e.printStackTrace();
+      }
+  }
+
+  private void _run2() throws InterruptedException
+  {
+    try
+    {
+      for(;_fillBuffer();)
+      {
+        //ystem.out.println("begin process");
+        _processBuffer();
+        //ystem.out.println("end process");
+      }
+      _out.write(new Token(Token.EOF, _lineNumber));
+    }
+    catch (IOException e)
+    {
+      System.out.println("Exception parsing line:"+_lineNumber);
+      _out.write(e);
+    }
+    catch (Exception e)
+    {
+      e.printStackTrace();
+      _out.write(new IOException("Exception parsing line:"+_lineNumber));
+    }
+    finally
+    {
+      _out.close();
+    }
+  }
+
+  private void _processBuffer() throws InterruptedException
+  {
+    _offset = 0;
+
+    for(;_offset<_len;)
+      {
+        //ystem.out.println("line:"+_lineNumber+" offset:"+_offset);
+        char ch = _buffer.charAt(_offset++);
+        switch(_status)
+          {
+          case ROOT_MODE :
+          case DIVISION_MODE :
+            _next = _rootMode(ch, _status);
+            break;
+          case READ_WORD_MODE :
+            _next = _readWordMode(ch, _status, _str);
+            break;
+          case QUOTE1_MODE :
+          case QUOTE2_MODE :
+            _next = _quoteMode(ch, _status, _prev, _str);
+            break;
+          case ESCAPED_CHAR_MODE :
+            _str.append('\\');
+            _str.append(ch);
+            _next = _prev;
+            break;
+          case POSSIBLE_COMMENT_MODE :
+            if (ch=='/') _next = COMMENT1_MODE;
+            else if (ch=='*') _next = COMMENT2_MODE;
+            else
+              {
+                _offset--; //Roll Back one
+                if (_prev == DIVISION_MODE)
+                  {
+                    _writeControl('/');
+                    _next = ROOT_MODE;
+                  }
+                else
+                  {
+                    _next = REGULAR_EXP_MODE;
+                  }
+              }
+            break;
+          case COMMENT1_MODE :
+          case COMMENT2_MODE :
+          case END_COMMENT_MODE :
+            _next = _commentMode(ch, _status, _str);
+            break;
+          case REGULAR_EXP_MODE :
+            _next = _regularExpMode(ch, _str);
+            break;
+          case END_REGULAR_EXP_MODE :
+            if ((ch=='g') || (ch=='i'))
+              {
+                _out.write(new Token(Token.REGULAR_EXP_MODIFIER,
+                                     _lineNumber,
+                                     ch));
+                _next = END_REGULAR_EXP_MODE;
+              }
+            else
+              {
+                _offset--; //Rollback
+                _next = ROOT_MODE;
+              }
+            break;
+          }
+        _prev = _status;
+        _status = _next;
+      }
+  }
+
+  private int _rootMode(char ch, int status) throws InterruptedException
+  {
+    switch(ch)
+      {
+      case '\'' :
+        return QUOTE1_MODE;
+      case '\"' :
+        return QUOTE2_MODE;
+      case ' '  :
+      case '\t' :
+        _out.write(WHITESPACE);
+        return status;
+      case '\n' :
+        _out.write(NEWLINE);
+        return ROOT_MODE;
+      case '.' :
+        _out.write(PERIOD);
+        return status;
+      case ';' :
+        _out.write(SEMICOLON);
+        return ROOT_MODE;
+      case '(' :
+      case '{' :
+      case '[' :
+        _out.write(new Token(Token.LEFT_BRACE, _lineNumber, ch));
+        return ROOT_MODE;
+      case ')' :
+        _out.write(new Token(Token.RIGHT_BRACE, _lineNumber, ch));
+        return DIVISION_MODE;
+      case '}' :
+      case ']' :
+        _out.write(new Token(Token.RIGHT_BRACE, _lineNumber, ch));
+        return ROOT_MODE;
+      case '/'  : return POSSIBLE_COMMENT_MODE;
+      default :
+        if (_isAlphaNumeric(ch))
+          {
+            _offset--; //Rollback
+            return READ_WORD_MODE;
+          }
+        else
+          {
+            _writeControl(ch);
+            return ROOT_MODE;
+          }
+      }
+  }
+
+  private int _regularExpMode(char ch, StringBuffer regExp)
+    throws InterruptedException
+  {
+    switch(ch)
+      {
+      case '\\' :
+        return ESCAPED_CHAR_MODE;
+      case '/' :
+        _out.write(new Token(Token.REGULAR_EXP,
+                             _lineNumber,
+                             regExp.toString()));
+        regExp.setLength(0);
+        return END_REGULAR_EXP_MODE;
+      default :
+        regExp.append(ch);
+        return REGULAR_EXP_MODE;
+      }
+  }
+
+  private int _quoteMode(char ch, int status, int prev,
+                         StringBuffer quoteString) throws InterruptedException
+  {
+    if (((ch=='\'') && (status==QUOTE1_MODE)) ||
+        ((ch=='\"') && (status==QUOTE2_MODE)))
+      {
+        _out.write(new Token(Token.QUOTED,
+                             _lineNumber, ch, quoteString.toString()));
+        quoteString.setLength(0);
+        return ROOT_MODE;
+      }
+    else if (ch=='\\') return ESCAPED_CHAR_MODE;
+    else
+      {
+        quoteString.append(ch);
+        return status;
+      }
+  }
+
+  private int _commentMode(char ch, int status,
+                           StringBuffer commentString)
+    throws InterruptedException
+  {
+    if (status==END_COMMENT_MODE)
+      {
+        if (ch=='/')
+          {
+            _writeComment(commentString);
+            return ROOT_MODE;
+          }
+        else
+          {
+            commentString.append('*');
+            _offset--; //Roll back
+            return COMMENT2_MODE;
+          }
+      }
+    else if ((status==COMMENT2_MODE) && (ch=='*'))
+      {
+        return END_COMMENT_MODE;
+      }
+    else if ((status==COMMENT1_MODE) && (ch=='\n'))
+      {
+        _writeComment(commentString);
+        _out.write(NEWLINE);
+        return ROOT_MODE;
+      }
+    else
+      {
+        commentString.append(ch);
+        return status;
+      }
+  }
+
+  private int _readWordMode(char ch, int status, StringBuffer wordBuffer)
+    throws InterruptedException
+  {
+    if (_isAlphaNumeric(ch))
+      {
+        wordBuffer.append(ch);
+        return status;
+      }
+    else
+      {
+        _writeAlphaNumeric(wordBuffer.toString());
+        wordBuffer.setLength(0);
+        _offset--; //Rollback
+        return DIVISION_MODE;
+      }
+  }
+
+  private void _writeComment(StringBuffer s) throws InterruptedException
+  {
+    _out.write(new Token(Token.COMMENT, _lineNumber, s.toString()));
+    s.setLength(0);
+  }
+
+  private void _writeControl(char ch) throws InterruptedException
+  {
+    _out.write(new Token(Token.CONTROL, _lineNumber, ch));
+  }
+
+  private void _writeAlphaNumeric(String s) throws InterruptedException
+  {
+    if (Character.isDigit(s.charAt(0)))
+      {
+        _out.write(new Token(Token.NUMBER, _lineNumber, s));
+      }
+    else if (_isReservedKeyword(s))
+      {
+        _out.write(new Token(Token.RESERVED, _lineNumber, s));
+      }
+    else
+      {
+        _out.write(new Token(Token.NAME, _lineNumber, s));
+      }
+  }
+
+  private boolean _isReservedKeyword(String s)
+  {
+    for(int i=0; i<reservedWords.length; i++)
+      {
+        if (s.equals(reservedWords[i])) return true;
+      }
+    return false;
+  }
+
+  private boolean _isAlphaNumeric(char ch)
+  {
+    return Character.isLetterOrDigit(ch) || (ch=='_') ;
+  }
+
+  /**
+   * @return false if EOF
+   */
+  private boolean _fillBuffer() throws IOException
+  {
+    _buffer.setLength(0);
+    String s = _in.readLine();
+    if (s==null)
+      {
+        _len=0;
+        return false;
+      }
+    else
+      {
+        _buffer.append(s).append('\n');
+        _len = _buffer.length();
+        _lineNumber++;
+        return true;
+      }
+  }
+
+  private void _reset()
+  {
+    _status = _prev = _next = ROOT_MODE;
+    _buffer.setLength(0);
+    _len = _lineNumber = _offset = 0;
+    _wordBuffer.setLength(0);
+    _str.setLength(0);
+  }
+
+  private int _status = ROOT_MODE;
+  private int _prev = ROOT_MODE;
+  private int _next = ROOT_MODE;
+
+  private int _len = 0;
+  private int _lineNumber = 0;
+  private int _offset = 0;
+
+  private final StringBuffer _buffer = new StringBuffer();
+  private final StringBuffer _wordBuffer = new StringBuffer();
+  private final StringBuffer _str = new StringBuffer();
+
+  private final BufferedReader _in;
+  private final TokenBuffer _out = new TokenBuffer();
+
+  /**
+   * These are not all the reserved words in JS but are the only ones
+   * important to the filters.
+   */
+  private static final String[] reservedWords =
+  {
+    "function", "var"
+  };
+
+  private final Token NEWLINE = new Token(Token.NEWLINE, 0);
+  private final Token WHITESPACE = new Token(Token.WHITESPACE, 0);
+  private final Token PERIOD = new Token(Token.PERIOD, 0);
+  private final Token SEMICOLON = new Token(Token.SEMICOLON, 0);
+
+  /**
+   * These are the FSM states
+   */
+  private static final int ROOT_MODE = 0;
+  private static final int READ_WORD_MODE = 10;
+  private static final int DIVISION_MODE = 15;
+  private static final int QUOTE1_MODE = 20;
+  private static final int QUOTE2_MODE = 30;
+  private static final int ESCAPED_CHAR_MODE = 40;
+  private static final int POSSIBLE_COMMENT_MODE = 50;
+  private static final int COMMENT1_MODE = 60;
+  private static final int COMMENT2_MODE = 70;
+  private static final int END_COMMENT_MODE = 80;
+  private static final int REGULAR_EXP_MODE = 90;
+  private static final int END_REGULAR_EXP_MODE = 95;
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/TokenReader.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Tokenizer.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Tokenizer.java?rev=425117&view=auto
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Tokenizer.java
(added)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Tokenizer.java
Mon Jul 24 09:58:43 2006
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2001,2006 The Apache Software Foundation.
+ * 
+ * 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 org.apache.myfaces.trinidadbuild.plugin.javascript.uixtools;
+
+import java.io.IOException;
+
+/**
+ * @version $Name:  $ ($Revision$) $Date$
+ * @author Arjuna Wijeyekoon - arjuna.wijeyekoon@oracle.com
+ */
+public interface TokenReader
+{
+
+  /*
+   * Reads a Token from this source. This method will block until data
+   * becomes available.
+   * @return null if there is no more data and EOF is reached.
+   */
+  public Token read() throws IOException, InterruptedException;
+}
\ No newline at end of file

Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/plugins/maven-javascript-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/javascript/uixtools/Tokenizer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



Mime
View raw message