Source code: com/obinary/cms/core/Atom.java
1 /**
2 *
3 * Magnolia and its source-code is licensed under the LGPL.
4 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6 * you are required to provide proper attribution to obinary.
7 * If you reproduce or distribute the document without making any substantive modifications to its content,
8 * please use the following attribution line:
9 *
10 * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18
19 package com.obinary.cms.core;
20
21
22 import javax.jcr.*;
23 import java.io.InputStream;
24 import java.util.Calendar;
25
26
27 /**
28 * User: sameercharles
29 * Date: Apr 28, 2003
30 * Time: 11:20:59 AM
31 * @author Sameer Charles
32 * @version 1.0
33 */
34
35
36 public class Atom {
37
38
39 public static final String HTML_LINEBREAK = "<br>";
40
41
42 private String value;
43 private Property property;
44
45
46
47
48 /**
49 * package private constructor
50 */
51 Atom() {
52 this.property = null;
53 }
54
55
56
57 /**
58 * <p>constructor | create atom object to work-on based on
59 * existing <code>Atom</code><p>
60 *
61 * @param workingNode current active <code>Node</code>
62 * @param name <code>Atom</code> name to be retrieved
63 */
64 public Atom (Node workingNode, String name) throws ElementNotFoundException, RepositoryException {
65 this.property = workingNode.getProperty(name);
66 }
67
68
69
70 /**
71 * <p>constructor | creates a new initialized Atom of default yype <b>String</b></p>
72 *
73 * @param workingNode current active <code>Node</code>
74 * @param name <code>Atom</code> name to be created
75 * @throws ElementNotFoundException
76 * @throws RepositoryException
77 */
78 public Atom (Node workingNode, String name, boolean createNew) throws ElementNotFoundException, RepositoryException {
79 this.property = workingNode.addProperty(name);
80 }
81
82
83
84 /**
85 * <p>constructor | creates a new initialized Atom</p>
86 *
87 * @param workingNode current active <code>Node</code>
88 * @param name <code>Atom</code> name to be created
89 * @param value Value to be set
90 * @param type PropertyType
91 * @throws ElementNotFoundException
92 * @throws RepositoryException
93 */
94 public Atom (Node workingNode, String name, Value value, int type) throws ElementNotFoundException, RepositoryException {
95 this.property = workingNode.addProperty(name,value,type);
96 }
97
98
99
100 /**
101 * <p>constructor | creates a new initialized Atom</p>
102 *
103 * @param prop current <code>property</code>
104 */
105 public Atom (Property prop) throws ElementNotFoundException, RepositoryException {
106 this.property = prop;
107 }
108
109
110
111 /**
112 * <p>Returns the <code>value</code> of this <code>Atom</code>. One of type:
113 * <ul>
114 * <li><code>PropertyType.STRING</code></li>
115 * <li><code>PropertyType.DATE</code></li>
116 * <li><code>PropertyType.SOFTLINK</code></li>
117 * <li><code>PropertyType.BINARY</code></li>
118 * <li><code>PropertyType.DOUBLE</code></li>
119 * <li><code>PropertyType.LONG</code></li>
120 * <li><code>PropertyType.BOOLEAN</code></li>
121 * </ul>
122 * </p>
123 *
124 * @return Value
125 */
126 public Value getValue() {
127 try {
128 return this.property.getValue();
129 } catch(Exception e) {return null;}
130 }
131
132
133
134 /**
135 * <p>Returns the <code>String</code> representation of the value: <br>
136 * decodes like breaks with the specified regular expresion
137 * </p>
138 *
139 * @param lineBreak , regular expession
140 * @return String
141 */
142 public String getString(String lineBreak) {
143 try {
144 return this.getString().replaceAll("\n",lineBreak);
145 } catch (Exception e) {return "";}
146 }
147
148
149
150 /**
151 * <p>Returns the <code>String</code> representation of the value:
152 * </p>
153 *
154 * @return String
155 */
156 public String getString() {
157 try {
158 return this.property.getValue().getString().replaceAll("<","<");
159 } catch (Exception e) {return "";}
160 }
161
162
163
164 /**
165 * <p>Returns the <code>long</code> representation of the value:
166 * </p>
167 *
168 * @return long
169 */
170 public long getLong() {
171 try {
172 return this.property.getValue().getLong();
173 } catch (Exception e) {return 0;}
174 }
175
176
177
178 /**
179 * <p>Returns the <code>double</code> representation of the value:
180 * </p>
181 *
182 * @return double
183 */
184 public double getDouble() {
185 try {
186 return this.property.getValue().getDouble();
187 } catch (Exception e) {return 0;}
188 }
189
190
191
192 /**
193 * <p>Returns the <code>Calendar</code> representation of the value:
194 * </p>
195 *
196 * @return Calendar
197 */
198 public Calendar getDate() {
199 try {
200 return this.property.getValue().getDate();
201 } catch (Exception e) {return null;}
202 }
203
204
205
206 /**
207 * <p>Returns the <code>boolean</code> representation of the value:
208 * </p>
209 *
210 * @return boolean
211 */
212 public boolean getBoolean() {
213 try {
214 return this.property.getValue().getBoolean();
215 } catch (Exception e) {return false;}
216 }
217
218
219
220 /**
221 * <p>Returns the <code>type</code> of this <code>Atom</code>. One of:
222 * <ul>
223 * <li><code>PropertyType.STRING</code></li>
224 * <li><code>PropertyType.DATE</code></li>
225 * <li><code>PropertyType.SOFTLINK</code></li>
226 * <li><code>PropertyType.BINARY</code></li>
227 * <li><code>PropertyType.DOUBLE</code></li>
228 * <li><code>PropertyType.LONG</code></li>
229 * <li><code>PropertyType.BOOLEAN</code></li>
230 * </ul>
231 * </p>
232 *
233 * @return PropertyType
234 */
235 public int getType () {
236 try {
237 return this.property.getType();
238 } catch (Exception e) {return PropertyType.UNDEFINED; }
239 }
240
241
242
243 /**
244 * @return atom name
245 */
246 public String getName() {
247 try {
248 return this.property.getName();
249 } catch (Exception e) {return "";}
250 }
251
252
253
254 /**
255 * <p> NOT AVAILABLE, always returns 0
256 * </p>
257 *
258 * @return content length
259 */
260 public int getContentLength() {
261 return 0;
262 //@todo implement this
263 }
264
265
266
267 /**
268 * <p>Access to property at the JCR level.
269 * <br><b>available only to be available, should not be used in normal circunstances!</b>
270 * </p>
271 *
272 * @return Property
273 */
274 public Property getJCRProperty() {
275 return this.property;
276 }
277
278
279
280 /**
281 * <p>set value of type <code>String</code></p>
282 *
283 * @throws RepositoryException
284 * @param value , string to be set
285 */
286 public void setValue(String value) throws RepositoryException {
287 this.property.setValue(value);
288 }
289
290
291
292 /**
293 * <p>set value of type <code>int</code></p>
294 *
295 * @throws RepositoryException
296 * @param value , int value to be set
297 */
298 public void setValue(int value) throws RepositoryException {
299 this.property.setValue(value);
300 }
301
302
303
304 /**
305 * <p>set value of type <code>long</code></p>
306 *
307 * @throws RepositoryException
308 * @param value , long value to be set
309 */
310 public void setValue(long value) throws RepositoryException {
311 this.property.setValue(value);
312 }
313
314
315
316 /**
317 * <p>set value of type <code>InputStream</code></p>
318 *
319 * @throws RepositoryException
320 * @param value , InputStream to be set
321 */
322 public void setValue(InputStream value) throws RepositoryException {
323 this.property.setValue(value);
324 }
325
326
327
328 /**
329 * <p>set value of type <code>double</code></p>
330 *
331 * @throws RepositoryException
332 * @param value , double value to be set
333 */
334 public void setValue(double value) throws RepositoryException {
335 this.property.setValue(value);
336 }
337
338
339
340 /**
341 * <p>set value of type <code>boolean</code></p>
342 *
343 * @throws RepositoryException
344 * @param value , boolean value to be set
345 */
346 public void setValue(boolean value) throws RepositoryException {
347 this.property.setValue(value);
348 }
349
350
351
352 /**
353 * <p>set value of type <code>Calendar</code></p>
354 *
355 * @throws RepositoryException
356 * @param value , Calendar value to be set
357 */
358 public void setValue(Calendar value) throws RepositoryException {
359 this.property.setValue(value);
360 }
361
362
363
364 /**
365 * <p>set value of type <code>Value</code></p>
366 *
367 * @throws RepositoryException
368 * @param value
369 */
370 public void setValue(Value value) throws RepositoryException {
371 this.property.setValue(value);
372 }
373
374
375
376 /**
377 * <p>checks if the atom exists in the repository</p>
378 *
379 * @return boolean
380 */
381 public boolean isExist() {
382 return (this.property != null);
383 }
384
385 }