
001package org.hl7.fhir.dstu2.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 032import java.io.FileNotFoundException; 033import java.io.IOException; 034import java.util.HashMap; 035import java.util.Map; 036 037import javax.xml.parsers.DocumentBuilder; 038import javax.xml.parsers.DocumentBuilderFactory; 039import javax.xml.parsers.ParserConfigurationException; 040 041import org.hl7.fhir.utilities.filesystem.CSFileInputStream; 042import org.hl7.fhir.utilities.xml.XMLUtil; 043import org.w3c.dom.Document; 044import org.w3c.dom.Element; 045import org.xml.sax.SAXException; 046 047@Deprecated 048public class Translations { 049 050 private String[] lang; 051 private Map<String, Element> messages = new HashMap<String, Element>(); 052 053 /** 054 * Set a default language to use 055 * 056 * @param lang 057 */ 058 public void setLang(String lang) { 059 this.lang = lang.split("[.;]"); 060 } 061 062 /** 063 * Load from the XML translations file maintained by the FHIR project 064 * 065 * @param filename 066 * @throws IOException 067 * @throws SAXException 068 * @throws FileNotFoundException 069 * @throws ParserConfigurationException 070 * @throws Exception 071 */ 072 public void load(String filename) 073 throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 074 DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory(); 075 DocumentBuilder builder = factory.newDocumentBuilder(); 076 loadMessages(builder.parse(new CSFileInputStream(filename))); 077 } 078 079 private void loadMessages(Document doc) { 080 // TODO Auto-generated method stub 081 Element element = XMLUtil.getFirstChild(doc.getDocumentElement()); 082 while (element != null) { 083 messages.put(element.getAttribute("id"), element); 084 element = XMLUtil.getNextSibling(element); 085 } 086 } 087 088 public boolean hasTranslation(String id) { 089 return messages.containsKey(id); 090 } 091 092 /** 093 * use configured language 094 * 095 * @param id - the id of the message to retrieve 096 * @param defaultMsg - string to use if the message is not defined or a language 097 * match is not found (if null, then will default to english) 098 * @return the message 099 */ 100 public String getMessage(String id, String defaultMsg) { 101 return getMessage(id, lang, defaultMsg); 102 } 103 104 /** 105 * return the message in a specified language 106 * 107 * @param id - the id of the message to retrieve 108 * @param lang - a language string from a browser 109 * @param defaultMsg - string to use if the message is not defined or a language 110 * match is not found (if null, then will default to the 111 * english message) 112 * @return the message 113 */ 114 public String getMessage(String id, String lang, String defaultMsg) { 115 return getMessage(id, lang.split("[.;]"), defaultMsg); 116 } 117 118 private String getMessage(String id, String[] lang, String defaultMsg) { 119 Element msg = messages.get(id); 120 if (msg == null) 121 return defaultMsg; 122 for (String l : lang) { 123 String res = getByLang(msg, l); 124 if (res != null) 125 return res; 126 } 127 if (defaultMsg == null) { 128 String res = getByLang(msg, "en"); 129 if (res != null) 130 return res; 131 } 132 return defaultMsg; 133 } 134 135 private String getByLang(Element msg, String lang) { 136 Element c = XMLUtil.getFirstChild(msg); 137 while (c != null) { 138 if (c.getAttribute("lang").equals(lang)) 139 return c.getTextContent(); 140 c = XMLUtil.getNextSibling(c); 141 } 142 return null; 143 } 144 145 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 146 public String getLangDesc(String s) { 147 if (s.equals("en")) 148 return "English"; 149 if (s.equals("nl")) 150 return "Nederlands (Dutch)"; 151 if (s.equals("de")) 152 return "Deutsch (German)"; 153 if (s.equals("du")) 154 return "\u0440\u0443\u0301\u0441\u0441\u043a\u0438\u0439 (Russian)"; 155 return "\"" + s + "\""; 156 } 157 158}