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.mods.enrichment;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  
24  import org.jdom2.Element;
25  import org.jdom2.filter.Filters;
26  import org.jdom2.xpath.XPathExpression;
27  import org.jdom2.xpath.XPathFactory;
28  import org.mycore.common.MCRConstants;
29  
30  /**
31   * Represents a type of publication identifier, like DOI or ISBN.
32   * Each type has a corresponding XPath expression
33   * used to locate or build identifiers of this type within MODS.
34   *
35   * If the corresponding XPath representation is
36   * mods:identifier[@type='TYPE'], no explicit configuration is needed.
37   *
38   * Otherwise, the XPath must be configured, e.g.
39   * MCR.MODS.EnrichmentResolver.IdentifierType.shelfmark=mods:location/mods:shelfLocator
40   *
41   * @author Frank L\u00FCtzenkirchen
42   */
43  public class MCRIdentifierType {
44  
45      private String typeID;
46  
47      private String xPath;
48  
49      private XPathExpression<Element> xPathExpr;
50  
51      MCRIdentifierType(String typeID, String xPath) {
52          this.typeID = typeID;
53          this.xPath = xPath;
54          this.xPathExpr = XPathFactory.instance().compile(xPath, Filters.element(), null,
55              MCRConstants.getStandardNamespaces());
56      }
57  
58      public String getTypeID() {
59          return typeID;
60      }
61  
62      public String getXPath() {
63          return xPath;
64      }
65  
66      /** Returns all identifiers of this type found in the given MODS element. */
67      List<MCRIdentifier> getIdentifiers(Element mods) {
68          return xPathExpr.evaluate(mods).stream()
69              .map(e -> new MCRIdentifier(this, e.getTextTrim()))
70              .collect(Collectors.toList());
71      }
72  
73      @Override
74      public String toString() {
75          return "identifier type " + typeID;
76      }
77  }