Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
JSONWriterImpl |
|
| 1.8333333333333333;1.833 |
1 | // Copyright 2006 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | package org.apache.tapestry.markup; |
|
15 | ||
16 | import java.io.PrintWriter; |
|
17 | ||
18 | import org.apache.hivemind.util.Defense; |
|
19 | import org.apache.tapestry.json.IJSONWriter; |
|
20 | import org.apache.tapestry.json.JSONArray; |
|
21 | import org.apache.tapestry.json.JSONObject; |
|
22 | ||
23 | /** |
|
24 | * Implementation of {@link IJSONWriter}. |
|
25 | * |
|
26 | * @author jkuhnert |
|
27 | */ |
|
28 | public class JSONWriterImpl implements IJSONWriter |
|
29 | { |
|
30 | /** Outputstream writer. */ |
|
31 | protected PrintWriter _writer; |
|
32 | ||
33 | /** |
|
34 | * Delegate object that handles object json renders. |
|
35 | */ |
|
36 | private JSONObject _json; |
|
37 | ||
38 | /** |
|
39 | * Delegate array object that handles object array json renders. |
|
40 | */ |
|
41 | private JSONArray _array; |
|
42 | ||
43 | /** |
|
44 | * Creates a new instance that will write all content to |
|
45 | * the specified {@link PrintWriter}. |
|
46 | * |
|
47 | * @param writer The outputstream to write to. |
|
48 | */ |
|
49 | public JSONWriterImpl(PrintWriter writer) |
|
50 | 2 | { |
51 | 2 | Defense.notNull(writer, "writer"); |
52 | ||
53 | 2 | _writer = writer; |
54 | 2 | } |
55 | ||
56 | /** |
|
57 | * |
|
58 | * {@inheritDoc} |
|
59 | */ |
|
60 | public void close() |
|
61 | { |
|
62 | 0 | if (_json == null && _array == null) |
63 | 0 | _json = new JSONObject(); |
64 | ||
65 | 0 | if (_json != null) { |
66 | ||
67 | 0 | _writer.write(_json.toString()); |
68 | } |
|
69 | ||
70 | 0 | if (_array != null) { |
71 | ||
72 | 0 | _writer.write(_array.toString()); |
73 | } |
|
74 | ||
75 | 0 | _writer.flush(); |
76 | 0 | _writer.close(); |
77 | 0 | } |
78 | ||
79 | /** |
|
80 | * {@inheritDoc} |
|
81 | */ |
|
82 | public JSONObject object() |
|
83 | { |
|
84 | 3 | if (_json == null) |
85 | 2 | _json = new JSONObject(); |
86 | ||
87 | 3 | return _json; |
88 | } |
|
89 | ||
90 | /** |
|
91 | * {@inheritDoc} |
|
92 | */ |
|
93 | public JSONArray array() |
|
94 | { |
|
95 | 0 | if (_array == null) |
96 | 0 | _array = new JSONArray(); |
97 | ||
98 | 0 | return _array; |
99 | } |
|
100 | ||
101 | public void flush() |
|
102 | { |
|
103 | 0 | _writer.flush(); |
104 | 0 | } |
105 | ||
106 | /** |
|
107 | * The outputstream being used to write this |
|
108 | * instance's content. |
|
109 | * |
|
110 | * @return The writer being written to. |
|
111 | */ |
|
112 | protected PrintWriter getWriter() |
|
113 | { |
|
114 | 0 | return _writer; |
115 | } |
|
116 | } |