KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > BaseValue


1 /*
2  * $Id: BaseValue.java,v 1.3 2004/08/07 16:45:05 benjmestrallet Exp $
3  *
4  * Copyright 2002-2004 Day Management AG, Switzerland.
5  *
6  * Licensed under the Day RI License, Version 2.0 (the "License"),
7  * as a reference implementation of the following specification:
8  *
9  * Content Repository API for Java Technology, revision 0.12
10  * <http://www.jcp.org/en/jsr/detail?id=170>
11  *
12  * You may not use this file except in compliance with the License.
13  * You may obtain a copy of the License files at
14  *
15  * http://www.day.com/content/en/licenses/day-ri-license-2.0
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package javax.jcr;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 /**
31  * This class is the superclass of the type-specific
32  * classes implementing the <code>Value</code> interfaces.
33  *
34  * @author Stefan Guggisberg
35  * @see javax.jcr.Value
36  * @see javax.jcr.StringValue
37  * @see javax.jcr.LongValue
38  * @see javax.jcr.DoubleValue
39  * @see javax.jcr.BooleanValue
40  * @see javax.jcr.DateValue
41  * @see javax.jcr.BinaryValue
42  * @see javax.jcr.SoftLinkValue
43  * @see javax.jcr.ReferenceValue
44  */

45 public abstract class BaseValue implements Value {
46
47   protected static final String JavaDoc DEFAULT_ENCODING = "UTF-8";
48
49   private static final short STATE_UNDEFINED = 0;
50   private static final short STATE_VALUE_CONSUMED = 1;
51   private static final short STATE_STREAM_CONSUMED = 2;
52
53   private short state = STATE_UNDEFINED;
54
55   protected final int type;
56
57   protected InputStream JavaDoc stream = null;
58
59   /**
60    * Package-private default constructor.
61    *
62    * @param type The type of this value.
63    */

64   BaseValue(int type) {
65     this.type = type;
66   }
67
68   /**
69    * Checks if the non-stream value of this instance has already been
70    * consumed (i.e. if any getter methods except <code>{@link #getStream()}</code> and
71    * <code>{@link #getType()}</code> have been previously called at least once) and
72    * sets the state to <code>STATE_STREAM_CONSUMED</code>.
73    *
74    * @throws IllegalStateException if any getter methods other than
75    * <code>getStream()</code> and <code>getState()</code> have been
76    * previously called at least once.
77    */

78   protected void setStreamConsumed() throws IllegalStateException JavaDoc {
79     if (state == STATE_VALUE_CONSUMED) {
80       throw new IllegalStateException JavaDoc("non-stream value has already been consumed");
81     }
82     state = STATE_STREAM_CONSUMED;
83   }
84
85   /**
86    * Checks if the stream value of this instance has already been
87    * consumed (i.e. if <code>{@link #getStream()}/<code> has been previously called
88    * at least once) and sets the state to <code>STATE_VALUE_CONSUMED</code>.
89    *
90    * @throws IllegalStateException if <code>getStream()</code> has been
91    * previously called at least once.
92    */

93   protected void setValueConsumed() throws IllegalStateException JavaDoc {
94     if (state == STATE_STREAM_CONSUMED) {
95       throw new IllegalStateException JavaDoc("stream value has already been consumed");
96     }
97     state = STATE_VALUE_CONSUMED;
98   }
99
100   //----------------------------------------------------------------< Value >
101
/**
102    * @see Value#getType
103    */

104   public int getType() {
105     return type;
106   }
107
108   /**
109    * @see Value#getStream
110    */

111   public InputStream JavaDoc getStream() throws ValueFormatException, RepositoryException {
112     setStreamConsumed();
113
114     if (stream != null) {
115       return stream;
116     }
117
118     try {
119       // convert via string
120
stream = new ByteArrayInputStream JavaDoc(getString().getBytes(DEFAULT_ENCODING));
121       return stream;
122     } catch (UnsupportedEncodingException JavaDoc e) {
123       throw new RepositoryException(DEFAULT_ENCODING + " not supported on this platform", e);
124     }
125   }
126 }
127
Popular Tags