Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible context
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 28 May 2013 13:31:42 +0200
branchcontext
changeset 11021bf4126e3a9
parent 104 c053a7d09c8c
child 111 b3ed6cf776df
Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible
context/pom.xml
context/src/main/java/net/java/html/BrwsrCtx.java
context/src/main/java/org/apidesign/html/context/impl/CtxAccssr.java
context/src/main/java/org/apidesign/html/context/impl/CtxImpl.java
context/src/main/java/org/apidesign/html/context/spi/Contexts.java
json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json-tck/src/main/java/net/java/html/json/tests/Utils.java
json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java
json/pom.xml
json/src/main/java/net/java/html/json/Context.java
json/src/main/java/net/java/html/json/Models.java
json/src/main/java/org/apidesign/html/json/impl/Bindings.java
json/src/main/java/org/apidesign/html/json/impl/ContextAccessor.java
json/src/main/java/org/apidesign/html/json/impl/FromJSON.java
json/src/main/java/org/apidesign/html/json/impl/JSON.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/main/java/org/apidesign/html/json/spi/ContextBuilder.java
json/src/main/java/org/apidesign/html/json/spi/ContextProvider.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/net/java/html/json/ModelTest.java
json/src/test/java/net/java/html/json/TypesTest.java
json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java
json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java
ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java
ko-fx/src/main/java/org/apidesign/html/kofx/Knockout.java
ko-fx/src/test/java/org/apidesign/html/kofx/KnockoutFXTest.java
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/context/pom.xml	Tue May 28 13:31:42 2013 +0200
     1.3 @@ -0,0 +1,20 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     1.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     1.7 +  <modelVersion>4.0.0</modelVersion>
     1.8 +  <parent>
     1.9 +    <groupId>org.apidesign</groupId>
    1.10 +    <artifactId>html</artifactId>
    1.11 +    <version>0.4-SNAPSHOT</version>
    1.12 +  </parent>
    1.13 +  <groupId>org.apidesign.html</groupId>
    1.14 +  <artifactId>net.java.html</artifactId>
    1.15 +  <version>0.4-SNAPSHOT</version>
    1.16 +  <name>HTML Context</name>
    1.17 +  <url>http://maven.apache.org</url>
    1.18 +  <properties>
    1.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.20 +  </properties>
    1.21 +  <dependencies>
    1.22 +  </dependencies>
    1.23 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/context/src/main/java/net/java/html/BrwsrCtx.java	Tue May 28 13:31:42 2013 +0200
     2.3 @@ -0,0 +1,86 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +package net.java.html;
    2.25 +
    2.26 +import java.util.ServiceLoader;
    2.27 +import org.apidesign.html.context.impl.CtxAccssr;
    2.28 +import org.apidesign.html.context.impl.CtxImpl;
    2.29 +import org.apidesign.html.context.spi.Contexts;
    2.30 +
    2.31 +/** Represents context where the {@link Model} and other objects
    2.32 + * operate in. The context is usually a particular HTML page in a browser.
    2.33 + * The context is also associated with the actual HTML rendering technology
    2.34 + * in the HTML page - there is likely to be different context for 
    2.35 + * <a href="http://knockoutjs.com">knockout.js</a> and different one
    2.36 + * for <a href="http://angularjs.org">angular</a>.
    2.37 + *
    2.38 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.39 + */
    2.40 +public final class BrwsrCtx {
    2.41 +    private final CtxImpl impl;
    2.42 +    private BrwsrCtx(CtxImpl impl) {
    2.43 +        this.impl = impl;
    2.44 +    }
    2.45 +    static {
    2.46 +        new CtxAccssr() {
    2.47 +            @Override
    2.48 +            protected BrwsrCtx newContext(CtxImpl impl) {
    2.49 +                return new BrwsrCtx(impl);
    2.50 +            }
    2.51 +
    2.52 +            @Override
    2.53 +            protected CtxImpl find(BrwsrCtx context) {
    2.54 +                return context.impl;
    2.55 +            }
    2.56 +        };
    2.57 +    }
    2.58 +    /** Dummy context without binding to any real browser or technology. 
    2.59 +     * Useful for simple unit testing of behavior of various business logic
    2.60 +     * code.
    2.61 +     */
    2.62 +    public static final BrwsrCtx EMPTY = Contexts.newBuilder().build();
    2.63 +    
    2.64 +    /** Seeks for the default context that is associated with the requesting
    2.65 +     * class. If no suitable context is found, a warning message is
    2.66 +     * printed and {@link #EMPTY} context is returned.
    2.67 +     * 
    2.68 +     * @param requestor the class that makes the request
    2.69 +     * @return appropriate context for the request
    2.70 +     */
    2.71 +    public static BrwsrCtx findDefault(Class<?> requestor) {
    2.72 +        org.apidesign.html.context.spi.Contexts.Builder cb = Contexts.newBuilder();
    2.73 +        boolean found = false;
    2.74 +        for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(org.apidesign.html.context.spi.Contexts.Provider.class)) {
    2.75 +            cp.fillContext(cb, requestor);
    2.76 +            found = true;
    2.77 +        }
    2.78 +        for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(org.apidesign.html.context.spi.Contexts.Provider.class, org.apidesign.html.context.spi.Contexts.Provider.class.getClassLoader())) {
    2.79 +            cp.fillContext(cb, requestor);
    2.80 +            found = true;
    2.81 +        }
    2.82 +        if (!found) {
    2.83 +            // XXX: print out a warning
    2.84 +            return EMPTY;
    2.85 +        }
    2.86 +        return cb.build();
    2.87 +    }
    2.88 +    
    2.89 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/context/src/main/java/org/apidesign/html/context/impl/CtxAccssr.java	Tue May 28 13:31:42 2013 +0200
     3.3 @@ -0,0 +1,52 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package org.apidesign.html.context.impl;
    3.25 +
    3.26 +import net.java.html.BrwsrCtx;
    3.27 +
    3.28 +/** Internal communication between API (e.g. {@link BrwsrCtx}), SPI
    3.29 + * (e.g. {@link ContextBuilder}) and the implementation package.
    3.30 + *
    3.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.32 + */
    3.33 +public abstract class CtxAccssr {
    3.34 +    private static CtxAccssr DEFAULT;
    3.35 +    static {
    3.36 +        // run initializers
    3.37 +        try {
    3.38 +            BrwsrCtx.EMPTY.getClass();
    3.39 +        } catch (NullPointerException ex) {
    3.40 +            // ignore
    3.41 +        }
    3.42 +    }
    3.43 +    
    3.44 +    protected CtxAccssr() {
    3.45 +        if (DEFAULT != null) throw new IllegalStateException();
    3.46 +        DEFAULT = this;
    3.47 +    }
    3.48 +    
    3.49 +    protected abstract BrwsrCtx newContext(CtxImpl impl);
    3.50 +    protected abstract CtxImpl find(BrwsrCtx context);
    3.51 +    
    3.52 +    static CtxAccssr getDefault() {
    3.53 +        return DEFAULT;
    3.54 +    }
    3.55 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/context/src/main/java/org/apidesign/html/context/impl/CtxImpl.java	Tue May 28 13:31:42 2013 +0200
     4.3 @@ -0,0 +1,83 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.context.impl;
    4.25 +
    4.26 +import java.util.ArrayList;
    4.27 +import java.util.Collections;
    4.28 +import java.util.List;
    4.29 +import net.java.html.BrwsrCtx;
    4.30 +
    4.31 +/** Implementation detail. Holds list of technologies for particular
    4.32 + * {@link BrwsrCtx}.
    4.33 + *
    4.34 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    4.35 + */
    4.36 +public final class CtxImpl {
    4.37 +    private final List<Bind<?>> techs;
    4.38 +    
    4.39 +    public CtxImpl() {
    4.40 +        techs = new ArrayList<Bind<?>>();
    4.41 +    }
    4.42 +    
    4.43 +    private CtxImpl(List<Bind<?>> techs) {
    4.44 +        this.techs = techs;
    4.45 +    }
    4.46 +    
    4.47 +    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
    4.48 +        CtxImpl impl = CtxAccssr.getDefault().find(context);
    4.49 +        for (Bind<?> bind : impl.techs) {
    4.50 +            if (technology == bind.clazz) {
    4.51 +                return technology.cast(bind.impl);
    4.52 +            }
    4.53 +        }
    4.54 +        return null;
    4.55 +    }
    4.56 +
    4.57 +    public BrwsrCtx build() {
    4.58 +        Collections.sort(techs);
    4.59 +        CtxImpl impl = new CtxImpl(Collections.unmodifiableList(techs));
    4.60 +        return CtxAccssr.getDefault().newContext(impl);
    4.61 +    }
    4.62 +
    4.63 +    public <Tech> void register(Class<Tech> type, Tech impl, int priority) {
    4.64 +        techs.add(new Bind<Tech>(type, impl, priority));
    4.65 +    }
    4.66 +    
    4.67 +    private static final class Bind<Tech> implements Comparable<Bind<?>> {
    4.68 +        private final Class<Tech> clazz;
    4.69 +        private final Tech impl;
    4.70 +        private final int priority;
    4.71 +
    4.72 +        public Bind(Class<Tech> clazz, Tech impl, int priority) {
    4.73 +            this.clazz = clazz;
    4.74 +            this.impl = impl;
    4.75 +            this.priority = priority;
    4.76 +        }
    4.77 +
    4.78 +        @Override
    4.79 +        public int compareTo(Bind<?> o) {
    4.80 +            if (priority != o.priority) {
    4.81 +                return priority - o.priority;
    4.82 +            }
    4.83 +            return clazz.getName().compareTo(o.clazz.getName());
    4.84 +        }
    4.85 +    }
    4.86 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/context/src/main/java/org/apidesign/html/context/spi/Contexts.java	Tue May 28 13:31:42 2013 +0200
     5.3 @@ -0,0 +1,115 @@
     5.4 +/**
     5.5 + * HTML via Java(tm) Language Bindings
     5.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, version 2 of the License.
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details. apidesign.org
    5.16 + * designates this particular file as subject to the
    5.17 + * "Classpath" exception as provided by apidesign.org
    5.18 + * in the License file that accompanied this code.
    5.19 + *
    5.20 + * You should have received a copy of the GNU General Public License
    5.21 + * along with this program. Look for COPYING file in the top folder.
    5.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    5.23 + */
    5.24 +package org.apidesign.html.context.spi;
    5.25 +
    5.26 +import net.java.html.BrwsrCtx;
    5.27 +import org.apidesign.html.context.impl.CtxImpl;
    5.28 +
    5.29 +/**
    5.30 + *
    5.31 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    5.32 + */
    5.33 +public final class Contexts {
    5.34 +    private Contexts() {
    5.35 +    }
    5.36 +
    5.37 +    /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the
    5.38 +     * end call the {@link #build()} method to generate the context.
    5.39 +     *
    5.40 +     * @return new instance of the builder
    5.41 +     */
    5.42 +    public static Builder newBuilder() {
    5.43 +        return new Builder();
    5.44 +    }
    5.45 +
    5.46 +    /** Seeks for the specified technology in the provided context.
    5.47 +     * 
    5.48 +     * @param <Tech> type of the technology
    5.49 +     * @param context the context to seek in 
    5.50 +     *    (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)})
    5.51 +     * @param technology class that identifies the technology
    5.52 +     * @return 
    5.53 +     */
    5.54 +    public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) {
    5.55 +        return CtxImpl.find(context, technology);
    5.56 +    }
    5.57 +
    5.58 +    /** Implementors of various HTML technologies should
    5.59 +     * register their implementation via {@link ServiceProvider} so
    5.60 +     * {@link ServiceProvider} can find them, when their JARs are included
    5.61 +     * on the classpath of the running application.
    5.62 +     *
    5.63 +     * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    5.64 +     */
    5.65 +    public static interface Provider {
    5.66 +
    5.67 +        /** Register into the context if suitable technology is
    5.68 +         * available for the requesting class.
    5.69 +         * The provider should check if its own technology is available in current
    5.70 +         * scope (e.g. proper JDK, proper browser, etc.). The provider
    5.71 +         * can also find the right context depending on requestor's classloader, etc.
    5.72 +         * <p>
    5.73 +         * Providers should use {@link Builder} to enrich appropriately
    5.74 +         * the context.
    5.75 +         *
    5.76 +         * @param context the context builder to fill with technologies
    5.77 +         * @param requestor the application class requesting access the the HTML page
    5.78 +         * @see BrwsrCtx#findDefault(java.lang.Class)
    5.79 +         */
    5.80 +        void fillContext(Builder context, Class<?> requestor);
    5.81 +    }
    5.82 +
    5.83 +    /** Support for providers of new {@link BrwsrCtx}. Providers of different
    5.84 +     * technologies should be of particular interest in this class. End users
    5.85 +     * designing their application with existing technologies should rather
    5.86 +     * point their attention to {@link BrwsrCtx} and co.
    5.87 +     *
    5.88 +     * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.89 +     */
    5.90 +    public static final class Builder {
    5.91 +        private CtxImpl impl = new CtxImpl();
    5.92 +
    5.93 +        Builder() {
    5.94 +        }
    5.95 +        
    5.96 +        /** Registers new technology into the context. Each technology is
    5.97 +         * exactly identified by its implementation class and can be associated
    5.98 +         * with (positive) priority. In case of presence of multiple technologies
    5.99 +         * with the same class, the one with higher lower priority takes precedence.
   5.100 +         */
   5.101 +        public <Tech> Builder register(Class<Tech> type, Tech impl, int priority) {
   5.102 +            if (priority <= 0) {
   5.103 +                throw new IllegalStateException();
   5.104 +            }
   5.105 +            this.impl.register(type, impl, priority);
   5.106 +            return this;
   5.107 +        }
   5.108 +
   5.109 +        /** Generates context based on values previously inserted into
   5.110 +         * this builder.
   5.111 +         *
   5.112 +         * @return new, immutable instance of {@link BrwsrCtx}
   5.113 +         */
   5.114 +        public BrwsrCtx build() {
   5.115 +            return impl.build();
   5.116 +        }
   5.117 +    }
   5.118 +}
     6.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Mon May 27 16:22:20 2013 +0200
     6.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Tue May 28 13:31:42 2013 +0200
     6.3 @@ -25,7 +25,7 @@
     6.4  import java.io.UnsupportedEncodingException;
     6.5  import java.util.HashMap;
     6.6  import java.util.Map;
     6.7 -import net.java.html.json.Context;
     6.8 +import net.java.html.BrwsrCtx;
     6.9  import net.java.html.json.Models;
    6.10  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    6.11  import org.apidesign.bck2brwsr.vmtest.VMTest;
    6.12 @@ -71,7 +71,7 @@
    6.13  
    6.14      @BrwsrTest
    6.15      public void parseConvertToPeople() throws Exception {
    6.16 -        final Context c = Utils.newContext();
    6.17 +        final BrwsrCtx c = Utils.newContext();
    6.18          final InputStream o = createIS(true);
    6.19          
    6.20          Person p = Models.parse(c, Person.class, o);
    6.21 @@ -94,7 +94,7 @@
    6.22      
    6.23      @BrwsrTest
    6.24      public void parseConvertToPeopleWithoutSex() throws Exception {
    6.25 -        final Context c = Utils.newContext();
    6.26 +        final BrwsrCtx c = Utils.newContext();
    6.27          final InputStream o = createIS(false);
    6.28          Person p = Models.parse(c, Person.class, o);
    6.29          
     7.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Mon May 27 16:22:20 2013 +0200
     7.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Tue May 28 13:31:42 2013 +0200
     7.3 @@ -21,7 +21,7 @@
     7.4  package net.java.html.json.tests;
     7.5  
     7.6  import java.io.ByteArrayInputStream;
     7.7 -import net.java.html.json.Context;
     7.8 +import net.java.html.BrwsrCtx;
     7.9  import net.java.html.json.Model;
    7.10  import net.java.html.json.Models;
    7.11  import net.java.html.json.OnReceive;
    7.12 @@ -169,7 +169,7 @@
    7.13              js = new JSONik(Utils.newContext());
    7.14              js.applyBindings();
    7.15  
    7.16 -            Person p = new Person(Context.EMPTY);
    7.17 +            Person p = new Person(BrwsrCtx.EMPTY);
    7.18              p.setFirstName("Jarda");
    7.19              js.putPerson("person.json", p);
    7.20          }
     8.1 --- a/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Mon May 27 16:22:20 2013 +0200
     8.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Tue May 28 13:31:42 2013 +0200
     8.3 @@ -22,7 +22,7 @@
     8.4  
     8.5  import java.util.Map;
     8.6  import java.util.ServiceLoader;
     8.7 -import net.java.html.json.Context;
     8.8 +import net.java.html.BrwsrCtx;
     8.9  import org.apidesign.html.json.tck.KnockoutTCK;
    8.10  
    8.11  /**
    8.12 @@ -33,9 +33,9 @@
    8.13      private Utils() {
    8.14      }
    8.15  
    8.16 -    static Context newContext() {
    8.17 +    static  BrwsrCtx newContext() {
    8.18          for (KnockoutTCK tck : ServiceLoader.load(KnockoutTCK.class)) {
    8.19 -            Context c = tck.createContext();
    8.20 +            BrwsrCtx c = tck.createContext();
    8.21              if (c != null) {
    8.22                  return c;
    8.23              }
     9.1 --- a/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Mon May 27 16:22:20 2013 +0200
     9.2 +++ b/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Tue May 28 13:31:42 2013 +0200
     9.3 @@ -21,13 +21,12 @@
     9.4  package org.apidesign.html.json.tck;
     9.5  
     9.6  import java.util.Map;
     9.7 +import net.java.html.BrwsrCtx;
     9.8  import net.java.html.json.tests.ConvertTypesTest;
     9.9  import net.java.html.json.tests.KnockoutTest;
    9.10  import net.java.html.json.tests.JSONTest;
    9.11 -import net.java.html.json.Context;
    9.12  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    9.13  import org.apidesign.bck2brwsr.vmtest.VMTest;
    9.14 -import org.apidesign.html.json.spi.ContextBuilder;
    9.15  import org.openide.util.lookup.ServiceProvider;
    9.16  
    9.17  /** Entry point for providers of different HTML binding technologies (like
    9.18 @@ -55,7 +54,7 @@
    9.19      /** Implement to create new context for the test. 
    9.20       * Use {@link ContextBuilder} to implement context for your technology.
    9.21       */
    9.22 -    public abstract Context createContext();
    9.23 +    public abstract BrwsrCtx createContext();
    9.24      
    9.25      /** Create a JSON object as seen by the technology
    9.26       * @param values mapping from names to values of properties
    10.1 --- a/json/pom.xml	Mon May 27 16:22:20 2013 +0200
    10.2 +++ b/json/pom.xml	Tue May 28 13:31:42 2013 +0200
    10.3 @@ -51,5 +51,10 @@
    10.4        <artifactId>org-openide-util-lookup</artifactId>
    10.5        <scope>provided</scope>
    10.6      </dependency>
    10.7 +    <dependency>
    10.8 +      <groupId>${project.groupId}</groupId>
    10.9 +      <artifactId>net.java.html</artifactId>
   10.10 +      <version>${project.version}</version>
   10.11 +    </dependency>
   10.12    </dependencies>
   10.13  </project>
    11.1 --- a/json/src/main/java/net/java/html/json/Context.java	Mon May 27 16:22:20 2013 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,96 +0,0 @@
    11.4 -/**
    11.5 - * HTML via Java(tm) Language Bindings
    11.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    11.7 - *
    11.8 - * This program is free software: you can redistribute it and/or modify
    11.9 - * it under the terms of the GNU General Public License as published by
   11.10 - * the Free Software Foundation, version 2 of the License.
   11.11 - *
   11.12 - * This program is distributed in the hope that it will be useful,
   11.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.15 - * GNU General Public License for more details. apidesign.org
   11.16 - * designates this particular file as subject to the
   11.17 - * "Classpath" exception as provided by apidesign.org
   11.18 - * in the License file that accompanied this code.
   11.19 - *
   11.20 - * You should have received a copy of the GNU General Public License
   11.21 - * along with this program. Look for COPYING file in the top folder.
   11.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   11.23 - */
   11.24 -package net.java.html.json;
   11.25 -
   11.26 -import java.util.ServiceLoader;
   11.27 -import org.apidesign.html.json.impl.ContextAccessor;
   11.28 -import org.apidesign.html.json.spi.ContextBuilder;
   11.29 -import org.apidesign.html.json.spi.ContextProvider;
   11.30 -import org.apidesign.html.json.spi.Technology;
   11.31 -import org.apidesign.html.json.spi.Transfer;
   11.32 -
   11.33 -/** Represents context where the {@link Model} and other objects
   11.34 - * operate in. The context is usually a particular HTML page in a browser.
   11.35 - * The context is also associated with the actual HTML rendering technology
   11.36 - * in the HTML page - there is likely to be different context for 
   11.37 - * <a href="http://knockoutjs.com">knockout.js</a> and different one
   11.38 - * for <a href="http://angularjs.org">angular</a>.
   11.39 - *
   11.40 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   11.41 - */
   11.42 -public final class Context {
   11.43 -    private final Technology<?> t;
   11.44 -    private final Transfer r;
   11.45 -    
   11.46 -    private Context(Technology<?> t, Transfer r) {
   11.47 -        t.getClass();
   11.48 -        r.getClass();
   11.49 -        this.t = t;
   11.50 -        this.r = r;
   11.51 -    }
   11.52 -    static {
   11.53 -        new ContextAccessor() {
   11.54 -            @Override
   11.55 -            protected Context newContext(Technology<?> t, Transfer r) {
   11.56 -                return new Context(t, r);
   11.57 -            }
   11.58 -            
   11.59 -            @Override
   11.60 -            protected Technology<?> technology(Context c) {
   11.61 -                return c.t;
   11.62 -            }
   11.63 -
   11.64 -            @Override
   11.65 -            protected Transfer transfer(Context c) {
   11.66 -                return c.r;
   11.67 -            }
   11.68 -        };
   11.69 -    }
   11.70 -    /** Dummy context without binding to any real browser or technology. 
   11.71 -     * Useful for simple unit testing of behavior of model classes.
   11.72 -     */
   11.73 -    public static final Context EMPTY = ContextBuilder.create().build();
   11.74 -    
   11.75 -    /** Seeks for the default context that is associated with the requesting
   11.76 -     * class. If no suitable context is found, a warning message is
   11.77 -     * printed and {@link #EMPTY} context is returned.
   11.78 -     * 
   11.79 -     * @param requestor the class that makes the request
   11.80 -     * @return appropriate context for the request
   11.81 -     */
   11.82 -    public static Context findDefault(Class<?> requestor) {
   11.83 -        for (ContextProvider cp : ServiceLoader.load(ContextProvider.class)) {
   11.84 -            Context c = cp.findContext(requestor);
   11.85 -            if (c != null) {
   11.86 -                return c;
   11.87 -            }
   11.88 -        }
   11.89 -        for (ContextProvider cp : ServiceLoader.load(ContextProvider.class, ContextProvider.class.getClassLoader())) {
   11.90 -            Context c = cp.findContext(requestor);
   11.91 -            if (c != null) {
   11.92 -                return c;
   11.93 -            }
   11.94 -        }
   11.95 -        // XXX: print out a warning
   11.96 -        return Context.EMPTY;
   11.97 -    }
   11.98 -    
   11.99 -}
    12.1 --- a/json/src/main/java/net/java/html/json/Models.java	Mon May 27 16:22:20 2013 +0200
    12.2 +++ b/json/src/main/java/net/java/html/json/Models.java	Tue May 28 13:31:42 2013 +0200
    12.3 @@ -20,6 +20,7 @@
    12.4   */
    12.5  package net.java.html.json;
    12.6  
    12.7 +import net.java.html.BrwsrCtx;
    12.8  import java.io.IOException;
    12.9  import java.io.InputStream;
   12.10  import java.lang.reflect.Method;
   12.11 @@ -54,7 +55,7 @@
   12.12       * @return new instance of the model class
   12.13       * @since 0.2
   12.14       */
   12.15 -    public static <M> M parse(Context c, Class<M> model, InputStream is) throws IOException {
   12.16 +    public static <M> M parse(BrwsrCtx c, Class<M> model, InputStream is) throws IOException {
   12.17          return JSON.readStream(c, model, is);
   12.18      }
   12.19  }
    13.1 --- a/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Mon May 27 16:22:20 2013 +0200
    13.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/Bindings.java	Tue May 28 13:31:42 2013 +0200
    13.3 @@ -21,7 +21,7 @@
    13.4  package org.apidesign.html.json.impl;
    13.5  
    13.6  import org.apidesign.html.json.spi.PropertyBinding;
    13.7 -import net.java.html.json.Context;
    13.8 +import net.java.html.BrwsrCtx;
    13.9  import org.apidesign.html.json.impl.PropertyBindingAccessor.FBData;
   13.10  import org.apidesign.html.json.impl.PropertyBindingAccessor.PBData;
   13.11  import org.apidesign.html.json.spi.FunctionBinding;
   13.12 @@ -52,8 +52,8 @@
   13.13          return fb;
   13.14      }
   13.15      
   13.16 -    public static Bindings<?> apply(Context c, Object model) {
   13.17 -        Technology<?> bp = ContextAccessor.findTechnology(c);
   13.18 +    public static Bindings<?> apply(BrwsrCtx c, Object model) {
   13.19 +        Technology<?> bp = JSON.findTechnology(c);
   13.20          return apply(bp, model);
   13.21      }
   13.22      
    14.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ContextAccessor.java	Mon May 27 16:22:20 2013 +0200
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,64 +0,0 @@
    14.4 -/**
    14.5 - * HTML via Java(tm) Language Bindings
    14.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    14.7 - *
    14.8 - * This program is free software: you can redistribute it and/or modify
    14.9 - * it under the terms of the GNU General Public License as published by
   14.10 - * the Free Software Foundation, version 2 of the License.
   14.11 - *
   14.12 - * This program is distributed in the hope that it will be useful,
   14.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14.15 - * GNU General Public License for more details. apidesign.org
   14.16 - * designates this particular file as subject to the
   14.17 - * "Classpath" exception as provided by apidesign.org
   14.18 - * in the License file that accompanied this code.
   14.19 - *
   14.20 - * You should have received a copy of the GNU General Public License
   14.21 - * along with this program. Look for COPYING file in the top folder.
   14.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   14.23 - */
   14.24 -package org.apidesign.html.json.impl;
   14.25 -
   14.26 -import net.java.html.json.Context;
   14.27 -import org.apidesign.html.json.spi.ContextBuilder;
   14.28 -import org.apidesign.html.json.spi.Technology;
   14.29 -import org.apidesign.html.json.spi.Transfer;
   14.30 -
   14.31 -/** Internal communication between API (e.g. {@link Context}), SPI
   14.32 - * (e.g. {@link ContextBuilder}) and the implementation package.
   14.33 - *
   14.34 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   14.35 - */
   14.36 -public abstract class ContextAccessor {
   14.37 -    private static ContextAccessor DEFAULT;
   14.38 -    static {
   14.39 -        // run initializers
   14.40 -        try {
   14.41 -            Context.EMPTY.getClass();
   14.42 -        } catch (NullPointerException ex) {
   14.43 -            // ignore
   14.44 -        }
   14.45 -    }
   14.46 -    
   14.47 -    protected ContextAccessor() {
   14.48 -        if (DEFAULT != null) throw new IllegalStateException();
   14.49 -        DEFAULT = this;
   14.50 -    }
   14.51 -    
   14.52 -    protected abstract Context newContext(Technology<?> t, Transfer r);
   14.53 -    protected abstract Technology<?> technology(Context c);
   14.54 -    protected abstract Transfer transfer(Context c);
   14.55 -    
   14.56 -    
   14.57 -    public static Context create(Technology<?> t, Transfer r) {
   14.58 -        return DEFAULT.newContext(t, r);
   14.59 -    }
   14.60 -    
   14.61 -    static Technology<?> findTechnology(Context c) {
   14.62 -        return DEFAULT.technology(c);
   14.63 -    }
   14.64 -    static Transfer findTransfer(Context c) {
   14.65 -        return DEFAULT.transfer(c);
   14.66 -    }
   14.67 -}
    15.1 --- a/json/src/main/java/org/apidesign/html/json/impl/FromJSON.java	Mon May 27 16:22:20 2013 +0200
    15.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/FromJSON.java	Tue May 28 13:31:42 2013 +0200
    15.3 @@ -21,7 +21,7 @@
    15.4  
    15.5  package org.apidesign.html.json.impl;
    15.6  
    15.7 -import net.java.html.json.Context;
    15.8 +import net.java.html.BrwsrCtx;
    15.9  
   15.10  /**
   15.11   *
   15.12 @@ -29,5 +29,5 @@
   15.13   */
   15.14  public interface FromJSON<Data> {
   15.15      public Class<Data> factoryFor();
   15.16 -    public Data read(Context c, Object d);
   15.17 +    public Data read(BrwsrCtx c, Object d);
   15.18  }
    16.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Mon May 27 16:22:20 2013 +0200
    16.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Tue May 28 13:31:42 2013 +0200
    16.3 @@ -20,13 +20,16 @@
    16.4   */
    16.5  package org.apidesign.html.json.impl;
    16.6  
    16.7 +import org.apidesign.html.context.impl.CtxAccssr;
    16.8  import java.io.IOException;
    16.9  import java.io.InputStream;
   16.10  import java.util.HashMap;
   16.11  import java.util.Map;
   16.12 -import net.java.html.json.Context;
   16.13 -import static org.apidesign.html.json.impl.ContextAccessor.findTechnology;
   16.14 +import net.java.html.BrwsrCtx;
   16.15 +import org.apidesign.html.context.spi.Contexts;
   16.16 +import org.apidesign.html.json.spi.FunctionBinding;
   16.17  import org.apidesign.html.json.spi.JSONCall;
   16.18 +import org.apidesign.html.json.spi.PropertyBinding;
   16.19  import org.apidesign.html.json.spi.Technology;
   16.20  import org.apidesign.html.json.spi.Transfer;
   16.21  
   16.22 @@ -35,16 +38,25 @@
   16.23   * @author Jaroslav Tulach <jtulach@netbeans.org>
   16.24   */
   16.25  public final class JSON {
   16.26 -
   16.27      private JSON() {
   16.28      }
   16.29  
   16.30 -    public static void extract(Context c, Object value, String[] props, Object[] values) {
   16.31 -        Transfer t = ContextAccessor.findTransfer(c);
   16.32 +    static Technology<?> findTechnology(BrwsrCtx c) {
   16.33 +        Technology<?> t = Contexts.find(c, Technology.class);
   16.34 +        return t == null ? EmptyTech.EMPTY : t;
   16.35 +    }
   16.36 +
   16.37 +    static Transfer findTransfer(BrwsrCtx c) {
   16.38 +        Transfer t = Contexts.find(c, Transfer.class);
   16.39 +        return t == null ? EmptyTech.EMPTY : t;
   16.40 +    }
   16.41 +    
   16.42 +    public static void extract(BrwsrCtx c, Object value, String[] props, Object[] values) {
   16.43 +        Transfer t = findTransfer(c);
   16.44          t.extract(value, props, values);
   16.45      }
   16.46      
   16.47 -    private static Object getProperty(Context c, Object obj, String prop) {
   16.48 +    private static Object getProperty(BrwsrCtx c, Object obj, String prop) {
   16.49          if (prop == null) return obj;
   16.50          
   16.51          String[] arr = { prop };
   16.52 @@ -72,18 +84,18 @@
   16.53          return value.toString();
   16.54      }
   16.55  
   16.56 -    public static String toString(Context c, Object obj, String prop) {
   16.57 +    public static String toString(BrwsrCtx c, Object obj, String prop) {
   16.58          obj = getProperty(c, obj, prop);
   16.59          return obj instanceof String ? (String)obj : null;
   16.60      }
   16.61 -    public static Number toNumber(Context c, Object obj, String prop) {
   16.62 +    public static Number toNumber(BrwsrCtx c, Object obj, String prop) {
   16.63          obj = getProperty(c, obj, prop);
   16.64          if (!(obj instanceof Number)) {
   16.65              obj = Double.NaN;
   16.66          }
   16.67          return (Number)obj;
   16.68      }
   16.69 -    public static <M> M toModel(Context c, Class<M> aClass, Object data, Object object) {
   16.70 +    public static <M> M toModel(BrwsrCtx c, Class<M> aClass, Object data, Object object) {
   16.71          Technology<?> t = findTechnology(c);
   16.72          Object o = t.toModel(aClass, data);
   16.73          return aClass.cast(o);
   16.74 @@ -91,19 +103,19 @@
   16.75  
   16.76      
   16.77      public static void loadJSON(
   16.78 -        Context c, Runnable whenDone, Object[] result, 
   16.79 +        BrwsrCtx c, Runnable whenDone, Object[] result, 
   16.80          String urlBefore, String urlAfter
   16.81      ) {
   16.82          loadJSON(c, whenDone, result, urlBefore, urlAfter, null, null);
   16.83      }
   16.84  
   16.85      public static void loadJSON(
   16.86 -        Context c, Runnable whenDone, Object[] result,
   16.87 +        BrwsrCtx c, Runnable whenDone, Object[] result,
   16.88          String urlBefore, String urlAfter, String method,
   16.89          Object data
   16.90      ) {
   16.91          JSONCall call = PropertyBindingAccessor.createCall(whenDone, result, urlBefore, urlAfter, method, data);
   16.92 -        Transfer t = ContextAccessor.findTransfer(c);
   16.93 +        Transfer t = findTransfer(c);
   16.94          t.loadJSON(call);
   16.95      }
   16.96      
   16.97 @@ -128,12 +140,12 @@
   16.98          return false;
   16.99      }
  16.100      
  16.101 -    public static <T> T readStream(Context c, Class<T> modelClazz, InputStream data) 
  16.102 +    public static <T> T readStream(BrwsrCtx c, Class<T> modelClazz, InputStream data) 
  16.103      throws IOException {
  16.104 -        Transfer tr = ContextAccessor.findTransfer(c);
  16.105 +        Transfer tr = findTransfer(c);
  16.106          return read(c, modelClazz, tr.toJSON((InputStream)data));
  16.107      }
  16.108 -    public static <T> T read(Context c, Class<T> modelClazz, Object data) {
  16.109 +    public static <T> T read(BrwsrCtx c, Class<T> modelClazz, Object data) {
  16.110          if (modelClazz == String.class) {
  16.111              return modelClazz.cast(data.toString());
  16.112          }
  16.113 @@ -164,4 +176,57 @@
  16.114              // ignore and try again
  16.115          }
  16.116      }
  16.117 +    
  16.118 +    private static final class EmptyTech implements Technology<Object>, Transfer {
  16.119 +        private static final EmptyTech EMPTY = new EmptyTech();
  16.120 +
  16.121 +        @Override
  16.122 +        public Object wrapModel(Object model) {
  16.123 +            return model;
  16.124 +        }
  16.125 +
  16.126 +        @Override
  16.127 +        public void valueHasMutated(Object data, String propertyName) {
  16.128 +        }
  16.129 +
  16.130 +        @Override
  16.131 +        public void bind(PropertyBinding b, Object model, Object data) {
  16.132 +        }
  16.133 +
  16.134 +        @Override
  16.135 +        public void expose(FunctionBinding fb, Object model, Object d) {
  16.136 +        }
  16.137 +
  16.138 +        @Override
  16.139 +        public void applyBindings(Object data) {
  16.140 +        }
  16.141 +
  16.142 +        @Override
  16.143 +        public Object wrapArray(Object[] arr) {
  16.144 +            return arr;
  16.145 +        }
  16.146 +
  16.147 +        @Override
  16.148 +        public void extract(Object obj, String[] props, Object[] values) {
  16.149 +            for (int i = 0; i < values.length; i++) {
  16.150 +                values[i] = null;
  16.151 +            }
  16.152 +        }
  16.153 +
  16.154 +        @Override
  16.155 +        public void loadJSON(JSONCall call) {
  16.156 +            call.notifyError(new UnsupportedOperationException());
  16.157 +        }
  16.158 +
  16.159 +        @Override
  16.160 +        public <M> M toModel(Class<M> modelClass, Object data) {
  16.161 +            return modelClass.cast(data);
  16.162 +        }
  16.163 +
  16.164 +        @Override
  16.165 +        public Object toJSON(InputStream is) throws IOException {
  16.166 +            throw new IOException("Not supported");
  16.167 +        }
  16.168 +    }
  16.169 +    
  16.170  }
    17.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon May 27 16:22:20 2013 +0200
    17.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Tue May 28 13:31:42 2013 +0200
    17.3 @@ -187,11 +187,11 @@
    17.4                  w.append("import net.java.html.json.*;\n");
    17.5                  w.append("public final class ").append(className).append(" implements Cloneable {\n");
    17.6                  w.append("  private boolean locked;\n");
    17.7 -                w.append("  private net.java.html.json.Context context;\n");
    17.8 +                w.append("  private net.java.html.BrwsrCtx context;\n");
    17.9                  w.append("  private org.apidesign.html.json.impl.Bindings ko;\n");
   17.10                  w.append(body.toString());
   17.11                  w.append("  private static Class<" + inPckName(e) + "> modelFor() { return null; }\n");
   17.12 -                w.append("  public ").append(className).append("(Context context) {\n");
   17.13 +                w.append("  public ").append(className).append("(net.java.html.BrwsrCtx context) {\n");
   17.14                  w.append("    this.context = context;\n");
   17.15                  w.append("  };\n");
   17.16                  w.append("  private org.apidesign.html.json.impl.Bindings intKnckt() {\n");
   17.17 @@ -250,10 +250,10 @@
   17.18                  w.append("      throw new UnsupportedOperationException();\n");
   17.19                  w.append("    }\n");
   17.20                  w.append("    public Class<" + className + "> factoryFor() { return " + className + ".class; }\n");
   17.21 -                w.append("    public " + className + " read(Context c, Object json) { return new " + className + "(c, json); }\n");
   17.22 +                w.append("    public " + className + " read(net.java.html.BrwsrCtx c, Object json) { return new " + className + "(c, json); }\n");
   17.23                  w.append("  }\n");
   17.24                  w.append("  static { org.apidesign.html.json.impl.JSON.register(new P(0)); }\n");
   17.25 -                w.append("  private ").append(className).append("(Context c, Object json) {\n");
   17.26 +                w.append("  private ").append(className).append("(net.java.html.BrwsrCtx c, Object json) {\n");
   17.27                  w.append("    this.context = c;\n");
   17.28                  int values = 0;
   17.29                  for (int i = 0; i < propsGetSet.size(); i += 5) {
    18.1 --- a/json/src/main/java/org/apidesign/html/json/spi/ContextBuilder.java	Mon May 27 16:22:20 2013 +0200
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,134 +0,0 @@
    18.4 -/**
    18.5 - * HTML via Java(tm) Language Bindings
    18.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    18.7 - *
    18.8 - * This program is free software: you can redistribute it and/or modify
    18.9 - * it under the terms of the GNU General Public License as published by
   18.10 - * the Free Software Foundation, version 2 of the License.
   18.11 - *
   18.12 - * This program is distributed in the hope that it will be useful,
   18.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   18.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18.15 - * GNU General Public License for more details. apidesign.org
   18.16 - * designates this particular file as subject to the
   18.17 - * "Classpath" exception as provided by apidesign.org
   18.18 - * in the License file that accompanied this code.
   18.19 - *
   18.20 - * You should have received a copy of the GNU General Public License
   18.21 - * along with this program. Look for COPYING file in the top folder.
   18.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   18.23 - */
   18.24 -package org.apidesign.html.json.spi;
   18.25 -
   18.26 -import java.io.IOException;
   18.27 -import java.io.InputStream;
   18.28 -import net.java.html.json.Context;
   18.29 -import org.apidesign.html.json.impl.ContextAccessor;
   18.30 -
   18.31 -/** Support for providers of new {@link Context}. Providers of different
   18.32 - * technologies should be of particular interest in this class. End users
   18.33 - * designing their application with existing technologies should rather
   18.34 - * point their attention to {@link Context} and co.
   18.35 - *
   18.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   18.37 - */
   18.38 -public final class ContextBuilder {
   18.39 -    private Technology<?> t;
   18.40 -    private Transfer r;
   18.41 -    
   18.42 -    private ContextBuilder() {
   18.43 -        EmptyTech et = new EmptyTech();
   18.44 -        t = et;
   18.45 -        r = et;
   18.46 -    }
   18.47 -    
   18.48 -    /** Creates new, empty builder for creation of {@link Context}. At the
   18.49 -     * end call the {@link #build()} method to generate the context.
   18.50 -     * 
   18.51 -     * @return new instance of the builder
   18.52 -     */
   18.53 -    public static ContextBuilder create() {
   18.54 -        return new ContextBuilder();
   18.55 -    }
   18.56 -    
   18.57 -    /** Provides technology for the context.
   18.58 -     * 
   18.59 -     * @param technology
   18.60 -     * @return this
   18.61 -     */
   18.62 -    public ContextBuilder withTechnology(Technology<?> technology) {
   18.63 -        this.t = technology;
   18.64 -        return this;
   18.65 -    }
   18.66 -
   18.67 -    /** Provides transfer for the context.
   18.68 -     * 
   18.69 -     * @param transfer
   18.70 -     * @return this
   18.71 -     */
   18.72 -    public ContextBuilder withTransfer(Transfer transfer) {
   18.73 -        this.r = transfer;
   18.74 -        return this;
   18.75 -    }
   18.76 -    
   18.77 -    /** Generates context based on values previously inserted into
   18.78 -     * this builder.
   18.79 -     * 
   18.80 -     * @return new, immutable instance of {@link Context}
   18.81 -     */
   18.82 -    public Context build() {
   18.83 -        return ContextAccessor.create(t, r);
   18.84 -    }
   18.85 -    
   18.86 -    private static final class EmptyTech
   18.87 -    implements Technology<Object>, Transfer {
   18.88 -        @Override
   18.89 -        public Object wrapModel(Object model) {
   18.90 -            return model;
   18.91 -        }
   18.92 -
   18.93 -        @Override
   18.94 -        public void valueHasMutated(Object data, String propertyName) {
   18.95 -        }
   18.96 -
   18.97 -        @Override
   18.98 -        public void bind(PropertyBinding b, Object model, Object data) {
   18.99 -        }
  18.100 -
  18.101 -        @Override
  18.102 -        public void expose(FunctionBinding fb, Object model, Object d) {
  18.103 -        }
  18.104 -
  18.105 -        @Override
  18.106 -        public void applyBindings(Object data) {
  18.107 -        }
  18.108 -
  18.109 -        @Override
  18.110 -        public Object wrapArray(Object[] arr) {
  18.111 -            return arr;
  18.112 -        }
  18.113 -
  18.114 -        @Override
  18.115 -        public void extract(Object obj, String[] props, Object[] values) {
  18.116 -            for (int i = 0; i < values.length; i++) {
  18.117 -                values[i] = null;
  18.118 -            }
  18.119 -        }
  18.120 -
  18.121 -        @Override
  18.122 -        public void loadJSON(JSONCall call) {
  18.123 -            call.notifyError(new UnsupportedOperationException());
  18.124 -        }
  18.125 -
  18.126 -        @Override
  18.127 -        public <M> M toModel(Class<M> modelClass, Object data) {
  18.128 -            return modelClass.cast(data);
  18.129 -        }
  18.130 -
  18.131 -        @Override
  18.132 -        public Object toJSON(InputStream is) throws IOException {
  18.133 -            throw new IOException("Not supported");
  18.134 -        }
  18.135 -    }
  18.136 -    
  18.137 -}
    19.1 --- a/json/src/main/java/org/apidesign/html/json/spi/ContextProvider.java	Mon May 27 16:22:20 2013 +0200
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,46 +0,0 @@
    19.4 -/**
    19.5 - * HTML via Java(tm) Language Bindings
    19.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    19.7 - *
    19.8 - * This program is free software: you can redistribute it and/or modify
    19.9 - * it under the terms of the GNU General Public License as published by
   19.10 - * the Free Software Foundation, version 2 of the License.
   19.11 - *
   19.12 - * This program is distributed in the hope that it will be useful,
   19.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   19.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19.15 - * GNU General Public License for more details. apidesign.org
   19.16 - * designates this particular file as subject to the
   19.17 - * "Classpath" exception as provided by apidesign.org
   19.18 - * in the License file that accompanied this code.
   19.19 - *
   19.20 - * You should have received a copy of the GNU General Public License
   19.21 - * along with this program. Look for COPYING file in the top folder.
   19.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   19.23 - */
   19.24 -package org.apidesign.html.json.spi;
   19.25 -
   19.26 -import net.java.html.json.Context;
   19.27 -import org.openide.util.lookup.ServiceProvider;
   19.28 -
   19.29 -/** Implementors of various {@link Technology technologies} should
   19.30 - * register their implementation via {@link ServiceProvider} so 
   19.31 - * {@link ServiceProvider} can find them, when their JARs are included
   19.32 - * on the classpath of the running application.
   19.33 - *
   19.34 - * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
   19.35 - */
   19.36 -public interface ContextProvider {
   19.37 -    /** Identify the context suitable for provided requesting class. 
   19.38 -     * The provider should check if its own technology is available in current
   19.39 -     * scope (e.g. proper JDK, proper browser, etc.). The provider
   19.40 -     * can also find the right context depending on requestor's classloader, etc.
   19.41 -     * <p>
   19.42 -     * Providers should use {@link ContextBuilder} to construct appropriately
   19.43 -     * configured context.
   19.44 -     * 
   19.45 -     * @param requestor the application class requesting access the the HTML page
   19.46 -     * @return 
   19.47 -     */
   19.48 -    Context findContext(Class<?> requestor);
   19.49 -}
    20.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Mon May 27 16:22:20 2013 +0200
    20.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Tue May 28 13:31:42 2013 +0200
    20.3 @@ -20,13 +20,14 @@
    20.4   */
    20.5  package net.java.html.json;
    20.6  
    20.7 +import net.java.html.BrwsrCtx;
    20.8  import java.io.IOException;
    20.9  import java.io.InputStream;
   20.10  import java.lang.reflect.InvocationTargetException;
   20.11  import java.util.HashMap;
   20.12  import java.util.Map;
   20.13 +import org.apidesign.html.context.spi.Contexts;
   20.14  import org.apidesign.html.json.impl.WrapperObject;
   20.15 -import org.apidesign.html.json.spi.ContextBuilder;
   20.16  import org.apidesign.html.json.spi.FunctionBinding;
   20.17  import org.apidesign.html.json.spi.JSONCall;
   20.18  import org.apidesign.html.json.spi.PropertyBinding;
   20.19 @@ -42,11 +43,12 @@
   20.20   */
   20.21  public class MapModelTest {
   20.22      private MapTechnology t;
   20.23 -    private Context c;
   20.24 +    private BrwsrCtx c;
   20.25  
   20.26      @BeforeMethod public void initTechnology() {
   20.27          t = new MapTechnology();
   20.28 -        c = ContextBuilder.create().withTechnology(t).withTransfer(t).build();
   20.29 +        c = Contexts.newBuilder().register(Technology.class, t, 1).
   20.30 +            register(Transfer.class, t, 1).build();
   20.31      }
   20.32      
   20.33      @Test public void isThereABinding() throws Exception {
    21.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java	Mon May 27 16:22:20 2013 +0200
    21.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java	Tue May 28 13:31:42 2013 +0200
    21.3 @@ -20,12 +20,13 @@
    21.4   */
    21.5  package net.java.html.json;
    21.6  
    21.7 +import net.java.html.BrwsrCtx;
    21.8  import java.util.ArrayList;
    21.9  import java.util.Collections;
   21.10  import java.util.Iterator;
   21.11  import java.util.List;
   21.12  import java.util.ListIterator;
   21.13 -import org.apidesign.html.json.spi.ContextBuilder;
   21.14 +import org.apidesign.html.context.spi.Contexts;
   21.15  import org.apidesign.html.json.spi.FunctionBinding;
   21.16  import org.apidesign.html.json.spi.PropertyBinding;
   21.17  import org.apidesign.html.json.spi.Technology;
   21.18 @@ -54,7 +55,7 @@
   21.19      @BeforeMethod
   21.20      public void createModel() {
   21.21          my = new MockTechnology();
   21.22 -        model = new Modelik(ContextBuilder.create().withTechnology(my).build());
   21.23 +        model = new Modelik(Contexts.newBuilder().register(Technology.class, my, 1).build());
   21.24      }
   21.25      
   21.26      @Test public void classGeneratedWithSetterGetter() {
   21.27 @@ -166,7 +167,7 @@
   21.28      static void loadPeople(Modelik thiz, People p) {
   21.29          Modelik m = null;
   21.30          m.applyBindings();
   21.31 -        m.loadPeople("http", "apidesign.org", "query", new Person(Context.EMPTY));
   21.32 +        m.loadPeople("http", "apidesign.org", "query", new Person(BrwsrCtx.EMPTY));
   21.33      }
   21.34  
   21.35      @OnReceive(url = "{protocol}://{host}?callback={back}&query={query}", jsonp = "back")
    22.1 --- a/json/src/test/java/net/java/html/json/TypesTest.java	Mon May 27 16:22:20 2013 +0200
    22.2 +++ b/json/src/test/java/net/java/html/json/TypesTest.java	Tue May 28 13:31:42 2013 +0200
    22.3 @@ -20,10 +20,13 @@
    22.4   */
    22.5  package net.java.html.json;
    22.6  
    22.7 +import net.java.html.BrwsrCtx;
    22.8  import java.util.Map;
    22.9  import net.java.html.json.MapModelTest.One;
   22.10 +import org.apidesign.html.context.spi.Contexts;
   22.11  import org.apidesign.html.json.impl.WrapperObject;
   22.12 -import org.apidesign.html.json.spi.ContextBuilder;
   22.13 +import org.apidesign.html.json.spi.Technology;
   22.14 +import org.apidesign.html.json.spi.Transfer;
   22.15  import org.testng.annotations.BeforeMethod;
   22.16  import org.testng.annotations.Test;
   22.17  import static org.testng.Assert.*;
   22.18 @@ -44,11 +47,12 @@
   22.19  })
   22.20  public class TypesTest {
   22.21      private MapModelTest.MapTechnology t;
   22.22 -    private Context c;
   22.23 +    private BrwsrCtx c;
   22.24  
   22.25      @BeforeMethod public void initTechnology() {
   22.26          t = new MapModelTest.MapTechnology();
   22.27 -        c = ContextBuilder.create().withTechnology(t).withTransfer(t).build();
   22.28 +        c = Contexts.newBuilder().register(Technology.class, t, 1).
   22.29 +            register(Transfer.class, t, 1).build();
   22.30      }
   22.31      @Function static void readFromEvent(int intX, 
   22.32          /*
    23.1 --- a/json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java	Mon May 27 16:22:20 2013 +0200
    23.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java	Tue May 28 13:31:42 2013 +0200
    23.3 @@ -20,7 +20,7 @@
    23.4   */
    23.5  package org.apidesign.html.json.impl;
    23.6  
    23.7 -import net.java.html.json.Context;
    23.8 +import net.java.html.BrwsrCtx;
    23.9  import net.java.html.json.Model;
   23.10  import net.java.html.json.Property;
   23.11  import org.testng.Assert;
   23.12 @@ -35,7 +35,7 @@
   23.13  })
   23.14  public class EmployerTest {
   23.15      @Test public void preLoadsTheClass() {
   23.16 -        Employer em = JSON.read(Context.EMPTY, Employer.class, this);
   23.17 +        Employer em = JSON.read(BrwsrCtx.EMPTY, Employer.class, this);
   23.18          Assert.assertNotNull(em, "Class loaded");
   23.19          em.applyBindings();
   23.20      }
    24.1 --- a/json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java	Mon May 27 16:22:20 2013 +0200
    24.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java	Tue May 28 13:31:42 2013 +0200
    24.3 @@ -22,11 +22,11 @@
    24.4  
    24.5  import java.util.HashMap;
    24.6  import java.util.Map;
    24.7 -import net.java.html.json.Context;
    24.8 +import net.java.html.BrwsrCtx;
    24.9  import net.java.html.json.People;
   24.10  import net.java.html.json.Person;
   24.11  import net.java.html.json.Sex;
   24.12 -import org.apidesign.html.json.spi.ContextBuilder;
   24.13 +import org.apidesign.html.context.spi.Contexts;
   24.14  import org.apidesign.html.json.spi.FunctionBinding;
   24.15  import org.apidesign.html.json.spi.PropertyBinding;
   24.16  import org.apidesign.html.json.spi.Technology;
   24.17 @@ -50,7 +50,7 @@
   24.18      }
   24.19  
   24.20      @Test public void testConvertorOnAnObject() {
   24.21 -        Context c = ContextBuilder.create().withTechnology(this).build();
   24.22 +        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
   24.23          
   24.24          Person p = new Person(c);
   24.25          p.setFirstName("1");
   24.26 @@ -62,7 +62,7 @@
   24.27      }
   24.28      
   24.29      @Test public void testConvertorOnAnArray() {
   24.30 -        Context c = ContextBuilder.create().withTechnology(this).build();
   24.31 +        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
   24.32          
   24.33          Person p = new Person(c);
   24.34          p.setFirstName("1");
   24.35 @@ -84,7 +84,7 @@
   24.36      }
   24.37      
   24.38      @Test public void testNicknames() {
   24.39 -        Context c = ContextBuilder.create().withTechnology(this).build();
   24.40 +        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
   24.41          
   24.42          People people = new People(c);
   24.43          people.getNicknames().add("One");
   24.44 @@ -103,7 +103,7 @@
   24.45      
   24.46      @Test public void testConvertorOnAnArrayWithWrapper() {
   24.47          this.replaceArray = true;
   24.48 -        Context c = ContextBuilder.create().withTechnology(this).build();
   24.49 +        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
   24.50          
   24.51          Person p = new Person(c);
   24.52          p.setFirstName("1");
   24.53 @@ -119,7 +119,7 @@
   24.54  
   24.55      @Test public void bindingsOnArray() {
   24.56          this.replaceArray = true;
   24.57 -        Context c = ContextBuilder.create().withTechnology(this).build();
   24.58 +        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
   24.59          
   24.60          People p = new People(c);
   24.61          p.getAge().add(30);
    25.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java	Mon May 27 16:22:20 2013 +0200
    25.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.3 @@ -1,135 +0,0 @@
    25.4 -/**
    25.5 - * HTML via Java(tm) Language Bindings
    25.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    25.7 - *
    25.8 - * This program is free software: you can redistribute it and/or modify
    25.9 - * it under the terms of the GNU General Public License as published by
   25.10 - * the Free Software Foundation, version 2 of the License.
   25.11 - *
   25.12 - * This program is distributed in the hope that it will be useful,
   25.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   25.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   25.15 - * GNU General Public License for more details. apidesign.org
   25.16 - * designates this particular file as subject to the
   25.17 - * "Classpath" exception as provided by apidesign.org
   25.18 - * in the License file that accompanied this code.
   25.19 - *
   25.20 - * You should have received a copy of the GNU General Public License
   25.21 - * along with this program. Look for COPYING file in the top folder.
   25.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   25.23 - */
   25.24 -package org.apidesign.html.ko2brwsr;
   25.25 -
   25.26 -import java.io.ByteArrayOutputStream;
   25.27 -import java.io.IOException;
   25.28 -import java.io.InputStream;
   25.29 -import java.io.InputStreamReader;
   25.30 -import java.util.logging.Level;
   25.31 -import java.util.logging.Logger;
   25.32 -import net.java.html.json.Context;
   25.33 -import org.apidesign.html.json.spi.ContextBuilder;
   25.34 -import org.apidesign.html.json.spi.FunctionBinding;
   25.35 -import org.apidesign.html.json.spi.JSONCall;
   25.36 -import org.apidesign.html.json.spi.PropertyBinding;
   25.37 -import org.apidesign.html.json.spi.Technology;
   25.38 -import org.apidesign.html.json.spi.Transfer;
   25.39 -
   25.40 -/**
   25.41 - *
   25.42 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   25.43 - */
   25.44 -final class BrwsrCntxt implements Technology<Object>, Transfer {
   25.45 -    private BrwsrCntxt() {}
   25.46 -    
   25.47 -    public static final Context DEFAULT;
   25.48 -    static {
   25.49 -        BrwsrCntxt c = new BrwsrCntxt();
   25.50 -        DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
   25.51 -    }
   25.52 -    
   25.53 -    @Override
   25.54 -    public void extract(Object obj, String[] props, Object[] values) {
   25.55 -        ConvertTypes.extractJSON(obj, props, values);
   25.56 -    }
   25.57 -
   25.58 -    @Override
   25.59 -    public void loadJSON(final JSONCall call) {
   25.60 -        class R implements Runnable {
   25.61 -            Object[] arr = { null };
   25.62 -            @Override
   25.63 -            public void run() {
   25.64 -                call.notifySuccess(arr[0]);
   25.65 -            }
   25.66 -        }
   25.67 -        R r = new R();
   25.68 -        if (call.isJSONP()) {
   25.69 -            String me = ConvertTypes.createJSONP(r.arr, r);
   25.70 -            ConvertTypes.loadJSONP(call.composeURL(me), me);
   25.71 -        } else {
   25.72 -            String data = null;
   25.73 -            if (call.isDoOutput()) {
   25.74 -                try {
   25.75 -                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
   25.76 -                    call.writeData(bos);
   25.77 -                    data = new String(bos.toByteArray(), "UTF-8");
   25.78 -                } catch (IOException ex) {
   25.79 -                    call.notifyError(ex);
   25.80 -                }
   25.81 -            }
   25.82 -            ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
   25.83 -        }
   25.84 -    }
   25.85 -
   25.86 -    @Override
   25.87 -    public Object wrapModel(Object model) {
   25.88 -        return model;
   25.89 -    }
   25.90 -
   25.91 -    @Override
   25.92 -    public void bind(PropertyBinding b, Object model, Object data) {
   25.93 -        Knockout.bind(data, b, b.getPropertyName(), 
   25.94 -            "getValue__Ljava_lang_Object_2", 
   25.95 -            b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
   25.96 -            false, false
   25.97 -        );
   25.98 -    }
   25.99 -
  25.100 -    @Override
  25.101 -    public void valueHasMutated(Object data, String propertyName) {
  25.102 -        Knockout.valueHasMutated(data, propertyName);
  25.103 -    }
  25.104 -
  25.105 -    @Override
  25.106 -    public void expose(FunctionBinding fb, Object model, Object d) {
  25.107 -        Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
  25.108 -    }
  25.109 -
  25.110 -    @Override
  25.111 -    public void applyBindings(Object data) {
  25.112 -        Knockout.applyBindings(data);
  25.113 -    }
  25.114 -
  25.115 -    @Override
  25.116 -    public Object wrapArray(Object[] arr) {
  25.117 -        return arr;
  25.118 -    }
  25.119 -
  25.120 -    @Override
  25.121 -    public <M> M toModel(Class<M> modelClass, Object data) {
  25.122 -        return modelClass.cast(data);
  25.123 -    }
  25.124 -
  25.125 -    @Override
  25.126 -    public Object toJSON(InputStream is) throws IOException {
  25.127 -        StringBuilder sb = new StringBuilder();
  25.128 -        InputStreamReader r = new InputStreamReader(is);
  25.129 -        for (;;) {
  25.130 -            int ch = r.read();
  25.131 -            if (ch == -1) {
  25.132 -                break;
  25.133 -            }
  25.134 -            sb.append((char)ch);
  25.135 -        }
  25.136 -        return ConvertTypes.parse(sb.toString());
  25.137 -    }
  25.138 -}
    26.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java	Mon May 27 16:22:20 2013 +0200
    26.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.3 @@ -1,49 +0,0 @@
    26.4 -/**
    26.5 - * HTML via Java(tm) Language Bindings
    26.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    26.7 - *
    26.8 - * This program is free software: you can redistribute it and/or modify
    26.9 - * it under the terms of the GNU General Public License as published by
   26.10 - * the Free Software Foundation, version 2 of the License.
   26.11 - *
   26.12 - * This program is distributed in the hope that it will be useful,
   26.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   26.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   26.15 - * GNU General Public License for more details. apidesign.org
   26.16 - * designates this particular file as subject to the
   26.17 - * "Classpath" exception as provided by apidesign.org
   26.18 - * in the License file that accompanied this code.
   26.19 - *
   26.20 - * You should have received a copy of the GNU General Public License
   26.21 - * along with this program. Look for COPYING file in the top folder.
   26.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   26.23 - */
   26.24 -package org.apidesign.html.ko2brwsr;
   26.25 -
   26.26 -import net.java.html.json.Context;
   26.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
   26.28 -import org.apidesign.html.json.spi.ContextProvider;
   26.29 -import org.openide.util.lookup.ServiceProvider;
   26.30 -
   26.31 -/** This is an implementation package - just
   26.32 - * include its JAR on classpath and use official {@link Context} API
   26.33 - * to access the functionality.
   26.34 - * <p>
   26.35 - * Provides binding between models and <a href="http://bck2brwsr.apidesign.org">
   26.36 - * Bck2Brwsr</a> VM.
   26.37 - * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
   26.38 - *
   26.39 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   26.40 - */
   26.41 -@ServiceProvider(service = ContextProvider.class)
   26.42 -public final class BrwsrCntxtPrvdr implements ContextProvider {
   26.43 -    @Override
   26.44 -    public Context findContext(Class<?> requestor) {
   26.45 -        return bck2BrwsrVM() ? BrwsrCntxt.DEFAULT : null;
   26.46 -    }
   26.47 -    
   26.48 -    @JavaScriptBody(args = {  }, body = "return true;")
   26.49 -    private static boolean bck2BrwsrVM() {
   26.50 -        return false;
   26.51 -    }
   26.52 -}
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java	Tue May 28 13:31:42 2013 +0200
    27.3 @@ -0,0 +1,129 @@
    27.4 +/**
    27.5 + * HTML via Java(tm) Language Bindings
    27.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    27.7 + *
    27.8 + * This program is free software: you can redistribute it and/or modify
    27.9 + * it under the terms of the GNU General Public License as published by
   27.10 + * the Free Software Foundation, version 2 of the License.
   27.11 + *
   27.12 + * This program is distributed in the hope that it will be useful,
   27.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   27.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   27.15 + * GNU General Public License for more details. apidesign.org
   27.16 + * designates this particular file as subject to the
   27.17 + * "Classpath" exception as provided by apidesign.org
   27.18 + * in the License file that accompanied this code.
   27.19 + *
   27.20 + * You should have received a copy of the GNU General Public License
   27.21 + * along with this program. Look for COPYING file in the top folder.
   27.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   27.23 + */
   27.24 +package org.apidesign.html.ko2brwsr;
   27.25 +
   27.26 +import java.io.ByteArrayOutputStream;
   27.27 +import java.io.IOException;
   27.28 +import java.io.InputStream;
   27.29 +import java.io.InputStreamReader;
   27.30 +import net.java.html.BrwsrCtx;
   27.31 +import org.apidesign.html.context.spi.Contexts;
   27.32 +import org.apidesign.html.json.spi.FunctionBinding;
   27.33 +import org.apidesign.html.json.spi.JSONCall;
   27.34 +import org.apidesign.html.json.spi.PropertyBinding;
   27.35 +import org.apidesign.html.json.spi.Technology;
   27.36 +import org.apidesign.html.json.spi.Transfer;
   27.37 +
   27.38 +/**
   27.39 + *
   27.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   27.41 + */
   27.42 +final class BrwsrCtxImpl implements Technology<Object>, Transfer {
   27.43 +    private BrwsrCtxImpl() {}
   27.44 +    
   27.45 +    public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
   27.46 +    
   27.47 +    @Override
   27.48 +    public void extract(Object obj, String[] props, Object[] values) {
   27.49 +        ConvertTypes.extractJSON(obj, props, values);
   27.50 +    }
   27.51 +
   27.52 +    @Override
   27.53 +    public void loadJSON(final JSONCall call) {
   27.54 +        class R implements Runnable {
   27.55 +            Object[] arr = { null };
   27.56 +            @Override
   27.57 +            public void run() {
   27.58 +                call.notifySuccess(arr[0]);
   27.59 +            }
   27.60 +        }
   27.61 +        R r = new R();
   27.62 +        if (call.isJSONP()) {
   27.63 +            String me = ConvertTypes.createJSONP(r.arr, r);
   27.64 +            ConvertTypes.loadJSONP(call.composeURL(me), me);
   27.65 +        } else {
   27.66 +            String data = null;
   27.67 +            if (call.isDoOutput()) {
   27.68 +                try {
   27.69 +                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
   27.70 +                    call.writeData(bos);
   27.71 +                    data = new String(bos.toByteArray(), "UTF-8");
   27.72 +                } catch (IOException ex) {
   27.73 +                    call.notifyError(ex);
   27.74 +                }
   27.75 +            }
   27.76 +            ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
   27.77 +        }
   27.78 +    }
   27.79 +
   27.80 +    @Override
   27.81 +    public Object wrapModel(Object model) {
   27.82 +        return model;
   27.83 +    }
   27.84 +
   27.85 +    @Override
   27.86 +    public void bind(PropertyBinding b, Object model, Object data) {
   27.87 +        Knockout.bind(data, b, b.getPropertyName(), 
   27.88 +            "getValue__Ljava_lang_Object_2", 
   27.89 +            b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
   27.90 +            false, false
   27.91 +        );
   27.92 +    }
   27.93 +
   27.94 +    @Override
   27.95 +    public void valueHasMutated(Object data, String propertyName) {
   27.96 +        Knockout.valueHasMutated(data, propertyName);
   27.97 +    }
   27.98 +
   27.99 +    @Override
  27.100 +    public void expose(FunctionBinding fb, Object model, Object d) {
  27.101 +        Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
  27.102 +    }
  27.103 +
  27.104 +    @Override
  27.105 +    public void applyBindings(Object data) {
  27.106 +        Knockout.applyBindings(data);
  27.107 +    }
  27.108 +
  27.109 +    @Override
  27.110 +    public Object wrapArray(Object[] arr) {
  27.111 +        return arr;
  27.112 +    }
  27.113 +
  27.114 +    @Override
  27.115 +    public <M> M toModel(Class<M> modelClass, Object data) {
  27.116 +        return modelClass.cast(data);
  27.117 +    }
  27.118 +
  27.119 +    @Override
  27.120 +    public Object toJSON(InputStream is) throws IOException {
  27.121 +        StringBuilder sb = new StringBuilder();
  27.122 +        InputStreamReader r = new InputStreamReader(is);
  27.123 +        for (;;) {
  27.124 +            int ch = r.read();
  27.125 +            if (ch == -1) {
  27.126 +                break;
  27.127 +            }
  27.128 +            sb.append((char)ch);
  27.129 +        }
  27.130 +        return ConvertTypes.parse(sb.toString());
  27.131 +    }
  27.132 +}
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java	Tue May 28 13:31:42 2013 +0200
    28.3 @@ -0,0 +1,54 @@
    28.4 +/**
    28.5 + * HTML via Java(tm) Language Bindings
    28.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    28.7 + *
    28.8 + * This program is free software: you can redistribute it and/or modify
    28.9 + * it under the terms of the GNU General Public License as published by
   28.10 + * the Free Software Foundation, version 2 of the License.
   28.11 + *
   28.12 + * This program is distributed in the hope that it will be useful,
   28.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   28.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   28.15 + * GNU General Public License for more details. apidesign.org
   28.16 + * designates this particular file as subject to the
   28.17 + * "Classpath" exception as provided by apidesign.org
   28.18 + * in the License file that accompanied this code.
   28.19 + *
   28.20 + * You should have received a copy of the GNU General Public License
   28.21 + * along with this program. Look for COPYING file in the top folder.
   28.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
   28.23 + */
   28.24 +package org.apidesign.html.ko2brwsr;
   28.25 +
   28.26 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
   28.27 +import org.apidesign.html.context.spi.Contexts;
   28.28 +import org.apidesign.html.json.spi.Technology;
   28.29 +import org.apidesign.html.json.spi.Transfer;
   28.30 +import org.openide.util.lookup.ServiceProvider;
   28.31 +
   28.32 +/** This is an implementation package - just
   28.33 + * include its JAR on classpath and use official {@link Context} API
   28.34 + * to access the functionality.
   28.35 + * <p>
   28.36 + * Provides binding between models and <a href="http://bck2brwsr.apidesign.org">
   28.37 + * Bck2Brwsr</a> VM.
   28.38 + * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
   28.39 + *
   28.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   28.41 + */
   28.42 +@ServiceProvider(service = Contexts.Provider.class)
   28.43 +public final class BrwsrCtxPrvdr implements Contexts.Provider {
   28.44 +
   28.45 +    @Override
   28.46 +    public void fillContext(Contexts.Builder context, Class<?> requestor) {
   28.47 +        if (bck2BrwsrVM()) {
   28.48 +            context.register(Technology.class, BrwsrCtxImpl.DEFAULT, 50).
   28.49 +            register(Transfer.class, BrwsrCtxImpl.DEFAULT, 50);
   28.50 +        }
   28.51 +    }
   28.52 +    
   28.53 +    @JavaScriptBody(args = {  }, body = "return true;")
   28.54 +    private static boolean bck2BrwsrVM() {
   28.55 +        return false;
   28.56 +    }
   28.57 +}
    29.1 --- a/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java	Mon May 27 16:22:20 2013 +0200
    29.2 +++ b/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java	Tue May 28 13:31:42 2013 +0200
    29.3 @@ -21,9 +21,12 @@
    29.4  package org.apidesign.html.ko2brwsr;
    29.5  
    29.6  import java.util.Map;
    29.7 -import net.java.html.json.Context;
    29.8 +import net.java.html.BrwsrCtx;
    29.9  import org.apidesign.bck2brwsr.core.JavaScriptBody;
   29.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
   29.11 +import org.apidesign.html.context.spi.Contexts;
   29.12 +import org.apidesign.html.json.spi.Technology;
   29.13 +import org.apidesign.html.json.spi.Transfer;
   29.14  import org.apidesign.html.json.tck.KnockoutTCK;
   29.15  import org.openide.util.lookup.ServiceProvider;
   29.16  import org.testng.annotations.Factory;
   29.17 @@ -42,8 +45,10 @@
   29.18      }
   29.19      
   29.20      @Override
   29.21 -    public Context createContext() {
   29.22 -        return BrwsrCntxt.DEFAULT;
   29.23 +    public BrwsrCtx createContext() {
   29.24 +        return Contexts.newBuilder().
   29.25 +            register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
   29.26 +            register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
   29.27      }
   29.28  
   29.29  
    30.1 --- a/ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java	Mon May 27 16:22:20 2013 +0200
    30.2 +++ b/ko-fx/src/main/java/org/apidesign/html/kofx/FXContext.java	Tue May 28 13:31:42 2013 +0200
    30.3 @@ -24,10 +24,8 @@
    30.4  import java.io.InputStream;
    30.5  import java.util.ServiceLoader;
    30.6  import java.util.logging.Logger;
    30.7 -import net.java.html.json.Context;
    30.8  import netscape.javascript.JSObject;
    30.9 -import org.apidesign.html.json.spi.ContextBuilder;
   30.10 -import org.apidesign.html.json.spi.ContextProvider;
   30.11 +import org.apidesign.html.context.spi.Contexts;
   30.12  import org.apidesign.html.json.spi.FunctionBinding;
   30.13  import org.apidesign.html.json.spi.JSONCall;
   30.14  import org.apidesign.html.json.spi.PropertyBinding;
   30.15 @@ -43,18 +41,16 @@
   30.16   *
   30.17   * @author Jaroslav Tulach <jtulach@netbeans.org>
   30.18   */
   30.19 -@ServiceProvider(service = ContextProvider.class)
   30.20 +@ServiceProvider(service = Contexts.Provider.class)
   30.21  public final class FXContext
   30.22 -implements Technology<JSObject>, Transfer, ContextProvider {
   30.23 +implements Technology<JSObject>, Transfer, Contexts.Provider {
   30.24      static final Logger LOG = Logger.getLogger(FXContext.class.getName());
   30.25  
   30.26      @Override
   30.27 -    public Context findContext(Class<?> requestor) {
   30.28 +    public void fillContext(Contexts.Builder context, Class<?> requestor) {
   30.29          if (Knockout.web() != null) {
   30.30 -            return ContextBuilder.create().withTechnology(this).
   30.31 -                withTransfer(this).build();
   30.32 -        } else {
   30.33 -            return null;
   30.34 +            context.register(Technology.class, this, 100);
   30.35 +            context.register(Transfer.class, this, 100);
   30.36          }
   30.37      }
   30.38  
    31.1 --- a/ko-fx/src/main/java/org/apidesign/html/kofx/Knockout.java	Mon May 27 16:22:20 2013 +0200
    31.2 +++ b/ko-fx/src/main/java/org/apidesign/html/kofx/Knockout.java	Tue May 28 13:31:42 2013 +0200
    31.3 @@ -27,7 +27,6 @@
    31.4  import java.util.logging.Level;
    31.5  import java.util.logging.Logger;
    31.6  import javafx.scene.web.WebEngine;
    31.7 -import net.java.html.json.Context;
    31.8  import net.java.html.json.Model;
    31.9  import netscape.javascript.JSObject;
   31.10  import org.apidesign.html.json.spi.FunctionBinding;
    32.1 --- a/ko-fx/src/test/java/org/apidesign/html/kofx/KnockoutFXTest.java	Mon May 27 16:22:20 2013 +0200
    32.2 +++ b/ko-fx/src/test/java/org/apidesign/html/kofx/KnockoutFXTest.java	Tue May 28 13:31:42 2013 +0200
    32.3 @@ -21,10 +21,12 @@
    32.4  package org.apidesign.html.kofx;
    32.5  
    32.6  import java.util.Map;
    32.7 -import net.java.html.json.Context;
    32.8 +import net.java.html.BrwsrCtx;
    32.9  import netscape.javascript.JSObject;
   32.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
   32.11 -import org.apidesign.html.json.spi.ContextBuilder;
   32.12 +import org.apidesign.html.context.spi.Contexts;
   32.13 +import org.apidesign.html.json.spi.Technology;
   32.14 +import org.apidesign.html.json.spi.Transfer;
   32.15  import org.apidesign.html.json.tck.KnockoutTCK;
   32.16  import org.json.JSONException;
   32.17  import org.json.JSONObject;
   32.18 @@ -45,10 +47,12 @@
   32.19      }
   32.20  
   32.21      @Override
   32.22 -    public Context createContext() {
   32.23 +    public BrwsrCtx createContext() {
   32.24          FXContext fx = new FXContext();
   32.25 -        return ContextBuilder.create().
   32.26 -            withTechnology(fx).withTransfer(fx).build();
   32.27 +        return Contexts.newBuilder().
   32.28 +            register(Technology.class, fx, 10).
   32.29 +            register(Transfer.class, fx, 10).
   32.30 +            build();
   32.31      }
   32.32  
   32.33      @Override
    33.1 --- a/pom.xml	Mon May 27 16:22:20 2013 +0200
    33.2 +++ b/pom.xml	Tue May 28 13:31:42 2013 +0200
    33.3 @@ -25,6 +25,7 @@
    33.4      <module>ko-archetype-test</module>
    33.5      <module>ko-bck2brwsr</module>
    33.6      <module>ko-fx</module>
    33.7 +    <module>context</module>
    33.8    </modules>
    33.9    <licenses>
   33.10        <license>
   33.11 @@ -255,4 +256,4 @@
   33.12          </dependency>
   33.13        </dependencies>
   33.14    </dependencyManagement>
   33.15 -</project>
   33.16 +</project>
   33.17 \ No newline at end of file