View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.importers.eag;
21  
22  import com.google.common.collect.ImmutableMap;
23  import com.google.common.collect.Lists;
24  import eu.ehri.project.definitions.Entities;
25  import eu.ehri.project.definitions.Ontology;
26  import eu.ehri.project.exceptions.ValidationError;
27  import eu.ehri.project.importers.base.ItemImporter;
28  import eu.ehri.project.importers.base.SaxXmlHandler;
29  import eu.ehri.project.importers.properties.XmlImportProperties;
30  import eu.ehri.project.importers.util.ImportHelpers;
31  import eu.ehri.project.models.MaintenanceEvent;
32  import eu.ehri.project.models.base.Entity;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.xml.sax.SAXException;
36  
37  import java.util.Map;
38  
39  /**
40   * Handler that reads EAG files. The resulting {@link Map}s should be imported by
41   * {@link EagImporter}.
42   */
43  public class EagHandler extends SaxXmlHandler {
44  
45      private final ImmutableMap<String, Class<? extends Entity>> possibleSubNodes = ImmutableMap.of(
46              Entities.MAINTENANCE_EVENT, MaintenanceEvent.class
47      );
48      private static final Logger logger = LoggerFactory.getLogger(EagHandler.class);
49  
50      public EagHandler(ItemImporter<Map<String, Object>, ?> importer) {
51          super(importer, new XmlImportProperties("eag.properties"));
52      }
53  
54      @Override
55      protected boolean needToCreateSubNode(String qName) {
56          return possibleSubNodes.containsKey(getMappedProperty(currentPath));
57      }
58  
59      @Override
60      public void endElement(String uri, String localName, String qName) throws SAXException {
61          //if a subnode is ended, add it to the super-supergraph
62          super.endElement(uri, localName, qName);
63  
64          if (needToCreateSubNode(qName)) {
65              logger.debug("endElement: {}", qName);
66  
67              logger.debug("just before popping: {} - {} - {}", depth, getMappedProperty(currentPath), qName);
68              Map<String, Object> currentGraph = currentGraphPath.pop();
69              putSubGraphInCurrentGraph(getMappedProperty(currentPath), currentGraph);
70              depth--;
71          }
72  
73          currentPath.pop();
74          //an EAG file consists of only 1 element, so if we're back at the root, we're done
75          if (currentPath.isEmpty()) {
76              try {
77                  logger.debug("depth close {} {}", depth, qName);
78                  //TODO: add any mandatory fields not yet there:
79                  if (!currentGraphPath.peek().containsKey(ImportHelpers.OBJECT_IDENTIFIER)) {
80                      logger.warn("no objectIdentifier found");
81                      putPropertyInCurrentGraph(ImportHelpers.OBJECT_IDENTIFIER, "id");
82                  }
83                  if (!currentGraphPath.peek().containsKey("typeOfEntity")) {
84                      putPropertyInCurrentGraph("typeOfEntity", "organisation");
85                  }
86                  if (!currentGraphPath.peek().containsKey(Ontology.NAME_KEY)) {
87                      logger.debug("no {} found", Ontology.NAME_KEY);
88                      putPropertyInCurrentGraph(Ontology.NAME_KEY, "title");
89                  }
90                  if (!currentGraphPath.peek().containsKey(Ontology.LANGUAGE_OF_DESCRIPTION)) {
91                      logger.debug("no {} found", Ontology.LANGUAGE_OF_DESCRIPTION);
92                      putPropertyInCurrentGraph(Ontology.LANGUAGE_OF_DESCRIPTION, "en");
93                  }
94                  if (!currentGraphPath.peek().containsKey("rulesAndConventions")) {
95                      logger.debug("no rulesAndConventions found");
96                      putPropertyInCurrentGraph("rulesAndConventions", "ISDIAH");
97                  }
98                  importer.importItem(currentGraphPath.pop(), Lists.<String>newArrayList());
99  
100             } catch (ValidationError ex) {
101                 logger.error(ex.getMessage());
102             }
103         }
104     }
105 }