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.hl7v3;
17  
18  import lombok.Getter;
19  import org.openehealth.ipf.commons.xml.CombinedXmlValidationProfile;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * @author Dmytro Rud
27   */
28  public class Hl7v3ValidationProfile implements CombinedXmlValidationProfile {
29  
30      public static final String DEFAULT_XSD = "";
31      public static final String GAZELLE_PIXPDQV3_SCHEMATRON = "/schematron/gazelle-pixpdqv3.sch.xml";
32  
33      static class Row {
34          private static final String HL7V3_SCHEMAS_PATH = "/schema/HL7V3/NE2008/multicacheschemas/";
35  
36          @Getter final String rootElementName;
37          @Getter final String xsdPath;
38          @Getter final String schematronPath;
39          @Getter final String schematronPhase;
40  
41          Row(String rootElementName, String xsdPath, String schematronPath) {
42              this.rootElementName = rootElementName;
43              if (DEFAULT_XSD.equals(xsdPath)) {
44                  int pos1 = rootElementName.indexOf('_');
45                  int pos2 = rootElementName.indexOf('_', pos1 + 1);
46                  String documentName = (pos2 > 0) ? rootElementName.substring(0, pos2) : rootElementName;
47                  this.xsdPath = HL7V3_SCHEMAS_PATH + documentName + ".xsd";
48              } else {
49                  this.xsdPath = xsdPath.startsWith("/") ? xsdPath : HL7V3_SCHEMAS_PATH + xsdPath;
50              }
51              this.schematronPath = schematronPath;
52              this.schematronPhase = GAZELLE_PIXPDQV3_SCHEMATRON.equals(schematronPath) ? rootElementName : null;
53          }
54      }
55  
56  
57      private final Map<String, Row> map;
58  
59      public Hl7v3ValidationProfile(Row... rows) {
60          map = new HashMap<>(rows.length);
61          for (Row row : rows) {
62              map.put(row.rootElementName, row);
63          }
64      }
65  
66  
67      @Override
68      public boolean isValidRootElement(String rootElementName) {
69          return map.containsKey(rootElementName);
70      }
71  
72      @Override
73      public String getXsdPath(String rootElementName) {
74          return map.get(rootElementName).getXsdPath();
75      }
76  
77      @Override
78      public String getSchematronPath(String rootElementName) {
79          return map.get(rootElementName).getSchematronPath();
80      }
81  
82      @Override
83      public Map<String, Object> getCustomSchematronParameters(String rootElementName) {
84          String phase = map.get(rootElementName).getSchematronPhase();
85          return (phase != null) ? Collections.singletonMap("phase", phase) : null;
86      }
87  
88  }