Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
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]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.knockout; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.HashSet; |
46 |
import java.util.LinkedHashSet; |
47 |
import java.util.List; |
48 |
import java.util.Set; |
49 |
import org.netbeans.spi.knockout.BindingsProvider.Response; |
50 |
import org.openide.filesystems.FileObject; |
51 |
import org.openide.util.Lookup; |
52 |
|
53 |
/** |
54 |
* Allows structural description of a <code>ko.applyBindings</code> parameter. |
55 |
* Register as {@link BindingsProvider}. When called build description of JSON |
56 |
* types using methods like {@link #stringProperty(java.lang.String, boolean)} |
57 |
* and then pass the result into |
58 |
* {@link Response#applyBindings(org.netbeans.spi.knockout.Bindings)} method. |
59 |
* |
60 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
61 |
*/ |
62 |
public final class Bindings { |
63 |
|
64 |
private final String name; |
65 |
private final List<Bindings> subBindings = new ArrayList<>(); |
66 |
private final List<String> props = new ArrayList<>(); |
67 |
|
68 |
private Bindings(String name) { |
69 |
this.name = name; |
70 |
} |
71 |
|
72 |
/** |
73 |
* Defines new variable with provided name and assigns a JSON object into |
74 |
* it. The created instance is a builder - continue calling its various |
75 |
* builder methods like {@link #intProperty(java.lang.String, boolean)}. |
76 |
* |
77 |
* @param name name of variable (must be valid JavaScript identifier) |
78 |
* @return empty JSON object ready to be filled using |
79 |
* {@link #doubleProperty(java.lang.String, boolean)}, and other methods |
80 |
*/ |
81 |
public static Bindings create(String name) { |
82 |
return new Bindings(name); |
83 |
} |
84 |
|
85 |
/** |
86 |
* Generates a boolean property (with value <code>true</code>) into the JSON |
87 |
* class. |
88 |
* |
89 |
* @param name name of variable (must be valid JavaScript identifier) |
90 |
* @param array should this property be an array of just a single value |
91 |
* @return this object |
92 |
*/ |
93 |
public final Bindings booleanProperty(String name, boolean array) { |
94 |
addProp(name, array, "true"); |
95 |
return this; |
96 |
} |
97 |
|
98 |
/** |
99 |
* Generates a string property (with value <code>''</code>) into the JSON |
100 |
* class. |
101 |
* |
102 |
* @param name name of variable (must be valid JavaScript identifier) |
103 |
* @param array should this property be an array of just a single value |
104 |
* @return this object |
105 |
*/ |
106 |
public final Bindings stringProperty(String name, boolean array) { |
107 |
addProp(name, array, "''"); |
108 |
return this; |
109 |
} |
110 |
|
111 |
/** |
112 |
* Generates a integer property (with value <code>0</code>) into the JSON |
113 |
* class. |
114 |
* |
115 |
* @param name name of variable (must be valid JavaScript identifier) |
116 |
* @param array should this property be an array of just a single value |
117 |
* @return this object |
118 |
*/ |
119 |
public final Bindings intProperty(String name, boolean array) { |
120 |
addProp(name, array, "0"); |
121 |
return this; |
122 |
} |
123 |
|
124 |
/** |
125 |
* Generates a floating point property (with value <code>0.1</code>) into |
126 |
* the JSON class. |
127 |
* |
128 |
* @param name name of variable (must be valid JavaScript identifier) |
129 |
* @param array should this property be an array of just a single value |
130 |
* @return this object |
131 |
*/ |
132 |
public final Bindings doubleProperty(String name, boolean array) { |
133 |
addProp(name, array, "0.1"); |
134 |
return this; |
135 |
} |
136 |
|
137 |
/** |
138 |
* Generates complex subtype based on another {@link Bindings} class. |
139 |
* |
140 |
* @param name name of variable (must be valid JavaScript identifier) |
141 |
* @param binding another description of a JSON like object |
142 |
* @param array should this property be an array of just a single value |
143 |
* @return this object |
144 |
*/ |
145 |
public final Bindings modelProperty(String name, Bindings binding, boolean array) { |
146 |
subBindings.add(binding); |
147 |
addProp(name, array, binding.name); |
148 |
return this; |
149 |
} |
150 |
|
151 |
final String generate() { |
152 |
StringBuilder sb = new StringBuilder(); |
153 |
//sb.append("(function() {\n"); |
154 |
HashSet<Bindings> visited = new HashSet<>(); |
155 |
LinkedHashSet<Bindings> lhs = new LinkedHashSet<>(); |
156 |
walkBindings(visited, lhs); |
157 |
for (Bindings b : lhs) { |
158 |
b.generate(sb); |
159 |
} |
160 |
sb.append(" ko.applyBindings(").append(name).append(");\n"); |
161 |
//sb.append("}());"); |
162 |
return sb.toString(); |
163 |
} |
164 |
|
165 |
private void generate(StringBuilder sb) { |
166 |
sb.append("var ").append(name).append(" = {"); |
167 |
String sep = "\n"; |
168 |
for (String s : props) { |
169 |
sb.append(sep).append(" ").append(s); |
170 |
sep = ",\n"; |
171 |
} |
172 |
sb.append("\n};\n"); |
173 |
} |
174 |
|
175 |
private void addProp(String name, boolean array, String value) { |
176 |
if (array) { |
177 |
value = "[ " + value + " ]"; |
178 |
} |
179 |
if (name.contains("\"")) { |
180 |
throw new IllegalStateException("Wrong name " + name); |
181 |
} |
182 |
props.add('\"' + name + "\" : " + value); |
183 |
} |
184 |
|
185 |
private void walkBindings(Set<Bindings> visited, Set<Bindings> collect) { |
186 |
if (!visited.add(this)) { |
187 |
return; |
188 |
} |
189 |
for (Bindings b : subBindings) { |
190 |
b.walkBindings(visited, collect); |
191 |
} |
192 |
collect.add(this); |
193 |
} |
194 |
|
195 |
static { |
196 |
new SPIAccessor() { |
197 |
@Override |
198 |
protected String findBindings(FileObject fo) { |
199 |
Response r = new Response(); |
200 |
for (BindingsProvider p : Lookup.getDefault().lookupAll(BindingsProvider.class)) { |
201 |
p.findBindings(fo, r); |
202 |
if (r.bindings != null) { |
203 |
return r.bindings.generate(); |
204 |
} |
205 |
} |
206 |
return null; |
207 |
} |
208 |
}; |
209 |
} |
210 |
} |