View Javadoc
1   /*
2    * Copyright 2011 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.HOME_COMMUNITY_ID_MUST_BE_SPECIFIED;
19  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_OID;
20  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
21  
22  /**
23   * Validates a homeCommunityId attribute.
24   * @author Dmytro Rud
25   */
26  public class HomeCommunityIdValidator implements ValueValidator {
27      private final boolean required;
28  
29      /**
30       * Constructor.
31       * @param required
32       *      <code>true</code> when an exception should be thrown when
33       *      the homeCommunityId attribute is not present.
34       */
35      public HomeCommunityIdValidator(boolean required) {
36          this.required = required;
37      }
38  
39      @Override
40      public void validate(String homeCommunityId) throws XDSMetaDataException {
41          if (required) {
42              metaDataAssert((homeCommunityId != null) && (! homeCommunityId.isEmpty()),
43                      HOME_COMMUNITY_ID_MUST_BE_SPECIFIED);
44          }
45          if (homeCommunityId != null) {
46              metaDataAssert(homeCommunityId.startsWith("urn:oid:"), INVALID_OID, homeCommunityId);
47              new OIDValidator().validate(homeCommunityId.substring(8));
48          }
49      }
50  }