jaroslav@59
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@59
|
3 |
*
|
jaroslav@365
|
4 |
* Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@59
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@59
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@358
|
30 |
* Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@59
|
42 |
*/
|
jaroslav@59
|
43 |
package net.java.html.json;
|
jaroslav@59
|
44 |
|
jaroslav@110
|
45 |
import net.java.html.BrwsrCtx;
|
jaroslav@60
|
46 |
import java.io.IOException;
|
jaroslav@60
|
47 |
import java.io.InputStream;
|
jaroslav@362
|
48 |
import org.netbeans.html.json.impl.JSON;
|
jaroslav@59
|
49 |
|
jaroslav@59
|
50 |
/** Information about and
|
jaroslav@59
|
51 |
* operations for classes generated by the {@link Model @Model}
|
jaroslav@59
|
52 |
* annotation.
|
jaroslav@59
|
53 |
*
|
jaroslav@59
|
54 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@59
|
55 |
*/
|
jaroslav@59
|
56 |
public final class Models {
|
jaroslav@59
|
57 |
private Models() {
|
jaroslav@59
|
58 |
}
|
jaroslav@59
|
59 |
|
jaroslav@59
|
60 |
/** Finds out whether given class is a model class - e.g. has been
|
jaroslav@59
|
61 |
* generated by {@link Model @Model} annotation.
|
jaroslav@59
|
62 |
*
|
jaroslav@60
|
63 |
* @param clazz the class to test
|
jaroslav@60
|
64 |
* @return true, if <code>clazz</code> was generated by {@link Model} annotation
|
jaroslav@59
|
65 |
* @since 0.2
|
jaroslav@59
|
66 |
*/
|
jaroslav@59
|
67 |
public static boolean isModel(Class<?> clazz) {
|
jaroslav@59
|
68 |
return JSON.isModel(clazz);
|
jaroslav@59
|
69 |
}
|
jaroslav@60
|
70 |
|
jaroslav@111
|
71 |
/** Binds given model to another context.
|
jaroslav@111
|
72 |
*
|
jaroslav@111
|
73 |
* @param <Model> class defined by {@link net.java.html.json.Model} annotation
|
jaroslav@111
|
74 |
* @param model instance of a model defined by {@link net.java.html.json.Model} annotation
|
jaroslav@111
|
75 |
* @param context context to which the model should be bound
|
jaroslav@111
|
76 |
* @return new instance of model bound to new <code>context</code>
|
jaroslav@111
|
77 |
* @throws IllegalArgumentException in case the instance is not generated by model interface
|
jaroslav@111
|
78 |
* @since 0.4
|
jaroslav@111
|
79 |
*/
|
jaroslav@111
|
80 |
public static <Model> Model bind(Model model, BrwsrCtx context) {
|
jaroslav@111
|
81 |
return JSON.bindTo(model, context);
|
jaroslav@111
|
82 |
}
|
jaroslav@111
|
83 |
|
jaroslav@60
|
84 |
/** Generic method to parse content of a model class from a stream.
|
jaroslav@60
|
85 |
*
|
jaroslav@60
|
86 |
* @param c context of the technology to use for reading
|
jaroslav@60
|
87 |
* @param model the model class generated by {@link Model} annotation
|
jaroslav@60
|
88 |
* @param is input stream with data
|
jaroslav@60
|
89 |
* @return new instance of the model class
|
jaroslav@60
|
90 |
* @since 0.2
|
jaroslav@60
|
91 |
*/
|
jaroslav@110
|
92 |
public static <M> M parse(BrwsrCtx c, Class<M> model, InputStream is) throws IOException {
|
jaroslav@60
|
93 |
return JSON.readStream(c, model, is);
|
jaroslav@60
|
94 |
}
|
jaroslav@326
|
95 |
|
jaroslav@326
|
96 |
/** Converts an existing, raw, JSON object into a {@link Model model class}.
|
jaroslav@326
|
97 |
*
|
jaroslav@326
|
98 |
* @param <M> the type of the model class
|
jaroslav@326
|
99 |
* @param ctx context of the technology to use for converting
|
jaroslav@326
|
100 |
* @param model the model class
|
jaroslav@326
|
101 |
* @param jsonObject original instance of the JSON object
|
jaroslav@326
|
102 |
* @return new instance of the model class
|
jaroslav@326
|
103 |
* @since 0.7
|
jaroslav@326
|
104 |
*/
|
jaroslav@326
|
105 |
public static <M> M fromRaw(BrwsrCtx ctx, Class<M> model, Object jsonObject) {
|
jaroslav@326
|
106 |
return JSON.read(ctx, model, jsonObject);
|
jaroslav@326
|
107 |
}
|
jaroslav@326
|
108 |
|
jaroslav@326
|
109 |
// /** Converts an existing {@link Model model} into its associated, raw
|
jaroslav@326
|
110 |
// * JSON object. The object may, but does not have to, be the same instance
|
jaroslav@326
|
111 |
// * as the model object.
|
jaroslav@326
|
112 |
// *
|
jaroslav@326
|
113 |
// * @param model the model object
|
jaroslav@326
|
114 |
// * @return the raw JSON object associated with the model
|
jaroslav@326
|
115 |
// * @since 0.7
|
jaroslav@326
|
116 |
// */
|
jaroslav@326
|
117 |
// public static Object toRaw(Object model) {
|
jaroslav@326
|
118 |
// return JSON.toJSON(model);
|
jaroslav@326
|
119 |
// }
|
jaroslav@59
|
120 |
}
|