geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r571366 - in /geronimo/sandbox/gshell/trunk: gshell-ansi/ gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/ gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/ gshel...
Date Fri, 31 Aug 2007 04:09:12 GMT
Author: jdillon
Date: Thu Aug 30 21:09:10 2007
New Revision: 571366

URL: http://svn.apache.org/viewvc?rev=571366&view=rev
Log:
Bring over the message source and ansi work I whipped up for the groovysh impl and re-ported
back to java5

Added:
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
      - copied, changed from r570694, geronimo/sandbox/gshell/trunk/gshell-clp/src/test/java/org/apache/geronimo/gshell/clp/PrinterTest.java
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/
    geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
  (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-ansi/pom.xml

Modified: geronimo/sandbox/gshell/trunk/gshell-ansi/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/pom.xml?rev=571366&r1=571365&r2=571366&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/pom.xml Thu Aug 30 21:09:10 2007
@@ -35,6 +35,17 @@
     
     <dependencies>
         <dependency>
+            <groupId>org.apache.geronimo.gshell</groupId>
+            <artifactId>gshell-i18n</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>jline</groupId>
+            <artifactId>jline</artifactId>
+        </dependency>
+
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,62 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+import jline.Terminal;
+
+/**
+ * Provides support for using ANSI color escape codes.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ANSI
+{
+    /**
+     * Tries to detect if the current system supports ANSI.
+     */
+    private static boolean detect() {
+        boolean enabled = Terminal.getTerminal().isANSISupported();
+
+        if (!enabled) {
+            String force = System.getProperty(ANSI.class.getName() + ".force", "false");
+            enabled = Boolean.valueOf(force);
+        }
+
+        return enabled;
+    }
+
+    public static boolean isDetected() {
+        return detect();
+    }
+
+    private static Boolean enabled;
+
+    public static void setEnabled(final boolean flag) {
+        enabled = flag;
+    }
+
+    public static boolean isEnabled() {
+        if (enabled == null) {
+            enabled = isDetected();
+        }
+
+        return enabled;
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/ANSI.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,104 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+/**
+ * A stringbuffer-like thingy to help with using ANSI escape-codes.
+ *
+ * @version $Rev$ $Date$
+ */
+public class Buffer
+{
+    private final StringBuffer buff = new StringBuffer();
+
+    public Boolean ansiEnabled;
+    
+    public boolean autoClear;
+
+    public Buffer(final Boolean ansiEnabled, final boolean autoClear) {
+        this.ansiEnabled = ansiEnabled;
+        this.autoClear = autoClear;
+    }
+
+    public Buffer() {
+        this(null, true);
+    }
+    
+    public String toString() {
+        try {
+            return buff.toString();
+        }
+        finally {
+            if (autoClear) clear();
+        }
+    }
+
+    public boolean isAnsiEnabled() {
+        // Late bind the current system detected ANSI state
+        if (ansiEnabled == null) {
+            ansiEnabled = ANSI.isEnabled();
+        }
+
+        return ansiEnabled;
+    }
+    
+    public void clear() {
+        buff.setLength(0);
+    }
+
+    public int size() {
+        return buff.length();
+    }
+
+    public Buffer append(final String text) {
+        buff.append(text);
+
+        return this;
+    }
+
+    public Buffer append(final Object obj) {
+        return append(String.valueOf(obj));
+    }
+
+    public Buffer attrib(final int code) {
+        if (isAnsiEnabled()) {
+            buff.append(Code.attrib(code));
+        }
+
+        return this;
+    }
+
+    public Buffer attrib(final String text, final int code) {
+        assert text != null;
+
+        if (isAnsiEnabled()) {
+            buff.append(Code.attrib(code)).append(text).append(Code.attrib(Code.OFF));
+        }
+        else {
+            buff.append(text);
+        }
+
+        return this;
+    }
+
+    public Buffer attrib(final String text, final String codeName) {
+        return attrib(text, Code.forName(codeName));
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Buffer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,142 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Container for ANSI color codes.
+ *
+ * @version $Rev$ $Date$
+ */
+public class Code
+{
+    public static final int OFF = 0;
+    public static final int BOLD = 1;
+    public static final int UNDERSCORE = 4;
+    public static final int BLINK = 5;
+    public static final int REVERSE = 7;
+    public static final int CONCEALED = 8;
+
+    public static final int FG_BLACK = 30;
+    public static final int FG_RED = 31;
+    public static final int FG_GREEN = 32;
+    public static final int FG_YELLOW = 33;
+    public static final int FG_BLUE = 34;
+    public static final int FG_MAGENTA = 35;
+    public static final int FG_CYAN = 36;
+    public static final int FG_WHITE = 37;
+
+    public static final int BLACK = FG_BLACK;
+    public static final int RED = FG_RED;
+    public static final int GREEN = FG_GREEN;
+    public static final int YELLOW = FG_YELLOW;
+    public static final int BLUE = FG_BLUE;
+    public static final int MAGENTA = FG_MAGENTA;
+    public static final int CYAN = FG_CYAN;
+    public static final int WHITE = FG_WHITE;
+
+    public static final int BG_BLACK = 40;
+    public static final int BG_RED = 41;
+    public static final int BG_GREEN = 42;
+    public static final int BG_YELLOW = 43;
+    public static final int BG_BLUE = 44;
+    public static final int BG_MAGENTA = 45;
+    public static final int BG_CYAN = 46;
+    public static final int BG_WHITE = 47;
+
+    /** The ANSI escape char which is used to start sequences. */
+    private static final char ESC = 27;
+
+    /** A map of code names to values. */
+    private static final Map<String,Integer> NAMES_TO_CODES;
+
+    /** A map of codes to name. */
+    private static final Map<Integer,String> CODES_TO_NAMES;
+
+    static {
+        Field[] fields = Code.class.getDeclaredFields();
+        Map<String,Integer> names = new HashMap<String,Integer>(fields.length);
+        Map<Integer,String> codes = new HashMap<Integer,String>(fields.length);
+
+        try {
+            for (Field field : fields) {
+                // Skip anything non-public, all public fields are codes
+                int mods = field.getModifiers();
+                if (!Modifier.isPublic(mods)) {
+                    continue;
+                }
+
+                String name = field.getName();
+                Integer code = (Integer) field.get(Code.class);
+
+                names.put(name, code);
+                codes.put(code, name);
+            }
+        }
+        catch (IllegalAccessException e) {
+            // This should never happen
+            throw new Error(e);
+        }
+
+        NAMES_TO_CODES = names;
+        CODES_TO_NAMES = codes;
+    }
+
+    /**
+     * Returns the ANSI code for the given symbolic name.  Supported symbolic names are all
defined as
+     * fields in {@link Code} where the case is not significant.
+     */
+    public static int forName(final String name) throws IllegalArgumentException {
+        assert name != null;
+
+        // All names in the map are upper-case
+        String tmp = name.toUpperCase();
+        Integer code = NAMES_TO_CODES.get(tmp);
+
+        if (code == null) {
+            throw new IllegalArgumentException("Invalid ANSI code name: " + name);
+        }
+
+        return code;
+    }
+
+    /**
+     * Returns the symbolic name for the given ANSI code.
+     */
+    public static String name(final int code) throws IllegalArgumentException {
+        assert code >= 0;
+        
+        String name = CODES_TO_NAMES.get(code);
+
+        if (name == null) {
+            throw new IllegalArgumentException("Invalid ANSI code: " + code);
+        }
+
+        return name;
+    }
+
+    public static String attrib(final int attr) {
+        return ESC + "[" + attr + "m";
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Code.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,59 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+import org.apache.geronimo.gshell.i18n.MessageSource;
+
+/**
+ * Message source implementation which supports automatic ANSI color rendering.
+ *
+ * @version $Rev$ $Date$
+ */
+public class RenderMessageSource
+    extends MessageSource
+{
+    private final Renderer renderer = new Renderer();
+
+    public RenderMessageSource(final String[] names) {
+        super(names);
+    }
+
+    public RenderMessageSource(final String name) {
+        super(name);
+    }
+
+    public RenderMessageSource(final Class[] types) {
+        super(types);
+    }
+
+    public RenderMessageSource(final Class type) {
+        super(type);
+    }
+
+    public String getMessage(final String code) {
+        final String msg = super.getMessage(code);
+
+        if (Renderer.test(msg)) {
+            return renderer.render(msg);
+        }
+
+        return msg;
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderMessageSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,60 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+import java.io.PrintWriter;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * Print writer implementation which supports automatic ANSI color rendering
+ *
+ * @version $Rev$ $Date$
+ */
+public class RenderWriter
+    extends PrintWriter
+{
+    private final Renderer renderer = new Renderer();
+
+    public RenderWriter(final OutputStream out) {
+        super(out);
+    }
+
+    public RenderWriter(final OutputStream out, final boolean autoFlush) {
+        super(out, autoFlush);
+    }
+
+    public RenderWriter(final Writer out) {
+        super(out);
+    }
+
+    public RenderWriter(final Writer out, final boolean autoFlush) {
+        super(out, autoFlush);
+    }
+
+    public void write(final String s) {
+        if (Renderer.test(s)) {
+            super.write(renderer.render(s));
+        }
+        else {
+            super.write(s);
+        }
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/RenderWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,129 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+/**
+ * Renders ANSI color escape-codes in strings by parsing out some special syntax to pick
up the correct fluff to use.
+ *
+ * <p>
+ * The syntax for embedded ANSI codes is:
+ * 
+ * <pre>
+ *  @|<code>(,<code>)*<space><text>|
+ * </pre>
+ *
+ * @version $Rev$ $Date$
+ */
+public class Renderer
+{
+    public static final String BEGIN_TOKEN = "@|";
+
+    private static final int BEGIN_TOKEN_SIZE = BEGIN_TOKEN.length();
+
+    public static final String END_TOKEN = "|";
+
+    private static final int END_TOKEN_SIZE = END_TOKEN.length();
+
+    public static final String CODE_TEXT_SEPARATOR  = " ";
+
+    public static final String CODE_LIST_SEPARATOR  = ",";
+
+    private final Buffer buff = new Buffer();
+
+    public String render(final String input) throws RenderException {
+        assert input != null;
+
+        // current, prefix and suffix positions
+        int c = 0, p, s;
+
+        while (c < input.length()) {
+            p = input.indexOf(BEGIN_TOKEN, c);
+            if (p < 0) { break; }
+
+            s = input.indexOf(END_TOKEN, p + BEGIN_TOKEN_SIZE);
+            if (s < 0) {
+                throw new RenderException("Missing '" + END_TOKEN + "': " + input);
+            }
+
+            String expr = input.substring(p + BEGIN_TOKEN_SIZE, s);
+
+            buff.append(input.substring(c, p));
+
+            evaluate(expr);
+
+            c = s + END_TOKEN_SIZE;
+        }
+
+        buff.append(input.substring(c));
+
+        return buff.toString();
+    }
+
+    private void evaluate(final String input) throws RenderException {
+        assert input != null;
+
+        int i = input.indexOf(CODE_TEXT_SEPARATOR);
+        if (i < 0) {
+            throw new RenderException("Missing ANSI code/text separator '" + CODE_TEXT_SEPARATOR
+ "': " + input);
+        }
+
+        String tmp = input.substring(0, i);
+        String[] codes = tmp.split(CODE_LIST_SEPARATOR);
+        String text = input.substring(i + 1, input.length());
+
+        for (String name : codes) {
+            int code = Code.forName(name);
+            buff.attrib(code);
+        }
+
+        buff.append(text);
+
+        buff.attrib(Code.OFF);
+    }
+
+    //
+    // RenderException
+    //
+
+    public static class RenderException
+        extends RuntimeException
+    {
+        public RenderException(final String msg) {
+            super(msg);
+        }
+    }
+
+    //
+    // Helpers
+    //
+
+    public static boolean test(final String text) {
+        return text != null && text.indexOf(BEGIN_TOKEN) >= 0;
+    }
+
+    public static String encode(final String text, final int code) {
+        return new StringBuffer(BEGIN_TOKEN).
+                append(Code.name(code)).
+                append(CODE_TEXT_SEPARATOR).
+                append(text).
+                append(END_TOKEN).
+                toString();
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/main/java/org/apache/geronimo/gshell/ansi/Renderer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,45 @@
+/*
+ * 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.geronimo.gshell.ansi;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for the {@link org.apache.geronimo.gshell.ansi.Code} class.
+ *
+ * @version $Rev$ $Date$
+ */
+public class BufferTest
+    extends TestCase
+{
+    public void testIsAnsiEnabledOverride() throws Exception {
+        boolean detected = ANSI.isEnabled();
+        Buffer buff = new Buffer();
+
+        // Make sure the buffer starts out with the system detected value
+        assertEquals(detected, buff.isAnsiEnabled());
+
+        // Then flip it
+        buff.ansiEnabled = !buff.ansiEnabled;
+
+        // And make sure it sticks
+        assertEquals(!detected, buff.isAnsiEnabled());
+    }
+}
\ No newline at end of file

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/BufferTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
(from r570694, geronimo/sandbox/gshell/trunk/gshell-clp/src/test/java/org/apache/geronimo/gshell/clp/PrinterTest.java)
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java?p2=geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java&p1=geronimo/sandbox/gshell/trunk/gshell-clp/src/test/java/org/apache/geronimo/gshell/clp/PrinterTest.java&r1=570694&r2=571366&rev=571366&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-clp/src/test/java/org/apache/geronimo/gshell/clp/PrinterTest.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-ansi/src/test/java/org/apache/geronimo/gshell/ansi/CodeTest.java
Thu Aug 30 21:09:10 2007
@@ -17,53 +17,24 @@
  * under the License.
  */
 
-package org.apache.geronimo.gshell.clp;
-
-import java.io.StringWriter;
-import java.util.List;
+package org.apache.geronimo.gshell.ansi;
 
 import junit.framework.TestCase;
 
 /**
- * Tests for the {@link Printer} class.
+ * Tests for the {@link Code} class.
  *
  * @version $Rev$ $Date$
  */
-public class PrinterTest
+public class CodeTest
     extends TestCase
 {
-    TestBean bean;
-
-    CommandLineProcessor clp;
-
-    protected void setUp() throws Exception {
-        bean = new TestBean();
-        clp = new CommandLineProcessor(bean);
+    public void testForName() throws Exception {
+        assertEquals(Code.OFF, Code.forName("OFF"));
+        assertEquals(Code.OFF, Code.forName("off"));
     }
 
-    protected void tearDown() throws Exception {
-        bean = null;
-        clp = null;
-    }
-
-    public void test1() throws Exception {
-        Printer printer = new Printer(clp);
-
-        StringWriter out = new StringWriter();
-        printer.printUsage(out);
-
-        System.out.println(out.getBuffer());
-    }
-
-    private static class TestBean
-    {
-        @Option(name="-1", aliases={"--foo", "-bar"}, description="this is a test")
-        String a;
-
-        @Option(name="-2", aliases={"--2", "-2"}, description="this is a really, really,
really, really, really, really, really, really, really, really, really, really, really, really,
really, really, really, really, really, really, really, really, really, really long description")
-        String b;
-
-        @Argument(description="these are arguments")
-        List<String> args;
+    public void testName() throws Exception {
+        assertEquals("OFF", Code.name(Code.OFF));
     }
 }

Added: geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,138 @@
+/*
+ * 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.geronimo.gshell.i18n;
+
+import java.util.ResourceBundle;
+import java.util.MissingResourceException;
+
+import java.text.MessageFormat;
+
+/**
+ * Message source backed up by one or more {@link ResourceBundle} instances for simple i18n
support.
+ *
+ * @version $Rev$ $Date$
+ */
+public class MessageSource
+{
+    private final String[] bundleNames;
+
+    private ResourceBundle[] cachedBundles;
+
+    public MessageSource(final String[] names) {
+        assert names != null;
+        assert names.length != 0;
+
+        this.bundleNames = names;
+    }
+
+    public MessageSource(final String name) {
+        this(new String[] { name });
+    }
+
+    private static String[] classNames(final Class[] types) {
+        assert types != null;
+        assert types.length != 0;
+
+        String[] names = new String[types.length];
+
+        for (int i=0; i<types.length; i++) {
+            assert types[i] != null;
+
+            names[i] = types[i].getName();
+        }
+
+        return names;
+    }
+
+    public MessageSource(final Class[] types) {
+        this(classNames(types));
+    }
+
+    public MessageSource(final Class type) {
+        this(new String[] { type.getName() });
+    }
+
+    private ResourceBundle[] createBundles() {
+        ResourceBundle[] bundles = new ResourceBundle[bundleNames.length];
+
+        for (int i=0; i<bundleNames.length; i++) {
+            assert bundleNames[i] != null;
+
+            bundles[i] = ResourceBundle.getBundle(bundleNames[i]);
+        }
+
+        return bundles;
+    }
+
+    private ResourceBundle[] getBundles() {
+        if (cachedBundles == null) {
+            cachedBundles = createBundles();
+        }
+        return cachedBundles;
+    }
+
+    /**
+     * Get a raw message from the resource bundles using the given code.
+     */
+    public String getMessage(final String code) {
+        assert code != null;
+
+        MissingResourceException error = null;
+
+        for (ResourceBundle bundle : getBundles()) {
+            try {
+                return bundle.getString(code);
+            }
+            catch (MissingResourceException e) {
+                //
+                // FIXME: For now just save the first error, should really roll a new message
with all of the details
+                //
+
+                if (error != null) {
+                    error = e;
+                }
+            }
+        }
+
+        assert error != null;
+
+        throw error;
+    }
+
+    /**
+     * Format a message (based on {@link MessageFormat} using the message
+     * from the resource bundles using the given code as a pattern and the
+     * given objects as arguments.
+     */
+    public String format(final String code, final Object... args) {
+        assert args != null;
+
+        String pattern = getMessage(code);
+
+        return MessageFormat.format(pattern, args);
+    }
+
+    /**
+     * @see #getMessage(String)
+     */
+    public Object getProperty(final String name) {
+        return getMessage(name);
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/main/java/org/apache/geronimo/gshell/i18n/MessageSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,52 @@
+/*
+ * 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.geronimo.gshell.i18n;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for the {@link MessageSource} class.
+ *
+ * @version $Id$
+ * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
+ */
+public class MessageSourceTest
+    extends TestCase
+{
+    MessageSource messages;
+
+    protected void setUp() throws Exception {
+        messages = new MessageSource(getClass());
+    }
+
+    public void testLoadAndGetMessage() {
+        String a = messages.getMessage("a");
+        assertEquals("1", a);
+
+        String b = messages.getMessage("b");
+        assertEquals("2", b);
+
+        String c = messages.getMessage("c");
+        assertEquals("3", c);
+
+        String f = messages.format("f", a, b, c);
+        assertEquals("1 2 3", f);
+    }
+}
\ No newline at end of file

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/java/org/apache/geronimo/gshell/i18n/MessageSourceTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties?rev=571366&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
Thu Aug 30 21:09:10 2007
@@ -0,0 +1,30 @@
+##
+## 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.
+##
+
+##
+## $Rev$ $Date$
+##
+
+a=1
+
+b=2
+
+c=3
+
+f={0} {1} {2}
\ No newline at end of file

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-i18n/src/test/resources/org/apache/geronimo/gshell/i18n/MessageSourceTest.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message