001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.property.basic;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.io.Reader;
029    import java.io.UnsupportedEncodingException;
030    import java.math.BigDecimal;
031    import java.net.URI;
032    import java.util.Calendar;
033    import java.util.Date;
034    import java.util.UUID;
035    import net.jcip.annotations.Immutable;
036    import org.jboss.dna.common.text.TextDecoder;
037    import org.jboss.dna.common.text.TextEncoder;
038    import org.jboss.dna.common.util.CheckArg;
039    import org.jboss.dna.common.util.IoUtil;
040    import org.jboss.dna.common.util.Logger;
041    import org.jboss.dna.graph.GraphI18n;
042    import org.jboss.dna.graph.property.Binary;
043    import org.jboss.dna.graph.property.DateTime;
044    import org.jboss.dna.graph.property.IoException;
045    import org.jboss.dna.graph.property.Name;
046    import org.jboss.dna.graph.property.NamespaceRegistry;
047    import org.jboss.dna.graph.property.Path;
048    import org.jboss.dna.graph.property.PropertyType;
049    import org.jboss.dna.graph.property.Reference;
050    import org.jboss.dna.graph.property.ValueFactory;
051    import org.jboss.dna.graph.property.ValueFormatException;
052    
053    /**
054     * The standard {@link ValueFactory} for {@link PropertyType#STRING} values.
055     * 
056     * @author Randall Hauch
057     * @author John Verhaeg
058     */
059    @Immutable
060    public class StringValueFactory extends AbstractValueFactory<String> {
061    
062        private final TextEncoder encoder;
063        private final NamespaceRegistry namespaceRegistry;
064    
065        public StringValueFactory( TextDecoder decoder,
066                                   TextEncoder encoder ) {
067            super(PropertyType.STRING, decoder, null);
068            CheckArg.isNotNull(encoder, "encoder");
069            this.encoder = encoder;
070            this.namespaceRegistry = null;
071        }
072    
073        public StringValueFactory( NamespaceRegistry namespaceRegistry,
074                                   TextDecoder decoder,
075                                   TextEncoder encoder ) {
076            super(PropertyType.STRING, decoder, null);
077            CheckArg.isNotNull(encoder, "encoder");
078            CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
079            this.encoder = encoder;
080            this.namespaceRegistry = namespaceRegistry;
081        }
082    
083        /**
084         * @return encoder
085         */
086        public TextEncoder getEncoder() {
087            return this.encoder;
088        }
089    
090        /**
091         * {@inheritDoc}
092         */
093        @Override
094        protected ValueFactory<String> getStringValueFactory() {
095            return this;
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        public String create( String value ) {
102            return value;
103        }
104    
105        /**
106         * {@inheritDoc}
107         */
108        public String create( String value,
109                              TextDecoder decoder ) {
110            if (value == null) return value;
111            if (decoder == null) decoder = getDecoder();
112            return decoder.decode(value);
113        }
114    
115        /**
116         * {@inheritDoc}
117         */
118        public String create( int value ) {
119            return Integer.toString(value);
120        }
121    
122        /**
123         * {@inheritDoc}
124         */
125        public String create( long value ) {
126            return Long.toString(value);
127        }
128    
129        /**
130         * {@inheritDoc}
131         */
132        public String create( boolean value ) {
133            return Boolean.toString(value);
134        }
135    
136        /**
137         * {@inheritDoc}
138         */
139        public String create( float value ) {
140            return Float.toString(value);
141        }
142    
143        /**
144         * {@inheritDoc}
145         */
146        public String create( double value ) {
147            return Double.toString(value);
148        }
149    
150        /**
151         * {@inheritDoc}
152         */
153        public String create( BigDecimal value ) {
154            if (value == null) return null;
155            return value.toString();
156        }
157    
158        /**
159         * {@inheritDoc}
160         */
161        public String create( Calendar value ) {
162            if (value == null) return null;
163            return new JodaDateTime(value).getString();
164        }
165    
166        /**
167         * {@inheritDoc}
168         */
169        public String create( Date value ) {
170            if (value == null) return null;
171            return new JodaDateTime(value).getString();
172        }
173    
174        /**
175         * {@inheritDoc}
176         * 
177         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.DateTime)
178         */
179        public String create( DateTime value ) throws ValueFormatException {
180            if (value == null) return null;
181            return value.getString(); // ISO representation
182        }
183    
184        /**
185         * {@inheritDoc}
186         */
187        public String create( Name value ) {
188            if (value == null) return null;
189            if (this.namespaceRegistry != null) {
190                return value.getString(this.namespaceRegistry, getEncoder());
191            }
192            return value.getString(getEncoder());
193        }
194    
195        /**
196         * {@inheritDoc}
197         */
198        public String create( Path value ) {
199            if (value == null) return null;
200            if (this.namespaceRegistry != null) {
201                return value.getString(this.namespaceRegistry, getEncoder());
202            }
203            return value.getString(getEncoder());
204        }
205    
206        /**
207         * {@inheritDoc}
208         */
209        public String create( Reference value ) {
210            if (value == null) return null;
211            return value.getString(getEncoder());
212        }
213    
214        /**
215         * {@inheritDoc}
216         */
217        public String create( URI value ) {
218            if (value == null) return null;
219            return value.toString();
220        }
221    
222        /**
223         * {@inheritDoc}
224         * 
225         * @see org.jboss.dna.graph.property.ValueFactory#create(java.util.UUID)
226         */
227        public String create( UUID value ) throws IoException {
228            if (value == null) return null;
229            return value.toString();
230        }
231    
232        /**
233         * {@inheritDoc}
234         */
235        public String create( byte[] value ) {
236            if (value == null) return null;
237            try {
238                return new String(value, "UTF-8");
239            } catch (UnsupportedEncodingException err) {
240                throw new ValueFormatException(value, getPropertyType(),
241                                               GraphI18n.errorConvertingType.text(byte[].class.getSimpleName(),
242                                                                                  String.class.getSimpleName(),
243                                                                                  value), err);
244            }
245        }
246    
247        /**
248         * {@inheritDoc}
249         * 
250         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.Binary)
251         */
252        public String create( Binary value ) throws ValueFormatException, IoException {
253            if (value == null) return null;
254            try {
255                value.acquire();
256                InputStream stream = value.getStream();
257                try {
258                    return create(stream, value.getSize());
259                } finally {
260                    try {
261                        stream.close();
262                    } catch (IOException e) {
263                        Logger.getLogger(getClass()).debug(e, "Error closing the stream while converting from Binary to String");
264                    }
265                }
266            } finally {
267                value.release();
268            }
269        }
270    
271        /**
272         * {@inheritDoc}
273         */
274        public String create( InputStream stream,
275                              long approximateLength ) throws IoException {
276            if (stream == null) return null;
277            byte[] value = null;
278            try {
279                value = IoUtil.readBytes(stream);
280                return new String(value, "UTF-8");
281            } catch (UnsupportedEncodingException err) {
282                throw new ValueFormatException(value, getPropertyType(),
283                                               GraphI18n.errorConvertingType.text(InputStream.class.getSimpleName(),
284                                                                                  String.class.getSimpleName(),
285                                                                                  value), err);
286            } catch (IOException err) {
287                throw new IoException(GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(),
288                                                                       String.class.getSimpleName()), err);
289            }
290        }
291    
292        /**
293         * {@inheritDoc}
294         */
295        public String create( Reader reader,
296                              long approximateLength ) throws IoException {
297            if (reader == null) return null;
298            try {
299                return IoUtil.read(reader);
300            } catch (IOException err) {
301                throw new IoException(GraphI18n.errorConvertingIo.text(Reader.class.getSimpleName(), String.class.getSimpleName()),
302                                      err);
303            }
304        }
305    
306        /**
307         * {@inheritDoc}
308         */
309        @Override
310        protected String[] createEmptyArray( int length ) {
311            return new String[length];
312        }
313    
314    }