1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.inno.env;
\r
25 import java.io.File;
\r
26 import java.io.FileInputStream;
\r
27 import java.io.FileNotFoundException;
\r
28 import java.io.IOException;
\r
30 import javax.xml.XMLConstants;
\r
31 import javax.xml.bind.annotation.XmlRootElement;
\r
32 import javax.xml.bind.annotation.XmlSchema;
\r
33 import javax.xml.namespace.QName;
\r
34 import javax.xml.transform.Source;
\r
35 import javax.xml.transform.stream.StreamSource;
\r
36 import javax.xml.validation.Schema;
\r
37 import javax.xml.validation.SchemaFactory;
\r
39 import org.onap.aaf.inno.env.impl.EnvFactory;
\r
40 import org.xml.sax.SAXException;
\r
45 * DataFactory Constructor will create the Stringifiers and Objectifiers necessary
\r
46 * by Type and store the Class of the Type for quick creation of Data Objects
\r
47 * with reused (and thread safe) components
\r
49 * Native Types are included.
\r
50 * Those types covered by Env Implementation are covered dynamically.
\r
51 * Types outside of Env mechanism can be added with "add" function
\r
56 public class BaseDataFactory {
\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(XMLConstants.W3C_XML_SCHEMA_NS_URI) { // 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
129 // public static class StringStringifier extends NullLifeCycle implements Stringifier<String> {
\r
130 // /* (non-Javadoc)
\r
131 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
133 // public String stringify(Env env, String input) throws APIException {
\r
139 // * StringObjectifier
\r
141 // * Support the Native Type String.. just return it back
\r
145 // public static class StringObjectifier extends NullLifeCycle implements Objectifier<String> {
\r
146 // /* (non-Javadoc)
\r
147 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
149 // public String objectify(Env env, String input) throws APIException {
\r
153 // /* (non-Javadoc)
\r
154 // * @see com.att.env.Objectifier#newObject()
\r
156 // public String newInstance() throws APIException {
\r
162 // * LongStringifier
\r
164 // * Support the Native Type Long.. use Long parse functions
\r
168 // public static class LongStringifier extends NullLifeCycle implements Stringifier<Long> {
\r
169 // public String stringify(Env env, Long input) throws APIException {
\r
170 // return input.toString();
\r
175 // * LongObjectifier
\r
177 // * Support the Native Type Long.. use Long parse functions
\r
181 // public static class LongObjectifier extends NullLifeCycle implements Objectifier<Long> {
\r
182 // /* (non-Javadoc)
\r
183 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
185 // public Long objectify(Env env, String input) throws APIException {
\r
187 // return new Long(input);
\r
188 // } catch (Exception e) {
\r
189 // APIException ae = new APIException("Cannot create a \"Long\" from [" + input + ']');
\r
190 // ae.initCause(e);
\r
195 // /* (non-Javadoc)
\r
196 // * @see com.att.env.Objectifier#newObject()
\r
198 // public Long newInstance() throws APIException {
\r
204 // * IntegerStringifier
\r
206 // * Support the Native Integer.. use Integer parse functions
\r
210 // public static class IntegerStringifier extends NullLifeCycle implements Stringifier<Integer> {
\r
211 // /* (non-Javadoc)
\r
212 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
214 // public String stringify(Env env, Integer input) throws APIException {
\r
215 // return input.toString();
\r
220 // * IntegerObjectifier
\r
222 // * Support the Native Integer.. use Integer parse functions
\r
226 // public static class IntegerObjectifier extends NullLifeCycle implements Objectifier<Integer> {
\r
227 // /* (non-Javadoc)
\r
228 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
230 // public Integer objectify(Env env, String input) throws APIException {
\r
232 // return new Integer(input);
\r
233 // } catch (Exception e) {
\r
234 // APIException ae = new APIException("Cannot create a \"Integer\" from [" + input + ']');
\r
235 // ae.initCause(e);
\r
240 // /* (non-Javadoc)
\r
241 // * @see com.att.env.Objectifier#newObject()
\r
243 // public Integer newInstance() throws APIException {
\r
249 // * ShortStringifier
\r
251 // * Support the Native Short.. use Short parse functions
\r
255 // public static class ShortStringifier extends NullLifeCycle implements Stringifier<Short> {
\r
256 // public String stringify(Env env, Short input) throws APIException {
\r
257 // return input.toString();
\r
262 // * ShortObjectifier
\r
264 // * Support the Native Short.. use Short parse functions
\r
268 // public static class ShortObjectifier extends NullLifeCycle implements Objectifier<Short> {
\r
269 // public Short objectify(Env env, String input) throws APIException {
\r
271 // return new Short(input);
\r
272 // } catch (Exception e) {
\r
273 // APIException ae = new APIException("Cannot create a \"Short\" from [" + input + ']');
\r
274 // ae.initCause(e);
\r
279 // public Short newInstance() throws APIException {
\r
285 // * ByteStringifier
\r
287 // * Support the Native Byte.. use Byte parse functions
\r
291 // public static class ByteStringifier extends NullLifeCycle implements Stringifier<Byte> {
\r
292 // /* (non-Javadoc)
\r
293 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
295 // public String stringify(Env env, Byte input) throws APIException {
\r
296 // return input.toString();
\r
301 // * ByteObjectifier
\r
303 // * Support the Native Byte.. use Byte parse functions
\r
307 // public static class ByteObjectifier extends NullLifeCycle implements Objectifier<Byte> {
\r
308 // /* (non-Javadoc)
\r
309 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
311 // public Byte objectify(Env env, String input) throws APIException {
\r
313 // return new Byte(input);
\r
314 // } catch (Exception e) {
\r
315 // APIException ae = new APIException("Cannot create a \"Byte\" from [" + input + ']');
\r
316 // ae.initCause(e);
\r
321 // /* (non-Javadoc)
\r
322 // * @see com.att.env.Objectifier#newObject()
\r
324 // public Byte newInstance() throws APIException {
\r
330 // * CharacterStringifier
\r
332 // * Support the Native Character.. use Character parse functions
\r
336 // public static class CharacterStringifier extends NullLifeCycle implements Stringifier<Character> {
\r
337 // /* (non-Javadoc)
\r
338 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
340 // public String stringify(Env env, Character input) throws APIException {
\r
341 // return input.toString();
\r
346 // * CharacterObjectifier
\r
348 // * Support the Native Character.. use Character parse functions
\r
352 // public static class CharacterObjectifier extends NullLifeCycle implements Objectifier<Character> {
\r
353 // /* (non-Javadoc)
\r
354 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
356 // public Character objectify(Env env, String input) throws APIException {
\r
357 // int length = input.length();
\r
358 // if(length<1 || length>1) {
\r
359 // throw new APIException("String [" + input + "] does not represent a single Character");
\r
361 // return input.charAt(0);
\r
364 // /* (non-Javadoc)
\r
365 // * @see com.att.env.Objectifier#newObject()
\r
367 // public Character newInstance() throws APIException {
\r
373 // * FloatStringifier
\r
375 // * Support the Native Float.. use Float parse functions
\r
379 // public static class FloatStringifier extends NullLifeCycle implements Stringifier<Float> {
\r
380 // /* (non-Javadoc)
\r
381 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
383 // public String stringify(Env env, Float input) throws APIException {
\r
384 // return input.toString();
\r
389 // * FloatObjectifier
\r
391 // * Support the Native Float.. use Float parse functions
\r
395 // public static class FloatObjectifier extends NullLifeCycle implements Objectifier<Float> {
\r
396 // /* (non-Javadoc)
\r
397 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
399 // public Float objectify(Env env, String input) throws APIException {
\r
401 // return new Float(input);
\r
402 // } catch (Exception e) {
\r
403 // APIException ae = new APIException("Cannot create a \"Float\" from [" + input + ']');
\r
404 // ae.initCause(e);
\r
409 // /* (non-Javadoc)
\r
410 // * @see com.att.env.Objectifier#newObject()
\r
412 // public Float newInstance() throws APIException {
\r
418 // * DoubleStringifier
\r
420 // * Support the Native Double.. use Double parse functions
\r
423 // public static class DoubleStringifier extends NullLifeCycle implements Stringifier<Double> {
\r
424 // /* (non-Javadoc)
\r
425 // * @see com.att.env.Stringifier#stringify(com.att.env.Env, java.lang.Object)
\r
427 // public String stringify(Env env, Double input) throws APIException {
\r
428 // return input.toString();
\r
433 // * DoubleObjectifier
\r
435 // * Support the Native Double.. use Double parse functions
\r
439 // public static class DoubleObjectifier extends NullLifeCycle implements Objectifier<Double> {
\r
440 // /* (non-Javadoc)
\r
441 // * @see com.att.env.Objectifier#objectify(com.att.env.Env, java.lang.String)
\r
443 // public Double objectify(Env env, String input) throws APIException {
\r
445 // return new Double(input);
\r
446 // } catch (Exception e) {
\r
447 // APIException ae = new APIException("Cannot create a \"Double\" from [" + input + ']');
\r
448 // ae.initCause(e);
\r
453 // /* (non-Javadoc)
\r
454 // * @see com.att.env.Objectifier#newObject()
\r
456 // public Double newInstance() throws APIException {
\r