2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json;
45 import net.java.html.BrwsrCtx;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import org.netbeans.html.json.impl.JSON;
50 /** Information about and
51 * operations for classes generated by the {@link Model @Model}
54 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
56 public final class Models {
60 /** Finds out whether given class is a model class - e.g. has been
61 * generated by {@link Model @Model} annotation.
63 * @param clazz the class to test
64 * @return true, if <code>clazz</code> was generated by {@link Model} annotation
67 public static boolean isModel(Class<?> clazz) {
68 return JSON.isModel(clazz);
71 /** Binds given model to another context.
73 * @param <Model> class defined by {@link net.java.html.json.Model} annotation
74 * @param model instance of a model defined by {@link net.java.html.json.Model} annotation
75 * @param context context to which the model should be bound
76 * @return new instance of model bound to new <code>context</code>
77 * @throws IllegalArgumentException in case the instance is not generated by model interface
80 public static <Model> Model bind(Model model, BrwsrCtx context) {
81 return JSON.bindTo(model, context);
84 /** Generic method to parse content of a model class from a stream.
86 * @param c context of the technology to use for reading
87 * @param model the model class generated by {@link Model} annotation
88 * @param is input stream with data
89 * @return new instance of the model class
92 public static <M> M parse(BrwsrCtx c, Class<M> model, InputStream is) throws IOException {
93 return JSON.readStream(c, model, is);
96 /** Converts an existing, raw, JSON object into a {@link Model model class}.
98 * @param <M> the type of the model class
99 * @param ctx context of the technology to use for converting
100 * @param model the model class
101 * @param jsonObject original instance of the JSON object
102 * @return new instance of the model class
105 public static <M> M fromRaw(BrwsrCtx ctx, Class<M> model, Object jsonObject) {
106 return JSON.read(ctx, model, jsonObject);
109 /** Converts an existing {@link Model model} into its associated, raw
110 * JSON object. The object may, but does not have to, be the same instance
111 * as the model object.
113 * @param model the model object (not <code>null</code>)
114 * @return the raw JSON object associated with the model
115 * @throws IllegalArgumentException if the <code>model</code> is
116 * not instance of a class
117 * generated by {@link Model model annotation} processor.
120 public static Object toRaw(Object model) {
121 final Class<? extends Object> type = model.getClass();
122 if (!isModel(type)) {
123 throw new IllegalStateException("Not a model " + type);
125 return JSON.find(model);
128 /** Apply bindings of a model class. Each model class has an
129 * apply bindings method that "activates" it. In <em>ko4j</em> mode,
130 * it binds the model values to the currently active page. One
131 * can call the generated method directly, or use this static
132 * helper method to perform the activation.
134 * @param model instance of a {@link Model class}
135 * @throws IllegalArgumentException if the <code>model</code> is not
136 * instance of a class generated by {@link Model model annotation}
141 public static void applyBindings(Object model) {
142 JSON.applyBindings(model);