ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutImpl.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutImpl.java Fri Feb 27 18:55:06 2015 +0100
1.3 @@ -0,0 +1,121 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.ko2brwsr;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.net.URI;
1.25 +import java.net.URISyntaxException;
1.26 +import java.net.URL;
1.27 +import java.util.Map;
1.28 +import net.java.html.BrwsrCtx;
1.29 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.30 +import org.netbeans.html.context.spi.Contexts;
1.31 +import org.netbeans.html.json.spi.Technology;
1.32 +import org.netbeans.html.json.spi.Transfer;
1.33 +import org.netbeans.html.json.spi.WSTransfer;
1.34 +import org.netbeans.html.json.tck.KnockoutTCK;
1.35 +import org.netbeans.html.ko4j.KO4J;
1.36 +import org.openide.util.lookup.ServiceProvider;
1.37 +
1.38 +/**
1.39 + *
1.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.41 + */
1.42 +@ServiceProvider(service = KnockoutTCK.class)
1.43 +public final class Bck2BrwsrKnockoutImpl extends KnockoutTCK {
1.44 + static Class[] createClasses() {
1.45 + final Class<?>[] arr = testClasses();
1.46 + for (int i = 0; i < arr.length; i++) {
1.47 + if (arr[i].getSimpleName().startsWith("GC")) {
1.48 + arr[i] = Object.class;
1.49 + }
1.50 + }
1.51 + return arr;
1.52 + }
1.53 +
1.54 + @Override
1.55 + public BrwsrCtx createContext() {
1.56 + KO4J ko = new KO4J(null);
1.57 + return Contexts.newBuilder().
1.58 + register(Transfer.class, ko.transfer(), 9).
1.59 + register(WSTransfer.class, ko.websockets(), 9).
1.60 + register(Technology.class, ko.knockout(), 9).build();
1.61 + }
1.62 +
1.63 +
1.64 +
1.65 + @Override
1.66 + public Object createJSON(Map<String, Object> values) {
1.67 + Object json = createJSON();
1.68 +
1.69 + for (Map.Entry<String, Object> entry : values.entrySet()) {
1.70 + putValue(json, entry.getKey(), entry.getValue());
1.71 + }
1.72 + return json;
1.73 + }
1.74 +
1.75 + @JavaScriptBody(args = {}, body = "return new Object();")
1.76 + private static native Object createJSON();
1.77 +
1.78 + @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
1.79 + private static native void putValue(Object json, String key, Object value);
1.80 +
1.81 + @Override
1.82 + public Object executeScript(String script, Object[] arguments) {
1.83 + return execScript(script, arguments);
1.84 + }
1.85 +
1.86 + @JavaScriptBody(args = { "s", "args" }, body =
1.87 + "var f = new Function(s); return f.apply(null, args);"
1.88 + )
1.89 + private static native Object execScript(String s, Object[] arguments);
1.90 +
1.91 + @JavaScriptBody(args = { }, body =
1.92 + "var h;"
1.93 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.94 + + " h = window.location.href;\n"
1.95 + + "else "
1.96 + + " h = null;"
1.97 + + "return h;\n"
1.98 + )
1.99 + private static native String findBaseURL();
1.100 +
1.101 + @Override
1.102 + public URI prepareURL(String content, String mimeType, String[] parameters) {
1.103 + try {
1.104 + final URL baseURL = new URL(findBaseURL());
1.105 + StringBuilder sb = new StringBuilder();
1.106 + sb.append("/dynamic?mimeType=").append(mimeType);
1.107 + for (int i = 0; i < parameters.length; i++) {
1.108 + sb.append("¶m" + i).append("=").append(parameters[i]);
1.109 + }
1.110 + String mangle = content.replace("\n", "%0a")
1.111 + .replace("\"", "\\\"").replace(" ", "%20");
1.112 + sb.append("&content=").append(mangle);
1.113 +
1.114 + URL query = new URL(baseURL, sb.toString());
1.115 + String uri = (String) query.getContent(new Class[] { String.class });
1.116 + URI connectTo = new URI(uri.trim());
1.117 + return connectTo;
1.118 + } catch (IOException ex) {
1.119 + throw new IllegalStateException(ex);
1.120 + } catch (URISyntaxException ex) {
1.121 + throw new IllegalStateException(ex);
1.122 + }
1.123 + }
1.124 +}