View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.model.data;
28  
29  import com.rapiddweller.common.LocaleUtil;
30  import com.rapiddweller.common.operation.FirstNonNullSelector;
31  
32  import java.util.Locale;
33  
34  /**
35   * Describes a type.<br/><br/>
36   * Created: 03.03.2008 08:37:30
37   *
38   * @author Volker Bergmann
39   * @since 0.5.0
40   */
41  public abstract class TypeDescriptor extends FeatureDescriptor {
42  
43    /**
44     * The constant VALIDATOR.
45     */
46  
47    // constraint names
48  
49    public static final String VALIDATOR = "validator";
50    /**
51     * The constant FILTER.
52     */
53    public static final String FILTER = "filter";
54    /**
55     * The constant CONDITION.
56     */
57    public static final String CONDITION = "condition";
58  
59    /**
60     * The constant GENERATOR.
61     */
62  
63    // config names
64  
65    public static final String GENERATOR = "generator";
66    /**
67     * The constant CONVERTER.
68     */
69    public static final String CONVERTER = "converter";
70    /**
71     * The constant PATTERN.
72     */
73    public static final String PATTERN = "pattern";
74    /**
75     * The constant SCRIPT.
76     */
77    public static final String SCRIPT = "script";
78  
79    /**
80     * The constant SOURCE.
81     */
82    public static final String SOURCE = "source";
83    /**
84     * The constant FORMAT.
85     */
86    public static final String FORMAT = "format";
87    /**
88     * The constant ROW_BASED.
89     */
90    public static final String ROW_BASED = "rowBased";
91    /**
92     * The constant SEGMENT.
93     */
94    public static final String SEGMENT = "segment";
95    /**
96     * The constant OFFSET.
97     */
98    public static final String OFFSET = "offset";
99    /**
100    * The constant SELECTOR.
101    */
102   public static final String SELECTOR = "selector";
103   /**
104    * The constant SUB_SELECTOR.
105    */
106   public static final String SUB_SELECTOR = "subSelector";
107   /**
108    * The constant ENCODING.
109    */
110   public static final String ENCODING = "encoding";
111   /**
112    * The constant SEPARATOR.
113    */
114   public static final String SEPARATOR = "separator";
115   /**
116    * The constant EMPTY_MARKER.
117    */
118   public static final String EMPTY_MARKER = "emptyMarker";
119   /**
120    * The constant NULL_MARKER.
121    */
122   public static final String NULL_MARKER = "nullMarker";
123 
124   /**
125    * The constant CYCLIC.
126    */
127   public static final String CYCLIC = "cyclic";
128   /**
129    * The constant SCOPE.
130    */
131   public static final String SCOPE = "scope";
132 
133   /**
134    * The constant LOCALE.
135    */
136   public static final String LOCALE = "locale";
137   /**
138    * The constant DATASET.
139    */
140   public static final String DATASET = "dataset";
141   /**
142    * The constant NESTING.
143    */
144   public static final String NESTING = "nesting";
145 
146   /**
147    * The constant DISTRIBUTION.
148    */
149   public static final String DISTRIBUTION = "distribution";
150 
151   // attributes ------------------------------------------------------------------------------------------------------
152 
153   /**
154    * The Parent name.
155    */
156   protected String parentName;
157   /**
158    * The Parent.
159    */
160   protected TypeDescriptor parent;
161 
162   // constructors ----------------------------------------------------------------------------------------------------
163 
164   /**
165    * Instantiates a new Type descriptor.
166    *
167    * @param name     the name
168    * @param provider the provider
169    * @param parent   the parent
170    */
171   public TypeDescriptor(String name, DescriptorProvider provider,
172                         TypeDescriptor parent) {
173     this(name, provider, (parent != null ? parent.getName() : null));
174     this.parent = parent;
175   }
176 
177   /**
178    * Instantiates a new Type descriptor.
179    *
180    * @param name       the name
181    * @param provider   the provider
182    * @param parentName the parent name
183    */
184   public TypeDescriptor(String name, DescriptorProvider provider,
185                         String parentName) {
186     super(name, provider);
187     this.parentName = parentName;
188     init();
189   }
190 
191   /**
192    * Init.
193    */
194   protected void init() {
195     // constraints
196     addConstraint(VALIDATOR, String.class, new FirstNonNullSelector<>());
197     addConstraint(FILTER, String.class, new FirstNonNullSelector<>());
198     addConstraint(CONDITION, String.class, new FirstNonNullSelector<>());
199 
200     // config
201     addConfig(GENERATOR, String.class);
202     addConfig(CONVERTER, String.class);
203     addConfig(PATTERN, String.class);
204     addConfig(SCRIPT, String.class);
205 
206     addConfig(SOURCE, String.class);
207     addConfig(FORMAT, Format.class);
208     addConfig(ROW_BASED, Boolean.class);
209     addConfig(SEGMENT, String.class);
210     addConfig(OFFSET, Integer.class);
211     addConfig(SELECTOR, String.class);
212     addConfig(SUB_SELECTOR, String.class);
213     addConfig(SEPARATOR, String.class);
214     addConfig(EMPTY_MARKER, String.class);
215     addConfig(NULL_MARKER, String.class);
216     addConfig(ENCODING, String.class);
217     addConfig(SCOPE, String.class);
218     addConfig(CYCLIC, Boolean.class);
219     // i18n config
220     addConfig(LOCALE, Locale.class);
221     addConfig(DATASET, String.class);
222     addConfig(NESTING, String.class);
223     // distribution
224     addConfig(DISTRIBUTION, String.class);
225   }
226 
227   // properties ------------------------------------------------------------------------------------------------------
228 
229   /**
230    * Gets parent name.
231    *
232    * @return the parent name
233    */
234   public String getParentName() {
235     return parentName;
236   }
237 
238   /**
239    * Sets parent name.
240    *
241    * @param parentName the parent name
242    */
243   public void setParentName(String parentName) {
244     this.parentName = parentName;
245   }
246 
247   /**
248    * Is row based boolean.
249    *
250    * @return the boolean
251    */
252   public Boolean isRowBased() {
253     return (Boolean) getDetailValue(ROW_BASED);
254   }
255 
256   /**
257    * Sets row based.
258    *
259    * @param rowBased the row based
260    */
261   public void setRowBased(Boolean rowBased) {
262     setDetailValue(ROW_BASED, rowBased);
263   }
264 
265   /**
266    * Gets validator.
267    *
268    * @return the validator
269    */
270   public String getValidator() {
271     return (String) getDetailValue(VALIDATOR);
272   }
273 
274   /**
275    * Sets validator.
276    *
277    * @param filter the filter
278    */
279   public void setValidator(String filter) {
280     setDetailValue(VALIDATOR, filter);
281   }
282 
283   /**
284    * Gets filter.
285    *
286    * @return the filter
287    */
288   public String getFilter() {
289     return (String) getDetailValue(FILTER);
290   }
291 
292   /**
293    * Sets filter.
294    *
295    * @param filter the filter
296    */
297   public void setFilter(String filter) {
298     setDetailValue(FILTER, filter);
299   }
300 
301   /**
302    * Gets condition.
303    *
304    * @return the condition
305    */
306   public String getCondition() {
307     return (String) getDetailValue(CONDITION);
308   }
309 
310   /**
311    * Sets condition.
312    *
313    * @param condition the condition
314    */
315   public void setCondition(String condition) {
316     setDetailValue(CONDITION, condition);
317   }
318 
319   /**
320    * Gets generator.
321    *
322    * @return the generator
323    */
324   public String getGenerator() {
325     return (String) getDetailValue(GENERATOR);
326   }
327 
328   /**
329    * Sets generator.
330    *
331    * @param generatorName the generator name
332    */
333   public void setGenerator(String generatorName) {
334     setDetailValue(GENERATOR, generatorName);
335   }
336 
337   /**
338    * Gets converter.
339    *
340    * @return the converter
341    */
342   public String getConverter() {
343     return (String) getDetailValue(CONVERTER);
344   }
345 
346   /**
347    * Sets converter.
348    *
349    * @param converter the converter
350    */
351   public void setConverter(String converter) {
352     setDetailValue(CONVERTER, converter);
353   }
354 
355   /**
356    * Gets pattern.
357    *
358    * @return the pattern
359    */
360   public String getPattern() {
361     return (String) getDetailValue(PATTERN);
362   }
363 
364   /**
365    * Sets pattern.
366    *
367    * @param pattern the pattern
368    */
369   public void setPattern(String pattern) {
370     setDetailValue(PATTERN, pattern);
371   }
372 
373   /**
374    * Gets script.
375    *
376    * @return the script
377    */
378   public String getScript() {
379     return (String) getDetailValue(SCRIPT);
380   }
381 
382   /**
383    * Sets script.
384    *
385    * @param script the script
386    */
387   public void setScript(String script) {
388     setDetailValue(SCRIPT, script);
389   }
390 
391   /**
392    * Gets source.
393    *
394    * @return the source
395    */
396   public String getSource() {
397     return (String) getDetailValue(SOURCE);
398   }
399 
400   /**
401    * Sets source.
402    *
403    * @param source the source
404    */
405   public void setSource(String source) {
406     setDetailValue(SOURCE, source);
407   }
408 
409   /**
410    * Gets format.
411    *
412    * @return the format
413    */
414   public Format getFormat() {
415     return (Format) getDetailValue(FORMAT);
416   }
417 
418   /**
419    * Sets format.
420    *
421    * @param format the format
422    */
423   public void setFormat(Format format) {
424     setDetailValue(FORMAT, format);
425   }
426 
427   /**
428    * Gets segment.
429    *
430    * @return the segment
431    */
432   public String getSegment() {
433     return (String) getDetailValue(SEGMENT);
434   }
435 
436   /**
437    * Sets segment.
438    *
439    * @param segment the segment
440    */
441   public void setSegment(String segment) {
442     setDetailValue(SEGMENT, segment);
443   }
444 
445   /**
446    * Gets offset.
447    *
448    * @return the offset
449    */
450   public Integer getOffset() {
451     return (Integer) getDetailValue(OFFSET);
452   }
453 
454   /**
455    * Sets offset.
456    *
457    * @param offset the offset
458    */
459   public void setOffset(Integer offset) {
460     setDetailValue(OFFSET, offset);
461   }
462 
463   /**
464    * Gets selector.
465    *
466    * @return the selector
467    */
468   public String getSelector() {
469     return (String) getDetailValue(SELECTOR);
470   }
471 
472   /**
473    * Sets selector.
474    *
475    * @param selector the selector
476    */
477   public void setSelector(String selector) {
478     setDetailValue(SELECTOR, selector);
479   }
480 
481   /**
482    * Gets sub selector.
483    *
484    * @return the sub selector
485    */
486   public String getSubSelector() {
487     return (String) getDetailValue(SUB_SELECTOR);
488   }
489 
490   /**
491    * Sets sub selector.
492    *
493    * @param selector the selector
494    */
495   public void setSubSelector(String selector) {
496     setDetailValue(SUB_SELECTOR, selector);
497   }
498 
499   /**
500    * Gets separator.
501    *
502    * @return the separator
503    */
504   public String getSeparator() {
505     return (String) getDetailValue(SEPARATOR);
506   }
507 
508   /**
509    * Sets separator.
510    *
511    * @param separator the separator
512    */
513   public void setSeparator(String separator) {
514     setDetailValue(SEPARATOR, separator);
515   }
516 
517   /**
518    * Gets empty marker.
519    *
520    * @return the empty marker
521    */
522   public String getEmptyMarker() {
523     return (String) getDetailValue(EMPTY_MARKER);
524   }
525 
526   /**
527    * Sets empty marker.
528    *
529    * @param emptyMarker the empty marker
530    */
531   public void setEmptyMarker(String emptyMarker) {
532     setDetailValue(EMPTY_MARKER, emptyMarker);
533   }
534 
535   /**
536    * Gets null marker.
537    *
538    * @return the null marker
539    */
540   public String getNullMarker() {
541     return (String) getDetailValue(NULL_MARKER);
542   }
543 
544   /**
545    * Sets null marker.
546    *
547    * @param nullMarker the null marker
548    */
549   public void setNullMarker(String nullMarker) {
550     setDetailValue(NULL_MARKER, nullMarker);
551   }
552 
553   /**
554    * Gets encoding.
555    *
556    * @return the encoding
557    */
558   public String getEncoding() {
559     return (String) getDetailValue(ENCODING);
560   }
561 
562   /**
563    * Sets encoding.
564    *
565    * @param encoding the encoding
566    */
567   public void setEncoding(String encoding) {
568     setDetailValue(ENCODING, encoding);
569   }
570 
571   /**
572    * Gets scope.
573    *
574    * @return the scope
575    */
576   public String getScope() {
577     return (String) getDetailValue(SCOPE);
578   }
579 
580   /**
581    * Sets scope.
582    *
583    * @param scope the scope
584    */
585   public void setScope(String scope) {
586     setDetailValue(SCOPE, scope);
587   }
588 
589   /**
590    * Is cyclic boolean.
591    *
592    * @return the boolean
593    */
594   public Boolean isCyclic() {
595     return (Boolean) getDetailValue(CYCLIC);
596   }
597 
598   /**
599    * Sets cyclic.
600    *
601    * @param cyclic the cyclic
602    */
603   public void setCyclic(boolean cyclic) {
604     setDetailValue(CYCLIC, cyclic);
605   }
606 
607   /**
608    * Gets dataset.
609    *
610    * @return the dataset
611    */
612   public String getDataset() {
613     return (String) getDetailValue(DATASET);
614   }
615 
616   /**
617    * Sets dataset.
618    *
619    * @param dataset the dataset
620    */
621   public void setDataset(String dataset) {
622     setDetailValue(DATASET, dataset);
623   }
624 
625   /**
626    * Gets nesting.
627    *
628    * @return the nesting
629    */
630   public String getNesting() {
631     return (String) getDetailValue(NESTING);
632   }
633 
634   /**
635    * Sets nesting.
636    *
637    * @param nesting the nesting
638    */
639   public void setNesting(String nesting) {
640     setDetailValue(NESTING, nesting);
641   }
642 
643   /**
644    * Gets locale.
645    *
646    * @return the locale
647    */
648   public Locale getLocale() {
649     return (Locale) getDetailValue(LOCALE);
650   }
651 
652   /**
653    * Sets locale id.
654    *
655    * @param localeId the locale id
656    */
657   public void setLocaleId(String localeId) {
658     setDetailValue(LOCALE, LocaleUtil.getLocale(localeId));
659   }
660 
661   /**
662    * Gets distribution.
663    *
664    * @return the distribution
665    */
666   public String getDistribution() {
667     return (String) getDetailValue(DISTRIBUTION);
668   }
669 
670   /**
671    * Sets distribution.
672    *
673    * @param distribution the distribution
674    */
675   public void setDistribution(String distribution) {
676     setDetailValue(DISTRIBUTION, distribution);
677   }
678 
679   // literal construction helpers ------------------------------------------------------------------------------------
680 
681   /**
682    * With source type descriptor.
683    *
684    * @param source the source
685    * @return the type descriptor
686    */
687   public TypeDescriptor withSource(String source) {
688     setSource(source);
689     return this;
690   }
691 
692   /**
693    * With separator type descriptor.
694    *
695    * @param separator the separator
696    * @return the type descriptor
697    */
698   public TypeDescriptor withSeparator(String separator) {
699     setSeparator(separator);
700     return this;
701   }
702 
703   /**
704    * With generator type descriptor.
705    *
706    * @param generator the generator
707    * @return the type descriptor
708    */
709   public TypeDescriptor withGenerator(String generator) {
710     setGenerator(generator);
711     return this;
712   }
713 
714   // generic functionality -------------------------------------------------------------------------------------------
715 
716   /**
717    * Gets parent.
718    *
719    * @return the parent
720    */
721   public TypeDescriptor getParent() {
722     if (parent != null) {
723       return parent;
724     }
725     if (parentName == null) {
726       return null;
727     }
728     // TODO v0.7.1 the following is a workaround for name conflicts with types of same name in different name spaces, e.g. xs:string <-> ben.string
729     TypeDescriptor candidate = getDataModel().getTypeDescriptor(parentName);
730     if (candidate != this) {
731       parent = candidate;
732     }
733     return parent;
734   }
735 
736   /**
737    * Sets parent.
738    *
739    * @param parent the parent
740    */
741   public void setParent(TypeDescriptor parent) {
742     this.parent = parent;
743   }
744 
745 }