summaryrefslogtreecommitdiffstats
path: root/env/src/main/java/org/onap/aaf/inno/env/BaseDataFactory.java
blob: b65ba560b827d7f0e92beeca9cbf3a07211ba1c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.inno.env;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.onap.aaf.inno.env.impl.EnvFactory;
import org.xml.sax.SAXException;



/**
 * DataFactory Constructor will create the Stringifiers and Objectifiers necessary 
 * by Type and store the Class of the Type for quick creation of Data Objects
 * with reused (and thread safe) components
 * s
 * Native Types are included.
 * Those types covered by Env Implementation are covered dynamically.
 * Types outside of Env mechanism can be added with "add" function
 * 
 *
 * @param <T>
 */
public class BaseDataFactory {

	/**
	 * Generate a Schema Object for use in validation based on FileNames.
	 * 
	 * WARNING: The java.xml.binding code requires YOU to figure out what order the
	 * files go in.  If there is an import from A in B, then you must list A first.
	 * 
	 * @param err
	 * @param filenames
	 * @return
	 * @throws APIException
	 */
	public static Schema genSchema(Store env, String ... filenames) throws APIException {
		String schemaDir = env.get(
				env.staticSlot(EnvFactory.SCHEMA_DIR),
				EnvFactory.DEFAULT_SCHEMA_DIR);
		File dir = new File(schemaDir);
		if(!dir.exists())throw new APIException("Schema Directory " + schemaDir + " does not exist.  You can set this with " + EnvFactory.SCHEMA_DIR + " property");
		FileInputStream[] fis = new FileInputStream[filenames.length];
		Source[] sources = new Source[filenames.length];
		File f; 
		for(int i=0; i<filenames.length; ++i) {
			if(!(f=new File(schemaDir + File.separatorChar + filenames[i])).exists()) {
				if(!f.exists()) throw new APIException("Cannot find " + f.getName() + " for schema validation");
			}
			try {
				fis[i]=new FileInputStream(f);
			} catch (FileNotFoundException e) {
				throw new APIException(e);
			}
			sources[i]= new StreamSource(fis[i]);
		}
		try {
			//Note: SchemaFactory is not reentrant or very thread safe either... see docs
			synchronized(XMLConstants.W3C_XML_SCHEMA_NS_URI) { // SchemaFactory is not reentrant
				return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
				 	.newSchema(sources);
			}
		} catch (SAXException e) {
			throw new APIException(e);
		} finally {
			for(FileInputStream d : fis) {
				try {
					d.close();
				} catch (IOException e) {
				 // Never mind... we did our best
				}
			}
		}

	}

	public static QName getQName(Class<?> clss) throws APIException {
		// Obtain the Necessary info for QName from Requirement
		XmlRootElement xre = clss.getAnnotation(XmlRootElement.class);
		if(xre==null)throw new APIException(clss.getName() + " does not have an XmlRootElement annotation");
		Package pkg = clss.getPackage();
		XmlSchema xs = pkg.getAnnotation(XmlSchema.class);
		if(xs==null) throw new APIException(clss.getName() + " package-info does not have an XmlSchema annotation");
		return new QName(xs.namespace(),xre.name());
	}

