View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.validate;
17  
18  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
19  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
20  
21  import java.util.List;
22  
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
24  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlotList;
25  
26  /**
27   * Performs a validation of a slot value.
28   * @author Jens Riemschneider
29   */
30  public class SlotValueValidation implements RegistryObjectValidator {
31      private final String slotName;
32      private final ValueValidator validator;
33      private final int min;
34      private final int max;
35  
36      /**
37       * Constructs the validation.
38       * @param slotName
39       *          the name of the slot.
40       * @param validator
41       *          the validator to use.
42       * @param min
43       *          the minimum number of values that the slot must have.
44       * @param max
45       *          the maximum number of values that the slot must have.
46       */
47      public SlotValueValidation(String slotName, ValueValidator validator, int min, int max) {
48          this.slotName = slotName;
49          this.validator = validator;
50          this.min = min;
51          this.max = max;
52      }
53  
54      /**
55       * Constructs the validation.
56       * @param slotName
57       *          the name of the slot.
58       * @param validator
59       *          the validator to use.
60       */
61      public SlotValueValidation(String slotName, ValueValidator validator) {
62          this.slotName = slotName;
63          this.validator = validator;
64          min = 1;
65          max = 1;
66      }
67  
68      @Override
69      public void validate(EbXMLRegistryObject obj) throws XDSMetaDataException {
70          validate((EbXMLSlotList)obj);
71      }
72  
73      /**
74       * Validates the given slot list.
75       * @param slotList
76       *          the slot list.
77       * @throws XDSMetaDataException
78       *          if the validation failed.
79       */
80      public void validate(EbXMLSlotList slotList) throws XDSMetaDataException {
81          List<String> slotValues = slotList.getSlotValues(slotName);
82          metaDataAssert(slotValues.size() >= min && slotValues.size() <= max,
83                  WRONG_NUMBER_OF_SLOT_VALUES, slotName, min, max, slotValues.size());
84  
85          for (String value : slotValues) {
86              metaDataAssert(value != null, EMPTY_SLOT_VALUE, slotName);            
87              validator.validate(value);
88          }
89      }
90  }