001package org.hl7.fhir.r5.elementmodel;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.io.OutputStream;
006import java.util.List;
007
008import org.apache.commons.lang3.NotImplementedException;
009import org.hl7.fhir.exceptions.DefinitionException;
010import org.hl7.fhir.exceptions.FHIRException;
011import org.hl7.fhir.exceptions.FHIRFormatError;
012import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
013import org.hl7.fhir.r5.context.IWorkerContext;
014import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
015import org.hl7.fhir.r5.formats.IParser.OutputStyle;
016import org.hl7.fhir.r5.model.Base;
017import org.hl7.fhir.r5.model.Resource;
018import org.hl7.fhir.r5.model.StructureDefinition;
019import org.hl7.fhir.utilities.Utilities;
020import org.hl7.fhir.utilities.i18n.I18nConstants;
021import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
022
023public class ResourceParser extends ParserBase {
024
025  public ResourceParser(IWorkerContext context) {
026    super(context);
027  }
028
029  @Override
030  public List<NamedElement> parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException {
031    throw new NotImplementedException("parse(InputStream stream)"); // doesns't make sense
032  }
033
034  @Override
035  public void compose(Element e, OutputStream destination, OutputStyle style, String base)
036      throws FHIRException, IOException {
037    throw new NotImplementedException("compose(Element e, OutputStream destination, OutputStyle style, String base)"); // doesns't make sense    
038  }
039
040  /**
041   * It's possible to get an element model from an resource by writing it to a stream, and reading it, 
042   * but this loads it directly, and links to the element model from the resource model
043   * 
044   * @param resource
045   * @return
046   * @throws IOException 
047   */
048  public Element parse(Resource resource) {
049    StructureDefinition sd = context.fetchTypeDefinition(resource.fhirType());
050    if (sd == null) {
051      throw new FHIRException("No definition exists for "+resource.fhirType());
052    }
053    Property p = new Property(context, sd.getSnapshot().getElement().get(0), sd, new ProfileUtilities(context, null, null));
054    String path = resource.fhirType();
055    
056    Element e = new Element(resource.fhirType(), p);
057    e.setPath(path);
058    e.setType(resource.fhirType());
059    parseChildren(path, resource, e);
060    e.numberChildren();
061    return e;
062  }
063
064  private void parseChildren(String path, Base src, Element dst) {
065    dst.setSource(src);
066    List<Property> properties = dst.getProperty().getChildProperties(dst.getName(), null);
067    for (org.hl7.fhir.r5.model.Property c : src.children()) {
068      if (c.hasValues()) {
069        Property p = getPropertyByName(properties, c.getName());
070        if (p == null) {
071          throw new FHIRException("Unable to find property for "+path+"."+c.getName());
072        }
073        int i = 0;
074        for (Base v : c.getValues()) {
075          String npath = path+"."+c.getName()+(p.isList() ? "["+i+"]" : "");
076          i++;
077          String name = c.getName();
078          if (name.endsWith("[x]")) {
079            name = name.substring(0, name.length()-3)+Utilities.capitalize(v.fhirType());
080          }
081          Element n = new Element(name, p);
082          dst.getChildren().add(n);
083          if (v.isPrimitive()) {
084            if (v.fhirType().equals("xhtml")) {
085              n.setXhtml(v.getXhtml());
086              try {
087                n.setValue(new XhtmlComposer(true).compose(n.getXhtml()));
088              } catch (Exception e) {
089                // won't happen here
090              }
091            } else {
092              n.setValue(v.primitiveValue());
093            }
094//            if (!n.getProperty().isChoice() && n.getType().equals("xhtml")) {
095//                try {
096//                  n.setXhtml(new XhtmlParser().setValidatorMode(policy == ValidationPolicy.EVERYTHING).parse(n.getValue(), null).getDocumentElement());
097//                } catch (Exception e) {
098//                  logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_XHTML_, e.getMessage()), IssueSeverity.ERROR);
099//                }
100//              }
101//            }
102          }      
103          n.setPath(npath);
104          if (p.isResource()) {
105            StructureDefinition sd = context.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(v.fhirType(), null));
106            if (sd == null)
107              throw new FHIRFormatError(context.formatMessage(I18nConstants.CONTAINED_RESOURCE_DOES_NOT_APPEAR_TO_BE_A_FHIR_RESOURCE_UNKNOWN_NAME_, v.fhirType()));
108            n.updateProperty(new Property(context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(n.getProperty()), p);
109            n.setType(v.fhirType());
110            parseChildren(npath, v, n);
111          } else {
112            parseChildren(npath, v, n);
113          }
114        }       
115      }
116    }
117
118  }
119
120  private Property getPropertyByName(List<Property> properties, String name) {
121    for (Property p : properties) {
122      if (p.getName().equals(name)) {
123        return p;
124      }
125    }
126    return null;
127
128  }
129}
130