001package org.hl7.fhir.utilities;
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.File;
035import java.io.IOException;
036import java.util.Date;
037import java.util.HashMap;
038import java.util.HashSet;
039import java.util.Map;
040import java.util.Set;
041
042import javax.xml.parsers.DocumentBuilder;
043import javax.xml.parsers.DocumentBuilderFactory;
044import javax.xml.parsers.ParserConfigurationException;
045
046import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
047import org.hl7.fhir.utilities.xml.XMLUtil;
048import org.w3c.dom.Document;
049import org.w3c.dom.Element;
050import org.w3c.dom.Node;
051import org.xml.sax.SAXException;
052
053public class TranslatorXml implements TranslationServices {
054
055  
056  public class TranslatedTerm {
057    private Set<String> props = new HashSet<String>();
058    private Map<String, String> translations = new HashMap<String, String>();
059  }
060
061
062  private Map<String, TranslatedTerm> termsById = new HashMap<String, TranslatedTerm>();  
063  private Map<String, TranslatedTerm> termsByTerm = new HashMap<String, TranslatedTerm>();  
064  
065  public TranslatorXml(String filename) throws ParserConfigurationException, SAXException, IOException {
066    super();
067    load(filename);
068  }
069
070
071  private void load(String filename) throws ParserConfigurationException, SAXException, IOException {
072    DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory();
073    factory.setNamespaceAware(false);
074    DocumentBuilder builder = factory.newDocumentBuilder();
075    Document xml = builder.parse(ManagedFileAccess.file(filename)); 
076    Element e = XMLUtil.getFirstChild(xml.getDocumentElement());
077    while (e != null) {
078      load(e);
079      e = XMLUtil.getNextSibling(e);
080    }
081  }
082
083  private void load(Element e) {
084    TranslatedTerm t = new TranslatedTerm();
085    for (int i = 0; i < e.getAttributes().getLength(); i++) {
086      Node a = e.getAttributes().item(i);
087      String n = a.getNodeName();
088      if (n.equals("id"))
089        termsById.put(a.getTextContent(), t);
090      else if (a.getNodeValue().equals("true"))
091        t.props.add(n);
092    }
093    Element c = XMLUtil.getFirstChild(e);
094    while (c != null) {
095      String l = c.getAttribute("lang");
096      String s = c.getTextContent();
097      if (l.equals("en"))
098        termsByTerm.put(s, t);
099      t.translations.put(l, s);
100      c = XMLUtil.getNextSibling(c);
101    }    
102  }
103
104
105  private Map<String, String> getTranslations(String code) {
106    TranslatedTerm t = termsById.get(code);
107    if (t == null)
108      t = termsByTerm.get(code);
109    return t == null ? null : t.translations;
110  }
111
112  
113  @Override
114  public String translate(String context, String value, String targetLang) {
115    if (targetLang == null)
116      return value;
117    Map<String, String> t = getTranslations(value);
118    if (t == null)
119      return value;
120    if (t.containsKey(targetLang))
121      return t.get(targetLang);
122    return value;
123  }
124
125  @Override
126  public String translateAndFormat(String context, String lang, String value, Object... args) {
127    value = translate(context, value, lang);
128    return value == null ? "":  String.format(value, args);
129  }
130
131  @Override
132  public String translate(String context, String value) {
133    return null;    
134  }
135
136  @Override
137  public String toStr(float value) {
138    return null;
139  }
140
141  @Override
142  public String toStr(Date value) {
143    return null;
144  }
145
146
147  @Override
148  public Map<String, String> translations(String value) {
149    return getTranslations(value);
150  }
151
152
153  @Override
154  public Set<String> listTranslations(String category) {
155    Set<String> res = new HashSet<String>();
156    for (String s : termsById.keySet()) {
157      if (termsById.get(s).props.contains(category))
158        res.add(s);
159    }
160    return res;
161  }
162 
163}