	/////////////////////////////////////////////
	// Native Type Converters
	/////////////////////////////////////////////
//	/**
//	 * StringStringifier
//	 * 
//	 * Support the Native Type String.. just return it back
//	 * 
//	 *
//	 */
//	public static class StringStringifier extends NullLifeCycle implements Stringifier<String> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, String input) throws APIException {
//			return input;
//		}
//	};		
//
//	/**
//	 * StringObjectifier
//	 * 
//	 * Support the Native Type String.. just return it back
//	 * 
//	 *
//	 */
//	public static class StringObjectifier extends NullLifeCycle implements Objectifier<String> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public String objectify(Env env, String input) throws APIException {
//			return input;
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public String newInstance() throws APIException {
//			return "";
//		}
//	};
//	
//	/**
//	 * LongStringifier
//	 * 
//	 * Support the Native Type Long.. use Long parse functions
//	 * 
//	 *
//	 */
//	public static class LongStringifier extends NullLifeCycle implements Stringifier<Long> {
//		public String stringify(Env env, Long input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * LongObjectifier
//	 * 
//	 * Support the Native Type Long.. use Long parse functions
//	 * 
//	 *
//	 */
//	public static class LongObjectifier extends NullLifeCycle implements Objectifier<Long> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Long objectify(Env env, String input) throws APIException {
//			try {
//				return new Long(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Long\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Long newInstance() throws APIException {
//			return 0L;
//		}
//	}
//
//	/**
//	 * IntegerStringifier
//	 * 
//	 * Support the Native Integer.. use Integer parse functions
//	 * 
//	 *
//	 */
//	public static class IntegerStringifier extends NullLifeCycle implements Stringifier<Integer> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, Integer input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * IntegerObjectifier
//	 * 
//	 * Support the Native Integer.. use Integer parse functions
//	 * 
//	 *
//	 */
//	public static class IntegerObjectifier extends NullLifeCycle implements Objectifier<Integer> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Integer objectify(Env env, String input) throws APIException {
//			try {
//				return new Integer(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Integer\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Integer newInstance() throws APIException {
//			return 0;
//		}
//	}
//
//	/**
//	 * ShortStringifier
//	 * 
//	 * Support the Native Short.. use Short parse functions
//	 * 
//	 *
//	 */
//	public static class ShortStringifier extends NullLifeCycle implements Stringifier<Short> {
//		public String stringify(Env env, Short input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * ShortObjectifier
//	 * 
//	 * Support the Native Short.. use Short parse functions
//	 * 
//	 *
//	 */
//	public static class ShortObjectifier extends NullLifeCycle implements Objectifier<Short> {
//		public Short objectify(Env env, String input) throws APIException {
//			try {
//				return new Short(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Short\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		public Short newInstance() throws APIException {
//			return 0;
//		}
//	}
//	
//	/**
//	 * ByteStringifier
//	 * 
//	 * Support the Native Byte.. use Byte parse functions
//	 * 
//	 *
//	 */
//	public static class ByteStringifier extends NullLifeCycle implements Stringifier<Byte> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, Byte input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * ByteObjectifier
//	 * 
//	 * Support the Native Byte.. use Byte parse functions
//	 * 
//	 *
//	 */
//	public static class ByteObjectifier extends NullLifeCycle implements Objectifier<Byte> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Byte objectify(Env env, String input) throws APIException {
//			try {
//				return new Byte(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Byte\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Byte newInstance() throws APIException {
//			return 0;
//		}
//	}
//
//	/**
//	 * CharacterStringifier
//	 * 
//	 * Support the Native Character.. use Character parse functions
//	 * 
//	 *
//	 */
//	public static class CharacterStringifier extends NullLifeCycle implements Stringifier<Character> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, Character input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * CharacterObjectifier
//	 * 
//	 * Support the Native Character.. use Character parse functions
//	 * 
//	 *
//	 */
//	public static class CharacterObjectifier extends NullLifeCycle implements Objectifier<Character> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Character objectify(Env env, String input) throws APIException {
//			int length = input.length();
//			if(length<1 || length>1) {
//				throw new APIException("String [" + input + "] does not represent a single Character");
//			}
//			return input.charAt(0);
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Character newInstance() throws APIException {
//			return 0;
//		}
//	}
//
//	/**
//	 * FloatStringifier
//	 * 
//	 * Support the Native Float.. use Float parse functions
//	 * 
//	 *
//	 */
//	public static class FloatStringifier extends NullLifeCycle implements Stringifier<Float> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, Float input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * FloatObjectifier
//	 * 
//	 * Support the Native Float.. use Float parse functions
//	 * 
//	 *
//	 */
//	public static class FloatObjectifier extends NullLifeCycle implements Objectifier<Float> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Float objectify(Env env, String input) throws APIException {
//			try {
//				return new Float(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Float\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Float newInstance() throws APIException {
//			return 0.0f;
//		}
//	}
//
//	/**
//	 * DoubleStringifier
//	 * 
//	 * Support the Native Double.. use Double parse functions
//	 *
//	 */
//	public static class DoubleStringifier extends NullLifeCycle implements Stringifier<Double> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
//		 */
//		public String stringify(Env env, Double input) throws APIException {
//			return input.toString();
//		}
//	}
//	
//	/**
//	 * DoubleObjectifier
//	 * 
//	 * Support the Native Double.. use Double parse functions
//	 * 
//	 *
//	 */
//	public static class DoubleObjectifier extends NullLifeCycle implements Objectifier<Double> {
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
//		 */
//		public Double objectify(Env env, String input) throws APIException {
//			try {
//				return new Double(input);
//			} catch (Exception e) {
//				APIException ae = new APIException("Cannot create a \"Double\" from [" + input + ']');
//				ae.initCause(e);
//				throw ae;
//			}
//		}
//
//		/* (non-Javadoc)
//		 * @see com.att.env.Objectifier#newObject()
//		 */
//		public Double newInstance() throws APIException {
//			return 0.0;
//		}
//	}

}