001package org.hl7.fhir.utilities;
002
003/*-
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.File;
025import java.io.IOException;
026import java.util.Date;
027import java.util.HashMap;
028import java.util.HashSet;
029import java.util.Map;
030import java.util.Set;
031
032import javax.xml.parsers.DocumentBuilder;
033import javax.xml.parsers.DocumentBuilderFactory;
034import javax.xml.parsers.ParserConfigurationException;
035
036import org.hl7.fhir.utilities.xml.XMLUtil;
037import org.w3c.dom.Document;
038import org.w3c.dom.Element;
039import org.w3c.dom.Node;
040import org.xml.sax.SAXException;
041
042public class TranslatorXml implements TranslationServices {
043
044  
045  public class TranslatedTerm {
046    private Set<String> props = new HashSet<String>();
047    private Map<String, String> translations = new HashMap<String, String>();
048  }
049
050
051  private Map<String, TranslatedTerm> termsById = new HashMap<String, TranslatedTerm>();  
052  private Map<String, TranslatedTerm> termsByTerm = new HashMap<String, TranslatedTerm>();  
053  
054  public TranslatorXml(String filename) throws ParserConfigurationException, SAXException, IOException {
055    super();
056    load(filename);
057  }
058
059
060  private void load(String filename) throws ParserConfigurationException, SAXException, IOException {
061    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
062    factory.setNamespaceAware(false);
063    DocumentBuilder builder = factory.newDocumentBuilder();
064    Document xml = builder.parse(new File(filename)); 
065    Element e = XMLUtil.getFirstChild(xml.getDocumentElement());
066    while (e != null) {
067      load(e);
068      e = XMLUtil.getNextSibling(e);
069    }
070  }
071
072  private void load(Element e) {
073    TranslatedTerm t = new TranslatedTerm();
074    for (int i = 0; i < e.getAttributes().getLength(); i++) {
075      Node a = e.getAttributes().item(i);
076      String n = a.getNodeName();
077      if (n.equals("id"))
078        termsById.put(a.getTextContent(), t);
079      else if (a.getNodeValue().equals("true"))
080        t.props.add(n);
081    }
082    Element c = XMLUtil.getFirstChild(e);
083    while (c != null) {
084      String l = c.getAttribute("lang");
085      String s = c.getTextContent();
086      if (l.equals("en"))
087        termsByTerm.put(s, t);
088      t.translations.put(l, s);
089      c = XMLUtil.getNextSibling(c);
090    }    
091  }
092
093
094  private Map<String, String> getTranslations(String code) {
095    TranslatedTerm t = termsById.get(code);
096    if (t == null)
097      t = termsByTerm.get(code);
098    return t == null ? null : t.translations;
099  }
100
101  
102  @Override
103  public String translate(String context, String value, String targetLang) {
104    if (targetLang == null)
105      return value;
106    Map<String, String> t = getTranslations(value);
107    if (t == null)
108      return value;
109    if (t.containsKey(targetLang))
110      return t.get(targetLang);
111    return value;
112  }
113
114  @Override
115  public String translateAndFormat(String context, String lang, String value, Object... args) {
116    value = translate(context, value, lang);
117    return value == null ? "":  String.format(value, args);
118  }
119
120  @Override
121  public String translate(String context, String value) {
122    return null;    
123  }
124
125  @Override
126  public String toStr(float value) {
127    // TODO Auto-generated method stub
128    return null;
129  }
130
131  @Override
132  public String toStr(Date value) {
133    // TODO Auto-generated method stub
134    return null;
135  }
136
137
138  @Override
139  public Map<String, String> translations(String value) {
140    return getTranslations(value);
141  }
142
143
144  @Override
145  public Set<String> listTranslations(String category) {
146    Set<String> res = new HashSet<String>();
147    for (String s : termsById.keySet()) {
148      if (termsById.get(s).props.contains(category))
149        res.add(s);
150    }
151    return res;
152  }
153 
154}