2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package net.java.html;
23 import java.util.ServiceLoader;
24 import org.apidesign.html.context.impl.CtxAccssr;
25 import org.apidesign.html.context.impl.CtxImpl;
26 import org.apidesign.html.context.spi.Contexts;
28 /** Represents context where the <code>net.java.html.json.Model</code>
30 * operate in. The context is usually a particular HTML page in a browser.
31 * The context is also associated with the actual HTML technology
32 * in the HTML page - there is likely to be different context for
33 * <a href="http://knockoutjs.com">knockout.js</a> and different one
34 * for <a href="http://angularjs.org">angular</a>.
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 public final class BrwsrCtx {
39 private final CtxImpl impl;
40 private BrwsrCtx(CtxImpl impl) {
46 protected BrwsrCtx newContext(CtxImpl impl) {
47 return new BrwsrCtx(impl);
51 protected CtxImpl find(BrwsrCtx context) {
56 /** Dummy context without binding to any real browser or technology.
57 * Useful for simple unit testing of behavior of various business logic
60 public static final BrwsrCtx EMPTY = Contexts.newBuilder().build();
62 /** Seeks for the default context that is associated with the requesting
63 * class. If no suitable context is found, a warning message is
64 * printed and {@link #EMPTY} context is returned.
66 * @param requestor the class that makes the request
67 * @return appropriate context for the request
69 public static BrwsrCtx findDefault(Class<?> requestor) {
70 org.apidesign.html.context.spi.Contexts.Builder cb = Contexts.newBuilder();
71 boolean found = false;
75 l = requestor.getClassLoader();
76 } catch (SecurityException ex) {
80 for (org.apidesign.html.context.spi.Contexts.Provider cp : ServiceLoader.load(
81 org.apidesign.html.context.spi.Contexts.Provider.class, l
83 cp.fillContext(cb, requestor);
87 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())) {
88 cp.fillContext(cb, requestor);
91 } catch (SecurityException ex) {
95 // if we have some data from regular provides, go on
98 // XXX: print out a warning