Revision 2714
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/README.txt | ||
---|---|---|
1 |
==== |
|
2 |
gvSIG. Desktop Geographic Information System. |
|
3 |
|
|
4 |
Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
|
|
6 |
This program is free software; you can redistribute it and/or |
|
7 |
modify it under the terms of the GNU General Public License |
|
8 |
as published by the Free Software Foundation; either version 2 |
|
9 |
of the License, or (at your option) any later version. |
|
10 |
|
|
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
|
|
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program; if not, write to the Free Software |
|
18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
MA 02110-1301, USA. |
|
20 |
|
|
21 |
For any additional information, do not hesitate to contact us |
|
22 |
at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
==== |
|
24 |
|
|
25 |
The first time you checkout the current project to a new workspace, |
|
26 |
you have to prepare it to be able to work easily with maven from |
|
27 |
eclipse itself. |
|
28 |
|
|
29 |
Perform the following steps: |
|
30 |
|
|
31 |
1.- Launch the *prepare-workspace.xml* ant build file. |
|
32 |
You can do it by loading the file into the ant view, |
|
33 |
and running the default task, or right-clicking the |
|
34 |
file from the package explorer or the navigator and |
|
35 |
select the option: *Run as > Ant build*. |
|
36 |
|
|
37 |
2.- Restart eclipse. |
|
38 |
|
|
39 |
3.- Import the subprojects of the project you have just checked out. |
|
40 |
|
|
41 |
Those steps are only needed once per workspace. |
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.tools.util.impl.ToolsUtilLibraryImpl |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/euclidean/DefaultEuclideanManager.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.euclidean; |
|
7 |
|
|
8 |
import java.awt.geom.Point2D; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author fdiaz |
|
13 |
*/ |
|
14 |
public class DefaultEuclideanManager implements EuclideanManager{ |
|
15 |
|
|
16 |
@Override |
|
17 |
public EuclideanLine2D createLine2D(double coefA, double coefB, double coefC) { |
|
18 |
return new EuclideanLine2DImpl(coefA, coefB, coefC); |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public EuclideanLine2D createLine2D(double m, double b) { |
|
23 |
return new EuclideanLine2DImpl(m, b); |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public EuclideanLine2D createLine2D(double x0, double y0, double x1, double y1) { |
|
28 |
return new EuclideanLine2DImpl(x0, y0, x1, y1); |
|
29 |
} |
|
30 |
|
|
31 |
@Override |
|
32 |
public EuclideanLine2D createLine2D(Point2D p1, Point2D p2) { |
|
33 |
return new EuclideanLine2DImpl(p2, p1); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public EuclideanLine2D createLine2D(double m, Point2D p) { |
|
38 |
if(Double.isInfinite(m)){ |
|
39 |
return new EuclideanLine2DImpl(1, 0, -p.getX()); |
|
40 |
} |
|
41 |
return new EuclideanLine2DImpl(m, getYIntercept(m, p)); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public double getYIntercept(double m, Point2D p) { |
|
46 |
return getYIntercept(m, p.getX(), p.getY()); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public double getYIntercept(double m, double x, double y) { |
|
51 |
return -m*x+y; |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public double distance(Point2D p1, Point2D p2) { |
|
56 |
double x = Math.sqrt( Math.pow(p2.getX()-p1.getX(), 2)+Math.pow(p2.getY()-p1.getY(), 2)); |
|
57 |
return x; |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public double distance(double x1, double y1, double x2, double y2) { |
|
62 |
double x = Math.sqrt( Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2)); |
|
63 |
return x; |
|
64 |
} |
|
65 |
|
|
66 |
@Override |
|
67 |
public Point2D getCenter(double ax, double ay, double bx, double by, double cx, double cy) { |
|
68 |
|
|
69 |
Point2D midPointAC = getMidPoint(ax, ay, cx, cy); |
|
70 |
EuclideanLine2D lineAC = createLine2D(ax, ay, cx, cy); |
|
71 |
EuclideanLine2D bisectorAC = lineAC.getPerpendicular(midPointAC.getX(), midPointAC.getY()); |
|
72 |
|
|
73 |
Point2D midPointBC = getMidPoint(bx, by, cx, cy); |
|
74 |
EuclideanLine2D lineBC = createLine2D(bx, by, cx, cy); |
|
75 |
EuclideanLine2D bisectorBC = lineBC.getPerpendicular(midPointBC.getX(), midPointBC.getY()); |
|
76 |
|
|
77 |
Point2D intersection = bisectorAC.getIntersection(bisectorBC); |
|
78 |
|
|
79 |
return new Point2D.Double(intersection.getX(), intersection.getY()); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public Point2D getCenter(Point2D a, Point2D b, Point2D c) { |
|
84 |
return this.getCenter(a.getX(), a.getY(), b.getX(), b.getY(), c.getX(), c.getY()); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public Point2D getMidPoint(Point2D a, Point2D b) { |
|
89 |
return getMidPoint(a.getX(), a.getY(), b.getX(), b.getY()); |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public Point2D getMidPoint(double ax, double ay, double bx, double by) { |
|
94 |
double x = (ax + bx) / 2; |
|
95 |
double y = (ay + by) / 2; |
|
96 |
return new Point2D.Double(x, y); |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public double getCoefDirection(double centerx, double centery, double point1x, double point1y, double point2x, double point2y) { |
|
101 |
return Math.signum((point1x - centerx) * (point2y - centery) - (point1y - centery) * (point2x - centerx)); |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public double getCoefDirection(Point2D center, Point2D point1, Point2D point2) { |
|
106 |
return this.getCoefDirection(center.getX(), center.getY(), point1.getX(), point1.getY(), point2.getX(), point2.getY()); |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public double calculateAngle(double vertexx, double vertexy, double p1x, double p1y, double p2x, double p2y){ |
|
111 |
double angle = Math.atan2( |
|
112 |
p2y - vertexy, |
|
113 |
p2x - vertexx |
|
114 |
) - Math.atan2( |
|
115 |
p1y - vertexy, |
|
116 |
p1x - vertexx |
|
117 |
); |
|
118 |
if (angle<0){ |
|
119 |
return 2*Math.PI+angle; |
|
120 |
} |
|
121 |
return angle; |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public double calculateAngle(double vertexx, double vertexy, double px, double py){ |
|
126 |
|
|
127 |
double angle = Math.atan2( |
|
128 |
py - vertexy, |
|
129 |
px - vertexx |
|
130 |
) - Math.atan2( |
|
131 |
0, |
|
132 |
1 |
|
133 |
); |
|
134 |
if (angle<0){ |
|
135 |
return 2*Math.PI+angle; |
|
136 |
} |
|
137 |
return angle; |
|
138 |
} |
|
139 |
|
|
140 |
@Override |
|
141 |
public boolean areThreePointsInLine(double ax, double ay, double bx, double by, double cx, double cy){ |
|
142 |
if( (ax==bx && ay==by) || (ax==cx && ay==cy) || (bx==cx && by==cy) ) { |
|
143 |
return true; |
|
144 |
} |
|
145 |
return Math.abs((bx-ax)/(cx-bx)-(by-ay)/(cy-by)) < 2 * Double.MIN_VALUE; |
|
146 |
} |
|
147 |
|
|
148 |
@Override |
|
149 |
public Point2D getPointAtDistance(double x1, double y1, double distance, double angle, String unit) { |
|
150 |
if ( "s".equals(unit) ){ |
|
151 |
angle = Math.toRadians(angle); |
|
152 |
} else if ( "g".equals(unit) ) { |
|
153 |
angle = Math.PI * angle / 200; |
|
154 |
} |
|
155 |
double deltaX = distance * Math.cos(angle); |
|
156 |
double deltaY = distance * Math.sin(angle); |
|
157 |
|
|
158 |
Point2D r = new Point2D.Double(x1+deltaX, y1+deltaY); |
|
159 |
return r; |
|
160 |
} |
|
161 |
|
|
162 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/euclidean/EuclideanLine2DImpl.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.euclidean; |
|
7 |
|
|
8 |
import java.awt.geom.Point2D; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author fdiaz |
|
13 |
*/ |
|
14 |
public class EuclideanLine2DImpl implements EuclideanLine2D { |
|
15 |
|
|
16 |
double coefA; |
|
17 |
double coefB; |
|
18 |
double coefC; |
|
19 |
double m; |
|
20 |
double b; |
|
21 |
|
|
22 |
public EuclideanLine2DImpl(double coefA, double coefB, double coefC) { |
|
23 |
|
|
24 |
this.coefA = coefA; |
|
25 |
this.coefB = coefB; |
|
26 |
this.coefC = coefC; |
|
27 |
|
|
28 |
reduceCoefs(); |
|
29 |
|
|
30 |
this.m = -coefA/coefB; |
|
31 |
if(this.m == -0.0){ |
|
32 |
this.m = 0.0; |
|
33 |
} |
|
34 |
this.b = -coefC / coefB; |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* If m parameter m is infinite, preferably use the constructor EuclideanLine2DImpl(double coefA, double coefB, double coefC) |
|
40 |
* |
|
41 |
* @param m |
|
42 |
* @param b |
|
43 |
*/ |
|
44 |
|
|
45 |
public EuclideanLine2DImpl(double m, double b) { |
|
46 |
|
|
47 |
this.m = m; |
|
48 |
this.b = b; |
|
49 |
|
|
50 |
this.coefA = m; |
|
51 |
this.coefB = -1.0; |
|
52 |
this.coefC = b; |
|
53 |
|
|
54 |
reduceCoefs(); |
|
55 |
|
|
56 |
} |
|
57 |
|
|
58 |
public EuclideanLine2DImpl(double x0, double y0, double x1, double y1) { |
|
59 |
this(y1 - y0, -(x1 - x0), -(y1 - y0) * x0 + (x1 - x0) * y0); |
|
60 |
} |
|
61 |
|
|
62 |
public EuclideanLine2DImpl(Point2D p0, Point2D p1) { |
|
63 |
this(p0.getX(), p0.getY(), p1.getX(), p1.getY()); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
private void reduceCoefs() { |
|
68 |
if(coefA != 0 && Double.isFinite(coefA)){ |
|
69 |
coefC = coefC/coefA; |
|
70 |
coefB = coefB/coefA; |
|
71 |
coefA = 1.0; |
|
72 |
} else if(coefB != 0 && Double.isFinite(coefB)){ |
|
73 |
coefA = coefA/coefB; // for change the sign of coefA when coefA is infinite && coefB = -1; |
|
74 |
coefC = coefC/coefB; |
|
75 |
coefB = 1.0; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public double getA() { |
|
81 |
return coefA; |
|
82 |
} |
|
83 |
|
|
84 |
@Override |
|
85 |
public double getB() { |
|
86 |
return coefB; |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public double getC() { |
|
91 |
return coefC; |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public double getSlope() { |
|
96 |
return m; |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public double getYIntercept() { |
|
101 |
return b; |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public double getY(double x) { |
|
106 |
return (-coefA*x-coefC)/coefB; |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public double getX(double y) { |
|
111 |
return (-coefB*y-coefC)/coefA; |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public double getAngle() { |
|
116 |
return Math.atan(m); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public double getDegreesAngle() { |
|
121 |
return Math.toDegrees(getAngle()); |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public double getAngle(EuclideanLine2D line) { |
|
126 |
double m1 = line.getSlope(); |
|
127 |
return Math.atan(Math.abs((m1-m)/(1+m1*m))); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public double getAngle(EuclideanLine2D line, Point2D quadrant) { |
|
132 |
EuclideanLine2D[] bisectors = this.getBisectors(line); |
|
133 |
if(bisectors[0].getDistance(quadrant)<=bisectors[1].getDistance(quadrant)){ |
|
134 |
return 2*this.getAngle(bisectors[0]); |
|
135 |
} else { |
|
136 |
return 2*this.getAngle(bisectors[1]); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
@Override |
|
141 |
public double getDegreesAngle(EuclideanLine2D line) { |
|
142 |
return Math.toDegrees(getAngle(line)); |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public double getDegreesAngle(EuclideanLine2D line, Point2D quadrant) { |
|
147 |
return Math.toDegrees(getAngle(line, quadrant)); |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public double getDistance(double pointX, double pointY) { |
|
152 |
// d(P,r)=|A*px+B*py+C|/SQRT(A?+B?) |
|
153 |
|
|
154 |
double den = Math.abs(Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2))); |
|
155 |
return Math.abs((coefA * pointX + coefB * pointY + coefC)) / den; |
|
156 |
} |
|
157 |
|
|
158 |
@Override |
|
159 |
public double getDistance(Point2D point) { |
|
160 |
return getDistance(point.getX(), point.getY()); |
|
161 |
} |
|
162 |
|
|
163 |
@Override |
|
164 |
public double getDistance(EuclideanLine2D line) { |
|
165 |
|
|
166 |
if (isParallel(line)) { //Parallel lines |
|
167 |
Double num = Math.abs(line.getC() - coefC); |
|
168 |
if (Double.isInfinite(m)) { //Vertical lines |
|
169 |
return num; |
|
170 |
} |
|
171 |
Double den = Math.sqrt(Math.pow(coefA, 2) + Math.pow(coefB, 2)); |
|
172 |
return num / den; |
|
173 |
} |
|
174 |
return 0d; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public boolean isParallel(EuclideanLine2D line) { |
|
179 |
|
|
180 |
double m1 = line.getSlope(); |
|
181 |
return (m == m1 || (Double.isInfinite(m) && Double.isInfinite(m1))); |
|
182 |
} |
|
183 |
|
|
184 |
@Override |
|
185 |
public boolean isPerpendicularl(EuclideanLine2D line) { |
|
186 |
double m1 = line.getSlope(); |
|
187 |
return (m * m1 == -1 || (Double.isInfinite(m) && m1 == 0.0) || (m==0 && Double.isInfinite(m1))); |
|
188 |
|
|
189 |
// return (getA()*line.getA()+getB()*line.getB() == 0); |
|
190 |
} |
|
191 |
|
|
192 |
@Override |
|
193 |
public Point2D getIntersection(EuclideanLine2D line) { |
|
194 |
//Using Cramer's rule |
|
195 |
double a1 = coefA; |
|
196 |
double b1 = coefB; |
|
197 |
double c1 = coefC; |
|
198 |
|
|
199 |
Double a2 = line.getA(); |
|
200 |
Double b2 = line.getB(); |
|
201 |
Double c2 = line.getC(); |
|
202 |
|
|
203 |
Double det = a1 * b2 - a2 * b1; |
|
204 |
Double detX = -c1 * b2 + c2 * b1; |
|
205 |
Double detY = -a1 * c2 + a2 * c1; |
|
206 |
|
|
207 |
Double x = detX / det; |
|
208 |
Double y = detY / det; |
|
209 |
|
|
210 |
return new Point2D.Double(x, y); |
|
211 |
|
|
212 |
//Using Apache commons math library, need import the library |
|
213 |
// RealMatrix coefficients = |
|
214 |
// new Array2DRowRealMatrix(new double[][] { { coefA, coefB }, { line.getA(), line.getB() } }, false); |
|
215 |
// DecompositionSolver solver = new LUDecomposition(coefficients).getSolver(); |
|
216 |
// RealVector constants = new ArrayRealVector(new double[] { -coefC, -line.getC() }, false); |
|
217 |
// RealVector solution = solver.solve(constants); |
|
218 |
// return new Point2D.Double(solution.getEntry(0), solution.getEntry(1)); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
222 |
@Override |
|
223 |
public EuclideanLine2D getPerpendicular(double pointX, double pointY) { |
|
224 |
if(Math.abs(m)==0.0){ |
|
225 |
return new EuclideanLine2DImpl(1, 0, -pointX); |
|
226 |
} |
|
227 |
if(Double.isInfinite(m)){ |
|
228 |
return new EuclideanLine2DImpl(0, 1, -pointY); |
|
229 |
} |
|
230 |
// perpendicular slope (m) |
|
231 |
Double m1 = -1 / m; |
|
232 |
|
|
233 |
// perpendicular y-intercept (b) |
|
234 |
Double b1 = pointY - (m1 * pointX); |
|
235 |
|
|
236 |
return new EuclideanLine2DImpl(m1, b1); |
|
237 |
} |
|
238 |
|
|
239 |
@Override |
|
240 |
public EuclideanLine2D getPerpendicular(Point2D point) { |
|
241 |
return getPerpendicular(point.getX(), point.getY()); |
|
242 |
} |
|
243 |
|
|
244 |
@Override |
|
245 |
public EuclideanLine2D getParallel(double pointX, double pointY) { |
|
246 |
if(Math.abs(m)==0.0){ |
|
247 |
return new EuclideanLine2DImpl(0, 1, -pointY); |
|
248 |
} |
|
249 |
if(Double.isInfinite(m)){ |
|
250 |
return new EuclideanLine2DImpl(1, 0, -pointX); |
|
251 |
} |
|
252 |
// parallel slope (m) |
|
253 |
Double m1 = m; |
|
254 |
|
|
255 |
// parallel y-intercept (b) |
|
256 |
Double b1 = pointY - (m1 * pointX); |
|
257 |
|
|
258 |
return new EuclideanLine2DImpl(m1, b1); |
|
259 |
} |
|
260 |
|
|
261 |
@Override |
|
262 |
public EuclideanLine2D getParallel(Point2D point) { |
|
263 |
return getParallel(point.getX(), point.getY()); |
|
264 |
} |
|
265 |
|
|
266 |
|
|
267 |
@Override |
|
268 |
public Point2D getNearestPoint(double pointX, double pointY) { |
|
269 |
double x; |
|
270 |
double y; |
|
271 |
|
|
272 |
if (Double.isInfinite(m)) { |
|
273 |
y = pointY; |
|
274 |
x = (-coefB * y - coefC) / coefA; |
|
275 |
} else if (Math.abs(m) == 0) { |
|
276 |
x = pointX; |
|
277 |
y = b; |
|
278 |
} else { |
|
279 |
|
|
280 |
EuclideanLine2D perp = getPerpendicular(pointX, pointY); |
|
281 |
Double m1 = perp.getSlope(); |
|
282 |
Double b1 = perp.getYIntercept(); |
|
283 |
|
|
284 |
x = (b1 - b) / (m - m1); |
|
285 |
y = m * x + b; |
|
286 |
} |
|
287 |
|
|
288 |
return new Point2D.Double(x, y); |
|
289 |
} |
|
290 |
|
|
291 |
@Override |
|
292 |
public Point2D getNearestPoint(Point2D point) { |
|
293 |
return getNearestPoint(point.getX(), point.getY()); |
|
294 |
} |
|
295 |
|
|
296 |
@Override |
|
297 |
public EuclideanLine2D[] getBisectors(EuclideanLine2D line) { |
|
298 |
/* |
|
299 |
r) A1x + B1y + C1 = 0 |
|
300 |
s) A2x + B2y + C2 = 0 |
|
301 |
|
|
302 |
|A1x+B1y+C1|/SQRT(A1?+B1?)= |A2x+B2y+C2|/SQRT(A2?+B2?) |
|
303 |
*/ |
|
304 |
|
|
305 |
Double a1 = coefA; |
|
306 |
Double b1 = coefB; |
|
307 |
Double c1 = coefC; |
|
308 |
|
|
309 |
Double a2 = line.getA(); |
|
310 |
Double b2 = line.getB(); |
|
311 |
Double c2 = line.getC(); |
|
312 |
|
|
313 |
Double den1 = Math.sqrt(Math.pow(a1,2) + Math.pow(b1,2)); |
|
314 |
Double den2 = Math.sqrt(Math.pow(a2,2) + Math.pow(b2,2)); |
|
315 |
|
|
316 |
EuclideanLine2D[] result = new EuclideanLine2DImpl[2]; |
|
317 |
result[0] = new EuclideanLine2DImpl( |
|
318 |
den2 * a1 - den1 * a2, |
|
319 |
den2 * b1 - den1 * b2, |
|
320 |
den2 * c1 - den1 * c2 |
|
321 |
); |
|
322 |
result[1] = new EuclideanLine2DImpl( |
|
323 |
den2 * a1 + den1 * a2, |
|
324 |
den2 * b1 + den1 * b2, |
|
325 |
den2 * c1 + den1 * c2 |
|
326 |
); |
|
327 |
|
|
328 |
return result; |
|
329 |
|
|
330 |
} |
|
331 |
|
|
332 |
@Override |
|
333 |
public Point2D getSymmetricalPoint(Point2D point) { |
|
334 |
Point2D nearestPoint = getNearestPoint(point); |
|
335 |
Point2D.Double symmetricalPoint = new Point2D.Double( |
|
336 |
2*nearestPoint.getX()-point.getX(), |
|
337 |
2*nearestPoint.getY()-point.getY() |
|
338 |
); |
|
339 |
return symmetricalPoint; |
|
340 |
} |
|
341 |
|
|
342 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonManagerImpl.java | ||
---|---|---|
1 |
package org.gvsig.json; |
|
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.StringWriter; |
|
5 |
import java.math.BigDecimal; |
|
6 |
import java.util.Arrays; |
|
7 |
import java.util.Collections; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.Iterator; |
|
10 |
import java.util.Map; |
|
11 |
import java.util.Objects; |
|
12 |
import javax.json.JsonArray; |
|
13 |
import javax.json.JsonNumber; |
|
14 |
import javax.json.JsonObject; |
|
15 |
import javax.json.JsonReader; |
|
16 |
import javax.json.JsonString; |
|
17 |
import javax.json.JsonStructure; |
|
18 |
import javax.json.JsonValue; |
|
19 |
import javax.json.JsonWriter; |
|
20 |
import javax.json.JsonWriterFactory; |
|
21 |
import javax.json.stream.JsonGenerator; |
|
22 |
import org.apache.commons.codec.digest.DigestUtils; |
|
23 |
import static org.gvsig.json.JsonManager.ATTRIBUTE_NAME_CLASS; |
|
24 |
import org.gvsig.json.serializers.DataTypeSerializer; |
|
25 |
import org.gvsig.json.serializers.DefaultObjectSerializer; |
|
26 |
import org.gvsig.json.serializers.DynObjectSerializer; |
|
27 |
import org.gvsig.json.serializers.DynObjectValueItemSerializer; |
|
28 |
import org.gvsig.json.serializers.FileSerializer; |
|
29 |
import org.gvsig.json.serializers.LocaleSerializer; |
|
30 |
import org.gvsig.json.serializers.TagsSerializer; |
|
31 |
import org.gvsig.json.serializers.UriSerializer; |
|
32 |
import org.gvsig.json.serializers.UrlSerializer; |
|
33 |
import org.gvsig.tools.dynobject.DynClass; |
|
34 |
import org.gvsig.tools.dynobject.DynField; |
|
35 |
import org.gvsig.tools.dynobject.DynObject; |
|
36 |
import org.gvsig.tools.util.GetItemWithSizeAndIterator; |
|
37 |
import org.gvsig.tools.util.IsApplicable; |
|
38 |
|
|
39 |
/** |
|
40 |
* |
|
41 |
* @author gvSIG Team |
|
42 |
*/ |
|
43 |
@SuppressWarnings("UseSpecificCatch") |
|
44 |
public class JsonManagerImpl implements JsonManager { |
|
45 |
|
|
46 |
private final Map<String,JsonSerializer> serializers1; |
|
47 |
private final Map<String,IsApplicable> serializers2; |
|
48 |
|
|
49 |
private final DynObjectSerializer dynObjectSerializer; |
|
50 |
|
|
51 |
public JsonManagerImpl() { |
|
52 |
this.dynObjectSerializer = new DynObjectSerializer(); |
|
53 |
this.serializers1 = new HashMap<>(); |
|
54 |
this.serializers2 = new HashMap<>(); |
|
55 |
this.registerDefaultSerializers(); |
|
56 |
} |
|
57 |
|
|
58 |
private void registerDefaultSerializers() { |
|
59 |
this.registerSerializer(new FileSerializer()); |
|
60 |
this.registerSerializer(new UrlSerializer()); |
|
61 |
this.registerSerializer(new UriSerializer()); |
|
62 |
this.registerSerializer(new LocaleSerializer()); |
|
63 |
this.registerSerializer(new TagsSerializer()); |
|
64 |
this.registerSerializer(new DynObjectValueItemSerializer()); |
|
65 |
this.registerSerializer(new DataTypeSerializer()); |
|
66 |
// this.registerSerializer(this.dynObjectSerializer); |
|
67 |
} |
|
68 |
|
|
69 |
public static String createSerializerKey(Class aClass) { |
|
70 |
String key = aClass.getName(); |
|
71 |
// String key = "0-" + DigestUtils.sha256Hex(aClass.getName()); |
|
72 |
return key; |
|
73 |
} |
|
74 |
|
|
75 |
public static String createSerializerKey(String className) { |
|
76 |
String key = className; |
|
77 |
// String key = "0-"+ DigestUtils.sha256Hex(className); |
|
78 |
return key; |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public void registerSerializer(Class<?extends SupportFromJson> theClass) { |
|
83 |
if (theClass == null) { |
|
84 |
return; |
|
85 |
} |
|
86 |
if (!SupportFromJson.class.isAssignableFrom(theClass)) { |
|
87 |
throw new IllegalArgumentException("the class '" + theClass.getName() + "'must implement SupportFromJson"); |
|
88 |
} |
|
89 |
String key = createSerializerKey(theClass); |
|
90 |
this.serializers1.put(key, new DefaultObjectSerializer(theClass)); |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public void registerSerializer(JsonSerializer serializer) { |
|
95 |
String key = createSerializerKey(serializer.getObjectClass()); |
|
96 |
this.serializers1.put(key, serializer); |
|
97 |
if( serializer instanceof IsApplicable ) { |
|
98 |
this.serializers2.put(key, (IsApplicable) serializer); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public JsonSerializer getSerializer(Object value) { |
|
104 |
String key = createSerializerKey(value.getClass()); |
|
105 |
JsonSerializer serializer1 = this.serializers1.get(key); |
|
106 |
if( serializer1!=null ) { |
|
107 |
return serializer1; |
|
108 |
} |
|
109 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
|
110 |
if( serializer2.isApplicable(value) ) { |
|
111 |
return (JsonSerializer) serializer2; |
|
112 |
} |
|
113 |
} |
|
114 |
if( value instanceof DynObject ) { |
|
115 |
return this.dynObjectSerializer; |
|
116 |
} |
|
117 |
return null; |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public JsonSerializer getSerializer(JsonObject json) { |
|
122 |
String className = json.getString(ATTRIBUTE_NAME_CLASS, null); |
|
123 |
String key = createSerializerKey(className); |
|
124 |
JsonSerializer serializer1 = this.serializers1.get(key); |
|
125 |
if( serializer1!=null ) { |
|
126 |
return serializer1; |
|
127 |
} |
|
128 |
for (IsApplicable serializer2 : this.serializers2.values()) { |
|
129 |
if( serializer2.isApplicable(json) ) { |
|
130 |
return (JsonSerializer) serializer2; |
|
131 |
} |
|
132 |
} |
|
133 |
if( this.dynObjectSerializer.isApplicable(json) ) { |
|
134 |
return this.dynObjectSerializer; |
|
135 |
} |
|
136 |
return null; |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public JsonObjectBuilder createObjectBuilder() { |
|
141 |
return new JsonObjectBuilderImpl(); |
|
142 |
} |
|
143 |
|
|
144 |
@Override |
|
145 |
public JsonArrayBuilder createArrayBuilder() { |
|
146 |
return new JsonArrayBuilderImpl(); |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public JsonObject createObject(String json) { |
|
151 |
try { |
|
152 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
|
153 |
JsonReader reader = javax.json.Json.createReader(data); |
|
154 |
JsonObject x = reader.readObject(); |
|
155 |
return new org.gvsig.json.JsonObjectImpl(x); |
|
156 |
} catch (Exception ex) { |
|
157 |
return null; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public JsonArray createArray(String json) { |
|
163 |
try { |
|
164 |
ByteArrayInputStream data = new ByteArrayInputStream(json.getBytes()); |
|
165 |
JsonReader reader = javax.json.Json.createReader(data); |
|
166 |
JsonArray x = reader.readArray(); |
|
167 |
return new org.gvsig.json.JsonArrayImpl(x); |
|
168 |
} catch (Exception ex) { |
|
169 |
return null; |
|
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
private String toString(JsonStructure obj) { |
|
174 |
StringWriter sw = new StringWriter(); |
|
175 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
|
176 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
|
177 |
); |
|
178 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { |
|
179 |
jsonWriter.write(obj); |
|
180 |
} |
|
181 |
String s = sw.toString(); |
|
182 |
return s; |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public String toString(JsonValue value) { |
|
187 |
if( value == null ) { |
|
188 |
return Objects.toString(null); |
|
189 |
} |
|
190 |
switch (value.getValueType()) { |
|
191 |
case ARRAY: |
|
192 |
case OBJECT: |
|
193 |
return this.toString((JsonStructure)value); |
|
194 |
case FALSE: |
|
195 |
case NUMBER: |
|
196 |
case STRING: |
|
197 |
case TRUE: |
|
198 |
case NULL: |
|
199 |
default: |
|
200 |
return value.toString(); |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
|
|
205 |
@Override |
|
206 |
public Object toObject(JsonObject json, String name) { |
|
207 |
JsonValue value = json.get(name); |
|
208 |
return toObject(value); |
|
209 |
} |
|
210 |
|
|
211 |
@Override |
|
212 |
public Object toObject(JsonArray json, int index) { |
|
213 |
JsonValue value = json.get(index); |
|
214 |
return toObject(value); |
|
215 |
} |
|
216 |
|
|
217 |
private Object createObject(JsonObject json, Object defaultValue) { |
|
218 |
if (json == null) { |
|
219 |
return defaultValue; |
|
220 |
} |
|
221 |
JsonSerializer serializer = this.getSerializer(json); |
|
222 |
if (serializer == null) { |
|
223 |
return defaultValue; |
|
224 |
} |
|
225 |
Object obj = serializer.toObject(json); |
|
226 |
if (obj == null) { |
|
227 |
return defaultValue; |
|
228 |
} |
|
229 |
return obj; |
|
230 |
} |
|
231 |
|
|
232 |
@Override |
|
233 |
public Iterable<Object> toIterable(final JsonArray jsonArray) { |
|
234 |
@SuppressWarnings("Convert2Lambda") |
|
235 |
Iterable<Object> iterable = new Iterable<Object>() { |
|
236 |
@Override |
|
237 |
public Iterator<Object> iterator() { |
|
238 |
Iterator<Object> it = new Iterator<Object>() { |
|
239 |
int current= 0; |
|
240 |
@Override |
|
241 |
public boolean hasNext() { |
|
242 |
return current<jsonArray.size(); |
|
243 |
} |
|
244 |
|
|
245 |
@Override |
|
246 |
public Object next() { |
|
247 |
return toObject(jsonArray.get(current++)); |
|
248 |
} |
|
249 |
}; |
|
250 |
return it; |
|
251 |
} |
|
252 |
}; |
|
253 |
return iterable; |
|
254 |
} |
|
255 |
|
|
256 |
@Override |
|
257 |
public GetItemWithSizeAndIterator<Object> toItems(final JsonArray jsonArray) { |
|
258 |
GetItemWithSizeAndIterator<Object> items = new GetItemWithSizeAndIterator<Object>() { |
|
259 |
|
|
260 |
@Override |
|
261 |
public int size() { |
|
262 |
return jsonArray.size(); |
|
263 |
} |
|
264 |
|
|
265 |
@Override |
|
266 |
public Object get(int position) { |
|
267 |
return toObject(jsonArray.get(position)); |
|
268 |
} |
|
269 |
|
|
270 |
@Override |
|
271 |
public Iterator<Object> iterator() { |
|
272 |
Iterator<Object> it = new Iterator<Object>() { |
|
273 |
int current= 0; |
|
274 |
@Override |
|
275 |
public boolean hasNext() { |
|
276 |
return current<jsonArray.size(); |
|
277 |
} |
|
278 |
|
|
279 |
@Override |
|
280 |
public Object next() { |
|
281 |
return toObject(jsonArray.get(current++)); |
|
282 |
} |
|
283 |
}; |
|
284 |
return it; |
|
285 |
}; |
|
286 |
}; |
|
287 |
return items; |
|
288 |
} |
|
289 |
|
|
290 |
@Override |
|
291 |
public Object[] toArray(JsonArray jsonArray, Object[] a) { |
|
292 |
if( jsonArray==null ) { |
|
293 |
return null; |
|
294 |
} |
|
295 |
int sz = jsonArray.size(); |
|
296 |
if( a==null ) { |
|
297 |
a = new Object[sz]; |
|
298 |
} else if (a.length != sz) { |
|
299 |
a = Arrays.copyOf(a, sz); |
|
300 |
} |
|
301 |
for (int i = 0; i < sz; i++) { |
|
302 |
JsonValue value = jsonArray.get(i); |
|
303 |
a[i] = this.toObject(value); |
|
304 |
} |
|
305 |
return a; |
|
306 |
} |
|
307 |
|
|
308 |
@Override |
|
309 |
public Object toObject(JsonValue value) { |
|
310 |
return this.toObjectOrDefault(value, null); |
|
311 |
} |
|
312 |
|
|
313 |
@Override |
|
314 |
public Object toObjectOrDefault(JsonValue value, Object defaultValue) { |
|
315 |
if( value == null ) { |
|
316 |
return defaultValue; |
|
317 |
} |
|
318 |
switch (value.getValueType()) { |
|
319 |
case ARRAY: |
|
320 |
return this.toArray((JsonArray) value, new Object[0]); |
|
321 |
case FALSE: |
|
322 |
return false; |
|
323 |
case NUMBER: |
|
324 |
JsonNumber n = (JsonNumber) value; |
|
325 |
if (n.isIntegral()) { |
|
326 |
int nint = n.intValue(); |
|
327 |
long nlong = n.longValue(); |
|
328 |
if (nint == nlong) { |
|
329 |
return nint; |
|
330 |
} |
|
331 |
if (n.bigDecimalValue().compareTo(new BigDecimal(nlong)) == 0) { |
|
332 |
return nlong; |
|
333 |
} |
|
334 |
return n.bigIntegerValue(); |
|
335 |
} else { |
|
336 |
double ndouble = n.doubleValue(); |
|
337 |
if (n.bigDecimalValue().compareTo(new BigDecimal(ndouble)) == 0) { |
|
338 |
return ndouble; |
|
339 |
} |
|
340 |
return n.bigDecimalValue(); |
|
341 |
} |
|
342 |
|
|
343 |
case OBJECT: |
|
344 |
return this.createObject((JsonObject) value, defaultValue); |
|
345 |
|
|
346 |
case STRING: |
|
347 |
return ((JsonString) value).getString(); |
|
348 |
case TRUE: |
|
349 |
return true; |
|
350 |
case NULL: |
|
351 |
return null; |
|
352 |
default: |
|
353 |
return defaultValue; |
|
354 |
} |
|
355 |
} |
|
356 |
|
|
357 |
@Override |
|
358 |
public DynObject addAll(DynObject target, JsonObject json) { |
|
359 |
if( json == null || target==null ) { |
|
360 |
return target; |
|
361 |
} |
|
362 |
DynClass dynClass = target.getDynClass(); |
|
363 |
String name = null; |
|
364 |
try { |
|
365 |
for (DynField dynField : dynClass.getDynFields()) { |
|
366 |
name = dynField.getName(); |
|
367 |
if( json.containsKey(name) ) { |
|
368 |
target.setDynValue(name, this.toObject(json.get(name))); |
|
369 |
} |
|
370 |
} |
|
371 |
} catch (Exception ex) { |
|
372 |
throw new RuntimeException("Can't copy values from json to DynObject (class "+dynClass.getFullName()+", field "+name+").", ex); |
|
373 |
} |
|
374 |
return target; |
|
375 |
} |
|
376 |
|
|
377 |
@Override |
|
378 |
public Map toMap(JsonObject json) { |
|
379 |
if( json == null ) { |
|
380 |
return null; |
|
381 |
} |
|
382 |
Map map = new HashMap(); |
|
383 |
for (String name : json.keySet()) { |
|
384 |
map.put(name, this.toObject(json.get(name))); |
|
385 |
} |
|
386 |
return map; |
|
387 |
} |
|
388 |
|
|
389 |
public static JsonValue wrap(JsonValue value) { |
|
390 |
if( value == null ) { |
|
391 |
return null; |
|
392 |
} else if( value == JsonValue.NULL ) { |
|
393 |
return null; |
|
394 |
} else if( value instanceof JsonObject && |
|
395 |
!(value instanceof org.gvsig.json.JsonObjectImpl)) { |
|
396 |
value = new org.gvsig.json.JsonObjectImpl((JsonObject) value); |
|
397 |
} else if( value instanceof JsonArray && |
|
398 |
!(value instanceof org.gvsig.json.JsonArrayImpl)) { |
|
399 |
value = new org.gvsig.json.JsonArrayImpl((JsonArray) value); |
|
400 |
} |
|
401 |
return value; |
|
402 |
} |
|
403 |
|
|
404 |
@Override |
|
405 |
public JsonObjectBuilder createObjectBuilder(String json) { |
|
406 |
JsonObject obj = this.createObject(json); |
|
407 |
return this.createObjectBuilder(obj); |
|
408 |
} |
|
409 |
|
|
410 |
@Override |
|
411 |
public JsonObjectBuilder createObjectBuilder(JsonObject obj) { |
|
412 |
JsonObjectBuilder builder = this.createObjectBuilder(); |
|
413 |
for (Map.Entry<String, JsonValue> entry : obj.entrySet()) { |
|
414 |
builder.add(entry.getKey(), entry.getValue()); |
|
415 |
} |
|
416 |
return builder; |
|
417 |
} |
|
418 |
|
|
419 |
@Override |
|
420 |
public JsonPathContext createJSonPathContext(JsonStructure jsonObject) { |
|
421 |
return new JsonPathContextImpl(this, jsonObject); |
|
422 |
} |
|
423 |
|
|
424 |
@Override |
|
425 |
public JsonPathContext createJSonPathContext(String jsonObject) { |
|
426 |
return new JsonPathContextImpl(this, jsonObject); |
|
427 |
} |
|
428 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonObjectBuilderImpl.java | ||
---|---|---|
1 |
package org.gvsig.json; |
|
2 |
|
|
3 |
import java.io.StringWriter; |
|
4 |
import java.math.BigDecimal; |
|
5 |
import java.math.BigInteger; |
|
6 |
import java.util.Collections; |
|
7 |
import java.util.Iterator; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import java.util.Set; |
|
11 |
import javax.json.JsonObject; |
|
12 |
import javax.json.JsonWriter; |
|
13 |
import javax.json.JsonWriterFactory; |
|
14 |
import javax.json.stream.JsonGenerator; |
|
15 |
import static org.gvsig.json.JsonManager.ATTRIBUTE_NAME_CLASS; |
|
16 |
import org.gvsig.json.JsonManager.JsonSerializer; |
|
17 |
import static org.gvsig.json.JsonManagerImpl.createSerializerKey; |
|
18 |
import org.gvsig.tools.dynobject.DynClass; |
|
19 |
import org.gvsig.tools.dynobject.DynField; |
|
20 |
import org.gvsig.tools.dynobject.DynObject; |
|
21 |
import org.gvsig.tools.util.GetItemByKeyWithGetKeys; |
|
22 |
import org.gvsig.tools.util.GetItemWithSize; |
|
23 |
|
|
24 |
/** |
|
25 |
* |
|
26 |
* @author gvSIG Team |
|
27 |
*/ |
|
28 |
public class JsonObjectBuilderImpl implements JsonObjectBuilder { |
|
29 |
|
|
30 |
private final javax.json.JsonObjectBuilder builder; |
|
31 |
private final JsonManager manager; |
|
32 |
|
|
33 |
public JsonObjectBuilderImpl() { |
|
34 |
this(Json.getManager()); |
|
35 |
} |
|
36 |
|
|
37 |
public JsonObjectBuilderImpl(JsonManager manager) { |
|
38 |
this.manager = manager; |
|
39 |
this.builder = javax.json.Json.createObjectBuilder(); |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public JsonObjectBuilder add(String name, SupportToJson value) { |
|
44 |
if (value == null) { |
|
45 |
this.builder.addNull(name); |
|
46 |
return this; |
|
47 |
} |
|
48 |
this.builder.add(name, value.toJsonBuilder()); |
|
49 |
return this; |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public JsonObjectBuilder add(String name, Object value) { |
|
54 |
if (value == null) { |
|
55 |
this.builder.addNull(name); |
|
56 |
} else if (value instanceof SupportToJson) { |
|
57 |
this.add(name, ((SupportToJson)value).toJsonBuilder()); |
|
58 |
} else if (value instanceof String) { |
|
59 |
this.builder.add(name, (String) value); |
|
60 |
} else if (value instanceof Integer) { |
|
61 |
this.builder.add(name, (int) value); |
|
62 |
} else if (value instanceof Long) { |
|
63 |
this.builder.add(name, (long) value); |
|
64 |
} else if (value instanceof BigInteger) { |
|
65 |
this.builder.add(name, (BigInteger) value); |
|
66 |
} else if (value instanceof BigDecimal) { |
|
67 |
this.builder.add(name, (BigDecimal) value); |
|
68 |
} else if (value instanceof Double) { |
|
69 |
this.builder.add(name, (double) value); |
|
70 |
} else if (value instanceof Boolean) { |
|
71 |
this.builder.add(name, (boolean) value); |
|
72 |
} else if (value.getClass().isArray()) { |
|
73 |
this.add(name, (Object[]) value); |
|
74 |
} else if (value instanceof Class) { |
|
75 |
this.add(name, (Class) value); |
|
76 |
|
|
77 |
} else { |
|
78 |
JsonSerializer serializer = manager.getSerializer(value); |
|
79 |
if( serializer!=null ) { |
|
80 |
this.add(name, serializer.toJsonBuilder(value)); |
|
81 |
} else if (value instanceof DynObject) { |
|
82 |
this.add(name, (DynObject) value); |
|
83 |
} else if (value instanceof GetItemByKeyWithGetKeys) { |
|
84 |
this.add(name, (GetItemByKeyWithGetKeys<Object,Object>) value); |
|
85 |
} else if (value instanceof List) { |
|
86 |
this.add(name, (List<Object>) value); |
|
87 |
} else if (value instanceof Set) { |
|
88 |
this.add(name, (Set<Object>) value); |
|
89 |
} else if (value instanceof Map) { |
|
90 |
this.add(name, (Map<String,Object>) value); |
|
91 |
} else if (value instanceof Iterable) { |
|
92 |
this.add(name, (Iterable<Object>) value); |
|
93 |
} else if (value instanceof Iterator) { |
|
94 |
this.add(name, (Iterator<Object>) value); |
|
95 |
} else { |
|
96 |
throw new IllegalArgumentException("Can't serialize to Json an object of class '"+value.getClass().getName()+"'."); |
|
97 |
} |
|
98 |
} |
|
99 |
return this; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public JsonObjectBuilder add(String name, Object[] value) { |
|
104 |
if (value == null) { |
|
105 |
this.builder.addNull(name); |
|
106 |
return this; |
|
107 |
} |
|
108 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
109 |
arrayBuilder.addAll(value); |
|
110 |
this.builder.add(name, arrayBuilder); |
|
111 |
return this; |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public JsonObjectBuilder add(String name, Iterable value) { |
|
116 |
if (value == null) { |
|
117 |
this.builder.addNull(name); |
|
118 |
return this; |
|
119 |
} |
|
120 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
121 |
if( serializer!=null ) { |
|
122 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
123 |
} else { |
|
124 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
125 |
arrayBuilder.addAll(value); |
|
126 |
this.builder.add(name, arrayBuilder); |
|
127 |
} |
|
128 |
return this; |
|
129 |
} |
|
130 |
|
|
131 |
@Override |
|
132 |
public JsonObjectBuilder add(String name, Map value) { |
|
133 |
if (value == null) { |
|
134 |
this.builder.addNull(name); |
|
135 |
return this; |
|
136 |
} |
|
137 |
JsonObjectBuilder objBuilder = new JsonObjectBuilderImpl(); |
|
138 |
objBuilder.addAll(value); |
|
139 |
this.builder.add(name, objBuilder); |
|
140 |
return this; |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public JsonObjectBuilder add(String name, GetItemByKeyWithGetKeys value) { |
|
145 |
if (value == null) { |
|
146 |
this.builder.addNull(name); |
|
147 |
return this; |
|
148 |
} |
|
149 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
150 |
if( serializer!=null ) { |
|
151 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
152 |
} else { |
|
153 |
JsonObjectBuilder objBuilder = new JsonObjectBuilderImpl(); |
|
154 |
objBuilder.addAll(value); |
|
155 |
this.builder.add(name, objBuilder); |
|
156 |
} |
|
157 |
return this; |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public JsonObjectBuilder add(String name, GetItemWithSize value) { |
|
162 |
if (value == null) { |
|
163 |
this.builder.addNull(name); |
|
164 |
return this; |
|
165 |
} |
|
166 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
167 |
if( serializer!=null ) { |
|
168 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
169 |
} else { |
|
170 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
171 |
arrayBuilder.addAll(value); |
|
172 |
this.builder.add(name, arrayBuilder); |
|
173 |
} |
|
174 |
return this; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public JsonObjectBuilder add(String name, List value) { |
|
179 |
if (value == null) { |
|
180 |
this.builder.addNull(name); |
|
181 |
return this; |
|
182 |
} |
|
183 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
184 |
if( serializer!=null ) { |
|
185 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
186 |
} else { |
|
187 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
188 |
arrayBuilder.addAll(value); |
|
189 |
this.builder.add(name, arrayBuilder); |
|
190 |
} |
|
191 |
return this; |
|
192 |
} |
|
193 |
|
|
194 |
@Override |
|
195 |
public JsonObjectBuilder add(String name, Iterator value) { |
|
196 |
if (value == null) { |
|
197 |
this.builder.addNull(name); |
|
198 |
return this; |
|
199 |
} |
|
200 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
201 |
if( serializer!=null ) { |
|
202 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
203 |
} else { |
|
204 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
205 |
arrayBuilder.addAll(value); |
|
206 |
this.builder.add(name, arrayBuilder); |
|
207 |
} |
|
208 |
return this; |
|
209 |
} |
|
210 |
|
|
211 |
@Override |
|
212 |
public JsonObjectBuilder add(String name, Set value) { |
|
213 |
if (value == null) { |
|
214 |
this.builder.addNull(name); |
|
215 |
return this; |
|
216 |
} |
|
217 |
JsonSerializer serializer = this.manager.getSerializer(value); |
|
218 |
if( serializer!=null ) { |
|
219 |
this.builder.add(name, serializer.toJsonBuilder(value)); |
|
220 |
} else { |
|
221 |
JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl(); |
|
222 |
arrayBuilder.addAll(value); |
|
223 |
this.builder.add(name, arrayBuilder); |
|
224 |
} |
|
225 |
return this; |
|
226 |
} |
|
227 |
|
|
228 |
@Override |
|
229 |
public JsonObjectBuilder add(String name, javax.json.JsonValue value) { |
|
230 |
if (value == null) { |
|
231 |
this.builder.addNull(name); |
|
232 |
return this; |
|
233 |
} |
|
234 |
this.builder.add(name, value); |
|
235 |
return this; |
|
236 |
} |
|
237 |
|
|
238 |
@Override |
|
239 |
public JsonObjectBuilder add(String name, String value) { |
|
240 |
if (value == null) { |
|
241 |
this.builder.addNull(name); |
|
242 |
return this; |
|
243 |
} |
|
244 |
this.builder.add(name, value); |
|
245 |
return this; |
|
246 |
} |
|
247 |
|
|
248 |
@Override |
|
249 |
public JsonObjectBuilder add(String name, BigInteger value) { |
|
250 |
if (value == null) { |
|
251 |
this.builder.addNull(name); |
|
252 |
return this; |
|
253 |
} |
|
254 |
this.builder.add(name, value); |
|
255 |
return this; |
|
256 |
} |
|
257 |
|
|
258 |
@Override |
|
259 |
public JsonObjectBuilder add(String name, BigDecimal value) { |
|
260 |
if (value == null) { |
|
261 |
this.builder.addNull(name); |
|
262 |
return this; |
|
263 |
} |
|
264 |
this.builder.add(name, value); |
|
265 |
return this; |
|
266 |
} |
|
267 |
|
|
268 |
@Override |
|
269 |
public JsonObjectBuilder add(String name, int value) { |
|
270 |
this.builder.add(name, value); |
|
271 |
return this; |
|
272 |
} |
|
273 |
|
|
274 |
@Override |
|
275 |
public JsonObjectBuilder add(String name, long value) { |
|
276 |
this.builder.add(name, value); |
|
277 |
return this; |
|
278 |
} |
|
279 |
|
|
280 |
@Override |
|
281 |
public JsonObjectBuilder add(String name, double value) { |
|
282 |
this.builder.add(name, value); |
|
283 |
return this; |
|
284 |
} |
|
285 |
|
|
286 |
@Override |
|
287 |
public JsonObjectBuilder add(String name, boolean value) { |
|
288 |
this.builder.add(name, value); |
|
289 |
return this; |
|
290 |
} |
|
291 |
|
|
292 |
@Override |
|
293 |
public JsonObjectBuilder add(String name, javax.json.JsonObjectBuilder value) { |
|
294 |
if (value == null) { |
|
295 |
this.builder.addNull(name); |
|
296 |
return this; |
|
297 |
} |
|
298 |
this.builder.add(name, value); |
|
299 |
return this; |
|
300 |
} |
|
301 |
|
|
302 |
@Override |
|
303 |
public JsonObjectBuilder add(String name, Class value) { |
|
304 |
if (value == null) { |
|
305 |
this.builder.addNull(name); |
|
306 |
return this; |
|
307 |
} |
|
308 |
this.builder.add(name, value.getName()); |
|
309 |
return this; |
|
310 |
} |
|
311 |
|
|
312 |
@Override |
|
313 |
public javax.json.JsonObjectBuilder add(String name, javax.json.JsonArrayBuilder value) { |
|
314 |
if (value == null) { |
|
315 |
this.builder.addNull(name); |
|
316 |
return this; |
|
317 |
} |
|
318 |
this.builder.add(name, value); |
|
319 |
return this; |
|
320 |
} |
|
321 |
|
|
322 |
@Override |
|
323 |
public JsonObjectBuilder add_class(Class value) { |
|
324 |
if (value == null) { |
|
325 |
this.builder.addNull(ATTRIBUTE_NAME_CLASS); |
|
326 |
return this; |
|
327 |
} |
|
328 |
this.builder.add(ATTRIBUTE_NAME_CLASS, createSerializerKey(value)); |
|
329 |
return this; |
|
330 |
} |
|
331 |
|
|
332 |
@Override |
|
333 |
public JsonObjectBuilder add_class(Object value) { |
|
334 |
if( value == null ) { |
|
335 |
this.add_class((Class)null); |
|
336 |
return this; |
|
337 |
} |
|
338 |
this.add_class(value.getClass()); |
|
339 |
return this; |
|
340 |
} |
|
341 |
|
|
342 |
@Override |
|
343 |
public JsonObjectBuilder addAll(GetItemByKeyWithGetKeys values) { |
|
344 |
if (values == null) { |
|
345 |
return this; |
|
346 |
} |
|
347 |
for (Object key : values.getKeys()) { |
|
348 |
if( key == null ) { |
|
349 |
continue; |
|
350 |
} |
|
351 |
this.add((String) key, values.get(key)); |
|
352 |
} |
|
353 |
return this; |
|
354 |
} |
|
355 |
|
|
356 |
@Override |
|
357 |
public JsonObjectBuilder addAll(Map values) { |
|
358 |
if (values == null) { |
|
359 |
return this; |
|
360 |
} |
|
361 |
Set<Map.Entry> entries = values.entrySet(); |
|
362 |
for (Map.Entry entry : entries) { |
|
363 |
if( entry.getKey()==null ) { |
|
364 |
continue; |
|
365 |
} |
|
366 |
this.add((String) entry.getKey(), entry.getValue()); |
|
367 |
} |
|
368 |
return this; |
|
369 |
} |
|
370 |
|
|
371 |
@Override |
|
372 |
public JsonObjectBuilder addAll(JsonObjectBuilder values) { |
|
373 |
return this.addAll(values.build()); |
|
374 |
} |
|
375 |
|
|
376 |
@Override |
|
377 |
public JsonObjectBuilder addAll(JsonObject values) { |
|
378 |
if (values == null) { |
|
379 |
return this; |
|
380 |
} |
|
381 |
for (String name : values.keySet()) { |
|
382 |
if( name==null || name.equals(ATTRIBUTE_NAME_CLASS)) { |
|
383 |
continue; |
|
384 |
} |
|
385 |
this.add(name, values.get(name)); |
|
386 |
} |
|
387 |
return this; |
|
388 |
} |
|
389 |
|
|
390 |
@Override |
|
391 |
public JsonObjectBuilder addAll(DynObject value) { |
|
392 |
DynClass dynClass = value.getDynClass(); |
|
393 |
for (DynField dynField : dynClass.getDynFields()) { |
|
394 |
String name = dynField.getName(); |
|
395 |
this.add(name,value.getDynValue(name)); |
|
396 |
} |
|
397 |
return this; |
|
398 |
} |
|
399 |
|
|
400 |
@Override |
|
401 |
public JsonObjectBuilder addNull(String name) { |
|
402 |
this.builder.addNull(name); |
|
403 |
return this; |
|
404 |
} |
|
405 |
|
|
406 |
@Override |
|
407 |
public javax.json.JsonObject build() { |
|
408 |
return new JsonObjectImpl(this.builder.build()); |
|
409 |
} |
|
410 |
|
|
411 |
@Override |
|
412 |
public String toString() { |
|
413 |
StringWriter sw = new StringWriter(); |
|
414 |
JsonWriterFactory writerFactory = javax.json.Json.createWriterFactory( |
|
415 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
|
416 |
); |
|
417 |
try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { |
|
418 |
jsonWriter.writeObject(this.builder.build()); |
|
419 |
} |
|
420 |
String s = sw.toString(); |
|
421 |
return s; |
|
422 |
} |
|
423 |
|
|
424 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.318/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonArrayImpl.java | ||
---|---|---|
1 |
/* |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2020 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
18 |
* |
|
19 |
* For any additional information, do not hesitate to contact us |
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
21 |
*/ |
|
22 |
|
|
23 |
package org.gvsig.json; |
|
24 |
|
|
25 |
import java.io.StringWriter; |
|
26 |
import java.util.Collection; |
|
27 |
import java.util.Collections; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.List; |
|
30 |
import java.util.ListIterator; |
|
31 |
import javax.json.JsonArray; |
|
32 |
import javax.json.JsonNumber; |
|
33 |
import javax.json.JsonObject; |
|
34 |
import javax.json.JsonString; |
Also available in: Unified diff