jtulach@0
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@0
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jtulach@0
|
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.
|
jtulach@0
|
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@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 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.
|
jtulach@0
|
42 |
*/
|
jtulach@0
|
43 |
package net.java.html.json;
|
jtulach@0
|
44 |
|
jtulach@0
|
45 |
import java.lang.annotation.ElementType;
|
jtulach@0
|
46 |
import java.lang.annotation.Retention;
|
jtulach@0
|
47 |
import java.lang.annotation.RetentionPolicy;
|
jtulach@0
|
48 |
import java.lang.annotation.Target;
|
jaroslav@232
|
49 |
import java.net.URL;
|
jtulach@0
|
50 |
|
jaroslav@232
|
51 |
/** Defines a model class that represents a single
|
jaroslav@232
|
52 |
* <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>-like object
|
jaroslav@232
|
53 |
* named {@link #className()}. The generated class contains
|
jaroslav@232
|
54 |
* getters and setters for properties defined via {@link #properties()} and
|
jaroslav@232
|
55 |
* getters for other, derived properties defined by annotating methods
|
jaroslav@232
|
56 |
* of this class by {@link ComputedProperty}. Each property
|
jaroslav@232
|
57 |
* can be of primitive type, an {@link Enum enum type} or (in order to create
|
jaroslav@232
|
58 |
* nested <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> structure)
|
jaroslav@232
|
59 |
* of another {@link Model class generated by @Model} annotation. Each property
|
jaroslav@232
|
60 |
* can either represent a single value or be an array of its values.
|
jtulach@0
|
61 |
* <p>
|
jaroslav@232
|
62 |
* The {@link #className() generated class}'s <code>toString</code> method
|
jtulach@0
|
63 |
* converts the state of the object into
|
jaroslav@232
|
64 |
* <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> format. One can
|
jaroslav@232
|
65 |
* use {@link Models#parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream)}
|
jaroslav@232
|
66 |
* method to read the JSON text stored in a file or other stream back into the Java model.
|
jaroslav@232
|
67 |
* One can also use {@link OnReceive @OnReceive} annotation to read the model
|
jaroslav@232
|
68 |
* asynchronously from a {@link URL}.
|
jaroslav@32
|
69 |
* <p>
|
jaroslav@229
|
70 |
* An example where one defines class <code>Person</code> with four
|
jaroslav@229
|
71 |
* properties (<code>firstName</code>, <code>lastName</code>, array of <code>addresses</code> and
|
jaroslav@32
|
72 |
* <code>fullName</code>) follows:
|
jaroslav@32
|
73 |
* <pre>
|
jaroslav@32
|
74 |
* {@link Model @Model}(className="Person", properties={
|
jaroslav@32
|
75 |
* {@link Property @Property}(name = "firstName", type=String.class),
|
jaroslav@32
|
76 |
* {@link Property @Property}(name = "lastName", type=String.class)
|
jaroslav@229
|
77 |
* {@link Property @Property}(name = "addresses", type=Address.class, array = true)
|
jaroslav@32
|
78 |
* })
|
jaroslav@229
|
79 |
* static class PersonModel {
|
jaroslav@32
|
80 |
* {@link ComputedProperty @ComputedProperty}
|
jaroslav@32
|
81 |
* static String fullName(String firstName, String lastName) {
|
jaroslav@32
|
82 |
* return firstName + " " + lastName;
|
jaroslav@32
|
83 |
* }
|
jaroslav@229
|
84 |
*
|
jaroslav@229
|
85 |
* {@link Model @Model}(className="Address", properties={
|
jaroslav@229
|
86 |
* {@link Property @Property}(name = "street", type=String.class),
|
jaroslav@229
|
87 |
* {@link Property @Property}(name = "town", type=String.class)
|
jaroslav@229
|
88 |
* })
|
jaroslav@229
|
89 |
* static class AddressModel {
|
jaroslav@229
|
90 |
* }
|
jaroslav@229
|
91 |
* }
|
jaroslav@229
|
92 |
* </pre>
|
jaroslav@232
|
93 |
* The generated model class has a default constructor, and also <em>quick
|
jaroslav@232
|
94 |
* instantiation</em> constructor that accepts all non-array properties
|
jaroslav@232
|
95 |
* (in the order used in the {@link #properties()} attribute) and vararg list
|
jaroslav@232
|
96 |
* for the first array property (if any). One can thus use following code
|
jaroslav@232
|
97 |
* to create an instance of the Person and Address classes:
|
jaroslav@229
|
98 |
* <pre>
|
jaroslav@232
|
99 |
*
|
jaroslav@232
|
100 |
* Person p = new Person("Jaroslav", "Tulach",
|
jaroslav@232
|
101 |
* new Address("Markoušovice", "Úpice"),
|
jaroslav@232
|
102 |
* new Address("V Parku", "Praha")
|
jaroslav@232
|
103 |
* );
|
jaroslav@232
|
104 |
* // p.toString() then returns equivalent of following <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> object
|
jaroslav@229
|
105 |
* {
|
jaroslav@229
|
106 |
* "firstName" : "Jaroslav",
|
jaroslav@229
|
107 |
* "lastName" : "Tulach",
|
jaroslav@229
|
108 |
* "addresses" : [
|
jaroslav@232
|
109 |
* { "street" : "Markoušovice", "town" : "Úpice" },
|
jaroslav@232
|
110 |
* { "street" : "V Parku", "town" : "Praha" },
|
jaroslav@229
|
111 |
* ]
|
jaroslav@32
|
112 |
* }
|
jaroslav@32
|
113 |
* </pre>
|
jaroslav@232
|
114 |
* <p>
|
jaroslav@232
|
115 |
* In case you are using <a href="http://knockoutjs.com/">Knockout technology</a>
|
jaroslav@232
|
116 |
* for Java then you can associate such model object with surrounding HTML page by
|
jaroslav@232
|
117 |
* calling: <code>p.applyBindings();</code>. The page can then use regular
|
jaroslav@232
|
118 |
* <a href="http://knockoutjs.com/">Knockout</a> bindings to reference your
|
jaroslav@232
|
119 |
* model and create dynamic connection between your model in Java and
|
jaroslav@232
|
120 |
* live DOM structure in the browser:
|
jaroslav@232
|
121 |
* <pre>
|
jaroslav@232
|
122 |
* Name: <span data-bind="text: fullName">
|
jaroslav@232
|
123 |
* <div data-bind="foreach: addresses">
|
jaroslav@232
|
124 |
* Lives in <span data-bind="text: town"/>
|
jaroslav@232
|
125 |
* </div>
|
jaroslav@232
|
126 |
* </pre>
|
jaroslav@549
|
127 |
* <p>
|
jaroslav@549
|
128 |
* Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7510833">on-line demo</a>
|
jaroslav@549
|
129 |
* to see a histogram driven by the {@link Model} annotation or try
|
jaroslav@549
|
130 |
* a little <a target="_blank" href="http://dew.apidesign.org/dew/#7263102">math test</a>.
|
jtulach@0
|
131 |
*
|
jtulach@0
|
132 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@0
|
133 |
*/
|
jtulach@0
|
134 |
@Retention(RetentionPolicy.SOURCE)
|
jtulach@0
|
135 |
@Target(ElementType.TYPE)
|
jtulach@0
|
136 |
public @interface Model {
|
jtulach@0
|
137 |
/** Name of the model class */
|
jtulach@0
|
138 |
String className();
|
jtulach@0
|
139 |
/** List of properties in the model.
|
jtulach@0
|
140 |
*/
|
jtulach@0
|
141 |
Property[] properties();
|
jtulach@0
|
142 |
}
|