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.filesystem.CSFileInputStream;
044import org.hl7.fhir.utilities.xml.XMLUtil;
045import org.w3c.dom.Document;
046import org.w3c.dom.Element;
047import org.xml.sax.SAXException;
048
049public class Translations {
050
051  private String[] lang;
052  private Map<String, Element> messages = new HashMap<String, Element>();
053
054  /**
055   * Set a default language to use
056   * 
057   * @param lang
058   */
059  public void setLang(String lang) {
060    this.lang = lang.split("[.;]");
061  }
062  
063  /**
064   * Load from the XML translations file maintained by the FHIR project
065   * 
066   * @param filename
067   * @throws IOException 
068   * @throws SAXException 
069   * @throws FileNotFoundException 
070   * @throws ParserConfigurationException 
071   * @throws Exception
072   */
073  public void load(String filename) 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 match is not found (if null, then will default to english)
097   * @return the message
098   */
099  public String getMessage(String id, String defaultMsg) {
100    return getMessage(id, lang, defaultMsg);
101  }
102
103  /**
104   * return the message in a specified language
105   * 
106   * @param id - the id of the message to retrieve
107   * @param lang - a language string from a browser
108   * @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)
109   * @return the message
110   */
111  public String getMessage(String id, String lang, String defaultMsg) {
112    return getMessage(id, lang.split("[.;]"), defaultMsg);
113  }
114
115  private String getMessage(String id, String[] lang, String defaultMsg) {
116    Element msg = messages.get(id);
117    if (msg == null)
118      return defaultMsg;
119    for (String l : lang) {
120      String res = getByLang(msg, l);
121      if (res != null)
122        return res;
123    }
124    if (defaultMsg == null) {
125      String res = getByLang(msg, "en");
126      if (res != null)
127        return res;
128    }
129    return defaultMsg;
130  }
131
132  private String getByLang(Element msg, String lang) {
133    Element c = XMLUtil.getFirstChild(msg);
134    while (c != null) {
135      if (c.getAttribute("lang").equals(lang))
136        return c.getTextContent();
137      c = XMLUtil.getNextSibling(c);
138    }
139    return null;
140  }
141 
142  // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
143  public String getLangDesc(String s) {
144    if (s.equals("en"))
145      return "English";
146    if (s.equals("nl"))
147      return "Nederlands (Dutch)";
148    if (s.equals("de"))
149      return "Deutsch (German)";
150    if (s.equals("ru"))
151      return "\u0440\u0443\u0301\u0441\u0441\u043a\u0438\u0439 (Russian)";
152    return "\"" + s + "\"";
153  }
154
155
156}