commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mr...@apache.org
Subject svn commit: r426200 - in /jakarta/commons/sandbox/js2j/trunk/src: main/java/org/apache/commons/js2j/ test/java/org/apache/commons/js2j/
Date Thu, 27 Jul 2006 19:01:59 GMT
Author: mrdon
Date: Thu Jul 27 12:01:59 2006
New Revision: 426200

URL: http://svn.apache.org/viewvc?rev=426200&view=rev
Log:
Cleanups, added Properties extension methods

Added:
    jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/PropertiesExtensions.java
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/PropertiesExtensionsTest.java
Modified:
    jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java
    jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java

Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java?rev=426200&r1=426199&r2=426200&view=diff
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java
(original)
+++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java
Thu Jul 27 12:01:59 2006
@@ -16,7 +16,6 @@
 package org.apache.commons.js2j;
 
 import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
@@ -25,7 +24,6 @@
 
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Function;
-import org.mozilla.javascript.NativeJavaObject;
 import org.mozilla.javascript.RegExpProxy;
 import org.mozilla.javascript.ScriptRuntime;
 import org.mozilla.javascript.Scriptable;

Added: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/PropertiesExtensions.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/PropertiesExtensions.java?rev=426200&view=auto
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/PropertiesExtensions.java
(added)
+++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/PropertiesExtensions.java
Thu Jul 27 12:01:59 2006
@@ -0,0 +1,87 @@
+/*
+ * Copyright 1999-2004 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.commons.js2j;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+
+/**
+ * Adds various functions to java.util.Properties
+ * @targetClass java.util.Properties
+ */
+public class PropertiesExtensions {
+
+
+    /**
+     *  Loads properties from a String or File.
+     *
+     *  @funcParams java.io.File file
+     *  @funcReturn java.util.Properties
+     *  @example props.load("file.properties")
+     */
+    public static ExtensionFunction load(final Properties props) {
+        return new ExtensionFunction() {    
+            public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[]
args) 
+                    throws IOException {
+                InputStream in = null;
+                if (args[0] instanceof String) {
+                    in = new FileInputStream((String)args[0]);
+                } else if (args[0] instanceof File) {
+                    in = new FileInputStream((File)args[0]);
+                } else {
+                    in = (InputStream) args[0];
+                }
+                
+                props.load(in);
+                return props;
+            }
+        };
+    }
+    
+    /**
+     *  Stores properties into a File.
+     *
+     *  @funcParams java.io.File file, String header
+     *  @funcReturn java.util.Properties
+     *  @example props.store("file.properties", "My Header")
+     */
+    public static ExtensionFunction store(final Properties props) {
+        return new ExtensionFunction() {    
+            public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[]
args) 
+                    throws IOException {
+                OutputStream in = null;
+                if (args[0] instanceof String) {
+                    in = new FileOutputStream((String)args[0]);
+                } else if (args[0] instanceof File) {
+                    in = new FileOutputStream((File)args[0]);
+                } else {
+                    in = (OutputStream) args[0];
+                }
+                
+                props.store(in, (String)args[1]);
+                return props;
+            }
+        };
+    }
+}

Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java?rev=426200&r1=426199&r2=426200&view=diff
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java
(original)
+++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java
Thu Jul 27 12:01:59 2006
@@ -46,6 +46,7 @@
         addExtensionFunctions(CollectionExtensions.class);
         addExtensionFunctions(ListExtensions.class);
         addExtensionFunctions(FileExtensions.class);
+        addExtensionFunctions(PropertiesExtensions.class);
         addExtensionFunctions(InputStreamExtensions.class);
         
         try {

Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/PropertiesExtensionsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/PropertiesExtensionsTest.java?rev=426200&view=auto
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/PropertiesExtensionsTest.java
(added)
+++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/PropertiesExtensionsTest.java
Thu Jul 27 12:01:59 2006
@@ -0,0 +1,57 @@
+/*
+ * Copyright 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.commons.js2j;
+
+import java.util.List;
+
+public class PropertiesExtensionsTest extends TestScript {
+
+    public void testLoad() throws Exception {
+        run("file = java.io.File.createTempFile('test', '.tmp');");
+        run("writer = new java.io.FileWriter(file);");
+        run("writer.write('# some file\\n');");
+        run("writer.write('foo=bar\\n');");
+        run("writer.close();");
+        run("props = new java.util.Properties()");
+        run("props.load(file)");
+        
+        test("props.getProperty('foo')", "bar");
+        
+        run("props = new java.util.Properties()");
+        run("props.load(file.toString())");
+        
+        test("props.getProperty('foo')", "bar");
+    }
+    
+    public void testStore() throws Exception {
+        run("file = java.io.File.createTempFile('test', '.tmp');");
+        run("props = new java.util.Properties()");
+        run("props.setProperty('foo', 'bar')");
+        run("props.store(file, 'boo')");
+        
+        List lines = load(run("file.toString()"));
+        
+        assertEquals(lines.get(0), "#boo");
+        assertEquals(lines.get(2), "foo=bar");
+        
+        run("props.store(file.toString(), 'boo')");
+        
+        lines = load(run("file.toString()"));
+        
+        assertEquals(lines.get(0), "#boo");
+        assertEquals(lines.get(2), "foo=bar");
+    }
+}

Modified: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java?rev=426200&r1=426199&r2=426200&view=diff
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java
(original)
+++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java
Thu Jul 27 12:01:59 2006
@@ -15,6 +15,14 @@
  */
 package org.apache.commons.js2j;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.tools.shell.Global;
@@ -48,6 +56,23 @@
             ret = ((JavaObjectWrapper)ret).unwrap();
         }
         return ret;
+    }
+    
+    protected List load(Object object) throws IOException {
+        ArrayList list = new ArrayList();
+        FileReader freader = null;
+        try {
+            freader = new FileReader(new File(object.toString()));
+            BufferedReader reader = new BufferedReader(freader);
+            String line = null;
+            
+            while ((line = reader.readLine()) != null) {
+                list.add(line);
+            }
+        } finally {
+            freader.close();
+        }
+        return list;
     }
 
     protected void tearDown() throws Exception {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message