]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/Datatypes.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / Datatypes.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.InputStream;\r
16 import java.nio.charset.Charset;\r
17 \r
18 import org.simantics.databoard.binding.error.BindingConstructionException;\r
19 import org.simantics.databoard.binding.error.DatatypeConstructionException;\r
20 import org.simantics.databoard.binding.error.RuntimeDatatypeConstructionException;\r
21 import org.simantics.databoard.binding.reflection.ClassBindingFactory;\r
22 import org.simantics.databoard.binding.reflection.VoidBinding;\r
23 import org.simantics.databoard.parser.repository.DataTypeRepository;\r
24 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
25 import org.simantics.databoard.type.ArrayType;\r
26 import org.simantics.databoard.type.BooleanType;\r
27 import org.simantics.databoard.type.ByteType;\r
28 import org.simantics.databoard.type.Datatype;\r
29 import org.simantics.databoard.type.DoubleType;\r
30 import org.simantics.databoard.type.FloatType;\r
31 import org.simantics.databoard.type.IntegerType;\r
32 import org.simantics.databoard.type.LongType;\r
33 import org.simantics.databoard.type.OptionalType;\r
34 import org.simantics.databoard.type.StringType;\r
35 import org.simantics.databoard.type.VariantType;\r
36 import org.simantics.databoard.util.StreamUtil;\r
37 \r
38 /**\r
39  * This class is a facade to the data type services. \r
40  * \r
41  * @author Hannu Niemisto\r
42  * @author Toni Kalajainen\r
43  */\r
44 public class Datatypes {\r
45         \r
46         public static final BooleanType   BOOLEAN = new BooleanType();\r
47         public static final ByteType      BYTE    = new ByteType();\r
48         public static final IntegerType   INTEGER = new IntegerType();\r
49         public static final LongType      LONG    = new LongType();\r
50         public static final FloatType     FLOAT   = new FloatType();\r
51         public static final DoubleType    DOUBLE  = new DoubleType();\r
52         public static final StringType    STRING  = new StringType();\r
53         public static final VariantType   VARIANT = new VariantType();\r
54         \r
55         public static final Datatype      VOID    = VoidBinding.VOID_BINDING.type();\r
56 \r
57         public static final ArrayType     BOOLEAN_ARRAY = new ArrayType( BOOLEAN );\r
58         public static final ArrayType     BYTE_ARRAY    = new ArrayType( BYTE );\r
59         public static final ArrayType     INTEGER_ARRAY = new ArrayType( INTEGER );\r
60         public static final ArrayType     LONG_ARRAY    = new ArrayType( LONG );\r
61         public static final ArrayType     FLOAT_ARRAY   = new ArrayType( FLOAT );\r
62         public static final ArrayType     DOUBLE_ARRAY  = new ArrayType( DOUBLE );\r
63         public static final ArrayType     STRING_ARRAY  = new ArrayType( STRING );\r
64         public static final ArrayType     VARIANT_ARRAY  = new ArrayType( VARIANT );\r
65         \r
66         public static final DataTypeRepository datatypeRepository = new DataTypeRepository();\r
67 \r
68         /** Make type optional */\r
69         public static Datatype optional( Datatype type ) { return new OptionalType( type ); }\r
70         \r
71         /**\r
72          * Read representation from a class. DataType details and parameters\r
73          * are read as annotations placed in the class.  \r
74          * (See org.simantics.databoard.annotations)  \r
75          * <p>\r
76          * As an exception, in the subclasses of {@link Throwable}, the fields of\r
77          * Throwable are omited.\r
78          * \r
79          * @see ClassBindingFactory  \r
80          * @param clazz\r
81          * @return data type\r
82          * @throws DatatypeConstructionException \r
83          */\r
84         @SuppressWarnings("unchecked")\r
85         public static <T extends Datatype> T getDatatype(Class<?> clazz) \r
86         throws DatatypeConstructionException {\r
87                 try {\r
88                         return (T) Bindings.getBinding(clazz).type();\r
89                 } catch (BindingConstructionException e) {\r
90                         throw new DatatypeConstructionException(e);\r
91                 }\r
92         }\r
93         \r
94         /**\r
95          * Read representation from a class. DataType details and parameters\r
96          * are read as annotations placed in the class.  \r
97          * (See org.simantics.databoard.annotations)  \r
98          * <p>\r
99          * This method is used when the caller is 100% sure that binding will be \r
100          * constructed without exceptions. Such classes\r
101          * are all primitive types (Double, Integer, etc, arrays, DataType, ...)\r
102          * \r
103          * This method is unchecked if binding construction to the clazz cannot be trusted.\r
104          * If construction fails, a RuntimeException is thrown.\r
105          * \r
106          * @see ClassBindingFactory  \r
107          * @param clazz\r
108          * @return data type\r
109          * @throws RuntimeDatatypeConstructionException \r
110          */\r
111         @SuppressWarnings("unchecked")\r
112         public static <T extends Datatype> T getDatatypeUnchecked(Class<?> clazz) \r
113         throws RuntimeDatatypeConstructionException {\r
114                 try {\r
115                         return (T) Bindings.getBinding(clazz).type();\r
116                 } catch (BindingConstructionException e) {\r
117                         throw new RuntimeDatatypeConstructionException(new DatatypeConstructionException(e));\r
118                 }\r
119         }       \r
120         \r
121         /**\r
122          * Adds a type to the repository.\r
123          * \r
124          * @param name Name of the type\r
125          * @param type Type to be added\r
126          */\r
127         public static void addDatatype(String name, Datatype type) {\r
128                 datatypeRepository.add(name, type);\r
129         }\r
130         \r
131         /**\r
132          * Get data type by name.\r
133          * \r
134          * e.g. get("Vec2");\r
135          * \r
136          * @param name\r
137          * @return data type\r
138          */\r
139         public static Datatype getDatatype(String name) {\r
140                 return datatypeRepository.get(name);\r
141         }       \r
142         \r
143         /**\r
144          * Parses and adds type definitions to the repository.\r
145          * \r
146          * The data type can be acquired with #getType\r
147          * \r
148          * e.g. "type Vec2 = { x : Double, y : Double }"\r
149          * \r
150          * @param definitions Definitions in textual format.\r
151          * @throws DataTypeSyntaxError \r
152          */\r
153         public static void addDefinitions(String definitions) throws DataTypeSyntaxError {\r
154                 datatypeRepository.addDefinitions(definitions);\r
155         }\r
156         \r
157         /**\r
158          * Parses an unnamed data type.\r
159          * \r
160          * <a href="http://dev.simantics.org/index.php/Data_type_notation">Datatype Notation</a>\r
161          * \r
162          * e.g. "{ direction : Vec, vector : Vec2 }" \r
163          * \r
164          * @param typeString The textual representation of the type to be translated\r
165          * @return Translated data type\r
166          * @throws DataTypeSyntaxError \r
167          */\r
168         public static Datatype translate(String typeString) throws DataTypeSyntaxError {\r
169                 return datatypeRepository.translate(typeString);\r
170         }       \r
171 \r
172         static {\r
173                 Charset UTF8        = Charset.forName("UTF-8");\r
174                 \r
175                 // Read File\r
176                 InputStream is = Datatypes.class.getResourceAsStream("standardTypes.dbt");\r
177                 try {\r
178                         String defs = StreamUtil.readString(is, UTF8);\r
179                         Datatypes.datatypeRepository.addDefinitions(defs);                      \r
180                 } catch (IOException e) {\r
181                         throw new RuntimeException( e );\r
182                 } catch (DataTypeSyntaxError e) {\r
183                         throw new RuntimeException( e );\r
184                 } finally {\r
185                         try { is.close(); } catch (IOException e) {}\r
186                 }\r
187                 \r
188         }\r
189         \r
190 }\r
191 \r
192 \r