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.Collections;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import org.jaxen.JaxenException;
26  import org.jdom2.Element;
27  import org.mycore.common.MCRException;
28  import org.mycore.common.xml.MCRNodeBuilder;
29  
30  import bibtex.dom.BibtexAbstractValue;
31  import bibtex.dom.BibtexEntry;
32  
33  /**
34   * Transforms a single BibTeX field to a MODS element.
35   *
36   * @author Frank L\u00FCtzenkirchen
37   */
38  class MCRFieldTransformer {
39  
40      static final String AS_NEW_ELEMENT = "[999]";
41  
42      protected String field;
43  
44      MCRFieldTransformer(String field) {
45          this.field = field;
46      }
47  
48      String getField() {
49          return field;
50      }
51  
52      /** Converts german umlauts and other special LaTeX characters */
53      protected String normalizeValue(String value) {
54          return value.replaceAll("\\s+", " ").trim().replace("{\\\"a}", "\u00e4").replace("{\\\"o}", "\u00f6")
55              .replace("{\\\"u}", "\u00fc").replace("{\\\"A}", "\u00c4").replace("{\\\"O}", "\u00d6")
56              .replace("{\\\"U}", "\u00dc").replace("{\\ss}", "\u00df").replace("\\\"a", "\u00e4")
57              .replace("\\\"o", "\u00f6").replace("\\\"u", "\u00fc").replace("\\\"A", "\u00c4")
58              .replace("\\\"O", "\u00d6").replace("\\\"U", "\u00dc").replace("{\\'a}", "\u00e1")
59              .replace("{\\'e}", "\u00e9").replace("{\\'i}", "\u00ed").replace("{\\'o}", "\u00f3")
60              .replace("{\\'u}", "\u00fa").replace("{\\`a}", "\u00e0").replace("{\\`e}", "\u00e8")
61              .replace("{\\`i}", "\u00ec").replace("{\\`o}", "\u00f2").replace("{\\`u}", "\u00f9")
62              .replace("{\\'\\i}", "\u00ed").replace("{\\`\\i}", "\u00ec").replace("{", "").replace("}", "")
63              .replace("---", "-").replace("--", "-");
64      }
65  
66      void transformField(BibtexEntry entry, Element parent) {
67          for (BibtexAbstractValue value : getFieldValues(entry, field)) {
68              buildField(value, parent);
69          }
70      }
71  
72      private List<BibtexAbstractValue> getFieldValues(BibtexEntry entry, String field) {
73          List<BibtexAbstractValue> fieldValues = entry.getFieldValuesAsList(field);
74          if (fieldValues == null) {
75              fieldValues = entry.getFieldValuesAsList(field.toUpperCase(Locale.ROOT));
76          }
77          return fieldValues == null ? Collections.emptyList() : fieldValues;
78      }
79  
80      void buildField(BibtexAbstractValue value, Element parent) {
81          String type = value.getClass().getSimpleName();
82          MCRMessageLogger.logMessage("Field " + field + " returns unsupported abstract value of type " + type, parent);
83      }
84  
85      static Element buildElement(String xPath, String content, Element parent) {
86          try {
87              return new MCRNodeBuilder().buildElement(xPath, content, parent);
88          } catch (JaxenException ex) {
89              throw new MCRException("Unable to build field " + xPath, ex);
90          }
91      }
92  }