2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ===========================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END====================================================
\r
22 package org.onap.aaf.misc.env;
\r
24 import java.io.File;
\r
25 import java.io.FileInputStream;
\r
26 import java.io.FileNotFoundException;
\r
27 import java.io.IOException;
\r
29 import javax.xml.XMLConstants;
\r
30 import javax.xml.bind.annotation.XmlRootElement;
\r
31 import javax.xml.bind.annotation.XmlSchema;
\r
32 import javax.xml.namespace.QName;
\r
33 import javax.xml.transform.Source;
\r
34 import javax.xml.transform.stream.StreamSource;
\r
35 import javax.xml.validation.Schema;
\r
36 import javax.xml.validation.SchemaFactory;
\r
38 import org.onap.aaf.misc.env.impl.EnvFactory;
\r
39 import org.xml.sax.SAXException;
\r
44 * DataFactory Constructor will create the Stringifiers and Objectifiers necessary
\r
45 * by Type and store the Class of the Type for quick creation of Data Objects
\r
46 * with reused (and thread safe) components
\r
48 * Native Types are included.
\r
49 * Those types covered by Env Implementation are covered dynamically.
\r
50 * Types outside of Env mechanism can be added with "add" function
\r
56 public class BaseDataFactory {
\r
57 private static final Object LOCK = new Object();
\r
59 * Generate a Schema Object for use in validation based on FileNames.
\r
61 * WARNING: The java.xml.binding code requires YOU to figure out what order the
\r
62 * files go in. If there is an import from A in B, then you must list A first.
\r
67 * @throws APIException
\r
69 public static Schema genSchema(Store env, String ... filenames) throws APIException {
\r
70 String schemaDir = env.get(
\r
71 env.staticSlot(EnvFactory.SCHEMA_DIR),
\r
72 EnvFactory.DEFAULT_SCHEMA_DIR);
\r
73 File dir = new File(schemaDir);
\r
74 if(!dir.exists())throw new APIException("Schema Directory " + schemaDir + " does not exist. You can set this with " + EnvFactory.SCHEMA_DIR + " property");
\r
75 FileInputStream[] fis = new FileInputStream[filenames.length];
\r
76 Source[] sources = new Source[filenames.length];
\r
78 for(int i=0; i<filenames.length; ++i) {
\r
79 if(!(f=new File(schemaDir + File.separatorChar + filenames[i])).exists()) {
\r
80 if(!f.exists()) throw new APIException("Cannot find " + f.getName() + " for schema validation");
\r
83 fis[i]=new FileInputStream(f);
\r
84 } catch (FileNotFoundException e) {
\r
85 throw new APIException(e);
\r
87 sources[i]= new StreamSource(fis[i]);
\r
90 //Note: SchemaFactory is not reentrant or very thread safe either... see docs
\r
91 synchronized(LOCK) { // SchemaFactory is not reentrant
\r
92 return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
\r
93 .newSchema(sources);
\r
95 } catch (SAXException e) {
\r
96 throw new APIException(e);
\r
98 for(FileInputStream d : fis) {
\r
101 } catch (IOException e) {
\r
102 // Never mind... we did our best
\r
109 public static QName getQName(Class<?> clss) throws APIException {
\r
110 // Obtain the Necessary info for QName from Requirement
\r
111 XmlRootElement xre = clss.getAnnotation(XmlRootElement.class);
\r
112 if(xre==null)throw new APIException(clss.getName() + " does not have an XmlRootElement annotation");
\r
113 Package pkg = clss.getPackage();
\r
114 XmlSchema xs = pkg.getAnnotation(XmlSchema.class);
\r
115 if(xs==null) throw new APIException(clss.getName() + " package-info does not have an XmlSchema annotation");
\r
116 return new QName(xs.namespace(),xre.name());
\r
119 /////////////////////////////////////////////
\r
120 // Native Type Converters
\r
121 /////////////////////////////////////////////
\r
123 // * StringStringifier
\r
125 // * Support the Native Type String.. just return it back
\r
127 // * @author Jonathan
\r
130 // public static class StringStringifier extends NullLifeCycle implements Stringifier<String> {
\r
131 // /* (non-Javadoc)
\r
132 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
134 // public String stringify(Env env, String input) throws APIException {
\r
140 // * StringObjectifier
\r
142 // * Support the Native Type String.. just return it back
\r
144 // * @author Jonathan
\r
147 // public static class StringObjectifier extends NullLifeCycle implements Objectifier<String> {
\r
148 // /* (non-Javadoc)
\r
149 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
151 // public String objectify(Env env, String input) throws APIException {
\r
155 // /* (non-Javadoc)
\r
156 // * @see com.att.env.Objectifier#newObject()
\r
158 // public String newInstance() throws APIException {
\r
164 // * LongStringifier
\r
166 // * Support the Native Type Long.. use Long parse functions
\r
168 // * @author Jonathan
\r
171 // public static class LongStringifier extends NullLifeCycle implements Stringifier<Long> {
\r
172 // public String stringify(Env env, Long input) throws APIException {
\r
173 // return input.toString();
\r
178 // * LongObjectifier
\r
180 // * Support the Native Type Long.. use Long parse functions
\r
182 // * @author Jonathan
\r
185 // public static class LongObjectifier extends NullLifeCycle implements Objectifier<Long> {
\r
186 // /* (non-Javadoc)
\r
187 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
189 // public Long objectify(Env env, String input) throws APIException {
\r
191 // return new Long(input);
\r
192 // } catch (Exception e) {
\r
193 // APIException ae = new APIException("Cannot create a \"Long\" from [" + input + ']');
\r
194 // ae.initCause(e);
\r
199 // /* (non-Javadoc)
\r
200 // * @see com.att.env.Objectifier#newObject()
\r
202 // public Long newInstance() throws APIException {
\r
208 // * IntegerStringifier
\r
210 // * Support the Native Integer.. use Integer parse functions
\r
212 // * @author Jonathan
\r
215 // public static class IntegerStringifier extends NullLifeCycle implements Stringifier<Integer> {
\r
216 // /* (non-Javadoc)
\r
217 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
219 // public String stringify(Env env, Integer input) throws APIException {
\r
220 // return input.toString();
\r
225 // * IntegerObjectifier
\r
227 // * Support the Native Integer.. use Integer parse functions
\r
229 // * @author Jonathan
\r
232 // public static class IntegerObjectifier extends NullLifeCycle implements Objectifier<Integer> {
\r
233 // /* (non-Javadoc)
\r
234 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
236 // public Integer objectify(Env env, String input) throws APIException {
\r
238 // return new Integer(input);
\r
239 // } catch (Exception e) {
\r
240 // APIException ae = new APIException("Cannot create a \"Integer\" from [" + input + ']');
\r
241 // ae.initCause(e);
\r
246 // /* (non-Javadoc)
\r
247 // * @see com.att.env.Objectifier#newObject()
\r
249 // public Integer newInstance() throws APIException {
\r
255 // * ShortStringifier
\r
257 // * Support the Native Short.. use Short parse functions
\r
259 // * @author Jonathan
\r
262 // public static class ShortStringifier extends NullLifeCycle implements Stringifier<Short> {
\r
263 // public String stringify(Env env, Short input) throws APIException {
\r
264 // return input.toString();
\r
269 // * ShortObjectifier
\r
271 // * Support the Native Short.. use Short parse functions
\r
273 // * @author Jonathan
\r
276 // public static class ShortObjectifier extends NullLifeCycle implements Objectifier<Short> {
\r
277 // public Short objectify(Env env, String input) throws APIException {
\r
279 // return new Short(input);
\r
280 // } catch (Exception e) {
\r
281 // APIException ae = new APIException("Cannot create a \"Short\" from [" + input + ']');
\r
282 // ae.initCause(e);
\r
287 // public Short newInstance() throws APIException {
\r
293 // * ByteStringifier
\r
295 // * Support the Native Byte.. use Byte parse functions
\r
297 // * @author Jonathan
\r
300 // public static class ByteStringifier extends NullLifeCycle implements Stringifier<Byte> {
\r
301 // /* (non-Javadoc)
\r
302 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
304 // public String stringify(Env env, Byte input) throws APIException {
\r
305 // return input.toString();
\r
310 // * ByteObjectifier
\r
312 // * Support the Native Byte.. use Byte parse functions
\r
314 // * @author Jonathan
\r
317 // public static class ByteObjectifier extends NullLifeCycle implements Objectifier<Byte> {
\r
318 // /* (non-Javadoc)
\r
319 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
321 // public Byte objectify(Env env, String input) throws APIException {
\r
323 // return new Byte(input);
\r
324 // } catch (Exception e) {
\r
325 // APIException ae = new APIException("Cannot create a \"Byte\" from [" + input + ']');
\r
326 // ae.initCause(e);
\r
331 // /* (non-Javadoc)
\r
332 // * @see com.att.env.Objectifier#newObject()
\r
334 // public Byte newInstance() throws APIException {
\r
340 // * CharacterStringifier
\r
342 // * Support the Native Character.. use Character parse functions
\r
344 // * @author Jonathan
\r
347 // public static class CharacterStringifier extends NullLifeCycle implements Stringifier<Character> {
\r
348 // /* (non-Javadoc)
\r
349 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
351 // public String stringify(Env env, Character input) throws APIException {
\r
352 // return input.toString();
\r
357 // * CharacterObjectifier
\r
359 // * Support the Native Character.. use Character parse functions
\r
361 // * @author Jonathan
\r
364 // public static class CharacterObjectifier extends NullLifeCycle implements Objectifier<Character> {
\r
365 // /* (non-Javadoc)
\r
366 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
368 // public Character objectify(Env env, String input) throws APIException {
\r
369 // int length = input.length();
\r
370 // if(length<1 || length>1) {
\r
371 // throw new APIException("String [" + input + "] does not represent a single Character");
\r
373 // return input.charAt(0);
\r
376 // /* (non-Javadoc)
\r
377 // * @see com.att.env.Objectifier#newObject()
\r
379 // public Character newInstance() throws APIException {
\r
385 // * FloatStringifier
\r
387 // * Support the Native Float.. use Float parse functions
\r
389 // * @author Jonathan
\r
392 // public static class FloatStringifier extends NullLifeCycle implements Stringifier<Float> {
\r
393 // /* (non-Javadoc)
\r
394 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
396 // public String stringify(Env env, Float input) throws APIException {
\r
397 // return input.toString();
\r
402 // * FloatObjectifier
\r
404 // * Support the Native Float.. use Float parse functions
\r
406 // * @author Jonathan
\r
409 // public static class FloatObjectifier extends NullLifeCycle implements Objectifier<Float> {
\r
410 // /* (non-Javadoc)
\r
411 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
413 // public Float objectify(Env env, String input) throws APIException {
\r
415 // return new Float(input);
\r
416 // } catch (Exception e) {
\r
417 // APIException ae = new APIException("Cannot create a \"Float\" from [" + input + ']');
\r
418 // ae.initCause(e);
\r
423 // /* (non-Javadoc)
\r
424 // * @see com.att.env.Objectifier#newObject()
\r
426 // public Float newInstance() throws APIException {
\r
432 // * DoubleStringifier
\r
434 // * Support the Native Double.. use Double parse functions
\r
436 // * @author Jonathan
\r
439 // public static class DoubleStringifier extends NullLifeCycle implements Stringifier<Double> {
\r
440 // /* (non-Javadoc)
\r
441 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
443 // public String stringify(Env env, Double input) throws APIException {
\r
444 // return input.toString();
\r
449 // * DoubleObjectifier
\r
451 // * Support the Native Double.. use Double parse functions
\r
453 // * @author Jonathan
\r
456 // public static class DoubleObjectifier extends NullLifeCycle implements Objectifier<Double> {
\r
457 // /* (non-Javadoc)
\r
458 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
460 // public Double objectify(Env env, String input) throws APIException {
\r
462 // return new Double(input);
\r
463 // } catch (Exception e) {
\r
464 // APIException ae = new APIException("Cannot create a \"Double\" from [" + input + ']');
\r
465 // ae.initCause(e);
\r
470 // /* (non-Javadoc)
\r
471 // * @see com.att.env.Objectifier#newObject()
\r
473 // public Double newInstance() throws APIException {
\r