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