View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.mets.iiif;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Objects;
25  import java.util.stream.Collectors;
26  
27  import org.jdom2.Element;
28  import org.jdom2.Text;
29  import org.jdom2.filter.Filters;
30  import org.jdom2.xpath.XPathExpression;
31  import org.jdom2.xpath.XPathFactory;
32  import org.mycore.common.MCRConstants;
33  import org.mycore.iiif.presentation.model.attributes.MCRIIIFMetadata;
34  
35  public class MCRMetsIIIFModsMetadataExtractor implements MCRMetsIIIFMetadataExtractor {
36  
37      @Override
38      public List<MCRIIIFMetadata> extractModsMetadata(Element xmlData) {
39          Map<String, String> elementLabelMap = new HashMap<>();
40  
41          elementLabelMap.put("title", "mods:mods/mods:titleInfo/mods:title/text()");
42          elementLabelMap.put("genre", "mods:mods/mods:genre/text()");
43          // TODO: add some more metadata
44  
45          return elementLabelMap.entrySet().stream().map(entry -> {
46              XPathExpression<Text> pathExpression = XPathFactory.instance().compile(entry.getValue(), Filters.text(),
47                  null, MCRConstants.MODS_NAMESPACE);
48              List<Text> texts = pathExpression.evaluate(xmlData);
49              if (texts.size() == 0) {
50                  return null;
51              }
52              return new MCRIIIFMetadata(entry.getKey(),
53                  texts.stream().map(Text::getText).collect(Collectors.joining(", ")));
54          }).filter(Objects::nonNull)
55              .collect(Collectors.toList());
56      }
57  
58  }