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.datamodel.language; 20 21 import org.jdom2.Element; 22 import org.jdom2.Namespace; 23 24 /** 25 * Helper class to map xml:lang and lang attributes in XML to MCRLanguage 26 * 27 * @author Frank L\u00FCtzenkirchen 28 */ 29 public class MCRLanguageXML { 30 31 /** 32 * Sets the lang attribute to the ISO 639-2 bibliographic code of the given language 33 */ 34 public static void setLangAttribute(MCRLanguage lang, Element element) { 35 String code = lang.getCode(MCRLanguageCodeType.biblCode); 36 if (code != null) { 37 element.setAttribute("lang", code); 38 } 39 } 40 41 /** 42 * Sets the xml:lang attribute to the ISO 639-1 code of the given language 43 */ 44 public static void setXMLLangAttribute(MCRLanguage lang, Element element) { 45 element.setAttribute("lang", lang.getCode(MCRLanguageCodeType.xmlCode), Namespace.XML_NAMESPACE); 46 } 47 48 /** 49 * Returns the language of the given XML element, by inspecting the xml:lang or lang attribute. 50 * If neither exists, the default language is returned. 51 */ 52 public static MCRLanguage getLanguage(Element element) { 53 String code = element.getAttributeValue("lang", Namespace.XML_NAMESPACE); 54 if ((code == null) || code.isEmpty()) { 55 code = element.getAttributeValue("lang"); 56 } 57 58 if ((code == null) || code.isEmpty()) { 59 return MCRLanguageFactory.instance().getDefaultLanguage(); 60 } else { 61 return MCRLanguageFactory.instance().getLanguage(code); 62 } 63 64 } 65 }