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.bibtex;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.jdom2.Element;
26  
27  import bibtex.dom.BibtexAbstractValue;
28  import bibtex.dom.BibtexEntry;
29  import bibtex.dom.BibtexString;
30  
31  /**
32   * Transforms any BibTeX field that can not be mapped to MODS to a mods:extension/field element.
33   *
34   * @author Frank L\u00FCtzenkirchen
35   */
36  class MCRUnsupportedFieldTransformer extends MCRFieldTransformer {
37  
38      Set<String> supportedFields = new HashSet<>();
39  
40      MCRUnsupportedFieldTransformer(Collection<MCRFieldTransformer> supportedTransformers) {
41          super("*");
42          determineSupportedFields(supportedTransformers);
43      }
44  
45      private void determineSupportedFields(Collection<MCRFieldTransformer> supportedTransformers) {
46          for (MCRFieldTransformer transformer : supportedTransformers) {
47              supportedFields.add(transformer.getField());
48          }
49      }
50  
51      private boolean isUnsupported(String field) {
52          return !supportedFields.contains(field);
53      }
54  
55      void transformField(BibtexEntry entry, Element parent) {
56          for (String field : (entry.getFields().keySet())) {
57              if (isUnsupported(field)) {
58                  this.field = field;
59                  super.transformField(entry, parent);
60              }
61          }
62      }
63  
64      void buildField(BibtexAbstractValue value, Element parent) {
65          MCRMessageLogger.logMessage("Field " + field + " is unsupported: " + value.toString().replaceAll("\\s+", " "));
66          String xPath = "mods:extension/field[@name='" + field + "']" + MCRFieldTransformer.AS_NEW_ELEMENT;
67          String content = ((BibtexString) value).getContent();
68          content = normalizeValue(content);
69          buildElement(xPath, content, parent);
70      }
71  }