
001package org.hl7.fhir.r5.utils; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.FileNotFoundException; 035import java.io.IOException; 036import java.util.HashMap; 037import java.util.Map; 038 039import javax.xml.parsers.DocumentBuilder; 040import javax.xml.parsers.DocumentBuilderFactory; 041import javax.xml.parsers.ParserConfigurationException; 042 043import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage; 044import org.hl7.fhir.utilities.filesystem.CSFileInputStream; 045import org.hl7.fhir.utilities.xml.XMLUtil; 046import org.w3c.dom.Document; 047import org.w3c.dom.Element; 048import org.xml.sax.SAXException; 049 050@MarkedToMoveToAdjunctPackage 051public class Translations { 052 053 private String[] lang; 054 private Map<String, Element> messages = new HashMap<String, Element>(); 055 056 /** 057 * Set a default language to use 058 * 059 * @param lang 060 */ 061 public void setLang(String lang) { 062 this.lang = lang.split("[.;]"); 063 } 064 065 /** 066 * Load from the XML translations file maintained by the FHIR project 067 * 068 * @param filename 069 * @throws IOException 070 * @throws SAXException 071 * @throws FileNotFoundException 072 * @throws ParserConfigurationException 073 * @throws Exception 074 */ 075 public void load(String filename) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 076 DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory(); 077 DocumentBuilder builder = factory.newDocumentBuilder(); 078 loadMessages(builder.parse(new CSFileInputStream(filename))); 079 } 080 081 private void loadMessages(Document doc) { 082 // TODO Auto-generated method stub 083 Element element = XMLUtil.getFirstChild(doc.getDocumentElement()); 084 while (element != null) { 085 messages.put(element.getAttribute("id"), element); 086 element = XMLUtil.getNextSibling(element); 087 } 088 } 089 090 public boolean hasTranslation(String id) { 091 return messages.containsKey(id); 092 } 093 094 /** 095 * use configured language 096 * 097 * @param id - the id of the message to retrieve 098 * @param defaultMsg - string to use if the message is not defined or a language match is not found (if null, then will default to english) 099 * @return the message 100 */ 101 public String getMessage(String id, String defaultMsg) { 102 return getMessage(id, lang, defaultMsg); 103 } 104 105 /** 106 * return the message in a specified language 107 * 108 * @param id - the id of the message to retrieve 109 * @param lang - a language string from a browser 110 * @param defaultMsg - string to use if the message is not defined or a language match is not found (if null, then will default to the english message) 111 * @return the message 112 */ 113 public String getMessage(String id, String lang, String defaultMsg) { 114 return getMessage(id, lang.split("[.;]"), defaultMsg); 115 } 116 117 private String getMessage(String id, String[] lang, String defaultMsg) { 118 Element msg = messages.get(id); 119 if (msg == null) 120 return defaultMsg; 121 for (String l : lang) { 122 String res = getByLang(msg, l); 123 if (res != null) 124 return res; 125 } 126 if (defaultMsg == null) { 127 String res = getByLang(msg, "en"); 128 if (res != null) 129 return res; 130 } 131 return defaultMsg; 132 } 133 134 private String getByLang(Element msg, String lang) { 135 Element c = XMLUtil.getFirstChild(msg); 136 while (c != null) { 137 if (c.getAttribute("lang").equals(lang)) 138 return c.getTextContent(); 139 c = XMLUtil.getNextSibling(c); 140 } 141 return null; 142 } 143 144 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 145 public String getLangDesc(String s) { 146 if (s.equals("en")) 147 return "English"; 148 if (s.equals("nl")) 149 return "Nederlands (Dutch)"; 150 if (s.equals("de")) 151 return "Deutsch (German)"; 152 if (s.equals("ru")) 153 return "\u0440\u0443\u0301\u0441\u0441\u043a\u0438\u0439 (Russian)"; 154 return "\"" + s + "\""; 155 } 156 157 158}