Return-Path: Delivered-To: apmail-incubator-cxf-commits-archive@locus.apache.org Received: (qmail 85443 invoked from network); 23 Feb 2008 02:56:53 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Feb 2008 02:56:53 -0000 Received: (qmail 61611 invoked by uid 500); 23 Feb 2008 02:56:48 -0000 Delivered-To: apmail-incubator-cxf-commits-archive@incubator.apache.org Received: (qmail 61573 invoked by uid 500); 23 Feb 2008 02:56:48 -0000 Mailing-List: contact cxf-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cxf-dev@incubator.apache.org Delivered-To: mailing list cxf-commits@incubator.apache.org Received: (qmail 61564 invoked by uid 99); 23 Feb 2008 02:56:48 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 22 Feb 2008 18:56:48 -0800 X-ASF-Spam-Status: No, hits=-1998.9 required=10.0 tests=ALL_TRUSTED,FB_GET_MEDS X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 23 Feb 2008 02:56:06 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E6D611A984E; Fri, 22 Feb 2008 18:56:24 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r630381 [5/8] - in /incubator/cxf/trunk/rt: bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/ core/src/main/java/org/apache/cxf/databinding/source/mime/ core/src/main/java/org/apache/cxf/interceptor/ core/src/main/java/org/ap... Date: Sat, 23 Feb 2008 02:56:00 -0000 To: cxf-commits@incubator.apache.org From: dkulp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080223025624.E6D611A984E@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java?rev=630381&r1=630380&r2=630381&view=diff ============================================================================== --- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java (original) +++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java Fri Feb 22 18:55:53 2008 @@ -1,396 +1,396 @@ -/** - * 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.cxf.javascript; - -import java.io.IOException; -import java.io.Reader; -import java.lang.reflect.InvocationTargetException; -import java.util.Collection; -import java.util.logging.Logger; - -import org.apache.cxf.common.logging.LogUtils; -import org.apache.cxf.javascript.service.ServiceJavascriptBuilder; -import org.apache.cxf.javascript.types.SchemaJavascriptBuilder; -import org.apache.cxf.service.model.SchemaInfo; -import org.apache.cxf.service.model.ServiceInfo; -import org.apache.cxf.test.TestUtilities; -import org.junit.Assert; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.ContextFactory; -import org.mozilla.javascript.Function; -import org.mozilla.javascript.JavaScriptException; -import org.mozilla.javascript.RhinoException; -import org.mozilla.javascript.Scriptable; -import org.mozilla.javascript.ScriptableObject; -import org.mozilla.javascript.tools.debugger.Main; - -/** - * Test utilities class with some Javascript capability included. - */ -public class JavascriptTestUtilities extends TestUtilities { - - private static final Logger LOG = LogUtils.getL7dLogger(JavascriptTestUtilities.class); - private static boolean rhinoDebuggerUp; - private ContextFactory rhinoContextFactory; - private ScriptableObject rhinoScope; - private Context rhinoContext; - - public static class JavaScriptAssertionFailed extends RuntimeException { - - public JavaScriptAssertionFailed(String what) { - super(what); - } - } - - public static class JsAssert extends ScriptableObject { - - public JsAssert() { - } - - public void jsConstructor(String exp) { - LOG.severe("Assertion failed: " + exp); - throw new JavaScriptAssertionFailed(exp); - } - - @Override - public String getClassName() { - return "Assert"; - } - } - - public static class Trace extends ScriptableObject { - - public Trace() { - } - - @Override - public String getClassName() { - return "org_apache_cxf_trace"; - } - - // CHECKSTYLE:OFF - public static void jsStaticFunction_trace(String message) { - LOG.fine(message); - } - // CHECKSTYLE:ON - } - - public static class Notifier extends ScriptableObject { - - private boolean notified; - - public Notifier() { - } - - @Override - public String getClassName() { - return "org_apache_cxf_notifier"; - } - - public synchronized boolean waitForJavascript(long timeout) { - while (!notified) { - try { - wait(timeout); - return notified; - } catch (InterruptedException e) { - // do nothing. - } - } - return true; // only here if true on entry. - } - - // CHECKSTYLE:OFF - public synchronized void jsFunction_notify() { - notified = true; - notifyAll(); - } - // CHECKSTYLE:ON - } - - public JavascriptTestUtilities(Class classpathReference) { - super(classpathReference); - } - - public void initializeRhino() { - - rhinoContextFactory = new ContextFactory(); - if (System.getProperty("cxf.jsdebug") != null && !rhinoDebuggerUp) { - Main.mainEmbedded(rhinoContextFactory, rhinoScope, "Debug embedded JavaScript."); - rhinoDebuggerUp = true; - } - - rhinoContext = rhinoContextFactory.enter(); - rhinoScope = rhinoContext.initStandardObjects(); - - try { - ScriptableObject.defineClass(rhinoScope, JsAssert.class); - ScriptableObject.defineClass(rhinoScope, Trace.class); - ScriptableObject.defineClass(rhinoScope, Notifier.class); - // so that the stock test for IE can gracefully fail. - rhinoContext.evaluateString(rhinoScope, "var window = new Object();", - "", 0, null); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } finally { - rhinoContextFactory.exit(); - } - JsSimpleDomNode.register(rhinoScope); - JsSimpleDomParser.register(rhinoScope); - JsNamedNodeMap.register(rhinoScope); - JsXMLHttpRequest.register(rhinoScope); - } - - public void readResourceIntoRhino(String resourceClasspath) throws IOException { - Reader js = getResourceAsReader(resourceClasspath); - rhinoContextFactory.enter(rhinoContext); - try { - rhinoContext.evaluateReader(rhinoScope, js, resourceClasspath, 1, null); - } finally { - rhinoContextFactory.exit(); - } - } - - public void readStringIntoRhino(String js, String sourceName) { - LOG.fine(sourceName + ":\n" + js); - rhinoContextFactory.enter(rhinoContext); - try { - rhinoContext.evaluateString(rhinoScope, js, sourceName, 1, null); - } finally { - rhinoContextFactory.exit(); - } - } - - public ScriptableObject getRhinoScope() { - return rhinoScope; - } - - public ContextFactory getRhinoContextFactory() { - return rhinoContextFactory; - } - - public static interface JSRunnable { - T run(Context context); - } - - public T runInsideContext(Class clazz, JSRunnable runnable) { - rhinoContextFactory.enter(rhinoContext); - try { - return clazz.cast(runnable.run(rhinoContext)); - } finally { - rhinoContextFactory.exit(); - } - } - - public Object javaToJS(Object value) { - return Context.javaToJS(value, rhinoScope); - } - - public Object rhinoNewObject(final String constructorName) { - return runInsideContext(Object.class, new JSRunnable() { - public Object run(Context context) { - return context.newObject(rhinoScope, constructorName); - } - }); - } - - /** - * Evaluate a javascript expression, returning the raw Rhino object. - * - * @param jsExpression the javascript expression. - * @return return value. - */ - public Object rhinoEvaluate(final String jsExpression) { - return runInsideContext(Object.class, new JSRunnable() { - public Object run(Context context) { - return rhinoContext.evaluateString(rhinoScope, jsExpression, "", 1, null); - } - }); - } - - /** - * Call a method on a Javascript object. - * - * @param that the object. - * @param methodName method name. - * @param args arguments. - * @return - */ - public Object rhinoCallMethod(Scriptable that, String methodName, Object... args) { - return ScriptableObject.callMethod(rhinoContext, that, methodName, args); - } - - /** - * Call a method on a Javascript object and convert result to specified class. Convert to the - * requested class. - * @param type - * @param clazz class object. - * @param that Javascript object. - * @param methodName method - * @param args arguments - * @return return value. - */ - public T rhinoCallMethodConvert(Class clazz, Scriptable that, String methodName, Object... args) { - return clazz.cast(Context.jsToJava(rhinoCallMethod(that, methodName, args), clazz)); - } - - /** - * Call a method on a Javascript object inside context brackets. - * @param return type. - * @param clazz class for the return type. - * @param that object - * @param methodName method - * @param args arguments. Caller must run javaToJS as appropriate - * @return return value. - */ - public T rhinoCallMethodInContext(final Class clazz, final Scriptable that, - final String methodName, - final Object... args) { - // we end up performing the cast twice to make the compiler happy. - return runInsideContext(clazz, new JSRunnable() { - public T run(Context context) { - return rhinoCallMethodConvert(clazz, that, methodName, args); - } - }); - } - - /** - * Evaluate a Javascript expression, converting the return value to a - * convenient Java type. - * - * @param The desired type - * @param jsExpression the javascript expression. - * @param clazz the Class object for the desired type. - * @return the result. - */ - public T rhinoEvaluateConvert(String jsExpression, Class clazz) { - return clazz.cast(Context.jsToJava(rhinoEvaluate(jsExpression), clazz)); - } - - /** - * Call a JavaScript function within the Context. Optionally, require it to - * throw an exception equal to a supplied object. If the exception is called - * for, this function will either return null or Assert. - * - * @param expectingException Exception desired, or null. - * @param functionName Function to call. - * @param args args for the function. Be sure to Javascript-ify them as - * appropriate. - * @return - */ - public Object rhinoCallExpectingExceptionInContext(final Object expectingException, - final String functionName, final Object... args) { - return runInsideContext(Object.class, new JSRunnable() { - public Object run(Context context) { - return rhinoCallExpectingException(expectingException, functionName, args); - } - }); - } - - /** - * Call a Javascript function, identified by name, on a set of arguments. - * Optionally, expect it to throw an exception. - * - * @param expectingException - * @param functionName - * @param args - * @return - */ - public Object rhinoCallExpectingException(final Object expectingException, final String functionName, - final Object... args) { - Object fObj = rhinoScope.get(functionName, rhinoScope); - if (!(fObj instanceof Function)) { - throw new RuntimeException("Missing test function " + functionName); - } - Function function = (Function)fObj; - try { - return function.call(rhinoContext, rhinoScope, rhinoScope, args); - } catch (RhinoException angryRhino) { - if (expectingException != null && angryRhino instanceof JavaScriptException) { - JavaScriptException jse = (JavaScriptException)angryRhino; - Assert.assertEquals(jse.getValue(), expectingException); - return null; - } - String trace = angryRhino.getScriptStackTrace(); - Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace); - } catch (JavaScriptAssertionFailed assertion) { - Assert.fail(assertion.getMessage()); - } - return null; - } - - public Object rhinoCallInContext(String functionName, Object... args) { - return rhinoCallExpectingExceptionInContext(null, functionName, args); - } - - public Object rhinoCall(String functionName, Object... args) { - return rhinoCallExpectingException(null, functionName, args); - } - - public T rhinoCallConvert(String functionName, Class clazz, Object... args) { - return clazz.cast(Context.jsToJava(rhinoCallInContext(functionName, args), clazz)); - } - - public void loadJavascriptForService(ServiceInfo serviceInfo) { - Collection schemata = serviceInfo.getSchemas(); - BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo); - NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo - .getXmlSchemaCollection()); - for (SchemaInfo schema : schemata) { - SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo - .getXmlSchemaCollection(), prefixManager, nameManager); - String allThatJavascript = builder.generateCodeForSchema(schema); - readStringIntoRhino(allThatJavascript, schema.toString() + ".js"); - } - - ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, prefixManager, - nameManager); - serviceBuilder.walk(); - String serviceJavascript = serviceBuilder.getCode(); - readStringIntoRhino(serviceJavascript, serviceInfo.getName() + ".js"); - } - - public static String scriptableToString(Scriptable scriptable) { - StringBuilder builder = new StringBuilder(); - for (Object propid : scriptable.getIds()) { - String propIdString = Context.toString(propid); - int propIntKey = -1; - try { - propIntKey = Integer.parseInt(propIdString); - } catch (NumberFormatException nfe) { - // dummy. - } - String propValue; - if (propIntKey >= 0) { - propValue = Context.toString(scriptable.get(propIntKey, scriptable)); - } else { - propValue = Context.toString(scriptable.get(propIdString, scriptable)); - } - builder.append(propIdString); - builder.append(": "); - builder.append(propValue); - builder.append("; "); - } - return builder.toString(); - } -} +/** + * 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.cxf.javascript; + +import java.io.IOException; +import java.io.Reader; +import java.lang.reflect.InvocationTargetException; +import java.util.Collection; +import java.util.logging.Logger; + +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.javascript.service.ServiceJavascriptBuilder; +import org.apache.cxf.javascript.types.SchemaJavascriptBuilder; +import org.apache.cxf.service.model.SchemaInfo; +import org.apache.cxf.service.model.ServiceInfo; +import org.apache.cxf.test.TestUtilities; +import org.junit.Assert; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ContextFactory; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.JavaScriptException; +import org.mozilla.javascript.RhinoException; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.tools.debugger.Main; + +/** + * Test utilities class with some Javascript capability included. + */ +public class JavascriptTestUtilities extends TestUtilities { + + private static final Logger LOG = LogUtils.getL7dLogger(JavascriptTestUtilities.class); + private static boolean rhinoDebuggerUp; + private ContextFactory rhinoContextFactory; + private ScriptableObject rhinoScope; + private Context rhinoContext; + + public static class JavaScriptAssertionFailed extends RuntimeException { + + public JavaScriptAssertionFailed(String what) { + super(what); + } + } + + public static class JsAssert extends ScriptableObject { + + public JsAssert() { + } + + public void jsConstructor(String exp) { + LOG.severe("Assertion failed: " + exp); + throw new JavaScriptAssertionFailed(exp); + } + + @Override + public String getClassName() { + return "Assert"; + } + } + + public static class Trace extends ScriptableObject { + + public Trace() { + } + + @Override + public String getClassName() { + return "org_apache_cxf_trace"; + } + + // CHECKSTYLE:OFF + public static void jsStaticFunction_trace(String message) { + LOG.fine(message); + } + // CHECKSTYLE:ON + } + + public static class Notifier extends ScriptableObject { + + private boolean notified; + + public Notifier() { + } + + @Override + public String getClassName() { + return "org_apache_cxf_notifier"; + } + + public synchronized boolean waitForJavascript(long timeout) { + while (!notified) { + try { + wait(timeout); + return notified; + } catch (InterruptedException e) { + // do nothing. + } + } + return true; // only here if true on entry. + } + + // CHECKSTYLE:OFF + public synchronized void jsFunction_notify() { + notified = true; + notifyAll(); + } + // CHECKSTYLE:ON + } + + public JavascriptTestUtilities(Class classpathReference) { + super(classpathReference); + } + + public void initializeRhino() { + + rhinoContextFactory = new ContextFactory(); + if (System.getProperty("cxf.jsdebug") != null && !rhinoDebuggerUp) { + Main.mainEmbedded(rhinoContextFactory, rhinoScope, "Debug embedded JavaScript."); + rhinoDebuggerUp = true; + } + + rhinoContext = rhinoContextFactory.enter(); + rhinoScope = rhinoContext.initStandardObjects(); + + try { + ScriptableObject.defineClass(rhinoScope, JsAssert.class); + ScriptableObject.defineClass(rhinoScope, Trace.class); + ScriptableObject.defineClass(rhinoScope, Notifier.class); + // so that the stock test for IE can gracefully fail. + rhinoContext.evaluateString(rhinoScope, "var window = new Object();", + "", 0, null); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } finally { + rhinoContextFactory.exit(); + } + JsSimpleDomNode.register(rhinoScope); + JsSimpleDomParser.register(rhinoScope); + JsNamedNodeMap.register(rhinoScope); + JsXMLHttpRequest.register(rhinoScope); + } + + public void readResourceIntoRhino(String resourceClasspath) throws IOException { + Reader js = getResourceAsReader(resourceClasspath); + rhinoContextFactory.enter(rhinoContext); + try { + rhinoContext.evaluateReader(rhinoScope, js, resourceClasspath, 1, null); + } finally { + rhinoContextFactory.exit(); + } + } + + public void readStringIntoRhino(String js, String sourceName) { + LOG.fine(sourceName + ":\n" + js); + rhinoContextFactory.enter(rhinoContext); + try { + rhinoContext.evaluateString(rhinoScope, js, sourceName, 1, null); + } finally { + rhinoContextFactory.exit(); + } + } + + public ScriptableObject getRhinoScope() { + return rhinoScope; + } + + public ContextFactory getRhinoContextFactory() { + return rhinoContextFactory; + } + + public static interface JSRunnable { + T run(Context context); + } + + public T runInsideContext(Class clazz, JSRunnable runnable) { + rhinoContextFactory.enter(rhinoContext); + try { + return clazz.cast(runnable.run(rhinoContext)); + } finally { + rhinoContextFactory.exit(); + } + } + + public Object javaToJS(Object value) { + return Context.javaToJS(value, rhinoScope); + } + + public Object rhinoNewObject(final String constructorName) { + return runInsideContext(Object.class, new JSRunnable() { + public Object run(Context context) { + return context.newObject(rhinoScope, constructorName); + } + }); + } + + /** + * Evaluate a javascript expression, returning the raw Rhino object. + * + * @param jsExpression the javascript expression. + * @return return value. + */ + public Object rhinoEvaluate(final String jsExpression) { + return runInsideContext(Object.class, new JSRunnable() { + public Object run(Context context) { + return rhinoContext.evaluateString(rhinoScope, jsExpression, "", 1, null); + } + }); + } + + /** + * Call a method on a Javascript object. + * + * @param that the object. + * @param methodName method name. + * @param args arguments. + * @return + */ + public Object rhinoCallMethod(Scriptable that, String methodName, Object... args) { + return ScriptableObject.callMethod(rhinoContext, that, methodName, args); + } + + /** + * Call a method on a Javascript object and convert result to specified class. Convert to the + * requested class. + * @param type + * @param clazz class object. + * @param that Javascript object. + * @param methodName method + * @param args arguments + * @return return value. + */ + public T rhinoCallMethodConvert(Class clazz, Scriptable that, String methodName, Object... args) { + return clazz.cast(Context.jsToJava(rhinoCallMethod(that, methodName, args), clazz)); + } + + /** + * Call a method on a Javascript object inside context brackets. + * @param return type. + * @param clazz class for the return type. + * @param that object + * @param methodName method + * @param args arguments. Caller must run javaToJS as appropriate + * @return return value. + */ + public T rhinoCallMethodInContext(final Class clazz, final Scriptable that, + final String methodName, + final Object... args) { + // we end up performing the cast twice to make the compiler happy. + return runInsideContext(clazz, new JSRunnable() { + public T run(Context context) { + return rhinoCallMethodConvert(clazz, that, methodName, args); + } + }); + } + + /** + * Evaluate a Javascript expression, converting the return value to a + * convenient Java type. + * + * @param The desired type + * @param jsExpression the javascript expression. + * @param clazz the Class object for the desired type. + * @return the result. + */ + public T rhinoEvaluateConvert(String jsExpression, Class clazz) { + return clazz.cast(Context.jsToJava(rhinoEvaluate(jsExpression), clazz)); + } + + /** + * Call a JavaScript function within the Context. Optionally, require it to + * throw an exception equal to a supplied object. If the exception is called + * for, this function will either return null or Assert. + * + * @param expectingException Exception desired, or null. + * @param functionName Function to call. + * @param args args for the function. Be sure to Javascript-ify them as + * appropriate. + * @return + */ + public Object rhinoCallExpectingExceptionInContext(final Object expectingException, + final String functionName, final Object... args) { + return runInsideContext(Object.class, new JSRunnable() { + public Object run(Context context) { + return rhinoCallExpectingException(expectingException, functionName, args); + } + }); + } + + /** + * Call a Javascript function, identified by name, on a set of arguments. + * Optionally, expect it to throw an exception. + * + * @param expectingException + * @param functionName + * @param args + * @return + */ + public Object rhinoCallExpectingException(final Object expectingException, final String functionName, + final Object... args) { + Object fObj = rhinoScope.get(functionName, rhinoScope); + if (!(fObj instanceof Function)) { + throw new RuntimeException("Missing test function " + functionName); + } + Function function = (Function)fObj; + try { + return function.call(rhinoContext, rhinoScope, rhinoScope, args); + } catch (RhinoException angryRhino) { + if (expectingException != null && angryRhino instanceof JavaScriptException) { + JavaScriptException jse = (JavaScriptException)angryRhino; + Assert.assertEquals(jse.getValue(), expectingException); + return null; + } + String trace = angryRhino.getScriptStackTrace(); + Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace); + } catch (JavaScriptAssertionFailed assertion) { + Assert.fail(assertion.getMessage()); + } + return null; + } + + public Object rhinoCallInContext(String functionName, Object... args) { + return rhinoCallExpectingExceptionInContext(null, functionName, args); + } + + public Object rhinoCall(String functionName, Object... args) { + return rhinoCallExpectingException(null, functionName, args); + } + + public T rhinoCallConvert(String functionName, Class clazz, Object... args) { + return clazz.cast(Context.jsToJava(rhinoCallInContext(functionName, args), clazz)); + } + + public void loadJavascriptForService(ServiceInfo serviceInfo) { + Collection schemata = serviceInfo.getSchemas(); + BasicNameManager nameManager = BasicNameManager.newNameManager(serviceInfo); + NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo + .getXmlSchemaCollection()); + for (SchemaInfo schema : schemata) { + SchemaJavascriptBuilder builder = new SchemaJavascriptBuilder(serviceInfo + .getXmlSchemaCollection(), prefixManager, nameManager); + String allThatJavascript = builder.generateCodeForSchema(schema); + readStringIntoRhino(allThatJavascript, schema.toString() + ".js"); + } + + ServiceJavascriptBuilder serviceBuilder = new ServiceJavascriptBuilder(serviceInfo, prefixManager, + nameManager); + serviceBuilder.walk(); + String serviceJavascript = serviceBuilder.getCode(); + readStringIntoRhino(serviceJavascript, serviceInfo.getName() + ".js"); + } + + public static String scriptableToString(Scriptable scriptable) { + StringBuilder builder = new StringBuilder(); + for (Object propid : scriptable.getIds()) { + String propIdString = Context.toString(propid); + int propIntKey = -1; + try { + propIntKey = Integer.parseInt(propIdString); + } catch (NumberFormatException nfe) { + // dummy. + } + String propValue; + if (propIntKey >= 0) { + propValue = Context.toString(scriptable.get(propIntKey, scriptable)); + } else { + propValue = Context.toString(scriptable.get(propIdString, scriptable)); + } + builder.append(propIdString); + builder.append(": "); + builder.append(propValue); + builder.append("; "); + } + return builder.toString(); + } +} Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JavascriptTestUtilities.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java?rev=630381&r1=630380&r2=630381&view=diff ============================================================================== --- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java (original) +++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java Fri Feb 22 18:55:53 2008 @@ -1,139 +1,139 @@ -/** - * 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.cxf.javascript; - -import java.io.File; -import java.io.Reader; -import java.io.StringWriter; -import java.net.URL; -import java.util.Properties; - -import org.w3c.dom.Document; - -import org.apache.cxf.Bus; -import org.apache.cxf.javascript.JavascriptTestUtilities.Notifier; -import org.apache.cxf.jaxws.EndpointImpl; -import org.apache.cxf.test.AbstractCXFSpringTest; -import org.jaxen.XPath; -import org.jaxen.dom.DOMXPath; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; -import org.springframework.context.support.GenericApplicationContext; - -/** - * - */ -public class JsHttpRequestTest extends AbstractCXFSpringTest { - - // shadow declaration from base class. - private JavascriptTestUtilities testUtilities; - - public JsHttpRequestTest() throws Exception { - testUtilities = new JavascriptTestUtilities(getClass()); - testUtilities.addDefaultNamespaces(); - } - - public void additionalSpringConfiguration(GenericApplicationContext applicationContext) throws Exception { - // bring in some property values from a Properties file - PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); - Properties properties = new Properties(); - properties.setProperty("staticResourceURL", getStaticResourceURL()); - cfg.setProperties(properties); - // now actually do the replacement - cfg.postProcessBeanFactory(applicationContext.getBeanFactory()); - } - - @Override - protected String[] getConfigLocations() { - return new String[] {"classpath:XMLHttpRequestTestBeans.xml"}; - } - - - @Before - public - void setupRhino() throws Exception { - testUtilities.setBus(getBean(Bus.class, "cxf")); - testUtilities.initializeRhino(); - testUtilities.readResourceIntoRhino("/org/apache/cxf/javascript/XMLHttpRequestTests.js"); - } - - // just one test function to avoid muddles with engine startup/shutdown - @Test - public void runTests() throws Exception { - testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testOpaqueURI"); - testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testNonAbsolute"); - testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testNonHttp"); - testUtilities.rhinoCallExpectingExceptionInContext("INVALID_STATE_ERR", "testSendNotOpenError"); - testUtilities.rhinoCallInContext("testStateNotificationSync"); - Notifier notifier = testUtilities.rhinoCallConvert("testAsyncHttpFetch1", Notifier.class); - testUtilities.rhinoCallInContext("testAsyncHttpFetch2"); - boolean notified = notifier.waitForJavascript(10); - assertTrue(notified); - assertEquals("HEADERS_RECEIVED", Boolean.TRUE, - testUtilities.rhinoEvaluateConvert("asyncGotHeadersReceived", Boolean.class)); - assertEquals("LOADING", Boolean.TRUE, - testUtilities.rhinoEvaluateConvert("asyncGotLoading", Boolean.class)); - assertEquals("DONE", Boolean.TRUE, - testUtilities.rhinoEvaluateConvert("asyncGotDone", Boolean.class)); - String outOfOrder = testUtilities.rhinoEvaluateConvert("outOfOrderError", String.class); - assertEquals("OutOfOrder", null, outOfOrder); - assertEquals("status 200", Integer.valueOf(200), - testUtilities.rhinoEvaluateConvert("asyncStatus", Integer.class)); - assertEquals("status text", "OK", - testUtilities.rhinoEvaluateConvert("asyncStatusText", String.class)); - assertTrue("headers", testUtilities.rhinoEvaluateConvert("asyncResponseHeaders", String.class) - .contains("Content-Type: text/html")); - Object httpObj = testUtilities.rhinoCallInContext("testSyncHttpFetch"); - assertNotNull(httpObj); - assertTrue(httpObj instanceof String); - String httpResponse = (String) httpObj; - // check for 'Shalom' in Hebrew as a charset check. - assertTrue(httpResponse.contains("\u05e9\u05dc\u05d5\u05dd")); - Reader r = getResourceAsReader("/org/apache/cxf/javascript/XML_GreetMeDocLiteralReq.xml"); - StringWriter writer = new StringWriter(); - char[] buffer = new char[1024]; - int readCount; - while ((readCount = r.read(buffer, 0, 1024)) > 0) { - writer.write(buffer, 0, readCount); - } - String xml = writer.toString(); - EndpointImpl endpoint = this.getBean(EndpointImpl.class, "greeter-service-endpoint"); - JsSimpleDomNode xmlResponse = - testUtilities.rhinoCallConvert("testSyncXml", - JsSimpleDomNode.class, - testUtilities.javaToJS(endpoint.getAddress()), - testUtilities.javaToJS(xml)); - assertNotNull(xmlResponse); - Document doc = (Document)xmlResponse.getWrappedNode(); - XPath echoStringPath = new DOMXPath("//t:responseType/text()"); - echoStringPath.addNamespace("t", "http://apache.org/hello_world_xml_http/wrapped/types"); - String nodeText = echoStringPath.stringValueOf(echoStringPath.selectSingleNode(doc)); - assertEquals(nodeText, "Hello \u05e9\u05dc\u05d5\u05dd"); - } - - public String getStaticResourceURL() throws Exception { - File staticFile = new File(this.getClass().getResource("test.html").toURI()); - staticFile = staticFile.getParentFile(); - staticFile = staticFile.getAbsoluteFile(); - URL furl = staticFile.toURI().toURL(); - return furl.toString(); - } -} +/** + * 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.cxf.javascript; + +import java.io.File; +import java.io.Reader; +import java.io.StringWriter; +import java.net.URL; +import java.util.Properties; + +import org.w3c.dom.Document; + +import org.apache.cxf.Bus; +import org.apache.cxf.javascript.JavascriptTestUtilities.Notifier; +import org.apache.cxf.jaxws.EndpointImpl; +import org.apache.cxf.test.AbstractCXFSpringTest; +import org.jaxen.XPath; +import org.jaxen.dom.DOMXPath; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.support.GenericApplicationContext; + +/** + * + */ +public class JsHttpRequestTest extends AbstractCXFSpringTest { + + // shadow declaration from base class. + private JavascriptTestUtilities testUtilities; + + public JsHttpRequestTest() throws Exception { + testUtilities = new JavascriptTestUtilities(getClass()); + testUtilities.addDefaultNamespaces(); + } + + public void additionalSpringConfiguration(GenericApplicationContext applicationContext) throws Exception { + // bring in some property values from a Properties file + PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); + Properties properties = new Properties(); + properties.setProperty("staticResourceURL", getStaticResourceURL()); + cfg.setProperties(properties); + // now actually do the replacement + cfg.postProcessBeanFactory(applicationContext.getBeanFactory()); + } + + @Override + protected String[] getConfigLocations() { + return new String[] {"classpath:XMLHttpRequestTestBeans.xml"}; + } + + + @Before + public + void setupRhino() throws Exception { + testUtilities.setBus(getBean(Bus.class, "cxf")); + testUtilities.initializeRhino(); + testUtilities.readResourceIntoRhino("/org/apache/cxf/javascript/XMLHttpRequestTests.js"); + } + + // just one test function to avoid muddles with engine startup/shutdown + @Test + public void runTests() throws Exception { + testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testOpaqueURI"); + testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testNonAbsolute"); + testUtilities.rhinoCallExpectingExceptionInContext("SYNTAX_ERR", "testNonHttp"); + testUtilities.rhinoCallExpectingExceptionInContext("INVALID_STATE_ERR", "testSendNotOpenError"); + testUtilities.rhinoCallInContext("testStateNotificationSync"); + Notifier notifier = testUtilities.rhinoCallConvert("testAsyncHttpFetch1", Notifier.class); + testUtilities.rhinoCallInContext("testAsyncHttpFetch2"); + boolean notified = notifier.waitForJavascript(10); + assertTrue(notified); + assertEquals("HEADERS_RECEIVED", Boolean.TRUE, + testUtilities.rhinoEvaluateConvert("asyncGotHeadersReceived", Boolean.class)); + assertEquals("LOADING", Boolean.TRUE, + testUtilities.rhinoEvaluateConvert("asyncGotLoading", Boolean.class)); + assertEquals("DONE", Boolean.TRUE, + testUtilities.rhinoEvaluateConvert("asyncGotDone", Boolean.class)); + String outOfOrder = testUtilities.rhinoEvaluateConvert("outOfOrderError", String.class); + assertEquals("OutOfOrder", null, outOfOrder); + assertEquals("status 200", Integer.valueOf(200), + testUtilities.rhinoEvaluateConvert("asyncStatus", Integer.class)); + assertEquals("status text", "OK", + testUtilities.rhinoEvaluateConvert("asyncStatusText", String.class)); + assertTrue("headers", testUtilities.rhinoEvaluateConvert("asyncResponseHeaders", String.class) + .contains("Content-Type: text/html")); + Object httpObj = testUtilities.rhinoCallInContext("testSyncHttpFetch"); + assertNotNull(httpObj); + assertTrue(httpObj instanceof String); + String httpResponse = (String) httpObj; + // check for 'Shalom' in Hebrew as a charset check. + assertTrue(httpResponse.contains("\u05e9\u05dc\u05d5\u05dd")); + Reader r = getResourceAsReader("/org/apache/cxf/javascript/XML_GreetMeDocLiteralReq.xml"); + StringWriter writer = new StringWriter(); + char[] buffer = new char[1024]; + int readCount; + while ((readCount = r.read(buffer, 0, 1024)) > 0) { + writer.write(buffer, 0, readCount); + } + String xml = writer.toString(); + EndpointImpl endpoint = this.getBean(EndpointImpl.class, "greeter-service-endpoint"); + JsSimpleDomNode xmlResponse = + testUtilities.rhinoCallConvert("testSyncXml", + JsSimpleDomNode.class, + testUtilities.javaToJS(endpoint.getAddress()), + testUtilities.javaToJS(xml)); + assertNotNull(xmlResponse); + Document doc = (Document)xmlResponse.getWrappedNode(); + XPath echoStringPath = new DOMXPath("//t:responseType/text()"); + echoStringPath.addNamespace("t", "http://apache.org/hello_world_xml_http/wrapped/types"); + String nodeText = echoStringPath.stringValueOf(echoStringPath.selectSingleNode(doc)); + assertEquals(nodeText, "Hello \u05e9\u05dc\u05d5\u05dd"); + } + + public String getStaticResourceURL() throws Exception { + File staticFile = new File(this.getClass().getResource("test.html").toURI()); + staticFile = staticFile.getParentFile(); + staticFile = staticFile.getAbsoluteFile(); + URL furl = staticFile.toURI().toURL(); + return furl.toString(); + } +} Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsHttpRequestTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java?rev=630381&r1=630380&r2=630381&view=diff ============================================================================== --- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java (original) +++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java Fri Feb 22 18:55:53 2008 @@ -1,218 +1,218 @@ -/** - * 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.cxf.javascript; - -import java.lang.reflect.InvocationTargetException; - -import org.w3c.dom.Attr; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; -import org.mozilla.javascript.ScriptableObject; - -/** - * A Rhino wrapper around org.w3c.dom.Node. Not comprehensive, but enough to test CXF JavaScript. - */ -public class JsSimpleDomNode extends ScriptableObject { - private Node wrappedNode; - private boolean childrenWrapped; - private boolean attributesWrapped; - private JsSimpleDomNode previousSibling; - private JsSimpleDomNode nextSibling; - private JsSimpleDomNode[] children; - private JsNamedNodeMap attributes; - - /** - * Only exists to make Rhino happy. Should never be used. - */ - public JsSimpleDomNode() { - } - - public static void register(ScriptableObject scope) { - try { - ScriptableObject.defineClass(scope, JsSimpleDomNode.class); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - @Override - public String getClassName() { - return "Node"; - } - - public Node getWrappedNode() { - return wrappedNode; - } - - //CHECKSTYLE:OFF - public String jsGet_localName() { - return wrappedNode.getLocalName(); - } - - public String jsGet_namespaceURI() { - return wrappedNode.getNamespaceURI(); - } - - public Object jsGet_firstChild() { - establishChildren(); - if (children.length > 0) - return children[0]; - else - return null; - } - - public Object jsGet_nextSibling() { - return nextSibling; - } - - public Object jsGet_previousSibling() { - return previousSibling; - } - - public Object jsGet_parentNode() { - // risk errors in object equality ... - return wrapNode(this, wrappedNode.getParentNode()); - } - - public int jsGet_nodeType() { - return wrappedNode.getNodeType(); - } - - public String jsGet_nodeValue() { - return wrappedNode.getNodeValue(); - } - - public String jsGet_nodeName() { - return wrappedNode.getNodeName(); - } - - // in a more complete version of this, we'd use a different object type to wrap documents. - public Object jsGet_documentElement() { - if(9 /* Document */ != wrappedNode.getNodeType()) { - return null; - } else { - establishChildren(); - return children[0]; // it is, after all, just a convenience feature. - } - } - - public Object[] jsGet_childNodes() { - establishChildren(); - return children; - } - - public Object jsGet_attributes() { - establishAttributes(); - return attributes; - } - - public String jsFunction_getAttributeNS(String namespaceURI, String localName) { - NamedNodeMap attributes = wrappedNode.getAttributes(); - Node attrNode = attributes.getNamedItemNS(namespaceURI, localName); - if(attrNode == null) { - return null; - } else { - Attr attribute = (Attr) attrNode; - return attribute.getValue(); - } - } - - public String jsFunction_getAttribute(String localName) { - NamedNodeMap attributes = wrappedNode.getAttributes(); - Node attrNode = attributes.getNamedItem(localName); - if(attrNode == null) { - return null; - } else { - Attr attribute = (Attr) attrNode; - return attribute.getValue(); - } - } - - //CHECKSTYLE:ON - - public static JsSimpleDomNode wrapNode(Scriptable scope, Node node) { - if (node == null) { - return null; - } - - Context cx = Context.enter(); - JsSimpleDomNode newObject = (JsSimpleDomNode)cx.newObject(scope, "Node"); - newObject.initialize(node, null); - return newObject; - } - - private JsSimpleDomNode newObject(Node node, JsSimpleDomNode prev) { - Context cx = Context.enter(); - JsSimpleDomNode newObject = (JsSimpleDomNode)cx.newObject(getParentScope(), "Node"); - newObject.initialize(node, prev); - return newObject; - } - - private void establishChildren() { - if (!childrenWrapped) { - if (wrappedNode.hasChildNodes()) { - NodeList nodeChildren = wrappedNode.getChildNodes(); - children = new JsSimpleDomNode[nodeChildren.getLength()]; - for (int x = 0; x < nodeChildren.getLength(); x++) { - JsSimpleDomNode prev = null; - if (x > 0) { - prev = (JsSimpleDomNode)children[x - 1]; - } - children[x] = newObject(nodeChildren.item(x), prev); - if (x > 0) { - children[x - 1].setNext(children[x]); - } - } - } else { - children = new JsSimpleDomNode[0]; - } - childrenWrapped = true; - } - } - - private void establishAttributes() { - if (!attributesWrapped) { - NamedNodeMap nodeAttributes = wrappedNode.getAttributes(); - attributes = JsNamedNodeMap.wrapMap(getParentScope(), nodeAttributes); - attributesWrapped = true; - } - } - - //rhino won't let us use a constructor. - void initialize(Node node, JsSimpleDomNode prev) { - wrappedNode = node; - childrenWrapped = false; - previousSibling = prev; - } - - void setNext(JsSimpleDomNode next) { - nextSibling = next; - } - - -} +/** + * 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.cxf.javascript; + +import java.lang.reflect.InvocationTargetException; + +import org.w3c.dom.Attr; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; + +/** + * A Rhino wrapper around org.w3c.dom.Node. Not comprehensive, but enough to test CXF JavaScript. + */ +public class JsSimpleDomNode extends ScriptableObject { + private Node wrappedNode; + private boolean childrenWrapped; + private boolean attributesWrapped; + private JsSimpleDomNode previousSibling; + private JsSimpleDomNode nextSibling; + private JsSimpleDomNode[] children; + private JsNamedNodeMap attributes; + + /** + * Only exists to make Rhino happy. Should never be used. + */ + public JsSimpleDomNode() { + } + + public static void register(ScriptableObject scope) { + try { + ScriptableObject.defineClass(scope, JsSimpleDomNode.class); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + @Override + public String getClassName() { + return "Node"; + } + + public Node getWrappedNode() { + return wrappedNode; + } + + //CHECKSTYLE:OFF + public String jsGet_localName() { + return wrappedNode.getLocalName(); + } + + public String jsGet_namespaceURI() { + return wrappedNode.getNamespaceURI(); + } + + public Object jsGet_firstChild() { + establishChildren(); + if (children.length > 0) + return children[0]; + else + return null; + } + + public Object jsGet_nextSibling() { + return nextSibling; + } + + public Object jsGet_previousSibling() { + return previousSibling; + } + + public Object jsGet_parentNode() { + // risk errors in object equality ... + return wrapNode(this, wrappedNode.getParentNode()); + } + + public int jsGet_nodeType() { + return wrappedNode.getNodeType(); + } + + public String jsGet_nodeValue() { + return wrappedNode.getNodeValue(); + } + + public String jsGet_nodeName() { + return wrappedNode.getNodeName(); + } + + // in a more complete version of this, we'd use a different object type to wrap documents. + public Object jsGet_documentElement() { + if(9 /* Document */ != wrappedNode.getNodeType()) { + return null; + } else { + establishChildren(); + return children[0]; // it is, after all, just a convenience feature. + } + } + + public Object[] jsGet_childNodes() { + establishChildren(); + return children; + } + + public Object jsGet_attributes() { + establishAttributes(); + return attributes; + } + + public String jsFunction_getAttributeNS(String namespaceURI, String localName) { + NamedNodeMap attributes = wrappedNode.getAttributes(); + Node attrNode = attributes.getNamedItemNS(namespaceURI, localName); + if(attrNode == null) { + return null; + } else { + Attr attribute = (Attr) attrNode; + return attribute.getValue(); + } + } + + public String jsFunction_getAttribute(String localName) { + NamedNodeMap attributes = wrappedNode.getAttributes(); + Node attrNode = attributes.getNamedItem(localName); + if(attrNode == null) { + return null; + } else { + Attr attribute = (Attr) attrNode; + return attribute.getValue(); + } + } + + //CHECKSTYLE:ON + + public static JsSimpleDomNode wrapNode(Scriptable scope, Node node) { + if (node == null) { + return null; + } + + Context cx = Context.enter(); + JsSimpleDomNode newObject = (JsSimpleDomNode)cx.newObject(scope, "Node"); + newObject.initialize(node, null); + return newObject; + } + + private JsSimpleDomNode newObject(Node node, JsSimpleDomNode prev) { + Context cx = Context.enter(); + JsSimpleDomNode newObject = (JsSimpleDomNode)cx.newObject(getParentScope(), "Node"); + newObject.initialize(node, prev); + return newObject; + } + + private void establishChildren() { + if (!childrenWrapped) { + if (wrappedNode.hasChildNodes()) { + NodeList nodeChildren = wrappedNode.getChildNodes(); + children = new JsSimpleDomNode[nodeChildren.getLength()]; + for (int x = 0; x < nodeChildren.getLength(); x++) { + JsSimpleDomNode prev = null; + if (x > 0) { + prev = (JsSimpleDomNode)children[x - 1]; + } + children[x] = newObject(nodeChildren.item(x), prev); + if (x > 0) { + children[x - 1].setNext(children[x]); + } + } + } else { + children = new JsSimpleDomNode[0]; + } + childrenWrapped = true; + } + } + + private void establishAttributes() { + if (!attributesWrapped) { + NamedNodeMap nodeAttributes = wrappedNode.getAttributes(); + attributes = JsNamedNodeMap.wrapMap(getParentScope(), nodeAttributes); + attributesWrapped = true; + } + } + + //rhino won't let us use a constructor. + void initialize(Node node, JsSimpleDomNode prev) { + wrappedNode = node; + childrenWrapped = false; + previousSibling = prev; + } + + void setNext(JsSimpleDomNode next) { + nextSibling = next; + } + + +} Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomNode.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java?rev=630381&r1=630380&r2=630381&view=diff ============================================================================== --- incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java (original) +++ incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java Fri Feb 22 18:55:53 2008 @@ -1,95 +1,95 @@ -/** - * 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.cxf.javascript; - -import java.io.IOException; -import java.io.StringReader; -import java.lang.reflect.InvocationTargetException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import org.mozilla.javascript.Context; -import org.mozilla.javascript.ScriptableObject; - -/** - * A Rhino wrapper to define DOMParser. - */ -public class JsSimpleDomParser extends ScriptableObject { - - private DocumentBuilder documentBuilder; - - public JsSimpleDomParser() { - try { - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - documentBuilderFactory.setNamespaceAware(true); - documentBuilderFactory.setCoalescing(true); - documentBuilder = documentBuilderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new RuntimeException(e); - } - } - - public static void register(ScriptableObject scope) { - try { - ScriptableObject.defineClass(scope, JsSimpleDomParser.class); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - @Override - public String getClassName() { - return "DOMParser"; - } - - //CHECKSTYLE:OFF - - public Object jsFunction_parseFromString(String xml, String mimeType) { - StringReader reader = new StringReader(xml); - InputSource inputSource = new InputSource(reader); - Document document; - try { - document = documentBuilder.parse(inputSource); - } catch (SAXException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } - - Context context = Context.enter(); - JsSimpleDomNode domNode = (JsSimpleDomNode)context.newObject(getParentScope(), "Node"); - domNode.initialize(document, null); - return domNode; - } - - //CHECKSTYLE:ON - -} +/** + * 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.cxf.javascript; + +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.InvocationTargetException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; + +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ScriptableObject; + +/** + * A Rhino wrapper to define DOMParser. + */ +public class JsSimpleDomParser extends ScriptableObject { + + private DocumentBuilder documentBuilder; + + public JsSimpleDomParser() { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setNamespaceAware(true); + documentBuilderFactory.setCoalescing(true); + documentBuilder = documentBuilderFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new RuntimeException(e); + } + } + + public static void register(ScriptableObject scope) { + try { + ScriptableObject.defineClass(scope, JsSimpleDomParser.class); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + @Override + public String getClassName() { + return "DOMParser"; + } + + //CHECKSTYLE:OFF + + public Object jsFunction_parseFromString(String xml, String mimeType) { + StringReader reader = new StringReader(xml); + InputSource inputSource = new InputSource(reader); + Document document; + try { + document = documentBuilder.parse(inputSource); + } catch (SAXException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Context context = Context.enter(); + JsSimpleDomNode domNode = (JsSimpleDomNode)context.newObject(getParentScope(), "Node"); + domNode.initialize(document, null); + return domNode; + } + + //CHECKSTYLE:ON + +} Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/rt/javascript/src/test/java/org/apache/cxf/javascript/JsSimpleDomParser.java ------------------------------------------------------------------------------ svn:keywords = Rev Date