Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 8480 invoked from network); 27 Jul 2006 19:02:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Jul 2006 19:02:24 -0000 Received: (qmail 68628 invoked by uid 500); 27 Jul 2006 19:02:22 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 68554 invoked by uid 500); 27 Jul 2006 19:02:22 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 68543 invoked by uid 500); 27 Jul 2006 19:02:21 -0000 Received: (qmail 68540 invoked by uid 99); 27 Jul 2006 19:02:21 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Jul 2006 12:02:21 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 Jul 2006 12:02:21 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id F3D6E1A981A; Thu, 27 Jul 2006 12:02:00 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commons-cvs@jakarta.apache.org From: mrdon@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060727190200.F3D6E1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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