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.apache.commons.lang3.Validate.noNullElements;
19  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
20  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.*;
21  
22  import java.util.List;
23  
24  
25  /**
26   * Validates a list of recipients containing organizations and/or persons
27   * and/or telecommunication addresses.
28   * @author Jens Riemschneider
29   */
30  public class RecipientListValidator implements ValueListValidator {
31      private final XCNValidator xcnValidator = new XCNValidator();
32      private final XONValidator xonValidator = new XONValidator();
33      
34      @Override
35      public void validate(List<String> values) throws XDSMetaDataException {
36          noNullElements(values, "values cannot contain null elements");
37  //        This check is disabled for compatibility with older versions.
38  //        metaDataAssert(!values.isEmpty(), RECIPIENT_LIST_EMPTY);
39          
40          for (String value : values) {
41              metaDataAssert(!value.isEmpty(), RECIPIENT_EMPTY);
42  
43              String[] parts = value.split("\\|", 4);
44              metaDataAssert((parts.length > 0) && (parts.length <= 3), INVALID_RECIPIENT, value);
45  
46              if (! parts[0].isEmpty()) {
47                  xonValidator.validate(parts[0]);
48              }
49  
50              if ((parts.length > 1) && (! parts[1].isEmpty())) {
51                  xcnValidator.validate(parts[1]);
52              }
53          }
54      }
55  }