001package org.hl7.fhir.r5.conformance;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
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
023import java.io.IOException;
024import java.io.OutputStream;
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.Comparator;
028import java.util.Date;
029import java.util.HashMap;
030import java.util.HashSet;
031import java.util.Iterator;
032import java.util.List;
033import java.util.Map;
034import java.util.Set;
035
036import org.apache.commons.lang3.StringUtils;
037import org.hl7.fhir.exceptions.DefinitionException;
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.exceptions.FHIRFormatError;
040import org.hl7.fhir.r5.conformance.ProfileUtilities.BaseTypeSlice;
041import org.hl7.fhir.r5.conformance.ProfileUtilities.ProfileKnowledgeProvider.BindingResolution;
042import org.hl7.fhir.r5.context.IWorkerContext;
043import org.hl7.fhir.r5.context.IWorkerContext.ValidationResult;
044import org.hl7.fhir.r5.elementmodel.ObjectConverter;
045import org.hl7.fhir.r5.elementmodel.Property;
046import org.hl7.fhir.r5.formats.IParser;
047import org.hl7.fhir.r5.model.Base;
048import org.hl7.fhir.r5.model.BooleanType;
049import org.hl7.fhir.r5.model.CanonicalType;
050import org.hl7.fhir.r5.model.CodeType;
051import org.hl7.fhir.r5.model.CodeableConcept;
052import org.hl7.fhir.r5.model.Coding;
053import org.hl7.fhir.r5.model.Element;
054import org.hl7.fhir.r5.model.ElementDefinition;
055import org.hl7.fhir.r5.model.ElementDefinition.AggregationMode;
056import org.hl7.fhir.r5.model.ElementDefinition.DiscriminatorType;
057import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBaseComponent;
058import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent;
059import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent;
060import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionExampleComponent;
061import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionMappingComponent;
062import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingComponent;
063import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent;
064import org.hl7.fhir.r5.model.ElementDefinition.PropertyRepresentation;
065import org.hl7.fhir.r5.model.ElementDefinition.SlicingRules;
066import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
067import org.hl7.fhir.r5.model.Enumeration;
068import org.hl7.fhir.r5.model.Enumerations.BindingStrength;
069import org.hl7.fhir.r5.model.Enumerations.FHIRVersion;
070import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
071import org.hl7.fhir.r5.model.Extension;
072import org.hl7.fhir.r5.model.IdType;
073import org.hl7.fhir.r5.model.IntegerType;
074import org.hl7.fhir.r5.model.PrimitiveType;
075import org.hl7.fhir.r5.model.Quantity;
076import org.hl7.fhir.r5.model.Resource;
077import org.hl7.fhir.r5.model.StringType;
078import org.hl7.fhir.r5.model.StructureDefinition;
079import org.hl7.fhir.r5.model.StructureDefinition.ExtensionContextType;
080import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionContextComponent;
081import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionDifferentialComponent;
082import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
083import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionMappingComponent;
084import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionSnapshotComponent;
085import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule;
086import org.hl7.fhir.r5.model.DataType;
087import org.hl7.fhir.r5.model.UriType;
088import org.hl7.fhir.r5.model.ValueSet;
089import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionComponent;
090import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent;
091import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
092import org.hl7.fhir.r5.utils.NarrativeGenerator;
093import org.hl7.fhir.r5.utils.ToolingExtensions;
094import org.hl7.fhir.r5.utils.TranslatingUtilities;
095import org.hl7.fhir.r5.utils.formats.CSVWriter;
096import org.hl7.fhir.r5.utils.formats.XLSXWriter;
097import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
098import org.hl7.fhir.utilities.TerminologyServiceOptions;
099import org.hl7.fhir.utilities.Utilities;
100import org.hl7.fhir.utilities.VersionUtilities;
101import org.hl7.fhir.utilities.validation.ValidationOptions;
102import org.hl7.fhir.utilities.validation.ValidationMessage;
103import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
104import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
105import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
106import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator;
107import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell;
108import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece;
109import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row;
110import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel;
111import org.hl7.fhir.utilities.xhtml.XhtmlNode;
112import org.hl7.fhir.utilities.xml.SchematronWriter;
113import org.hl7.fhir.utilities.xml.SchematronWriter.Rule;
114import org.hl7.fhir.utilities.xml.SchematronWriter.SchematronType;
115import org.hl7.fhir.utilities.xml.SchematronWriter.Section;
116
117/** 
118 * This class provides a set of utility operations for working with Profiles.
119 * Key functionality:
120 *  * getChildMap --?
121 *  * getChildList
122 *  * generateSnapshot: Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
123 *  * closeDifferential: fill out a differential by excluding anything not mentioned
124 *  * generateExtensionsTable: generate the HTML for a hierarchical table presentation of the extensions
125 *  * generateTable: generate  the HTML for a hierarchical table presentation of a structure
126 *  * generateSpanningTable: generate the HTML for a table presentation of a network of structures, starting at a nominated point
127 *  * summarize: describe the contents of a profile
128 *  
129 * note to maintainers: Do not make modifications to the snapshot generation without first changing the snapshot generation test cases to demonstrate the grounds for your change
130 *  
131 * @author Grahame
132 *
133 */
134public class ProfileUtilities extends TranslatingUtilities {
135
136  public class ElementRedirection {
137
138    private String path;
139    private ElementDefinition element;
140
141    public ElementRedirection(ElementDefinition element, String path) {
142      this.path = path;
143      this.element = element;
144    }
145
146    public ElementDefinition getElement() {
147      return element;
148    }
149
150    @Override
151    public String toString() {
152      return element.toString() + " : "+path;
153    }
154
155    public String getPath() {
156      return path;
157    }
158
159  }
160  
161  public class TypeSlice {
162    private ElementDefinition defn;
163    private String type;
164    public TypeSlice(ElementDefinition defn, String type) {
165      super();
166      this.defn = defn;
167      this.type = type;
168    }
169    public ElementDefinition getDefn() {
170      return defn;
171    }
172    public String getType() {
173      return type;
174    }
175    
176  }
177  public class BaseTypeSlice {
178    private ElementDefinition defn;
179    private String type;
180    private int start;
181    private int end;
182    public boolean handled;
183    public BaseTypeSlice(ElementDefinition defn, String type, int start, int end) {
184      super();
185      this.defn = defn;
186      this.type = type;
187      this.start = start;
188      this.end = end;
189    }
190  }
191
192
193  private static final int MAX_RECURSION_LIMIT = 10;
194  
195  public class ExtensionContext {
196
197    private ElementDefinition element;
198    private StructureDefinition defn;
199
200    public ExtensionContext(StructureDefinition ext, ElementDefinition ed) {
201      this.defn = ext;
202      this.element = ed;
203    }
204
205    public ElementDefinition getElement() {
206      return element;
207    }
208
209    public StructureDefinition getDefn() {
210      return defn;
211    }
212
213    public String getUrl() {
214      if (element == defn.getSnapshot().getElement().get(0))
215        return defn.getUrl();
216      else
217        return element.getSliceName();
218    }
219
220    public ElementDefinition getExtensionValueDefinition() {
221      int i = defn.getSnapshot().getElement().indexOf(element)+1;
222      while (i < defn.getSnapshot().getElement().size()) {
223        ElementDefinition ed = defn.getSnapshot().getElement().get(i);
224        if (ed.getPath().equals(element.getPath()))
225          return null;
226        if (ed.getPath().startsWith(element.getPath()+".value"))
227          return ed;
228        i++;
229      }
230      return null;
231    }
232  }
233
234  private static final String ROW_COLOR_ERROR = "#ffcccc";
235  private static final String ROW_COLOR_FATAL = "#ff9999";
236  private static final String ROW_COLOR_WARNING = "#ffebcc";
237  private static final String ROW_COLOR_HINT = "#ebf5ff";
238  private static final String ROW_COLOR_NOT_MUST_SUPPORT = "#d6eaf8";
239  public static final int STATUS_OK = 0;
240  public static final int STATUS_HINT = 1;
241  public static final int STATUS_WARNING = 2;
242  public static final int STATUS_ERROR = 3;
243  public static final int STATUS_FATAL = 4;
244
245
246  private static final String DERIVATION_EQUALS = "derivation.equals";
247  public static final String DERIVATION_POINTER = "derived.pointer";
248  public static final String IS_DERIVED = "derived.fact";
249  public static final String UD_ERROR_STATUS = "error-status";
250  private static final String GENERATED_IN_SNAPSHOT = "profileutilities.snapshot.processed";
251  private final boolean ADD_REFERENCE_TO_TABLE = true;
252
253  private boolean useTableForFixedValues = true;
254  private boolean debug;
255
256  // note that ProfileUtilities are used re-entrantly internally, so nothing with process state can be here
257  private final IWorkerContext context;
258  private List<ValidationMessage> messages;
259  private List<String> snapshotStack = new ArrayList<String>();
260  private ProfileKnowledgeProvider pkp;
261  private boolean igmode;
262  private boolean exception;
263  private ValidationOptions terminologyServiceOptions = new ValidationOptions();
264  private boolean newSlicingProcessing;
265  private String defWebRoot;
266
267  public ProfileUtilities(IWorkerContext context, List<ValidationMessage> messages, ProfileKnowledgeProvider pkp) {
268    super();
269    this.context = context;
270    this.messages = messages;
271    this.pkp = pkp;
272  }
273
274  private class UnusedTracker {
275    private boolean used;
276  }
277
278  public boolean isIgmode() {
279    return igmode;
280  }
281
282
283  public void setIgmode(boolean igmode) {
284    this.igmode = igmode;
285  }
286
287  public interface ProfileKnowledgeProvider {
288    public class BindingResolution {
289      public String display;
290      public String url;
291    }
292    public boolean isDatatype(String typeSimple);
293    public boolean isResource(String typeSimple);
294    public boolean hasLinkFor(String typeSimple);
295    public String getLinkFor(String corePath, String typeSimple);
296    public BindingResolution resolveBinding(StructureDefinition def, ElementDefinitionBindingComponent binding, String path) throws FHIRException;
297    public BindingResolution resolveBinding(StructureDefinition def, String url, String path) throws FHIRException;
298    public String getLinkForProfile(StructureDefinition profile, String url);
299    public boolean prependLinks();
300    public String getLinkForUrl(String corePath, String s);
301  }
302
303
304
305  public static List<ElementDefinition> getChildMap(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
306    if (element.getContentReference()!=null) {
307      for (ElementDefinition e : profile.getSnapshot().getElement()) {
308        if (element.getContentReference().equals("#"+e.getId()))
309          return getChildMap(profile, e);
310      }
311      throw new DefinitionException("Unable to resolve name reference "+element.getContentReference()+" at path "+element.getPath());
312
313    } else {
314      List<ElementDefinition> res = new ArrayList<ElementDefinition>();
315      List<ElementDefinition> elements = profile.getSnapshot().getElement();
316      String path = element.getPath();
317      for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
318        ElementDefinition e = elements.get(index);
319        if (e.getPath().startsWith(path + ".")) {
320          // We only want direct children, not all descendants
321          if (!e.getPath().substring(path.length()+1).contains("."))
322            res.add(e);
323        } else
324          break;
325      }
326      return res;
327    }
328  }
329
330
331  public static List<ElementDefinition> getSliceList(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
332    if (!element.hasSlicing())
333      throw new Error("getSliceList should only be called when the element has slicing");
334
335    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
336    List<ElementDefinition> elements = profile.getSnapshot().getElement();
337    String path = element.getPath();
338    for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
339      ElementDefinition e = elements.get(index);
340      if (e.getPath().startsWith(path + ".") || e.getPath().equals(path)) {
341        // We want elements with the same path (until we hit an element that doesn't start with the same path)
342        if (e.getPath().equals(element.getPath()))
343          res.add(e);
344      } else
345        break;
346    }
347    return res;
348  }
349
350
351  /**
352   * Given a Structure, navigate to the element given by the path and return the direct children of that element
353   *
354   * @param structure The structure to navigate into
355   * @param path The path of the element within the structure to get the children for
356   * @return A List containing the element children (all of them are Elements)
357   */
358  public static List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id) {
359    return getChildList(profile, path, id, false);
360  }
361  
362  public static List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id, boolean diff) {
363    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
364
365    boolean capturing = id==null;
366    if (id==null && !path.contains("."))
367      capturing = true;
368  
369    List<ElementDefinition> list = diff ? profile.getDifferential().getElement() : profile.getSnapshot().getElement();
370    for (ElementDefinition e : list) {
371      if (e == null)
372        throw new Error("element = null: "+profile.getUrl());
373      if (e.getId() == null)
374        throw new Error("element id = null: "+e.toString()+" on "+profile.getUrl());
375      
376      if (!capturing && id!=null && e.getId().equals(id)) {
377        capturing = true;
378      }
379      
380      // If our element is a slice, stop capturing children as soon as we see the next slice
381      if (capturing && e.hasId() && id!= null && !e.getId().equals(id) && e.getPath().equals(path))
382        break;
383      
384      if (capturing) {
385        String p = e.getPath();
386  
387        if (!Utilities.noString(e.getContentReference()) && path.startsWith(p)) {
388          if (path.length() > p.length())
389            return getChildList(profile, e.getContentReference()+"."+path.substring(p.length()+1), null, diff);
390          else
391            return getChildList(profile, e.getContentReference(), null, diff);
392          
393        } else if (p.startsWith(path+".") && !p.equals(path)) {
394          String tail = p.substring(path.length()+1);
395          if (!tail.contains(".")) {
396            res.add(e);
397          }
398        }
399      }
400    }
401
402    return res;
403  }
404
405  public static List<ElementDefinition> getChildList(StructureDefinition structure, ElementDefinition element, boolean diff) {
406    return getChildList(structure, element.getPath(), element.getId(), diff);
407  }
408
409  public static List<ElementDefinition> getChildList(StructureDefinition structure, ElementDefinition element) {
410    return getChildList(structure, element.getPath(), element.getId(), false);
411        }
412
413  public void updateMaps(StructureDefinition base, StructureDefinition derived) throws DefinitionException {
414    if (base == null)
415        throw new DefinitionException("no base profile provided");
416    if (derived == null)
417      throw new DefinitionException("no derived structure provided");
418    
419    for (StructureDefinitionMappingComponent baseMap : base.getMapping()) {
420      boolean found = false;
421      for (StructureDefinitionMappingComponent derivedMap : derived.getMapping()) {
422        if (derivedMap.getUri() != null && derivedMap.getUri().equals(baseMap.getUri())) {
423          found = true;
424          break;
425        }
426      }
427      if (!found) {
428        derived.getMapping().add(baseMap);
429      }
430    }
431  }
432  
433  /**
434   * Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
435   *
436   * @param base - the base structure on which the differential will be applied
437   * @param differential - the differential to apply to the base
438   * @param url - where the base has relative urls for profile references, these need to be converted to absolutes by prepending this URL (e.g. the canonical URL)
439   * @param webUrl - where the base has relative urls in markdown, these need to be converted to absolutes by prepending this URL (this is not the same as the canonical URL)
440   * @param trimDifferential - if this is true, then the snap short generator will remove any material in the element definitions that is not different to the base
441   * @return
442   * @throws FHIRException 
443   * @throws DefinitionException 
444   * @throws Exception
445   */
446  public void generateSnapshot(StructureDefinition base, StructureDefinition derived, String url, String webUrl, String profileName) throws DefinitionException, FHIRException {
447    if (base == null) {
448      throw new DefinitionException("no base profile provided");
449    }
450    if (derived == null) {
451      throw new DefinitionException("no derived structure provided");
452    }
453
454    if (!base.hasType()) {
455      throw new DefinitionException("Base profile "+base.getUrl()+" has no type");
456    }
457    if (!derived.hasType()) {
458      throw new DefinitionException("Derived profile "+derived.getUrl()+" has no type");
459    }
460    if (!derived.hasDerivation()) {
461      throw new DefinitionException("Derived profile "+derived.getUrl()+" has no derivation value and so can't be processed");
462    }
463    if (!base.getType().equals(derived.getType()) && derived.getDerivation() == TypeDerivationRule.CONSTRAINT) {
464      throw new DefinitionException("Base & Derived profiles have different types ("+base.getUrl()+" = "+base.getType()+" vs "+derived.getUrl()+" = "+derived.getType()+")");
465    }
466    
467    if (snapshotStack.contains(derived.getUrl())) {
468      throw new DefinitionException("Circular snapshot references detected; cannot generate snapshot (stack = "+snapshotStack.toString()+")");
469    }
470    snapshotStack.add(derived.getUrl());
471
472    if (!Utilities.noString(webUrl) && !webUrl.endsWith("/"))
473      webUrl = webUrl + '/';
474
475    if (defWebRoot == null)
476      defWebRoot = webUrl;
477    derived.setSnapshot(new StructureDefinitionSnapshotComponent());
478
479    try {
480      // so we have two lists - the base list, and the differential list
481      // the differential list is only allowed to include things that are in the base list, but
482      // is allowed to include them multiple times - thereby slicing them
483
484      // our approach is to walk through the base list, and see whether the differential
485      // says anything about them.
486      int baseCursor = 0;
487      int diffCursor = 0; // we need a diff cursor because we can only look ahead, in the bound scoped by longer paths
488
489      if (derived.hasDifferential() && !derived.getDifferential().getElementFirstRep().getPath().contains(".") && !derived.getDifferential().getElementFirstRep().getType().isEmpty())
490        throw new Error("type on first differential element!");
491
492      for (ElementDefinition e : derived.getDifferential().getElement()) 
493        e.clearUserData(GENERATED_IN_SNAPSHOT);
494
495      // we actually delegate the work to a subroutine so we can re-enter it with a different cursors
496      StructureDefinitionDifferentialComponent diff = cloneDiff(derived.getDifferential()); // we make a copy here because we're sometimes going to hack the differential while processing it. Have to migrate user data back afterwards
497      StructureDefinitionSnapshotComponent baseSnapshot  = base.getSnapshot(); 
498      if (derived.getDerivation() == TypeDerivationRule.SPECIALIZATION) {
499        String derivedType = derived.getType();
500        if (StructureDefinitionKind.LOGICAL.equals(derived.getKind()) && derived.getType().contains("/")) {
501          derivedType = derivedType.substring(derivedType.lastIndexOf("/")+1);
502        }
503        baseSnapshot = cloneSnapshot(baseSnapshot, base.getType(), derivedType);
504      }
505      if (derived.getUrl().equals("http://sharedhealth.exchange/fhir/StructureDefinition/profile-operationoutcome")) {
506        debug = true;
507      }
508      processPaths("", derived.getSnapshot(), baseSnapshot, diff, baseCursor, diffCursor, baseSnapshot.getElement().size()-1, 
509          derived.getDifferential().hasElement() ? derived.getDifferential().getElement().size()-1 : -1, url, webUrl, derived.present(), null, null, false, base.getUrl(), null, false, null, new ArrayList<ElementRedirection>(), base);
510      if (derived.getDerivation() == TypeDerivationRule.SPECIALIZATION) {
511        for (ElementDefinition e : diff.getElement()) {
512          if (!e.hasUserData(GENERATED_IN_SNAPSHOT)) {
513            ElementDefinition outcome = updateURLs(url, webUrl, e.copy());
514            e.setUserData(GENERATED_IN_SNAPSHOT, outcome);
515            derived.getSnapshot().addElement(outcome);
516          }
517        }
518      }
519      
520      if (!derived.getSnapshot().getElementFirstRep().getType().isEmpty())
521        throw new Error("type on first snapshot element for "+derived.getSnapshot().getElementFirstRep().getPath()+" in "+derived.getUrl()+" from "+base.getUrl());
522      updateMaps(base, derived);
523
524      if (debug) {
525        System.out.println("Differential: ");
526        for (ElementDefinition ed : derived.getDifferential().getElement())
527          System.out.println("  "+ed.getId()+" : "+typeSummaryWithProfile(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  "+constraintSummary(ed));
528        System.out.println("Snapshot: ");
529        for (ElementDefinition ed : derived.getSnapshot().getElement())
530          System.out.println("  "+ed.getId()+" : "+typeSummaryWithProfile(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  "+constraintSummary(ed));
531      }
532      setIds(derived, false);
533      CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
534      //Check that all differential elements have a corresponding snapshot element
535      int ce = 0;
536      for (ElementDefinition e : diff.getElement()) {
537        if (!e.hasUserData("diff-source"))
538          throw new Error("Unxpected internal condition - no source on diff element");
539        else {
540          if (e.hasUserData(DERIVATION_EQUALS))
541            ((Base) e.getUserData("diff-source")).setUserData(DERIVATION_EQUALS, e.getUserData(DERIVATION_EQUALS));
542          if (e.hasUserData(DERIVATION_POINTER))
543            ((Base) e.getUserData("diff-source")).setUserData(DERIVATION_POINTER, e.getUserData(DERIVATION_POINTER));
544        }
545        if (!e.hasUserData(GENERATED_IN_SNAPSHOT)) {
546          b.append(e.hasId() ? "id: "+e.getId() : "path: "+e.getPath());
547          if (e.hasId()) {
548            ce++;
549            String msg = "No match found in the generated snapshot: check that the path and definitions are legal in the differential (including order)";
550            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.VALUE, url+"#"+e.getId(), msg, ValidationMessage.IssueSeverity.ERROR));
551          }
552        }
553      }
554      if (!Utilities.noString(b.toString())) {
555        String msg = "The profile "+derived.getUrl()+" has "+ce+" "+Utilities.pluralize("element", ce)+" in the differential ("+b.toString()+") that don't have a matching element in the snapshot: check that the path and definitions are legal in the differential (including order)";
556        System.out.println("Error in snapshot generation: "+msg);
557        if (!debug) {
558          System.out.println("Differential: ");
559          for (ElementDefinition ed : derived.getDifferential().getElement())
560            System.out.println("  "+ed.getId()+" : "+typeSummaryWithProfile(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  "+constraintSummary(ed));
561          System.out.println("Snapshot: ");
562          for (ElementDefinition ed : derived.getSnapshot().getElement())
563            System.out.println("  "+ed.getId()+" : "+typeSummaryWithProfile(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  "+constraintSummary(ed));
564        }
565        if (exception)
566          throw new DefinitionException(msg);
567        else
568          messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.VALUE, url, msg, ValidationMessage.IssueSeverity.ERROR));
569      }
570      if (derived.getDerivation() == TypeDerivationRule.SPECIALIZATION) {
571        for (ElementDefinition ed : derived.getSnapshot().getElement()) {
572          if (!ed.hasBase()) {
573            ed.getBase().setPath(ed.getPath()).setMin(ed.getMin()).setMax(ed.getMax());
574          }
575        }
576      }
577    } catch (Exception e) {
578      // if we had an exception generating the snapshot, make sure we don't leave any half generated snapshot behind
579      derived.setSnapshot(null);
580      throw e;
581    }
582  }
583
584  private StructureDefinitionDifferentialComponent cloneDiff(StructureDefinitionDifferentialComponent source) {
585    StructureDefinitionDifferentialComponent diff = new StructureDefinitionDifferentialComponent();
586    for (ElementDefinition sed : source.getElement()) {
587      ElementDefinition ted = sed.copy();
588      diff.getElement().add(ted);
589      ted.setUserData("diff-source", sed);
590    }
591    return diff;
592  }
593
594  private StructureDefinitionSnapshotComponent cloneSnapshot(StructureDefinitionSnapshotComponent source, String baseType, String derivedType) {
595        StructureDefinitionSnapshotComponent diff = new StructureDefinitionSnapshotComponent();
596    for (ElementDefinition sed : source.getElement()) {
597      ElementDefinition ted = sed.copy();
598      ted.setId(ted.getId().replaceFirst(baseType,derivedType));
599      ted.setPath(ted.getPath().replaceFirst(baseType,derivedType));
600      diff.getElement().add(ted);
601    }
602    return diff;
603  }
604
605  private String constraintSummary(ElementDefinition ed) {
606    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
607    if (ed.hasPattern())
608      b.append("pattern="+ed.getPattern().fhirType());
609    if (ed.hasFixed())
610      b.append("fixed="+ed.getFixed().fhirType());
611    if (ed.hasConstraint())
612      b.append("constraints="+ed.getConstraint().size());
613    return b.toString();
614  }
615
616
617  private String sliceSummary(ElementDefinition ed) {
618    if (!ed.hasSlicing() && !ed.hasSliceName())
619      return "";
620    if (ed.hasSliceName())
621      return " (slicename = "+ed.getSliceName()+")";
622    
623    StringBuilder b = new StringBuilder();
624    boolean first = true;
625    for (ElementDefinitionSlicingDiscriminatorComponent d : ed.getSlicing().getDiscriminator()) {
626      if (first) 
627        first = false;
628      else
629        b.append("|");
630      b.append(d.getPath());
631    }
632    return " (slicing by "+b.toString()+")";
633  }
634
635
636  private String typeSummary(ElementDefinition ed) {
637    StringBuilder b = new StringBuilder();
638    boolean first = true;
639    for (TypeRefComponent tr : ed.getType()) {
640      if (first) 
641        first = false;
642      else
643        b.append("|");
644      b.append(tr.getWorkingCode());
645    }
646    return b.toString();
647  }
648
649  private String typeSummaryWithProfile(ElementDefinition ed) {
650    StringBuilder b = new StringBuilder();
651    boolean first = true;
652    for (TypeRefComponent tr : ed.getType()) {
653      if (first) 
654        first = false;
655      else
656        b.append("|");
657      b.append(tr.getWorkingCode());
658      if (tr.hasProfile()) {
659        b.append("(");
660        b.append(tr.getProfile());
661        b.append(")");
662        
663      }
664    }
665    return b.toString();
666  }
667
668
669  private boolean findMatchingElement(String id, List<ElementDefinition> list) {
670    for (ElementDefinition ed : list) {
671      if (ed.getId().equals(id))
672        return true;
673      if (id.endsWith("[x]")) {
674        if (ed.getId().startsWith(id.substring(0, id.length()-3)) && !ed.getId().substring(id.length()-3).contains("."))
675          return true;
676      }
677    }
678    return false;
679  }
680
681
682  /**
683   * @param trimDifferential
684   * @param srcSD 
685   * @throws DefinitionException, FHIRException 
686   * @throws Exception
687   */
688  private ElementDefinition processPaths(String indent, StructureDefinitionSnapshotComponent result, StructureDefinitionSnapshotComponent base, StructureDefinitionDifferentialComponent differential, int baseCursor, int diffCursor, int baseLimit,
689      int diffLimit, String url, String webUrl, String profileName, String contextPathSrc, String contextPathDst, boolean trimDifferential, String contextName, String resultPathBase, boolean slicingDone, String typeSlicingPath, List<ElementRedirection> redirector, StructureDefinition srcSD) throws DefinitionException, FHIRException {
690    if (debug) {
691      System.out.println(indent+"PP @ "+resultPathBase+" / "+contextPathSrc+" : base = "+baseCursor+" to "+baseLimit+", diff = "+diffCursor+" to "+diffLimit+" (slicing = "+slicingDone+", redirector = "+(redirector == null ? "null" : redirector.toString())+")");
692    }
693    ElementDefinition res = null; 
694    List<TypeSlice> typeList = new ArrayList<>();
695    // just repeat processing entries until we run out of our allowed scope (1st entry, the allowed scope is all the entries)
696    while (baseCursor <= baseLimit) {
697      // get the current focus of the base, and decide what to do
698      ElementDefinition currentBase = base.getElement().get(baseCursor);
699      String cpath = fixedPathSource(contextPathSrc, currentBase.getPath(), redirector);
700      if (debug) {
701        System.out.println(indent+" - "+cpath+": base = "+baseCursor+" ("+descED(base.getElement(),baseCursor)+") to "+baseLimit+" ("+descED(base.getElement(),baseLimit)+"), diff = "+diffCursor+" ("+descED(differential.getElement(),diffCursor)+") to "+diffLimit+" ("+descED(differential.getElement(),diffLimit)+") "+
702           "(slicingDone = "+slicingDone+") (diffpath= "+(differential.getElement().size() > diffCursor ? differential.getElement().get(diffCursor).getPath() : "n/a")+")");
703      }
704      List<ElementDefinition> diffMatches = getDiffMatches(differential, cpath, diffCursor, diffLimit, profileName); // get a list of matching elements in scope
705
706      // in the simple case, source is not sliced.
707      if (!currentBase.hasSlicing() || cpath.equals(typeSlicingPath)) {
708        if (diffMatches.isEmpty()) { // the differential doesn't say anything about this item
709          // so we just copy it in
710          ElementDefinition outcome = updateURLs(url, webUrl, currentBase.copy());
711          outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
712          updateFromBase(outcome, currentBase);
713          markDerived(outcome);
714          if (resultPathBase == null)
715            resultPathBase = outcome.getPath();
716          else if (!outcome.getPath().startsWith(resultPathBase))
717            throw new DefinitionException("Adding wrong path - outcome.getPath() = "+outcome.getPath()+", resultPathBase = "+resultPathBase);
718          result.getElement().add(outcome);
719          if (hasInnerDiffMatches(differential, cpath, diffCursor, diffLimit, base.getElement(), true)) {
720            // well, the profile walks into this, so we need to as well
721            // did we implicitly step into a new type?
722            if (baseHasChildren(base, currentBase)) { // not a new type here
723              processPaths(indent+"  ", result, base, differential, baseCursor+1, diffCursor, baseLimit, diffLimit, url, webUrl, profileName, contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
724              baseCursor = indexOfFirstNonChild(base, currentBase, baseCursor+1, baseLimit);
725            } else {
726              if (outcome.getType().size() == 0) {
727                throw new DefinitionException(diffMatches.get(0).getPath()+" has no children ("+differential.getElement().get(diffCursor).getPath()+") and no types in profile "+profileName);
728              }
729              if (outcome.getType().size() > 1) {
730                for (TypeRefComponent t : outcome.getType()) {
731                  if (!t.getWorkingCode().equals("Reference"))
732                    throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
733                }
734              }
735              StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
736              if (dt == null)
737                throw new DefinitionException("Unknown type "+outcome.getType().get(0)+" at "+diffMatches.get(0).getPath());
738              contextName = dt.getUrl();
739              int start = diffCursor;
740              while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath+"."))
741                diffCursor++;
742              processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start, dt.getSnapshot().getElement().size()-1,
743                  diffCursor-1, url, getWebUrl(dt, webUrl, indent), profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
744            }
745          }
746          baseCursor++;
747        } else if (diffMatches.size() == 1 && (slicingDone || (!isImplicitSlicing(diffMatches.get(0), cpath) && !(diffMatches.get(0).hasSlicing() || (isExtension(diffMatches.get(0)) && diffMatches.get(0).hasSliceName()))))) {// one matching element in the differential
748          ElementDefinition template = null;
749          if (diffMatches.get(0).hasType() && diffMatches.get(0).getType().size() == 1 && diffMatches.get(0).getType().get(0).hasProfile() && !"Reference".equals(diffMatches.get(0).getType().get(0).getWorkingCode())) {
750            CanonicalType p = diffMatches.get(0).getType().get(0).getProfile().get(0);
751            StructureDefinition sd = context.fetchResource(StructureDefinition.class, p.getValue());
752            if (sd != null) {
753              if (!sd.hasSnapshot()) {
754                StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
755                if (sdb == null)
756                  throw new DefinitionException("Unable to find base "+sd.getBaseDefinition()+" for "+sd.getUrl());
757                generateSnapshot(sdb, sd, sd.getUrl(), (sdb.hasUserData("path")) ? Utilities.extractBaseUrl(sdb.getUserString("path")) : webUrl, sd.getName());
758              }
759              ElementDefinition src;
760              if (p.hasExtension(ToolingExtensions.EXT_PROFILE_ELEMENT)) {
761                 src = null;
762                 String eid = p.getExtensionString(ToolingExtensions.EXT_PROFILE_ELEMENT);
763                 for (ElementDefinition t : sd.getSnapshot().getElement()) {
764                   if (eid.equals(t.getId()))
765                     src = t;
766                 }
767                 if (src == null)
768                   throw new DefinitionException("Unable to find element "+eid+" in "+p.getValue());
769              } else 
770                src = sd.getSnapshot().getElement().get(0);
771              template = src.copy().setPath(currentBase.getPath());
772              template.setSliceName(null);
773              // temporary work around
774              if (!"Extension".equals(diffMatches.get(0).getType().get(0).getCode())) {
775                template.setMin(currentBase.getMin());
776                template.setMax(currentBase.getMax());
777              }
778            }
779          } 
780          if (template == null)
781            template = currentBase.copy();
782          else
783            // some of what's in currentBase overrides template
784            template = fillOutFromBase(template, currentBase);
785          
786          ElementDefinition outcome = updateURLs(url, webUrl, template);
787          outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
788          if (res == null)
789            res = outcome;
790          updateFromBase(outcome, currentBase);
791          if (diffMatches.get(0).hasSliceName())
792            outcome.setSliceName(diffMatches.get(0).getSliceName());
793          updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url, srcSD);
794          removeStatusExtensions(outcome);
795//          if (outcome.getPath().endsWith("[x]") && outcome.getType().size() == 1 && !outcome.getType().get(0).getCode().equals("*") && !diffMatches.get(0).hasSlicing()) // if the base profile allows multiple types, but the profile only allows one, rename it
796//            outcome.setPath(outcome.getPath().substring(0, outcome.getPath().length()-3)+Utilities.capitalize(outcome.getType().get(0).getCode()));
797          outcome.setSlicing(null);
798          if (resultPathBase == null)
799            resultPathBase = outcome.getPath();
800          else if (!outcome.getPath().startsWith(resultPathBase))
801            throw new DefinitionException("Adding wrong path");
802          result.getElement().add(outcome);
803          baseCursor++;
804          diffCursor = differential.getElement().indexOf(diffMatches.get(0))+1;
805          if (diffLimit >= diffCursor && outcome.getPath().contains(".") && (isDataType(outcome.getType()) || outcome.hasContentReference())) {  // don't want to do this for the root, since that's base, and we're already processing it
806            if (pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+".") && !baseWalksInto(base.getElement(), baseCursor)) {
807              if (outcome.getType().size() > 1) {
808                if (outcome.getPath().endsWith("[x]") && !diffMatches.get(0).getPath().endsWith("[x]")) {
809                  String en = tail(outcome.getPath());
810                  String tn = tail(diffMatches.get(0).getPath());
811                  String t = tn.substring(en.length()-3);
812                  if (isPrimitive(Utilities.uncapitalize(t)))
813                    t = Utilities.uncapitalize(t);
814                  List<TypeRefComponent> ntr = getByTypeName(outcome.getType(), t); // keep any additional information
815                  if (ntr.isEmpty()) 
816                    ntr.add(new TypeRefComponent().setCode(t));
817                  outcome.getType().clear();
818                  outcome.getType().addAll(ntr);
819                }
820                if (outcome.getType().size() > 1)
821                  for (TypeRefComponent t : outcome.getType()) {
822                    if (!t.getCode().equals("Reference")) {
823                      boolean nonExtension = false;
824                      for (ElementDefinition ed : diffMatches)
825                        if (ed != diffMatches.get(0) && !ed.getPath().endsWith(".extension"))
826                          nonExtension = true;
827                      if (nonExtension)
828                        throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
829                    }
830                }
831              }
832              int start = diffCursor;
833              while (diffCursor <= diffLimit && differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
834                diffCursor++;
835              if (outcome.hasContentReference()) {
836                ElementDefinition tgt = getElementById(base.getElement(), outcome.getContentReference());
837                if (tgt == null)
838                  throw new DefinitionException("Unable to resolve reference to "+outcome.getContentReference());
839                replaceFromContentReference(outcome, tgt);
840                int nbc = base.getElement().indexOf(tgt)+1;
841                int nbl = nbc;
842                while (nbl < base.getElement().size() && base.getElement().get(nbl).getPath().startsWith(tgt.getPath()+"."))
843                  nbl++;
844                processPaths(indent+"  ", result, base, differential, nbc, start - 1, nbl-1, diffCursor - 1, url, webUrl, profileName, tgt.getPath(), diffMatches.get(0).getPath(), trimDifferential, contextName, resultPathBase, false, null, redirectorStack(redirector, outcome, cpath), srcSD);
845              } else {
846                StructureDefinition dt = outcome.getType().size() == 1 ? getProfileForDataType(outcome.getType().get(0)) : getProfileForDataType("Element");
847                if (dt == null)
848                  throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") for type "+typeCode(outcome.getType())+" in profile "+profileName+", but can't find type");
849                contextName = dt.getUrl();
850                processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start, dt.getSnapshot().getElement().size()-1,
851                    diffCursor - 1, url, getWebUrl(dt, webUrl, indent), profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false, null, new ArrayList<ElementRedirection>(), srcSD);
852              }
853            }
854          }
855        } else if (diffsConstrainTypes(diffMatches, cpath, typeList)) {
856          int start = 0;
857          int nbl = findEndOfElement(base, baseCursor);
858          int ndc = differential.getElement().indexOf(diffMatches.get(0));
859          ElementDefinition elementToRemove = null;
860          boolean shortCut = !typeList.isEmpty() && typeList.get(0).type != null;
861          // we come here whether they are sliced in the diff, or whether the short cut is used. 
862          if (shortCut) {
863            // this is the short cut method, we've just dived in and specified a type slice. 
864            // in R3 (and unpatched R4, as a workaround right now...
865            if (!FHIRVersion.isR4Plus(context.getVersion()) || !newSlicingProcessing) { // newSlicingProcessing is a work around for editorial loop dependency
866              // we insert a cloned element with the right types at the start of the diffMatches
867              ElementDefinition ed = new ElementDefinition();
868              ed.setPath(determineTypeSlicePath(diffMatches.get(0).getPath(), cpath));
869              for (TypeSlice ts : typeList) 
870                ed.addType().setCode(ts.type);
871              ed.setSlicing(new ElementDefinitionSlicingComponent());
872              ed.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
873              ed.getSlicing().setRules(SlicingRules.CLOSED);
874              ed.getSlicing().setOrdered(false);
875              diffMatches.add(0, ed);
876              differential.getElement().add(ndc, ed);
877              elementToRemove = ed;
878            } else {
879              // as of R4, this changed; if there's no slice, there's no constraint on the slice types, only one the type. 
880              // so the element we insert specifies no types (= all types) allowed in the base, not just the listed type. 
881              // see also discussion here: https://chat.fhir.org/#narrow/stream/179177-conformance/topic/Slicing.20a.20non-repeating.20element             
882              ElementDefinition ed = new ElementDefinition();
883              ed.setPath(determineTypeSlicePath(diffMatches.get(0).getPath(), cpath));
884              ed.setSlicing(new ElementDefinitionSlicingComponent());
885              ed.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
886              ed.getSlicing().setRules(SlicingRules.CLOSED);
887              ed.getSlicing().setOrdered(false);
888              diffMatches.add(0, ed);
889              differential.getElement().add(ndc, ed);
890              elementToRemove = ed;
891            }
892          }
893          int ndl = findEndOfElement(differential, ndc);
894          // the first element is setting up the slicing
895
896          if (diffMatches.get(0).getSlicing().hasOrdered()) {
897            if (diffMatches.get(0).getSlicing().getOrdered()) {
898              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.ordered = true");
899            }
900          }
901          if (diffMatches.get(0).getSlicing().hasDiscriminator()) {
902            if (diffMatches.get(0).getSlicing().getDiscriminator().size() != 1) {
903              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.count() > 1");
904            }
905            if (diffMatches.get(0).getSlicing().getDiscriminatorFirstRep().getType() != DiscriminatorType.TYPE) {
906              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.type != 'type'");
907            }
908            if (!"$this".equals(diffMatches.get(0).getSlicing().getDiscriminatorFirstRep().getPath())) {
909              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.path != '$this'");
910            }
911          }
912          // check the slice names too while we're at it...
913          for (TypeSlice ts : typeList) {
914            if (ts.type != null) {
915              String tn = rootName(cpath)+Utilities.capitalize(ts.type);
916              if (!ts.defn.hasSliceName()) {
917                ts.defn.setSliceName(tn);
918              } else if (!ts.defn.getSliceName().equals(tn)) {
919                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice name must be '"+tn+"' but is '"+ts.defn.getSliceName()+"'"); 
920              } if (!ts.defn.hasType()) {
921                ts.defn.addType().setCode(ts.type);
922              } else if (ts.defn.getType().size() > 1) {
923                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice for type '"+tn+"' has more than one type '"+ts.defn.typeSummary()+"'"); 
924              } else if (!ts.defn.getType().get(0).getCode().equals(ts.type)) {
925                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice for type '"+tn+"' has wrong type '"+ts.defn.typeSummary()+"'");
926              }
927            }
928          }
929
930          // ok passed the checks. 
931          // copy the root diff, and then process any children it has
932          ElementDefinition e = processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, 0), contextPathSrc, contextPathDst, 
933              trimDifferential, contextName, resultPathBase, true, null, redirector, srcSD);
934          if (e==null)
935            throw new FHIRException("Did not find type root: " + diffMatches.get(0).getPath());
936          // now set up slicing on the e (cause it was wiped by what we called.
937          e.setSlicing(new ElementDefinitionSlicingComponent());
938          e.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
939          e.getSlicing().setRules(SlicingRules.CLOSED); // type slicing is always closed; the differential might call it open, but that just means it's not constraining the slices it doesn't mention 
940          e.getSlicing().setOrdered(false);
941          start++;
942
943          String fixedType = null;
944          // now process the siblings, which should each be type constrained - and may also have their own children
945          // now we process the base scope repeatedly for each instance of the item in the differential list
946          for (int i = start; i < diffMatches.size(); i++) {
947            // our processing scope for the differential is the item in the list, and all the items before the next one in the list
948            if (diffMatches.get(i).getMin() > 0) {
949              if (diffMatches.size() > i+1) {
950                throw new FHIRException("Invalid slicing : there is more than one type slice at "+diffMatches.get(i).getPath()+", but one of them ("+diffMatches.get(i).getSliceName()+") has min = 1, so the other slices cannot exist");
951              } else {
952                e.setMin(1);
953              }
954              fixedType = determineFixedType(diffMatches, fixedType, i);
955            }
956            ndc = differential.getElement().indexOf(diffMatches.get(i));
957            ndl = findEndOfElement(differential, ndc);
958            processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, i), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true, null, redirector, srcSD);
959          }
960          if (elementToRemove != null) {
961            differential.getElement().remove(elementToRemove);
962            ndl--;
963          }
964          if (fixedType != null) {
965            for (Iterator<TypeRefComponent> iter = e.getType().iterator(); iter.hasNext(); ) {
966              TypeRefComponent tr = iter.next();
967              if (!tr.getCode().equals(fixedType)) {
968                iter.remove();
969              }
970            }
971          }
972          
973          // ok, done with that - next in the base list
974          baseCursor = nbl+1;
975          diffCursor = ndl+1;
976          
977        } else {
978          // ok, the differential slices the item. Let's check our pre-conditions to ensure that this is correct
979          if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))
980            // you can only slice an element that doesn't repeat if the sum total of your slices is limited to 1
981            // (but you might do that in order to split up constraints by type)
982            throw new DefinitionException("Attempt to a slice an element that does not repeat: "+currentBase.getPath()+"/"+currentBase.getPath()+" from "+contextName+" in "+url);
983          if (!diffMatches.get(0).hasSlicing() && !isExtension(currentBase)) // well, the diff has set up a slice, but hasn't defined it. this is an error
984            throw new DefinitionException("Differential does not have a slice: "+currentBase.getPath()+"/ (b:"+baseCursor+" of "+ baseLimit+" / "+ diffCursor +"/ "+diffLimit+") in profile "+url);
985
986          // well, if it passed those preconditions then we slice the dest.
987          int start = 0;
988          int nbl = findEndOfElement(base, baseCursor);
989//          if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0))+1) {
990          if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && (nbl > baseCursor || differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0))+1)) { // there's a default set before the slices
991            int ndc = differential.getElement().indexOf(diffMatches.get(0));
992            int ndl = findEndOfElement(differential, ndc);
993            ElementDefinition e = processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, 0), contextPathSrc, contextPathDst, 
994                trimDifferential, contextName, resultPathBase, true, null, redirector, srcSD);
995            if (e==null)
996              throw new FHIRException("Did not find single slice: " + diffMatches.get(0).getPath());
997            e.setSlicing(diffMatches.get(0).getSlicing());
998            start++;
999          } else {
1000            // we're just going to accept the differential slicing at face value
1001            ElementDefinition outcome = updateURLs(url, webUrl, currentBase.copy());
1002            outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1003            updateFromBase(outcome, currentBase);
1004
1005            if (!diffMatches.get(0).hasSlicing())
1006              outcome.setSlicing(makeExtensionSlicing());
1007            else
1008              outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
1009            if (!outcome.getPath().startsWith(resultPathBase))
1010              throw new DefinitionException("Adding wrong path");
1011            result.getElement().add(outcome);
1012
1013            // differential - if the first one in the list has a name, we'll process it. Else we'll treat it as the base definition of the slice.
1014            if (!diffMatches.get(0).hasSliceName()) {
1015              updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url, srcSD);
1016              removeStatusExtensions(outcome);
1017              if (!outcome.hasContentReference() && !outcome.hasType()) {
1018                throw new DefinitionException("not done yet");
1019              }
1020              start++;
1021              // result.getElement().remove(result.getElement().size()-1);
1022            } else 
1023              checkExtensionDoco(outcome);
1024          }
1025          // now, for each entry in the diff matches, we're going to process the base item
1026          // our processing scope for base is all the children of the current path
1027          int ndc = diffCursor;
1028          int ndl = diffCursor;
1029          for (int i = start; i < diffMatches.size(); i++) {
1030            // our processing scope for the differential is the item in the list, and all the items before the next one in the list
1031            ndc = differential.getElement().indexOf(diffMatches.get(i));
1032            ndl = findEndOfElement(differential, ndc);
1033/*            if (skipSlicingElement && i == 0) {
1034              ndc = ndc + 1;
1035              if (ndc > ndl)
1036                continue;
1037            }*/
1038            // now we process the base scope repeatedly for each instance of the item in the differential list
1039            processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, i), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true, null, redirector, srcSD);
1040          }
1041          // ok, done with that - next in the base list
1042          baseCursor = nbl+1;
1043          diffCursor = ndl+1;
1044        }
1045      } else {
1046        // the item is already sliced in the base profile.
1047        // here's the rules
1048        //  1. irrespective of whether the slicing is ordered or not, the definition order must be maintained
1049        //  2. slice element names have to match.
1050        //  3. new slices must be introduced at the end
1051        // corallory: you can't re-slice existing slices. is that ok?
1052
1053        // we're going to need this:
1054        String path = currentBase.getPath();
1055        ElementDefinition original = currentBase;
1056
1057        if (diffMatches.isEmpty()) {
1058          if (hasInnerDiffMatches(differential, path, diffCursor, diffLimit, base.getElement(), true)) {
1059            // so we just copy it in
1060            ElementDefinition outcome = updateURLs(url, webUrl, currentBase.copy());
1061            outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1062            updateFromBase(outcome, currentBase);
1063            markDerived(outcome);
1064            if (resultPathBase == null)
1065              resultPathBase = outcome.getPath();
1066            else if (!outcome.getPath().startsWith(resultPathBase))
1067              throw new DefinitionException("Adding wrong path");
1068            result.getElement().add(outcome);
1069            // the profile walks into this, so we need to as well
1070            // did we implicitly step into a new type?
1071            if (baseHasChildren(base, currentBase)) { // not a new type here
1072              processPaths(indent+"  ", result, base, differential, baseCursor+1, diffCursor, baseLimit, diffLimit, url, webUrl, profileName, contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
1073              baseCursor = indexOfFirstNonChild(base, currentBase, baseCursor, baseLimit);
1074            } else {
1075              if (outcome.getType().size() == 0) {
1076                throw new DefinitionException(diffMatches.get(0).getPath()+" has no children ("+differential.getElement().get(diffCursor).getPath()+") and no types in profile "+profileName);
1077              }
1078              if (outcome.getType().size() > 1) {
1079                for (TypeRefComponent t : outcome.getType()) {
1080                  if (!t.getWorkingCode().equals("Reference"))
1081                    throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
1082                }
1083              }
1084              StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
1085              if (dt == null)
1086                throw new DefinitionException("Unknown type "+outcome.getType().get(0)+" at "+diffMatches.get(0).getPath());
1087              contextName = dt.getUrl();
1088              int start = diffCursor;
1089              while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath+"."))
1090                diffCursor++;
1091              processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start, dt.getSnapshot().getElement().size()-1,
1092                  diffCursor-1, url, getWebUrl(dt, webUrl, indent), profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
1093            }
1094            baseCursor++;
1095          } else {
1096            // the differential doesn't say anything about this item
1097            // copy across the currentbase, and all of its children and siblings
1098            while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
1099              ElementDefinition outcome = updateURLs(url, webUrl, base.getElement().get(baseCursor).copy());
1100              outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1101              if (!outcome.getPath().startsWith(resultPathBase))
1102                throw new DefinitionException("Adding wrong path in profile " + profileName + ": "+outcome.getPath()+" vs " + resultPathBase);
1103              result.getElement().add(outcome); // so we just copy it in
1104              baseCursor++;
1105            }
1106          }
1107        } else if (diffsConstrainTypes(diffMatches, cpath, typeList)) {
1108          int start = 0;
1109          int nbl = findEndOfElement(base, baseCursor);
1110          int ndc = differential.getElement().indexOf(diffMatches.get(0));
1111          ElementDefinition elementToRemove = null;
1112          boolean shortCut = (!typeList.isEmpty() && typeList.get(0).type != null) || (diffMatches.get(0).hasSliceName() && !diffMatches.get(0).hasSlicing());
1113          // we come here whether they are sliced in the diff, or whether the short cut is used. 
1114          if (shortCut) {
1115            // this is the short cut method, we've just dived in and specified a type slice. 
1116            // in R3 (and unpatched R4, as a workaround right now...
1117            if (!FHIRVersion.isR4Plus(context.getVersion()) || !newSlicingProcessing) { // newSlicingProcessing is a work around for editorial loop dependency
1118              // we insert a cloned element with the right types at the start of the diffMatches
1119              ElementDefinition ed = new ElementDefinition();
1120              ed.setPath(determineTypeSlicePath(diffMatches.get(0).getPath(), cpath));
1121              for (TypeSlice ts : typeList) 
1122                ed.addType().setCode(ts.type);
1123              ed.setSlicing(new ElementDefinitionSlicingComponent());
1124              ed.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
1125              ed.getSlicing().setRules(SlicingRules.CLOSED);
1126              ed.getSlicing().setOrdered(false);
1127              diffMatches.add(0, ed);
1128              differential.getElement().add(ndc, ed);
1129              elementToRemove = ed;
1130            } else {
1131              // as of R4, this changed; if there's no slice, there's no constraint on the slice types, only one the type. 
1132              // so the element we insert specifies no types (= all types) allowed in the base, not just the listed type. 
1133              // see also discussion here: https://chat.fhir.org/#narrow/stream/179177-conformance/topic/Slicing.20a.20non-repeating.20element             
1134              ElementDefinition ed = new ElementDefinition();
1135              ed.setPath(determineTypeSlicePath(diffMatches.get(0).getPath(), cpath));
1136              ed.setSlicing(new ElementDefinitionSlicingComponent());
1137              ed.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
1138              ed.getSlicing().setRules(SlicingRules.CLOSED);
1139              ed.getSlicing().setOrdered(false);
1140              diffMatches.add(0, ed);
1141              differential.getElement().add(ndc, ed);
1142              elementToRemove = ed;
1143            }
1144          }
1145          int ndl = findEndOfElement(differential, ndc);
1146          // the first element is setting up the slicing
1147
1148          if (diffMatches.get(0).getSlicing().hasOrdered()) {
1149            if (diffMatches.get(0).getSlicing().getOrdered()) {
1150              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.ordered = true");
1151            }
1152          }
1153          if (diffMatches.get(0).getSlicing().hasDiscriminator()) {
1154            if (diffMatches.get(0).getSlicing().getDiscriminator().size() != 1) {
1155              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.count() > 1");
1156            }
1157            if (diffMatches.get(0).getSlicing().getDiscriminatorFirstRep().getType() != DiscriminatorType.TYPE) {
1158              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.type != 'type'");
1159            }
1160            if (!"$this".equals(diffMatches.get(0).getSlicing().getDiscriminatorFirstRep().getPath())) {
1161              throw new FHIRException("Error at path "+cpath+" in "+url+": Type slicing with slicing.discriminator.path != '$this'");
1162            }
1163          }
1164          // check the slice names too while we're at it...
1165          for (TypeSlice ts : typeList) {
1166            if (ts.type != null) {
1167              String tn = rootName(cpath)+Utilities.capitalize(ts.type);
1168              if (!ts.defn.hasSliceName()) {
1169                ts.defn.setSliceName(tn);
1170              } else if (!ts.defn.getSliceName().equals(tn)) {
1171                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice name must be '"+tn+"' but is '"+ts.defn.getSliceName()+"'"); 
1172              } if (!ts.defn.hasType()) {
1173                ts.defn.addType().setCode(ts.type);
1174              } else if (ts.defn.getType().size() > 1) {
1175                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice for type '"+tn+"' has more than one type '"+ts.defn.typeSummary()+"'"); 
1176              } else if (!ts.defn.getType().get(0).getCode().equals(ts.type)) {
1177                throw new FHIRException("Error at path "+(!Utilities.noString(contextPathSrc) ? contextPathSrc : cpath)+": Slice for type '"+tn+"' has wrong type '"+ts.defn.typeSummary()+"'");
1178              }
1179            }
1180          }
1181
1182          // ok passed the checks. 
1183          // copy the root diff, and then process any children it has
1184          ElementDefinition e = processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, 0), contextPathSrc, contextPathDst, 
1185              trimDifferential, contextName, resultPathBase, true, cpath, redirector, srcSD);
1186          if (e==null)
1187            throw new FHIRException("Did not find type root: " + diffMatches.get(0).getPath());
1188          // now set up slicing on the e (cause it was wiped by what we called.
1189          e.setSlicing(new ElementDefinitionSlicingComponent());
1190          e.getSlicing().addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
1191          e.getSlicing().setRules(SlicingRules.CLOSED); // type slicing is always closed; the differential might call it open, but that just means it's not constraining the slices it doesn't mention 
1192          e.getSlicing().setOrdered(false);
1193          start++;
1194
1195          String fixedType = null;
1196          List<BaseTypeSlice> baseSlices = findBaseSlices(base, nbl);
1197          // now process the siblings, which should each be type constrained - and may also have their own children. they may match existing slices
1198          // now we process the base scope repeatedly for each instance of the item in the differential list
1199          for (int i = start; i < diffMatches.size(); i++) {
1200            String type = determineFixedType(diffMatches, fixedType, i);
1201            // our processing scope for the differential is the item in the list, and all the items before the next one in the list
1202            if (diffMatches.get(i).getMin() > 0) {
1203              if (diffMatches.size() > i+1) {
1204                throw new FHIRException("Invalid slicing : there is more than one type slice at "+diffMatches.get(i).getPath()+", but one of them ("+diffMatches.get(i).getSliceName()+") has min = 1, so the other slices cannot exist");
1205              }
1206              fixedType = type;
1207            }
1208            ndc = differential.getElement().indexOf(diffMatches.get(i));
1209            ndl = findEndOfElement(differential, ndc);
1210            int sStart = baseCursor;
1211            int sEnd = nbl;
1212            BaseTypeSlice bs = chooseMatchingBaseSlice(baseSlices, type);
1213            if (bs != null) {
1214              sStart = bs.start;
1215              sEnd = bs.end;
1216              bs.handled = true;
1217            }
1218            processPaths(indent+"  ", result, base, differential, sStart, ndc, sEnd, ndl, url, webUrl, profileName+pathTail(diffMatches, i), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true, cpath, redirector, srcSD);
1219          }
1220          if (elementToRemove != null) {
1221            differential.getElement().remove(elementToRemove);
1222            ndl--;
1223          }
1224          if (fixedType != null) {
1225            for (Iterator<TypeRefComponent> iter = e.getType().iterator(); iter.hasNext(); ) {
1226              TypeRefComponent tr = iter.next();
1227              if (!tr.getCode().equals(fixedType)) {
1228                iter.remove();
1229              }
1230            }
1231          }
1232          for (BaseTypeSlice bs : baseSlices) {
1233            if (!bs.handled) {
1234              // ok we gimme up a fake differential that says nothing, and run that against the slice.
1235              StructureDefinitionDifferentialComponent fakeDiff = new StructureDefinitionDifferentialComponent();
1236              fakeDiff.getElementFirstRep().setPath(bs.defn.getPath());
1237              processPaths(indent+"  ", result, base, fakeDiff, bs.start, 0, bs.end, 0, url, webUrl, profileName+tail(bs.defn.getPath()), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true, cpath, redirector, srcSD);
1238              
1239            }
1240          }
1241          // ok, done with that - next in the base list
1242          baseCursor = baseSlices.get(baseSlices.size()-1).end+1;
1243          diffCursor = ndl+1;
1244          //throw new Error("not done yet - slicing / types @ "+cpath);
1245        } else {
1246          // first - check that the slicing is ok
1247          boolean closed = currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
1248          int diffpos = 0;
1249          boolean isExtension = cpath.endsWith(".extension") || cpath.endsWith(".modifierExtension");
1250          if (diffMatches.get(0).hasSlicing()) { // it might be null if the differential doesn't want to say anything about slicing
1251//            if (!isExtension)
1252//              diffpos++; // if there's a slice on the first, we'll ignore any content it has
1253            ElementDefinitionSlicingComponent dSlice = diffMatches.get(0).getSlicing();
1254            ElementDefinitionSlicingComponent bSlice = currentBase.getSlicing();
1255            if (dSlice.hasOrderedElement() && bSlice.hasOrderedElement() && !orderMatches(dSlice.getOrderedElement(), bSlice.getOrderedElement()))
1256              throw new DefinitionException("Slicing rules on differential ("+summarizeSlicing(dSlice)+") do not match those on base ("+summarizeSlicing(bSlice)+") - order @ "+path+" ("+contextName+")");
1257            if (!discriminatorMatches(dSlice.getDiscriminator(), bSlice.getDiscriminator()))
1258             throw new DefinitionException("Slicing rules on differential ("+summarizeSlicing(dSlice)+") do not match those on base ("+summarizeSlicing(bSlice)+") - disciminator @ "+path+" ("+contextName+")");
1259            if (!currentBase.isChoice() && !ruleMatches(dSlice.getRules(), bSlice.getRules()))
1260             throw new DefinitionException("Slicing rules on differential ("+summarizeSlicing(dSlice)+") do not match those on base ("+summarizeSlicing(bSlice)+") - rule @ "+path+" ("+contextName+")");
1261          }
1262          ElementDefinition outcome = updateURLs(url, webUrl, currentBase.copy());
1263          outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1264          updateFromBase(outcome, currentBase);
1265          if (diffMatches.get(0).hasSlicing() || !diffMatches.get(0).hasSliceName()) {
1266            updateFromSlicing(outcome.getSlicing(), diffMatches.get(0).getSlicing());
1267            updateFromDefinition(outcome, diffMatches.get(0), profileName, closed, url, srcSD); // if there's no slice, we don't want to update the unsliced description
1268            removeStatusExtensions(outcome);
1269          } else if (!diffMatches.get(0).hasSliceName())
1270            diffMatches.get(0).setUserData(GENERATED_IN_SNAPSHOT, outcome); // because of updateFromDefinition isn't called 
1271          
1272          result.getElement().add(outcome);
1273
1274          if (!diffMatches.get(0).hasSliceName()) { // it's not real content, just the slice
1275            diffpos++; 
1276          }
1277          if (hasInnerDiffMatches(differential, cpath, diffpos, diffLimit, base.getElement(), false)) {
1278            int nbl = findEndOfElement(base, baseCursor);
1279            int ndx = differential.getElement().indexOf(diffMatches.get(0));
1280            int ndc = ndx+(diffMatches.get(0).hasSlicing() ? 1 : 0);
1281            int ndl = findEndOfElement(differential, ndx);
1282            if (nbl == baseCursor) {
1283              if (base.getElement().get(baseCursor).getType().size() != 1) {
1284                throw new Error("Differential walks into '"+cpath+" (@ "+diffMatches.get(0).toString()+")', but the base does not, and there is not a single fixed type. The type is "+base.getElement().get(baseCursor).typeSummary()+". This is not handled yet");
1285              }
1286              StructureDefinition dt = getProfileForDataType(base.getElement().get(baseCursor).getType().get(0));
1287              if (dt == null) {
1288                throw new DefinitionException("Unknown type "+outcome.getType().get(0)+" at "+diffMatches.get(0).getPath());
1289              }
1290              contextName = dt.getUrl();
1291              while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath+"."))
1292                diffCursor++;
1293              processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1, ndc, dt.getSnapshot().getElement().size()-1, ndl, 
1294                  url, getWebUrl(dt, webUrl, indent), profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
1295            } else {
1296              processPaths(indent+"  ", result, base, differential, baseCursor+1, ndc, nbl, ndl, 
1297                  url, webUrl, profileName+pathTail(diffMatches, 0), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, false, null, null, srcSD);
1298            }
1299//            throw new Error("Not done yet");
1300//          } else if (currentBase.getType().get(0).getCode().equals("BackboneElement") && diffMatches.size() > 0 && diffMatches.get(0).hasSliceName()) {
1301          } else if (currentBase.getType().get(0).getCode().equals("BackboneElement")) {
1302            // We need to copy children of the backbone element before we start messing around with slices
1303            int nbl = findEndOfElement(base, baseCursor);
1304            for (int i = baseCursor+1; i<=nbl; i++) {
1305              outcome = updateURLs(url, webUrl, base.getElement().get(i).copy());
1306              result.getElement().add(outcome);
1307            }
1308          }
1309
1310          // now, we have two lists, base and diff. we're going to work through base, looking for matches in diff.
1311          List<ElementDefinition> baseMatches = getSiblings(base.getElement(), currentBase);
1312          for (ElementDefinition baseItem : baseMatches) {
1313            baseCursor = base.getElement().indexOf(baseItem);
1314            outcome = updateURLs(url, webUrl, baseItem.copy());
1315            updateFromBase(outcome, currentBase);
1316            outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1317            outcome.setSlicing(null);
1318            if (!outcome.getPath().startsWith(resultPathBase))
1319              throw new DefinitionException("Adding wrong path");
1320            if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getSliceName().equals(outcome.getSliceName())) {
1321              // if there's a diff, we update the outcome with diff
1322              // no? updateFromDefinition(outcome, diffMatches.get(diffpos), profileName, closed, url);
1323              //then process any children
1324              int nbl = findEndOfElement(base, baseCursor);
1325              int ndc = differential.getElement().indexOf(diffMatches.get(diffpos));
1326              int ndl = findEndOfElement(differential, ndc);
1327              // now we process the base scope repeatedly for each instance of the item in the differential list
1328              processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, webUrl, profileName+pathTail(diffMatches, diffpos), contextPathSrc, contextPathDst, closed, contextName, resultPathBase, true, null, redirector, srcSD);
1329              // ok, done with that - now set the cursors for if this is the end
1330              baseCursor = nbl;
1331              diffCursor = ndl+1;
1332              diffpos++;
1333            } else {
1334              result.getElement().add(outcome);
1335              baseCursor++;
1336              // just copy any children on the base
1337              while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
1338                outcome = updateURLs(url, webUrl, base.getElement().get(baseCursor).copy());
1339                outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1340                if (!outcome.getPath().startsWith(resultPathBase))
1341                  throw new DefinitionException("Adding wrong path");
1342                result.getElement().add(outcome);
1343                baseCursor++;
1344              }
1345              //Lloyd - add this for test T15
1346              baseCursor--;
1347            }
1348          }
1349          // finally, we process any remaining entries in diff, which are new (and which are only allowed if the base wasn't closed
1350          boolean checkImplicitTypes = false;
1351          if (closed && diffpos < diffMatches.size()) {
1352            // this is a problem, unless we're on a polymorhpic type and we're going to constrain a slice that actually implicitly exists 
1353            if (currentBase.getPath().endsWith("[x]")) {
1354              checkImplicitTypes = true;
1355            } else {
1356              throw new DefinitionException("The base snapshot marks a slicing as closed, but the differential tries to extend it in "+profileName+" at "+path+" ("+cpath+")");
1357            }
1358          } 
1359          if (diffpos == diffMatches.size()) {
1360//Lloyd This was causing problems w/ Telus
1361//            diffCursor++;
1362          } else {
1363            while (diffpos < diffMatches.size()) {
1364              ElementDefinition diffItem = diffMatches.get(diffpos);
1365              for (ElementDefinition baseItem : baseMatches)
1366                if (baseItem.getSliceName().equals(diffItem.getSliceName()))
1367                  throw new DefinitionException("Named items are out of order in the slice");
1368              outcome = updateURLs(url, webUrl, currentBase.copy());
1369              //            outcome = updateURLs(url, diffItem.copy());
1370              outcome.setPath(fixedPathDest(contextPathDst, outcome.getPath(), redirector, contextPathSrc));
1371              updateFromBase(outcome, currentBase);
1372              outcome.setSlicing(null);
1373              if (!outcome.getPath().startsWith(resultPathBase))
1374                throw new DefinitionException("Adding wrong path");
1375              result.getElement().add(outcome);
1376              updateFromDefinition(outcome, diffItem, profileName, trimDifferential, url, srcSD);
1377              removeStatusExtensions(outcome);
1378              // --- LM Added this
1379              diffCursor = differential.getElement().indexOf(diffItem)+1;
1380              if (!outcome.getType().isEmpty() && (/*outcome.getType().get(0).getCode().equals("Extension") || */differential.getElement().size() > diffCursor) && outcome.getPath().contains(".") && isDataType(outcome.getType())) {  // don't want to do this for the root, since that's base, and we're already processing it
1381                if (!baseWalksInto(base.getElement(), baseCursor)) {
1382                  if (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+".")) {
1383                    if (outcome.getType().size() > 1)
1384                      for (TypeRefComponent t : outcome.getType()) {
1385                        if (!t.getCode().equals("Reference"))
1386                          throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
1387                      }
1388                    TypeRefComponent t = outcome.getType().get(0);
1389                    if (t.getCode().equals("BackboneElement")) {
1390                      int baseStart = base.getElement().indexOf(currentBase)+1;
1391                      int baseMax = baseStart + 1;
1392                      while (baseMax < base.getElement().size() && base.getElement().get(baseMax).getPath().startsWith(currentBase.getPath()+"."))
1393                       baseMax++;
1394                      int start = diffCursor;
1395                      while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
1396                        diffCursor++;
1397                      processPaths(indent+"  ", result, base, differential, baseStart, start-1, baseMax-1,
1398                          diffCursor - 1, url, webUrl, profileName+pathTail(diffMatches, 0), base.getElement().get(0).getPath(), base.getElement().get(0).getPath(), trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
1399                      
1400                    } else {
1401                      StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
1402                      //                if (t.getCode().equals("Extension") && t.hasProfile() && !t.getProfile().contains(":")) {
1403                      // lloydfix                  dt = 
1404                      //                }
1405                      if (dt == null)
1406                        throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") for type "+typeCode(outcome.getType())+" in profile "+profileName+", but can't find type");
1407                      contextName = dt.getUrl();
1408                      int start = diffCursor;
1409                      while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
1410                        diffCursor++;
1411                      processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start-1, dt.getSnapshot().getElement().size()-1,
1412                          diffCursor - 1, url, getWebUrl(dt, webUrl, indent), profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false, null, redirector, srcSD);
1413                    }
1414                  }
1415                }
1416              }
1417              // ---
1418              diffpos++;
1419            }
1420          }
1421          baseCursor++;
1422        }
1423      }
1424    }
1425    
1426    int i = 0;
1427    for (ElementDefinition e : result.getElement()) {
1428      i++;
1429      if (e.hasMinElement() && e.getMinElement().getValue()==null)
1430        throw new Error("null min");
1431    }
1432    return res;
1433  }
1434
1435
1436  public String determineFixedType(List<ElementDefinition> diffMatches, String fixedType, int i) {
1437    if (diffMatches.get(i).getType().size() == 0 && diffMatches.get(i).hasSliceName()) {
1438      String n = tail(diffMatches.get(i).getPath()).replace("[x]", "");
1439      String t = diffMatches.get(i).getSliceName().substring(n.length());
1440      if (isDataType(t)) {
1441        fixedType = t;
1442      } else if (isPrimitive(Utilities.uncapitalize(t))) {
1443        fixedType = Utilities.uncapitalize(t);
1444      } else {
1445        throw new FHIRException("Unexpected condition in differential: type-slice.type-list.size() == 10 and implicit slice name does not contain a valid type ('"+t+"'?) at "+diffMatches.get(i).getPath()+"/"+diffMatches.get(i).getSliceName());
1446      }                
1447    } else if (diffMatches.get(i).getType().size() == 1) {
1448      fixedType = diffMatches.get(i).getType().get(0).getCode();
1449    } else {
1450      throw new FHIRException("Unexpected condition in differential: type-slice.type-list.size() != 1 at "+diffMatches.get(i).getPath()+"/"+diffMatches.get(i).getSliceName());
1451    }
1452    return fixedType;
1453  }
1454
1455
1456  private BaseTypeSlice chooseMatchingBaseSlice(List<BaseTypeSlice> baseSlices, String type) {
1457    for (BaseTypeSlice bs : baseSlices) {
1458      if (bs.type.equals(type)) {
1459        return bs;
1460      }
1461    }
1462    return null;
1463  }
1464
1465
1466  private List<BaseTypeSlice> findBaseSlices(StructureDefinitionSnapshotComponent list, int start) {
1467    List<BaseTypeSlice> res = new ArrayList<>();
1468    ElementDefinition base = list.getElement().get(start);
1469    int i = start + 1;
1470    while (i <  list.getElement().size() && list.getElement().get(i).getPath().startsWith(base.getPath()+".")) {
1471      i++;      
1472    };
1473    while (i <  list.getElement().size() && list.getElement().get(i).getPath().equals(base.getPath()) && list.getElement().get(i).hasSliceName()) {
1474      int s = i;
1475      i++;
1476      while (i <  list.getElement().size() && list.getElement().get(i).getPath().startsWith(base.getPath()+".")) {
1477        i++;      
1478      };
1479      res.add(new BaseTypeSlice(list.getElement().get(s), list.getElement().get(s).getTypeFirstRep().getCode(), s, i-1));
1480    }
1481    return res;
1482  }
1483
1484
1485  private String getWebUrl(StructureDefinition dt, String webUrl, String indent) {
1486    if (dt.hasUserData("path")) {
1487      // this is a hack, but it works for now, since we don't have deep folders
1488      String url = dt.getUserString("path");
1489      int i = url.lastIndexOf("/");
1490      if (i < 1) {
1491        return defWebRoot;
1492      } else {
1493        return url.substring(0, i+1);
1494      }
1495    } else {  
1496      return webUrl;
1497    }
1498  }
1499
1500
1501  private void removeStatusExtensions(ElementDefinition outcome) {
1502    outcome.removeExtension(ToolingExtensions.EXT_FMM_LEVEL);
1503    outcome.removeExtension(ToolingExtensions.EXT_STANDARDS_STATUS);
1504    outcome.removeExtension(ToolingExtensions.EXT_NORMATIVE_VERSION);
1505    outcome.removeExtension(ToolingExtensions.EXT_WORKGROUP);    
1506  }
1507
1508
1509  private String descED(List<ElementDefinition> list, int index) {
1510    return index >=0 && index < list.size() ? list.get(index).present() : "X";
1511  }
1512
1513
1514  private boolean baseHasChildren(StructureDefinitionSnapshotComponent base, ElementDefinition ed) {
1515    int index = base.getElement().indexOf(ed);
1516    if (index == -1 || index >= base.getElement().size()-1)
1517      return false;
1518    String p = base.getElement().get(index+1).getPath();
1519    return isChildOf(p, ed.getPath());
1520  }
1521
1522
1523  private boolean isChildOf(String sub, String focus) {
1524    if (focus.endsWith("[x]")) {
1525      focus = focus.substring(0, focus.length()-3);
1526      return sub.startsWith(focus);
1527    } else 
1528      return sub.startsWith(focus+".");
1529  }
1530
1531
1532  private int indexOfFirstNonChild(StructureDefinitionSnapshotComponent base, ElementDefinition currentBase, int i, int baseLimit) {
1533    return baseLimit+1;
1534  }
1535
1536
1537  private String rootName(String cpath) {
1538    String t = tail(cpath);
1539    return t.replace("[x]", "");
1540  }
1541
1542
1543  private String determineTypeSlicePath(String path, String cpath) {
1544    String headP = path.substring(0, path.lastIndexOf("."));
1545//    String tailP = path.substring(path.lastIndexOf(".")+1);
1546    String tailC = cpath.substring(cpath.lastIndexOf(".")+1);
1547    return headP+"."+tailC;
1548  }
1549
1550
1551  private boolean isImplicitSlicing(ElementDefinition ed, String path) {
1552    if (ed == null || ed.getPath() == null || path == null)
1553      return false;
1554    if (path.equals(ed.getPath()))
1555      return false;
1556    boolean ok = path.endsWith("[x]") && ed.getPath().startsWith(path.substring(0, path.length()-3));
1557    return ok;
1558  }
1559
1560
1561  private boolean diffsConstrainTypes(List<ElementDefinition> diffMatches, String cPath, List<TypeSlice> typeList) {
1562//    if (diffMatches.size() < 2)
1563//      return false;
1564    String p = diffMatches.get(0).getPath();
1565    if (!p.endsWith("[x]") && !cPath.endsWith("[x]"))
1566      return false;
1567    typeList.clear();
1568    String rn = tail(cPath);
1569    rn = rn.substring(0, rn.length()-3);
1570    for (int i = 0; i < diffMatches.size(); i++) {
1571      ElementDefinition ed = diffMatches.get(i);
1572      String n = tail(ed.getPath());
1573      if (!n.startsWith(rn))
1574        return false;
1575      String s = n.substring(rn.length());
1576      if (!s.contains(".")) {
1577        if (ed.hasSliceName() && ed.getType().size() == 1) {
1578          typeList.add(new TypeSlice(ed, ed.getTypeFirstRep().getWorkingCode()));
1579        } else if (ed.hasSliceName() && ed.getType().size() == 0) {
1580          String tn = ed.getSliceName().substring(rn.length());
1581          if (isDataType(tn)) {
1582            typeList.add(new TypeSlice(ed, tn));
1583          } else if (isDataType(Utilities.uncapitalize(tn))) {
1584            typeList.add(new TypeSlice(ed, Utilities.uncapitalize(tn)));
1585          }
1586        } else if (!ed.hasSliceName() && !s.equals("[x]")) {
1587          if (isDataType(s))
1588            typeList.add(new TypeSlice(ed, s));
1589          else if (isConstrainedDataType(s))
1590            typeList.add(new TypeSlice(ed, baseType(s)));
1591          else if (isPrimitive(Utilities.uncapitalize(s)))
1592            typeList.add(new TypeSlice(ed, Utilities.uncapitalize(s)));
1593        } else if (!ed.hasSliceName() && s.equals("[x]"))
1594          typeList.add(new TypeSlice(ed, null));
1595      }
1596    }
1597    return true;
1598  }
1599
1600
1601  private List<ElementRedirection> redirectorStack(List<ElementRedirection> redirector, ElementDefinition outcome, String path) {
1602    List<ElementRedirection> result = new ArrayList<ElementRedirection>();
1603    result.addAll(redirector);
1604    result.add(new ElementRedirection(outcome, path));
1605    return result;
1606  }
1607
1608
1609  private List<TypeRefComponent> getByTypeName(List<TypeRefComponent> type, String t) {
1610    List<TypeRefComponent> res = new ArrayList<TypeRefComponent>();
1611    for (TypeRefComponent tr : type) {
1612      if (t.equals(tr.getWorkingCode()))
1613          res.add(tr);
1614    }
1615    return res;
1616  }
1617
1618
1619  private void replaceFromContentReference(ElementDefinition outcome, ElementDefinition tgt) {
1620    outcome.setContentReference(null);
1621    outcome.getType().clear(); // though it should be clear anyway
1622    outcome.getType().addAll(tgt.getType());    
1623  }
1624
1625
1626  private boolean baseWalksInto(List<ElementDefinition> elements, int cursor) {
1627    if (cursor >= elements.size())
1628      return false;
1629    String path = elements.get(cursor).getPath();
1630    String prevPath = elements.get(cursor - 1).getPath();
1631    return path.startsWith(prevPath + ".");
1632  }
1633
1634
1635  private ElementDefinition fillOutFromBase(ElementDefinition profile, ElementDefinition usage) throws FHIRFormatError {
1636    ElementDefinition res = profile.copy();
1637    if (!res.hasSliceName())
1638      res.setSliceName(usage.getSliceName());
1639    if (!res.hasLabel())
1640      res.setLabel(usage.getLabel());
1641    for (Coding c : usage.getCode())
1642      if (!res.hasCode(c))
1643        res.addCode(c);
1644    
1645    if (!res.hasDefinition())
1646      res.setDefinition(usage.getDefinition());
1647    if (!res.hasShort() && usage.hasShort())
1648      res.setShort(usage.getShort());
1649    if (!res.hasComment() && usage.hasComment())
1650      res.setComment(usage.getComment());
1651    if (!res.hasRequirements() && usage.hasRequirements())
1652      res.setRequirements(usage.getRequirements());
1653    for (StringType c : usage.getAlias())
1654      if (!res.hasAlias(c.getValue()))
1655        res.addAlias(c.getValue());
1656    if (!res.hasMin() && usage.hasMin())
1657      res.setMin(usage.getMin());
1658    if (!res.hasMax() && usage.hasMax())
1659      res.setMax(usage.getMax());
1660     
1661    if (!res.hasFixed() && usage.hasFixed())
1662      res.setFixed(usage.getFixed());
1663    if (!res.hasPattern() && usage.hasPattern())
1664      res.setPattern(usage.getPattern());
1665    if (!res.hasExample() && usage.hasExample())
1666      res.setExample(usage.getExample());
1667    if (!res.hasMinValue() && usage.hasMinValue())
1668      res.setMinValue(usage.getMinValue());
1669    if (!res.hasMaxValue() && usage.hasMaxValue())
1670      res.setMaxValue(usage.getMaxValue());     
1671    if (!res.hasMaxLength() && usage.hasMaxLength())
1672      res.setMaxLength(usage.getMaxLength());
1673    if (!res.hasMustSupport() && usage.hasMustSupport())
1674      res.setMustSupport(usage.getMustSupport());
1675    if (!res.hasBinding() && usage.hasBinding())
1676      res.setBinding(usage.getBinding().copy());
1677    for (ElementDefinitionConstraintComponent c : usage.getConstraint())
1678      if (!res.hasConstraint(c.getKey()))
1679        res.addConstraint(c);
1680    for (Extension e : usage.getExtension()) {
1681      if (!res.hasExtension(e.getUrl()))
1682        res.addExtension(e.copy());
1683    }
1684    
1685    return res;
1686  }
1687
1688
1689  private boolean checkExtensionDoco(ElementDefinition base) {
1690    // see task 3970. For an extension, there's no point copying across all the underlying definitional stuff
1691    boolean isExtension = base.getPath().equals("Extension") || base.getPath().endsWith(".extension") || base.getPath().endsWith(".modifierExtension");
1692    if (isExtension) {
1693      base.setDefinition("An Extension");
1694      base.setShort("Extension");
1695      base.setCommentElement(null);
1696      base.setRequirementsElement(null);
1697      base.getAlias().clear();
1698      base.getMapping().clear();
1699    }
1700    return isExtension;
1701  }
1702
1703
1704  private String pathTail(List<ElementDefinition> diffMatches, int i) {
1705    
1706    ElementDefinition d = diffMatches.get(i);
1707    String s = d.getPath().contains(".") ? d.getPath().substring(d.getPath().lastIndexOf(".")+1) : d.getPath();
1708    return "."+s + (d.hasType() && d.getType().get(0).hasProfile() ? "["+d.getType().get(0).getProfile()+"]" : "");
1709  }
1710
1711
1712  private void markDerived(ElementDefinition outcome) {
1713    for (ElementDefinitionConstraintComponent inv : outcome.getConstraint())
1714      inv.setUserData(IS_DERIVED, true);
1715  }
1716
1717
1718  public static String summarizeSlicing(ElementDefinitionSlicingComponent slice) {
1719    StringBuilder b = new StringBuilder();
1720    boolean first = true;
1721    for (ElementDefinitionSlicingDiscriminatorComponent d : slice.getDiscriminator()) {
1722      if (first)
1723        first = false;
1724      else
1725        b.append(", ");
1726      b.append(d.getType().toCode()+":"+d.getPath());
1727    }
1728    b.append(" (");
1729    if (slice.hasOrdered())
1730      b.append(slice.getOrdered() ? "ordered" : "unordered");
1731    b.append("/");
1732    if (slice.hasRules())
1733      b.append(slice.getRules().toCode());
1734    b.append(")");
1735    if (slice.hasDescription()) {
1736      b.append(" \"");
1737      b.append(slice.getDescription());
1738      b.append("\"");
1739    }
1740    return b.toString();
1741  }
1742
1743
1744  private void updateFromBase(ElementDefinition derived, ElementDefinition base) {
1745    if (base.hasBase()) {
1746      if (!derived.hasBase())
1747        derived.setBase(new ElementDefinitionBaseComponent());
1748      derived.getBase().setPath(base.getBase().getPath());
1749      derived.getBase().setMin(base.getBase().getMin());
1750      derived.getBase().setMax(base.getBase().getMax());
1751    } else {
1752      if (!derived.hasBase())
1753        derived.setBase(new ElementDefinitionBaseComponent());
1754      derived.getBase().setPath(base.getPath());
1755      derived.getBase().setMin(base.getMin());
1756      derived.getBase().setMax(base.getMax());
1757    }
1758  }
1759
1760
1761  private boolean pathStartsWith(String p1, String p2) {
1762    return p1.startsWith(p2) || (p2.endsWith("[x].") && p1.startsWith(p2.substring(0, p2.length()-4)));
1763  }
1764
1765  private boolean pathMatches(String p1, String p2) {
1766    return p1.equals(p2) || (p2.endsWith("[x]") && p1.startsWith(p2.substring(0, p2.length()-3)) && !p1.substring(p2.length()-3).contains("."));
1767  }
1768
1769
1770  private String fixedPathSource(String contextPath, String pathSimple, List<ElementRedirection> redirector) {
1771    if (contextPath == null)
1772      return pathSimple;
1773//    String ptail = pathSimple.substring(contextPath.length() + 1);
1774    if (redirector.size() > 0) {
1775      String ptail = pathSimple.substring(contextPath.length()+1);
1776      return redirector.get(redirector.size()-1).getPath()+"."+ptail;
1777//      return contextPath+"."+tail(redirector.getPath())+"."+ptail.substring(ptail.indexOf(".")+1);
1778    } else {
1779      String ptail = pathSimple.substring(pathSimple.indexOf(".")+1);
1780      return contextPath+"."+ptail;
1781    }
1782  }
1783  
1784  private String fixedPathDest(String contextPath, String pathSimple, List<ElementRedirection> redirector, String redirectSource) {
1785    String s;
1786    if (contextPath == null)
1787      s = pathSimple;
1788    else {
1789      if (redirector.size() > 0) {
1790        String ptail = pathSimple.substring(redirectSource.length() + 1);
1791  //      ptail = ptail.substring(ptail.indexOf(".")+1);
1792        s = contextPath+"."+/*tail(redirector.getPath())+"."+*/ptail;
1793      } else {
1794        String ptail = pathSimple.substring(pathSimple.indexOf(".")+1);
1795        s = contextPath+"."+ptail;
1796      }
1797    }
1798    return s;
1799  }  
1800
1801  private StructureDefinition getProfileForDataType(TypeRefComponent type)  {
1802    StructureDefinition sd = null;
1803    if (type.hasProfile()) {
1804      sd = context.fetchResource(StructureDefinition.class, type.getProfile().get(0).getValue());
1805      if (sd == null)
1806        System.out.println("Failed to find referenced profile: " + type.getProfile());
1807    }
1808    if (sd == null)
1809      sd = context.fetchTypeDefinition(type.getWorkingCode());
1810    if (sd == null)
1811      System.out.println("XX: failed to find profle for type: " + type.getWorkingCode()); // debug GJM
1812    return sd;
1813  }
1814
1815  private StructureDefinition getProfileForDataType(String type)  {
1816    StructureDefinition sd = context.fetchTypeDefinition(type);
1817    if (sd == null)
1818      System.out.println("XX: failed to find profle for type: " + type); // debug GJM
1819    return sd;
1820  }
1821
1822
1823  public static String typeCode(List<TypeRefComponent> types) {
1824    StringBuilder b = new StringBuilder();
1825    boolean first = true;
1826    for (TypeRefComponent type : types) {
1827      if (first) first = false; else b.append(", ");
1828      b.append(type.getWorkingCode());
1829      if (type.hasTargetProfile())
1830        b.append("{"+type.getTargetProfile()+"}");
1831      else if (type.hasProfile())
1832        b.append("{"+type.getProfile()+"}");
1833    }
1834    return b.toString();
1835  }
1836
1837
1838  private boolean isDataType(List<TypeRefComponent> types) {
1839    if (types.isEmpty())
1840      return false;
1841    for (TypeRefComponent type : types) {
1842      String t = type.getWorkingCode();
1843      if (!isDataType(t) && !isPrimitive(t))
1844        return false;
1845    }
1846    return true;
1847  }
1848
1849
1850  /**
1851   * Finds internal references in an Element's Binding and StructureDefinition references (in TypeRef) and bases them on the given url
1852   * @param url - the base url to use to turn internal references into absolute references
1853   * @param element - the Element to update
1854   * @return - the updated Element
1855   */
1856  private ElementDefinition updateURLs(String url, String webUrl, ElementDefinition element) {
1857    if (element != null) {
1858      ElementDefinition defn = element;
1859      if (defn.hasBinding() && defn.getBinding().hasValueSet() && defn.getBinding().getValueSet().startsWith("#"))
1860        defn.getBinding().setValueSet(url+defn.getBinding().getValueSet());
1861      for (TypeRefComponent t : defn.getType()) {
1862        for (UriType u : t.getProfile()) {
1863          if (u.getValue().startsWith("#"))
1864            u.setValue(url+t.getProfile());
1865        }
1866        for (UriType u : t.getTargetProfile()) {
1867          if (u.getValue().startsWith("#"))
1868            u.setValue(url+t.getTargetProfile());
1869        }
1870      }
1871      if (webUrl != null) {
1872        // also, must touch up the markdown
1873        if (element.hasDefinition())
1874          element.setDefinition(processRelativeUrls(element.getDefinition(), webUrl));
1875        if (element.hasComment())
1876          element.setComment(processRelativeUrls(element.getComment(), webUrl));
1877        if (element.hasRequirements())
1878          element.setRequirements(processRelativeUrls(element.getRequirements(), webUrl));
1879        if (element.hasMeaningWhenMissing())
1880          element.setMeaningWhenMissing(processRelativeUrls(element.getMeaningWhenMissing(), webUrl));
1881      }
1882    }
1883    return element;
1884  }
1885
1886  private String processRelativeUrls(String markdown, String webUrl) {
1887    StringBuilder b = new StringBuilder();
1888    int i = 0;
1889    while (i < markdown.length()) {
1890      if (i < markdown.length()-3 && markdown.substring(i, i+2).equals("](")) {
1891         int j = i + 2;
1892        while (j < markdown.length() && markdown.charAt(j) != ')')
1893          j++;
1894        if (j < markdown.length()) {
1895          String url = markdown.substring(i+2, j);
1896          if (!Utilities.isAbsoluteUrl(url) && !url.startsWith("..")) {
1897            b.append("](");
1898            b.append(webUrl);
1899            i = i + 1;
1900          } else
1901            b.append(markdown.charAt(i));
1902        } else 
1903          b.append(markdown.charAt(i));
1904      } else {
1905        b.append(markdown.charAt(i));
1906      }
1907      i++;
1908    }
1909    return b.toString();
1910  }
1911
1912
1913  private List<ElementDefinition> getSiblings(List<ElementDefinition> list, ElementDefinition current) {
1914    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
1915    String path = current.getPath();
1916    int cursor = list.indexOf(current)+1;
1917    while (cursor < list.size() && list.get(cursor).getPath().length() >= path.length()) {
1918      if (pathMatches(list.get(cursor).getPath(), path))
1919        result.add(list.get(cursor));
1920      cursor++;
1921    }
1922    return result;
1923  }
1924
1925  private void updateFromSlicing(ElementDefinitionSlicingComponent dst, ElementDefinitionSlicingComponent src) {
1926    if (src.hasOrderedElement())
1927      dst.setOrderedElement(src.getOrderedElement().copy());
1928    if (src.hasDiscriminator()) {
1929      //    dst.getDiscriminator().addAll(src.getDiscriminator());  Can't use addAll because it uses object equality, not string equality
1930      for (ElementDefinitionSlicingDiscriminatorComponent s : src.getDiscriminator()) {
1931        boolean found = false;
1932        for (ElementDefinitionSlicingDiscriminatorComponent d : dst.getDiscriminator()) {
1933          if (matches(d, s)) {
1934            found = true;
1935            break;
1936          }
1937        }
1938        if (!found)
1939          dst.getDiscriminator().add(s);
1940      }
1941    }
1942    if (src.hasRulesElement())
1943      dst.setRulesElement(src.getRulesElement().copy());
1944  }
1945
1946  private boolean orderMatches(BooleanType diff, BooleanType base) {
1947    return (diff == null) || (base == null) || (diff.getValue() == base.getValue());
1948  }
1949
1950  private boolean discriminatorMatches(List<ElementDefinitionSlicingDiscriminatorComponent> diff, List<ElementDefinitionSlicingDiscriminatorComponent> base) {
1951    if (diff.isEmpty() || base.isEmpty())
1952        return true;
1953    if (diff.size() != base.size())
1954        return false;
1955    for (int i = 0; i < diff.size(); i++)
1956        if (!matches(diff.get(i), base.get(i)))
1957                return false;
1958    return true;
1959  }
1960
1961  private boolean matches(ElementDefinitionSlicingDiscriminatorComponent c1, ElementDefinitionSlicingDiscriminatorComponent c2) {
1962    return c1.getType().equals(c2.getType()) && c1.getPath().equals(c2.getPath());
1963  }
1964
1965
1966  private boolean ruleMatches(SlicingRules diff, SlicingRules base) {
1967    return (diff == null) || (base == null) || (diff == base) || (base == SlicingRules.OPEN) ||
1968        ((diff == SlicingRules.OPENATEND && base == SlicingRules.CLOSED));
1969  }
1970
1971  private boolean isSlicedToOneOnly(ElementDefinition e) {
1972    return (e.hasSlicing() && e.hasMaxElement() && e.getMax().equals("1"));
1973  }
1974
1975  private ElementDefinitionSlicingComponent makeExtensionSlicing() {
1976        ElementDefinitionSlicingComponent slice = new ElementDefinitionSlicingComponent();
1977    slice.addDiscriminator().setPath("url").setType(DiscriminatorType.VALUE);
1978    slice.setOrdered(false);
1979    slice.setRules(SlicingRules.OPEN);
1980    return slice;
1981  }
1982
1983  private boolean isExtension(ElementDefinition currentBase) {
1984    return currentBase.getPath().endsWith(".extension") || currentBase.getPath().endsWith(".modifierExtension");
1985  }
1986
1987  private boolean hasInnerDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, List<ElementDefinition> base, boolean allowSlices) throws DefinitionException {
1988    end = Math.min(context.getElement().size(), end);
1989    start = Math.max(0,  start);
1990    
1991    for (int i = start; i <= end; i++) {
1992      ElementDefinition ed = context.getElement().get(i);
1993      String statedPath = ed.getPath();
1994      if (!allowSlices && statedPath.equals(path) && ed.hasSliceName()) {
1995        return false;
1996      } else if (statedPath.startsWith(path+".")) {
1997        return true;
1998      } else if (path.endsWith("[x]") && statedPath.startsWith(path.substring(0, path.length() -3))) {
1999        return true;
2000      } else if (i != start && !allowSlices && !statedPath.startsWith(path+".")) {
2001        break;
2002      } else if (i != start && allowSlices && !statedPath.startsWith(path)) {
2003        break;
2004      }
2005    }
2006    return false;
2007  }
2008
2009  private List<ElementDefinition> getDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, String profileName) throws DefinitionException {
2010    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
2011    String[] p = path.split("\\.");
2012    for (int i = start; i <= end; i++) {
2013      String statedPath = context.getElement().get(i).getPath();
2014      String[] sp = statedPath.split("\\.");
2015      boolean ok = sp.length == p.length;
2016      for (int j = 0; j < p.length; j++) {
2017        ok = ok && sp.length > j && (p[j].equals(sp[j]) || isSameBase(p[j], sp[j]));
2018      }
2019// don't need this debug check - everything is ok      
2020//      if (ok != (statedPath.equals(path) || (path.endsWith("[x]") && statedPath.length() > path.length() - 2 && 
2021//            statedPath.substring(0, path.length()-3).equals(path.substring(0, path.length()-3)) && 
2022//            (statedPath.length() < path.length() || !statedPath.substring(path.length()).contains("."))))) {
2023//        System.out.println("mismatch in paths: "+statedPath +" vs " +path);
2024//      }
2025      if (ok) {
2026        /* 
2027         * Commenting this out because it raises warnings when profiling inherited elements.  For example,
2028         * Error: unknown element 'Bundle.meta.profile' (or it is out of order) in profile ... (looking for 'Bundle.entry')
2029         * Not sure we have enough information here to do the check properly.  Might be better done when we're sorting the profile?
2030
2031        if (i != start && result.isEmpty() && !path.startsWith(context.getElement().get(start).getPath()))
2032          messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.VALUE, "StructureDefinition.differential.element["+Integer.toString(start)+"]", "Error: unknown element '"+context.getElement().get(start).getPath()+"' (or it is out of order) in profile '"+url+"' (looking for '"+path+"')", IssueSeverity.WARNING));
2033
2034         */
2035        result.add(context.getElement().get(i));
2036      }
2037    }
2038    return result;
2039  }
2040
2041
2042  public boolean isSameBase(String p, String sp) {
2043    return (p.endsWith("[x]") && sp.startsWith(p.substring(0, p.length()-3))) || (sp.endsWith("[x]") && p.startsWith(sp.substring(0, sp.length()-3))) ;
2044  }
2045
2046  private int findEndOfElement(StructureDefinitionDifferentialComponent context, int cursor) {
2047            int result = cursor;
2048            if (cursor >= context.getElement().size())
2049              return result;
2050            String path = context.getElement().get(cursor).getPath()+".";
2051            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
2052              result++;
2053            return result;
2054          }
2055
2056  private int findEndOfElement(StructureDefinitionSnapshotComponent context, int cursor) {
2057            int result = cursor;
2058            String path = context.getElement().get(cursor).getPath()+".";
2059            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
2060              result++;
2061            return result;
2062          }
2063
2064  private boolean unbounded(ElementDefinition definition) {
2065    StringType max = definition.getMaxElement();
2066    if (max == null)
2067      return false; // this is not valid
2068    if (max.getValue().equals("1"))
2069      return false;
2070    if (max.getValue().equals("0"))
2071      return false;
2072    return true;
2073  }
2074
2075  private void updateFromDefinition(ElementDefinition dest, ElementDefinition source, String pn, boolean trimDifferential, String purl, StructureDefinition srcSD) throws DefinitionException, FHIRException {
2076    source.setUserData(GENERATED_IN_SNAPSHOT, dest);
2077    // we start with a clone of the base profile ('dest') and we copy from the profile ('source')
2078    // over the top for anything the source has
2079    ElementDefinition base = dest;
2080    ElementDefinition derived = source;
2081    derived.setUserData(DERIVATION_POINTER, base);
2082    boolean isExtension = checkExtensionDoco(base);
2083
2084
2085    // Before applying changes, apply them to what's in the profile
2086    StructureDefinition profile = null;
2087    if (base.hasSliceName())
2088      profile = base.getType().size() == 1 && base.getTypeFirstRep().hasProfile() ? context.fetchResource(StructureDefinition.class, base.getTypeFirstRep().getProfile().get(0).getValue()) : null;
2089    if (profile==null)
2090      profile = source.getType().size() == 1 && source.getTypeFirstRep().hasProfile() ? context.fetchResource(StructureDefinition.class, source.getTypeFirstRep().getProfile().get(0).getValue()) : null;
2091    if (profile != null) {
2092      ElementDefinition e = profile.getSnapshot().getElement().get(0);
2093      base.setDefinition(e.getDefinition());
2094      base.setShort(e.getShort());
2095      if (e.hasCommentElement())
2096        base.setCommentElement(e.getCommentElement());
2097      if (e.hasRequirementsElement())
2098        base.setRequirementsElement(e.getRequirementsElement());
2099      base.getAlias().clear();
2100      base.getAlias().addAll(e.getAlias());
2101      base.getMapping().clear();
2102      base.getMapping().addAll(e.getMapping());
2103    } 
2104    if (derived != null) {
2105      if (derived.hasSliceName()) {
2106        base.setSliceName(derived.getSliceName());
2107      }
2108      
2109      if (derived.hasShortElement()) {
2110        if (!Base.compareDeep(derived.getShortElement(), base.getShortElement(), false))
2111          base.setShortElement(derived.getShortElement().copy());
2112        else if (trimDifferential)
2113          derived.setShortElement(null);
2114        else if (derived.hasShortElement())
2115          derived.getShortElement().setUserData(DERIVATION_EQUALS, true);
2116      }
2117
2118      if (derived.hasDefinitionElement()) {
2119        if (derived.getDefinition().startsWith("..."))
2120          base.setDefinition(base.getDefinition()+"\r\n"+derived.getDefinition().substring(3));
2121        else if (!Base.compareDeep(derived.getDefinitionElement(), base.getDefinitionElement(), false))
2122          base.setDefinitionElement(derived.getDefinitionElement().copy());
2123        else if (trimDifferential)
2124          derived.setDefinitionElement(null);
2125        else if (derived.hasDefinitionElement())
2126          derived.getDefinitionElement().setUserData(DERIVATION_EQUALS, true);
2127      }
2128
2129      if (derived.hasCommentElement()) {
2130        if (derived.getComment().startsWith("..."))
2131          base.setComment(base.getComment()+"\r\n"+derived.getComment().substring(3));
2132        else if (derived.hasCommentElement()!= base.hasCommentElement() || !Base.compareDeep(derived.getCommentElement(), base.getCommentElement(), false))
2133          base.setCommentElement(derived.getCommentElement().copy());
2134        else if (trimDifferential)
2135          base.setCommentElement(derived.getCommentElement().copy());
2136        else if (derived.hasCommentElement())
2137          derived.getCommentElement().setUserData(DERIVATION_EQUALS, true);
2138      }
2139
2140      if (derived.hasLabelElement()) {
2141        if (derived.getLabel().startsWith("..."))
2142          base.setLabel(base.getLabel()+"\r\n"+derived.getLabel().substring(3));
2143        else if (!base.hasLabelElement() || !Base.compareDeep(derived.getLabelElement(), base.getLabelElement(), false))
2144          base.setLabelElement(derived.getLabelElement().copy());
2145        else if (trimDifferential)
2146          base.setLabelElement(derived.getLabelElement().copy());
2147        else if (derived.hasLabelElement())
2148          derived.getLabelElement().setUserData(DERIVATION_EQUALS, true);
2149      }
2150
2151      if (derived.hasRequirementsElement()) {
2152        if (derived.getRequirements().startsWith("..."))
2153          base.setRequirements(base.getRequirements()+"\r\n"+derived.getRequirements().substring(3));
2154        else if (!base.hasRequirementsElement() || !Base.compareDeep(derived.getRequirementsElement(), base.getRequirementsElement(), false))
2155          base.setRequirementsElement(derived.getRequirementsElement().copy());
2156        else if (trimDifferential)
2157          base.setRequirementsElement(derived.getRequirementsElement().copy());
2158        else if (derived.hasRequirementsElement())
2159          derived.getRequirementsElement().setUserData(DERIVATION_EQUALS, true);
2160      }
2161      // sdf-9
2162      if (derived.hasRequirements() && !base.getPath().contains("."))
2163        derived.setRequirements(null);
2164      if (base.hasRequirements() && !base.getPath().contains("."))
2165        base.setRequirements(null);
2166
2167      if (derived.hasAlias()) {
2168        if (!Base.compareDeep(derived.getAlias(), base.getAlias(), false))
2169          for (StringType s : derived.getAlias()) {
2170            if (!base.hasAlias(s.getValue()))
2171              base.getAlias().add(s.copy());
2172          }
2173        else if (trimDifferential)
2174          derived.getAlias().clear();
2175        else
2176          for (StringType t : derived.getAlias())
2177            t.setUserData(DERIVATION_EQUALS, true);
2178      }
2179
2180      if (derived.hasMinElement()) {
2181        if (!Base.compareDeep(derived.getMinElement(), base.getMinElement(), false)) {
2182          if (derived.getMin() < base.getMin() && !derived.hasSliceName()) // in a slice, minimum cardinality rules do not apply 
2183            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+source.getPath(), "Element "+base.getPath()+": derived min ("+Integer.toString(derived.getMin())+") cannot be less than base min ("+Integer.toString(base.getMin())+")", ValidationMessage.IssueSeverity.ERROR));
2184          base.setMinElement(derived.getMinElement().copy());
2185        } else if (trimDifferential)
2186          derived.setMinElement(null);
2187        else
2188          derived.getMinElement().setUserData(DERIVATION_EQUALS, true);
2189      }
2190
2191      if (derived.hasMaxElement()) {
2192        if (!Base.compareDeep(derived.getMaxElement(), base.getMaxElement(), false)) {
2193          if (isLargerMax(derived.getMax(), base.getMax()))
2194            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+source.getPath(), "Element "+base.getPath()+": derived max ("+derived.getMax()+") cannot be greater than base max ("+base.getMax()+")", ValidationMessage.IssueSeverity.ERROR));
2195          base.setMaxElement(derived.getMaxElement().copy());
2196        } else if (trimDifferential)
2197          derived.setMaxElement(null);
2198        else
2199          derived.getMaxElement().setUserData(DERIVATION_EQUALS, true);
2200      }
2201
2202      if (derived.hasFixed()) {
2203        if (!Base.compareDeep(derived.getFixed(), base.getFixed(), true)) {
2204          base.setFixed(derived.getFixed().copy());
2205        } else if (trimDifferential)
2206          derived.setFixed(null);
2207        else
2208          derived.getFixed().setUserData(DERIVATION_EQUALS, true);
2209      }
2210
2211      if (derived.hasPattern()) {
2212        if (!Base.compareDeep(derived.getPattern(), base.getPattern(), false)) {
2213          base.setPattern(derived.getPattern().copy());
2214        } else
2215          if (trimDifferential)
2216            derived.setPattern(null);
2217          else
2218            derived.getPattern().setUserData(DERIVATION_EQUALS, true);
2219      }
2220
2221      for (ElementDefinitionExampleComponent ex : derived.getExample()) {
2222        boolean found = false;
2223        for (ElementDefinitionExampleComponent exS : base.getExample())
2224          if (Base.compareDeep(ex, exS, false))
2225            found = true;
2226        if (!found)
2227          base.addExample(ex.copy());
2228        else if (trimDifferential)
2229          derived.getExample().remove(ex);
2230        else
2231          ex.setUserData(DERIVATION_EQUALS, true);
2232      }
2233
2234      if (derived.hasMaxLengthElement()) {
2235        if (!Base.compareDeep(derived.getMaxLengthElement(), base.getMaxLengthElement(), false))
2236          base.setMaxLengthElement(derived.getMaxLengthElement().copy());
2237        else if (trimDifferential)
2238          derived.setMaxLengthElement(null);
2239        else
2240          derived.getMaxLengthElement().setUserData(DERIVATION_EQUALS, true);
2241      }
2242
2243      // todo: what to do about conditions?
2244      // condition : id 0..*
2245
2246      if (derived.hasMustSupportElement()) {
2247        if (!(base.hasMustSupportElement() && Base.compareDeep(derived.getMustSupportElement(), base.getMustSupportElement(), false)))
2248          base.setMustSupportElement(derived.getMustSupportElement().copy());
2249        else if (trimDifferential)
2250          derived.setMustSupportElement(null);
2251        else
2252          derived.getMustSupportElement().setUserData(DERIVATION_EQUALS, true);
2253      }
2254
2255
2256      // profiles cannot change : isModifier, defaultValue, meaningWhenMissing
2257      // but extensions can change isModifier
2258      if (isExtension) {
2259        if (derived.hasIsModifierElement() && !(base.hasIsModifierElement() && Base.compareDeep(derived.getIsModifierElement(), base.getIsModifierElement(), false)))
2260          base.setIsModifierElement(derived.getIsModifierElement().copy());
2261        else if (trimDifferential)
2262          derived.setIsModifierElement(null);
2263        else if (derived.hasIsModifierElement())
2264          derived.getIsModifierElement().setUserData(DERIVATION_EQUALS, true);
2265        if (derived.hasIsModifierReasonElement() && !(base.hasIsModifierReasonElement() && Base.compareDeep(derived.getIsModifierReasonElement(), base.getIsModifierReasonElement(), false)))
2266          base.setIsModifierReasonElement(derived.getIsModifierReasonElement().copy());
2267        else if (trimDifferential)
2268          derived.setIsModifierReasonElement(null);
2269        else if (derived.hasIsModifierReasonElement())
2270          derived.getIsModifierReasonElement().setUserData(DERIVATION_EQUALS, true);
2271      }
2272
2273      if (derived.hasBinding()) {
2274        if (!base.hasBinding() || !Base.compareDeep(derived.getBinding(), base.getBinding(), false)) {
2275          if (base.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && derived.getBinding().getStrength() != BindingStrength.REQUIRED)
2276            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "illegal attempt to change the binding on "+derived.getPath()+" from "+base.getBinding().getStrength().toCode()+" to "+derived.getBinding().getStrength().toCode(), ValidationMessage.IssueSeverity.ERROR));
2277//            throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": illegal attempt to change a binding from "+base.getBinding().getStrength().toCode()+" to "+derived.getBinding().getStrength().toCode());
2278          else if (base.hasBinding() && derived.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && base.getBinding().hasValueSet() && derived.getBinding().hasValueSet()) {
2279            ValueSet baseVs = context.fetchResource(ValueSet.class, base.getBinding().getValueSet());
2280            ValueSet contextVs = context.fetchResource(ValueSet.class, derived.getBinding().getValueSet());
2281            if (baseVs == null) {
2282              messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+base.getPath(), "Binding "+base.getBinding().getValueSet()+" could not be located", ValidationMessage.IssueSeverity.WARNING));
2283            } else if (contextVs == null) {
2284              messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSet()+" could not be located", ValidationMessage.IssueSeverity.WARNING));
2285            } else {
2286              ValueSetExpansionOutcome expBase = context.expandVS(baseVs, true, false);
2287              ValueSetExpansionOutcome expDerived = context.expandVS(contextVs, true, false);
2288              if (expBase.getValueset() == null)
2289                messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+base.getPath(), "Binding "+base.getBinding().getValueSet()+" could not be expanded", ValidationMessage.IssueSeverity.WARNING));
2290              else if (expDerived.getValueset() == null)
2291                messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSet()+" could not be expanded", ValidationMessage.IssueSeverity.WARNING));
2292              else if (ToolingExtensions.hasExtension(expBase.getValueset().getExpansion(), "http://hl7.org/fhir/StructureDefinition/valueset-toocostly"))
2293                messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Unable to check if "+derived.getBinding().getValueSet()+" is a proper subset of " +base.getBinding().getValueSet()+" - base value set is too large to check", ValidationMessage.IssueSeverity.WARNING));
2294              else if (!isSubset(expBase.getValueset(), expDerived.getValueset()))
2295                messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSet()+" is not a subset of binding "+base.getBinding().getValueSet(), ValidationMessage.IssueSeverity.ERROR));
2296
2297            }
2298          }
2299          base.setBinding(derived.getBinding().copy());
2300        } else if (trimDifferential)
2301          derived.setBinding(null);
2302        else
2303          derived.getBinding().setUserData(DERIVATION_EQUALS, true);
2304      } // else if (base.hasBinding() && doesn't have bindable type )
2305        //  base
2306
2307      if (derived.hasIsSummaryElement()) {
2308        if (!Base.compareDeep(derived.getIsSummaryElement(), base.getIsSummaryElement(), false)) {
2309          if (base.hasIsSummary())
2310            throw new Error("Error in profile "+pn+" at "+derived.getPath()+": Base isSummary = "+base.getIsSummaryElement().asStringValue()+", derived isSummary = "+derived.getIsSummaryElement().asStringValue());
2311          base.setIsSummaryElement(derived.getIsSummaryElement().copy());
2312        } else if (trimDifferential)
2313          derived.setIsSummaryElement(null);
2314        else
2315          derived.getIsSummaryElement().setUserData(DERIVATION_EQUALS, true);
2316      }
2317
2318      if (derived.hasType()) {
2319//        if (derived.getPath().endsWith("[x]") && derived.hasSlicing() && derived.getSlicing().getRules() != SlicingRules.CLOSED) {
2320//          // if we're slicing, and not closed, then it's the same list
2321//
2322//          derived.getType().clear();
2323//          derived.getType().addAll(base.getType());
2324//        } else
2325          if (!Base.compareDeep(derived.getType(), base.getType(), false)) {
2326          if (base.hasType()) {
2327            for (TypeRefComponent ts : derived.getType()) {
2328//              if (!ts.hasCode()) { // ommitted in the differential; copy it over....
2329//                if (base.getType().size() > 1) 
2330//                  throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": constrained type code must be present if there are multiple types ("+base.typeSummary()+")");
2331//                if (base.getType().get(0).getCode() != null)
2332//                  ts.setCode(base.getType().get(0).getCode());
2333//              }
2334              boolean ok = false;
2335              CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
2336              String t = ts.getWorkingCode();
2337              for (TypeRefComponent td : base.getType()) {;
2338                String tt = td.getWorkingCode();
2339                b.append(tt);
2340                if (td.hasCode() && (tt.equals(t) || "Extension".equals(tt) || (t.equals("uri") && tt.equals("string"))  || // work around for old badly generated SDs
2341                    "Element".equals(tt) || "*".equals(tt) ||
2342                    (("Resource".equals(tt) || ("DomainResource".equals(tt)) && pkp.isResource(t)))))
2343                  ok = true;
2344              }
2345              if (!ok)
2346                throw new DefinitionException("StructureDefinition "+purl+" at "+derived.getPath()+": illegal constrained type "+t+" from "+b.toString()+" in "+srcSD.getUrl());
2347            }
2348          }
2349          base.getType().clear();
2350          for (TypeRefComponent t : derived.getType()) {
2351            TypeRefComponent tt = t.copy();
2352//            tt.setUserData(DERIVATION_EQUALS, true);
2353            base.getType().add(tt);
2354          }
2355        }
2356        else if (trimDifferential)
2357          derived.getType().clear();
2358        else
2359          for (TypeRefComponent t : derived.getType())
2360            t.setUserData(DERIVATION_EQUALS, true);
2361      }
2362
2363      if (derived.hasMapping()) {
2364        // todo: mappings are not cumulative - one replaces another
2365        if (!Base.compareDeep(derived.getMapping(), base.getMapping(), false)) {
2366          for (ElementDefinitionMappingComponent s : derived.getMapping()) {
2367            boolean found = false;
2368            for (ElementDefinitionMappingComponent d : base.getMapping()) {
2369              found = found || (d.getIdentity().equals(s.getIdentity()) && d.getMap().equals(s.getMap()));
2370            }
2371            if (!found)
2372              base.getMapping().add(s);
2373          }
2374        }
2375        else if (trimDifferential)
2376          derived.getMapping().clear();
2377        else
2378          for (ElementDefinitionMappingComponent t : derived.getMapping())
2379            t.setUserData(DERIVATION_EQUALS, true);
2380      }
2381      for (ElementDefinitionMappingComponent m : base.getMapping()) {
2382        if (m.hasMap()) {
2383          m.setMap(m.getMap().trim());
2384        }
2385      }
2386
2387      // todo: constraints are cumulative. there is no replacing
2388      for (ElementDefinitionConstraintComponent s : base.getConstraint()) { 
2389        s.setUserData(IS_DERIVED, true);
2390        if (!s.hasSource())
2391          s.setSource(srcSD.getUrl());
2392      }
2393      if (derived.hasConstraint()) {
2394        for (ElementDefinitionConstraintComponent s : derived.getConstraint()) {
2395          if (!base.hasConstraint(s.getKey())) {
2396            ElementDefinitionConstraintComponent inv = s.copy();
2397            base.getConstraint().add(inv);
2398          }
2399        }
2400      }
2401      for (IdType id : derived.getCondition()) {
2402        if (!base.hasCondition(id))
2403          base.getCondition().add(id);
2404      }
2405      
2406      // now, check that we still have a bindable type; if not, delete the binding - see task 8477
2407      if (dest.hasBinding() && !hasBindableType(dest))
2408        dest.setBinding(null);
2409        
2410      // finally, we copy any extensions from source to dest
2411      for (Extension ex : derived.getExtension()) {
2412        StructureDefinition sd  = context.fetchResource(StructureDefinition.class, ex.getUrl());
2413        if (sd == null || sd.getSnapshot() == null || sd.getSnapshot().getElementFirstRep().getMax().equals("1"))
2414          ToolingExtensions.removeExtension(dest, ex.getUrl());
2415        dest.addExtension(ex.copy());
2416      }
2417    }
2418    if (dest.hasFixed()) {
2419      checkTypeOk(dest, dest.getFixed().fhirType());
2420    }
2421    if (dest.hasPattern()) {
2422      checkTypeOk(dest, dest.getPattern().fhirType());
2423    }
2424  }
2425
2426
2427  public void checkTypeOk(ElementDefinition dest, String ft) {
2428    boolean ok = false;
2429    Set<String> types = new HashSet<>();
2430    for (TypeRefComponent t : dest.getType()) {
2431      if (t.hasCode()) {
2432        types.add(t.getWorkingCode());
2433      }
2434      ok = ft.equals(t.getWorkingCode());
2435    }
2436    if (!ok) {
2437      messages.add(new ValidationMessage(Source.InstanceValidator, IssueType.CONFLICT, dest.getId(), "The fixed value has type '"+ft+"' which is not valid (valid "+Utilities.pluralize("type", dest.getType().size())+": "+types.toString()+")", IssueSeverity.ERROR));
2438    }
2439  }
2440
2441  private boolean hasBindableType(ElementDefinition ed) {
2442    for (TypeRefComponent tr : ed.getType()) {
2443      if (Utilities.existsInList(tr.getWorkingCode(), "Coding", "CodeableConcept", "Quantity", "uri", "string", "code"))
2444        return true;
2445    }
2446    return false;
2447  }
2448
2449
2450  private boolean isLargerMax(String derived, String base) {
2451    if ("*".equals(base))
2452      return false;
2453    if ("*".equals(derived))
2454      return true;
2455    return Integer.parseInt(derived) > Integer.parseInt(base);
2456  }
2457
2458
2459  private boolean isSubset(ValueSet expBase, ValueSet expDerived) {
2460    return codesInExpansion(expDerived.getExpansion().getContains(), expBase.getExpansion());
2461  }
2462
2463
2464  private boolean codesInExpansion(List<ValueSetExpansionContainsComponent> contains, ValueSetExpansionComponent expansion) {
2465    for (ValueSetExpansionContainsComponent cc : contains) {
2466      if (!inExpansion(cc, expansion.getContains()))
2467        return false;
2468      if (!codesInExpansion(cc.getContains(), expansion))
2469        return false;
2470    }
2471    return true;
2472  }
2473
2474
2475  private boolean inExpansion(ValueSetExpansionContainsComponent cc, List<ValueSetExpansionContainsComponent> contains) {
2476    for (ValueSetExpansionContainsComponent cc1 : contains) {
2477      if (cc.getSystem().equals(cc1.getSystem()) && cc.getCode().equals(cc1.getCode()))
2478        return true;
2479      if (inExpansion(cc,  cc1.getContains()))
2480        return true;
2481    }
2482    return false;
2483  }
2484
2485  public void closeDifferential(StructureDefinition base, StructureDefinition derived) throws FHIRException {
2486    for (ElementDefinition edb : base.getSnapshot().getElement()) {
2487      if (isImmediateChild(edb) && !edb.getPath().endsWith(".id")) {
2488        ElementDefinition edm = getMatchInDerived(edb, derived.getDifferential().getElement());
2489        if (edm == null) {
2490          ElementDefinition edd = derived.getDifferential().addElement();
2491          edd.setPath(edb.getPath());
2492          edd.setMax("0");
2493        } else if (edb.hasSlicing()) {
2494          closeChildren(base, edb, derived, edm);
2495        }
2496      }
2497    }
2498    sortDifferential(base, derived, derived.getName(), new ArrayList<String>());
2499  }
2500
2501  private void closeChildren(StructureDefinition base, ElementDefinition edb, StructureDefinition derived, ElementDefinition edm) {
2502    String path = edb.getPath()+".";
2503    int baseStart = base.getSnapshot().getElement().indexOf(edb);
2504    int baseEnd = findEnd(base.getSnapshot().getElement(), edb, baseStart+1);
2505    int diffStart = derived.getDifferential().getElement().indexOf(edm);
2506    int diffEnd = findEnd(derived.getDifferential().getElement(), edm, diffStart+1);
2507    
2508    for (int cBase = baseStart; cBase < baseEnd; cBase++) {
2509      ElementDefinition edBase = base.getSnapshot().getElement().get(cBase);
2510      if (isImmediateChild(edBase, edb)) {
2511        ElementDefinition edMatch = getMatchInDerived(edBase, derived.getDifferential().getElement(), diffStart, diffEnd);
2512        if (edMatch == null) {
2513          ElementDefinition edd = derived.getDifferential().addElement();
2514          edd.setPath(edBase.getPath());
2515          edd.setMax("0");
2516        } else {
2517          closeChildren(base, edBase, derived, edMatch);
2518        }        
2519      }
2520    }
2521  }
2522
2523
2524
2525
2526  private int findEnd(List<ElementDefinition> list, ElementDefinition ed, int cursor) {
2527    String path = ed.getPath()+".";
2528    while (cursor < list.size() && list.get(cursor).getPath().startsWith(path))
2529      cursor++;
2530    return cursor;
2531  }
2532
2533
2534  private ElementDefinition getMatchInDerived(ElementDefinition ed, List<ElementDefinition> list) {
2535    for (ElementDefinition t : list)
2536      if (t.getPath().equals(ed.getPath()))
2537        return t;
2538    return null;
2539  }
2540
2541  private ElementDefinition getMatchInDerived(ElementDefinition ed, List<ElementDefinition> list, int start, int end) {
2542    for (int i = start; i < end; i++) {
2543      ElementDefinition t = list.get(i);
2544      if (t.getPath().equals(ed.getPath()))
2545        return t;
2546    }
2547    return null;
2548  }
2549
2550
2551  private boolean isImmediateChild(ElementDefinition ed) {
2552    String p = ed.getPath();
2553    if (!p.contains("."))
2554      return false;
2555    p = p.substring(p.indexOf(".")+1);
2556    return !p.contains(".");
2557  }
2558
2559  private boolean isImmediateChild(ElementDefinition candidate, ElementDefinition base) {
2560    String p = candidate.getPath();
2561    if (!p.contains("."))
2562      return false;
2563    if (!p.startsWith(base.getPath()+"."))
2564      return false;
2565    p = p.substring(base.getPath().length()+1);
2566    return !p.contains(".");
2567  }
2568
2569  public XhtmlNode generateExtensionTable(String defFile, StructureDefinition ed, String imageFolder, boolean inlineGraphics, boolean full, String corePath, String imagePath, Set<String> outputTracker) throws IOException, FHIRException {
2570    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics, true);
2571    gen.setTranslator(getTranslator());
2572    TableModel model = gen.initNormalTable(corePath, false, true, ed.getId()+(full ? "f" : "n"), true);
2573
2574    boolean deep = false;
2575    String m = "";
2576    boolean vdeep = false;
2577    if (ed.getSnapshot().getElementFirstRep().getIsModifier())
2578      m = "modifier_";
2579    for (ElementDefinition eld : ed.getSnapshot().getElement()) {
2580      deep = deep || eld.getPath().contains("Extension.extension.");
2581      vdeep = vdeep || eld.getPath().contains("Extension.extension.extension.");
2582    }
2583    Row r = gen.new Row();
2584    model.getRows().add(r);
2585    String en;
2586    if (!full)
2587      en = ed.getName();
2588    else if (ed.getSnapshot().getElement().get(0).getIsModifier())
2589      en = "modifierExtension";
2590    else 
2591      en = "extension";
2592    
2593    r.getCells().add(gen.new Cell(null, defFile == null ? "" : defFile+"-definitions.html#extension."+ed.getName(), en, null, null));
2594    r.getCells().add(gen.new Cell());
2595    r.getCells().add(gen.new Cell(null, null, describeCardinality(ed.getSnapshot().getElement().get(0), null, new UnusedTracker()), null, null));
2596
2597    ElementDefinition ved = null;
2598    if (full || vdeep) {
2599      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
2600
2601      r.setIcon(deep ? "icon_"+m+"extension_complex.png" : "icon_extension_simple.png", deep ? HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX : HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
2602      List<ElementDefinition> children = getChildren(ed.getSnapshot().getElement(), ed.getSnapshot().getElement().get(0));
2603      for (ElementDefinition child : children)
2604        if (!child.getPath().endsWith(".id")) {
2605          List<StructureDefinition> sdl = new ArrayList<>();
2606          sdl.add(ed);
2607          genElement(defFile == null ? "" : defFile+"-definitions.html#extension.", gen, r.getSubRows(), child, ed.getSnapshot().getElement(), sdl, true, defFile, true, full, corePath, imagePath, true, false, false, false, null);
2608        }
2609    } else if (deep) {
2610      List<ElementDefinition> children = new ArrayList<ElementDefinition>();
2611      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
2612        if (ted.getPath().equals("Extension.extension"))
2613          children.add(ted);
2614      }
2615
2616      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
2617      r.setIcon("icon_"+m+"extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
2618      
2619      for (ElementDefinition c : children) {
2620        ved = getValueFor(ed, c);
2621        ElementDefinition ued = getUrlFor(ed, c);
2622        if (ved != null && ued != null) {
2623          Row r1 = gen.new Row();
2624          r.getSubRows().add(r1);
2625          r1.getCells().add(gen.new Cell(null, defFile == null ? "" : defFile+"-definitions.html#extension."+ed.getName(), ((UriType) ued.getFixed()).getValue(), null, null));
2626          r1.getCells().add(gen.new Cell());
2627          r1.getCells().add(gen.new Cell(null, null, describeCardinality(c, null, new UnusedTracker()), null, null));
2628          genTypes(gen, r1, ved, defFile, ed, corePath, imagePath, false);
2629          Cell cell = gen.new Cell();
2630          cell.addMarkdown(c.getDefinition());
2631          r1.getCells().add(cell);
2632          r1.setIcon("icon_"+m+"extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
2633        }
2634      }
2635    } else  {
2636      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
2637        if (ted.getPath().startsWith("Extension.value"))
2638          ved = ted;
2639      }
2640
2641      genTypes(gen, r, ved, defFile, ed, corePath, imagePath, false);
2642
2643      r.setIcon("icon_"+m+"extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
2644    }
2645    Cell c = gen.new Cell("", "", "URL = "+ed.getUrl(), null, null);
2646    Piece cc = gen.new Piece(null, ed.getName()+": ", null);
2647    c.addPiece(gen.new Piece("br")).addPiece(cc);
2648    c.addMarkdown(ed.getDescription());
2649    
2650    if (!full && !(deep || vdeep) && ved != null && ved.hasBinding()) {  
2651        c.addPiece(gen.new Piece("br"));
2652      BindingResolution br = pkp.resolveBinding(ed, ved.getBinding(), ved.getPath());
2653      c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(null, translate("sd.table", "Binding")+": ", null).addStyle("font-weight:bold")));
2654      c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url) || !pkp.prependLinks() ? br.url : corePath+br.url, br.display, null)));
2655      if (ved.getBinding().hasStrength()) {
2656        c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(null, " (", null)));
2657        c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(corePath+"terminologies.html#"+ved.getBinding().getStrength().toCode(), egt(ved.getBinding().getStrengthElement()), ved.getBinding().getStrength().getDefinition())));              
2658        c.getPieces().add(gen.new Piece(null, ")", null));
2659      }
2660    }
2661    c.addPiece(gen.new Piece("br")).addPiece(gen.new Piece(null, describeExtensionContext(ed), null));
2662    r.getCells().add(c);
2663    
2664    try {
2665      return gen.generate(model, corePath, 0, outputTracker);
2666        } catch (org.hl7.fhir.exceptions.FHIRException e) {
2667                throw new FHIRException(e.getMessage(), e);
2668        }
2669  }
2670
2671  private ElementDefinition getUrlFor(StructureDefinition ed, ElementDefinition c) {
2672    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
2673    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
2674      if (ed.getSnapshot().getElement().get(i).getPath().equals(c.getPath()+".url"))
2675        return ed.getSnapshot().getElement().get(i);
2676      i++;
2677    }
2678    return null;
2679  }
2680
2681  private ElementDefinition getValueFor(StructureDefinition ed, ElementDefinition c) {
2682    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
2683    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
2684      if (ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".value"))
2685        return ed.getSnapshot().getElement().get(i);
2686      i++;
2687    }
2688    return null;
2689  }
2690
2691  private static final int AGG_NONE = 0;
2692  private static final int AGG_IND = 1;
2693  private static final int AGG_GR = 2;
2694  private static final boolean TABLE_FORMAT_FOR_FIXED_VALUES = false;
2695  
2696  private Cell genTypes(HierarchicalTableGenerator gen, Row r, ElementDefinition e, String profileBaseFileName, StructureDefinition profile, String corePath, String imagePath, boolean root) {
2697    Cell c = gen.new Cell();
2698    r.getCells().add(c);
2699    List<TypeRefComponent> types = e.getType();
2700    if (!e.hasType()) {
2701      if (root) { // we'll use base instead of types then
2702        StructureDefinition bsd = context.fetchResource(StructureDefinition.class, profile.getBaseDefinition());
2703        if (bsd != null) {
2704          if (bsd.hasUserData("path")) {
2705            c.getPieces().add(gen.new Piece(Utilities.isAbsoluteUrl(bsd.getUserString("path")) ? bsd.getUserString("path") : imagePath +bsd.getUserString("path"), bsd.getName(), null));
2706          } else {
2707            c.getPieces().add(gen.new Piece(null, bsd.getName(), null));
2708          }
2709        }
2710        return c;
2711      } else if (e.hasContentReference()) {
2712        return c;
2713      } else {
2714        ElementDefinition d = (ElementDefinition) e.getUserData(DERIVATION_POINTER);
2715        if (d != null && d.hasType()) {
2716          types = new ArrayList<ElementDefinition.TypeRefComponent>();
2717          for (TypeRefComponent tr : d.getType()) {
2718            TypeRefComponent tt = tr.copy();
2719            tt.setUserData(DERIVATION_EQUALS, true);
2720            types.add(tt);
2721          }
2722        } else {
2723          return c;
2724        }
2725      }
2726    }
2727
2728    boolean first = true;
2729
2730    TypeRefComponent tl = null;
2731    for (TypeRefComponent t : types) {
2732      if (first) {
2733        first = false;
2734      } else {
2735        c.addPiece(checkForNoChange(tl, gen.new Piece(null,", ", null)));
2736      }
2737      tl = t;
2738      if (t.hasTarget()) {
2739        c.getPieces().add(gen.new Piece(corePath+"references.html", t.getWorkingCode(), null));
2740        c.getPieces().add(gen.new Piece(null, "(", null));
2741        boolean tfirst = true;
2742        for (UriType u : t.getTargetProfile()) {
2743          if (tfirst)
2744            tfirst = false;
2745          else
2746            c.addPiece(gen.new Piece(null, " | ", null));
2747          genTargetLink(gen, profileBaseFileName, corePath, c, t, u.getValue());
2748        }
2749        c.getPieces().add(gen.new Piece(null, ")", null));
2750        if (t.getAggregation().size() > 0) {
2751          c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", " {", null));
2752          boolean firstA = true;
2753          for (Enumeration<AggregationMode> a : t.getAggregation()) {
2754            if (firstA = true)
2755              firstA = false;
2756            else
2757              c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", ", ", null));
2758            c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", codeForAggregation(a.getValue()), hintForAggregation(a.getValue())));
2759          }
2760          c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", "}", null));
2761        }
2762      } else if (t.hasProfile() && (!t.getWorkingCode().equals("Extension") || isProfiledType(t.getProfile()))) { // a profiled type
2763        String ref;
2764        ref = pkp.getLinkForProfile(profile, t.getProfile().get(0).getValue());
2765        if (ref != null) {
2766          String[] parts = ref.split("\\|");
2767          if (parts[0].startsWith("http:") || parts[0].startsWith("https:")) {
2768//            c.addPiece(checkForNoChange(t, gen.new Piece(parts[0], "<" + parts[1] + ">", t.getCode()))); Lloyd
2769            c.addPiece(checkForNoChange(t, gen.new Piece(parts[0], parts[1], t.getWorkingCode())));
2770          } else {
2771//            c.addPiece(checkForNoChange(t, gen.new Piece((t.getProfile().startsWith(corePath)? corePath: "")+parts[0], "<" + parts[1] + ">", t.getCode())));
2772            c.addPiece(checkForNoChange(t, gen.new Piece((t.getProfile().get(0).getValue().startsWith(corePath+"StructureDefinition")? corePath: "")+parts[0], parts[1], t.getWorkingCode())));
2773          }
2774        } else
2775          c.addPiece(checkForNoChange(t, gen.new Piece((t.getProfile().get(0).getValue().startsWith(corePath)? corePath: "")+ref, t.getWorkingCode(), null)));
2776      } else {
2777        String tc = t.getWorkingCode();
2778        if (Utilities.isAbsoluteUrl(tc)) {
2779          StructureDefinition sd = context.fetchTypeDefinition(tc);
2780          if (sd == null) {
2781            c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, tc), tc, null)));
2782          } else {
2783            c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, tc), sd.getType(), null)));           
2784          }
2785        } else if (pkp != null && pkp.hasLinkFor(tc)) {
2786          c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, tc), tc, null)));
2787        } else
2788          c.addPiece(checkForNoChange(t, gen.new Piece(null, tc, null)));
2789      }
2790    }
2791    return c;
2792  }
2793
2794
2795  public void genTargetLink(HierarchicalTableGenerator gen, String profileBaseFileName, String corePath, Cell c, TypeRefComponent t, String u) {
2796    if (u.startsWith("http://hl7.org/fhir/StructureDefinition/")) {
2797      StructureDefinition sd = context.fetchResource(StructureDefinition.class, u);
2798      if (sd != null) {
2799        String disp = sd.hasTitle() ? sd.getTitle() : sd.getName();
2800        c.addPiece(checkForNoChange(t, gen.new Piece(checkPrepend(corePath, sd.getUserString("path")), disp, null)));
2801      } else {
2802        String rn = u.substring(40);
2803        c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, rn), rn, null)));
2804      }
2805    } else if (Utilities.isAbsoluteUrl(u)) {
2806      StructureDefinition sd = context.fetchResource(StructureDefinition.class, u);
2807      if (sd != null) {
2808        String disp = sd.hasTitle() ? sd.getTitle() : sd.getName();
2809        String ref = pkp.getLinkForProfile(null, sd.getUrl());
2810        if (ref != null && ref.contains("|"))
2811          ref = ref.substring(0,  ref.indexOf("|"));
2812        c.addPiece(checkForNoChange(t, gen.new Piece(ref, disp, null)));
2813      } else
2814        c.addPiece(checkForNoChange(t, gen.new Piece(null, u, null)));        
2815    } else if (t.hasTargetProfile() && u.startsWith("#"))
2816      c.addPiece(checkForNoChange(t, gen.new Piece(corePath+profileBaseFileName+"."+u.substring(1).toLowerCase()+".html", u, null)));
2817  }
2818
2819  private boolean isProfiledType(List<CanonicalType> theProfile) {
2820    for (CanonicalType next : theProfile){
2821      if (StringUtils.defaultString(next.getValueAsString()).contains(":")) {
2822        return true;
2823      }
2824    }
2825    return false;
2826  }
2827
2828
2829  private String codeForAggregation(AggregationMode a) {
2830    switch (a) {
2831    case BUNDLED : return "b";
2832    case CONTAINED : return "c";
2833    case REFERENCED: return "r";
2834    default: return "?";
2835    }
2836  }
2837
2838  private String hintForAggregation(AggregationMode a) {
2839    if (a != null)
2840      return a.getDefinition();
2841    else 
2842      return null;
2843  }
2844
2845
2846  private String checkPrepend(String corePath, String path) {
2847    if (pkp.prependLinks() && !(path.startsWith("http:") || path.startsWith("https:")))
2848      return corePath+path;
2849    else 
2850      return path;
2851  }
2852
2853
2854  private ElementDefinition getElementByName(List<ElementDefinition> elements, String contentReference) {
2855    for (ElementDefinition ed : elements)
2856      if (ed.hasSliceName() && ("#"+ed.getSliceName()).equals(contentReference))
2857        return ed;
2858    return null;
2859  }
2860
2861  private ElementDefinition getElementById(List<ElementDefinition> elements, String contentReference) {
2862    for (ElementDefinition ed : elements)
2863      if (ed.hasId() && ("#"+ed.getId()).equals(contentReference))
2864        return ed;
2865    return null;
2866  }
2867
2868
2869  public static String describeExtensionContext(StructureDefinition ext) {
2870    StringBuilder b = new StringBuilder();
2871    b.append("Use on ");
2872    for (int i = 0; i < ext.getContext().size(); i++) {
2873      StructureDefinitionContextComponent ec = ext.getContext().get(i);
2874      if (i > 0) 
2875        b.append(i < ext.getContext().size() - 1 ? ", " : " or ");
2876      b.append(ec.getType().getDisplay());
2877      b.append(" ");
2878      b.append(ec.getExpression());
2879    }
2880    if (ext.hasContextInvariant()) {
2881      b.append(", with <a href=\"structuredefinition-definitions.html#StructureDefinition.contextInvariant\">Context Invariant</a> = ");
2882      boolean first = true;
2883      for (StringType s : ext.getContextInvariant()) {
2884        if (first)
2885          first = false;
2886        else
2887          b.append(", ");
2888        b.append("<code>"+s.getValue()+"</code>");
2889      }
2890    }
2891    return b.toString(); 
2892  }
2893
2894  private String describeCardinality(ElementDefinition definition, ElementDefinition fallback, UnusedTracker tracker) {
2895    IntegerType min = definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
2896    StringType max = definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
2897    if (min.isEmpty() && fallback != null)
2898      min = fallback.getMinElement();
2899    if (max.isEmpty() && fallback != null)
2900      max = fallback.getMaxElement();
2901
2902    tracker.used = !max.isEmpty() && !max.getValue().equals("0");
2903
2904    if (min.isEmpty() && max.isEmpty())
2905      return null;
2906    else
2907      return (!min.hasValue() ? "" : Integer.toString(min.getValue())) + ".." + (!max.hasValue() ? "" : max.getValue());
2908  }
2909
2910  private void genCardinality(HierarchicalTableGenerator gen, ElementDefinition definition, Row row, boolean hasDef, UnusedTracker tracker, ElementDefinition fallback) {
2911    IntegerType min = !hasDef ? new IntegerType() : definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
2912    StringType max = !hasDef ? new StringType() : definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
2913    if (min.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
2914      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
2915      if (base.hasMinElement()) {
2916        min = base.getMinElement().copy();
2917        min.setUserData(DERIVATION_EQUALS, true);
2918      }
2919    }
2920    if (max.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
2921      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
2922      if (base.hasMaxElement()) {
2923        max = base.getMaxElement().copy();
2924        max.setUserData(DERIVATION_EQUALS, true);
2925      }
2926    }
2927    if (min.isEmpty() && fallback != null)
2928      min = fallback.getMinElement();
2929    if (max.isEmpty() && fallback != null)
2930      max = fallback.getMaxElement();
2931
2932    if (!max.isEmpty())
2933      tracker.used = !max.getValue().equals("0");
2934
2935    Cell cell = gen.new Cell(null, null, null, null, null);
2936    row.getCells().add(cell);
2937    if (!min.isEmpty() || !max.isEmpty()) {
2938      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !min.hasValue() ? "" : Integer.toString(min.getValue()), null)));
2939      cell.addPiece(checkForNoChange(min, max, gen.new Piece(null, "..", null)));
2940      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !max.hasValue() ? "" : max.getValue(), null)));
2941    }
2942  }
2943
2944
2945  private Piece checkForNoChange(Element source, Piece piece) {
2946    if (source.hasUserData(DERIVATION_EQUALS)) {
2947      piece.addStyle("opacity: 0.4");
2948    }
2949    return piece;
2950  }
2951
2952  private Piece checkForNoChange(Element src1, Element src2, Piece piece) {
2953    if (src1.hasUserData(DERIVATION_EQUALS) && src2.hasUserData(DERIVATION_EQUALS)) {
2954      piece.addStyle("opacity: 0.5");
2955    }
2956    return piece;
2957  }
2958
2959  public XhtmlNode generateTable(String defFile, StructureDefinition profile, boolean diff, String imageFolder, boolean inlineGraphics, String profileBaseFileName, boolean snapshot, String corePath, String imagePath, 
2960      boolean logicalModel, boolean allInvariants, Set<String> outputTracker, boolean active) throws IOException, FHIRException {
2961    assert(diff != snapshot);// check it's ok to get rid of one of these
2962    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics, true);
2963    gen.setTranslator(getTranslator());
2964    TableModel model = gen.initNormalTable(corePath, false, true, profile.getId()+(diff ? "d" : "s"), active);
2965    List<ElementDefinition> list = new ArrayList<>();
2966    if (diff)
2967      list.addAll(profile.getDifferential().getElement());
2968    else
2969      list.addAll(profile.getSnapshot().getElement());
2970    List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
2971    profiles.add(profile);
2972    if (list.isEmpty()) {
2973      ElementDefinition root = new ElementDefinition().setPath(profile.getType());
2974      root.setId(profile.getType());
2975      list.add(root);
2976    } else {
2977      if (list.get(0).getPath().contains(".")) {
2978        ElementDefinition root = new ElementDefinition().setPath(profile.getType());
2979        root.setId(profile.getType());
2980        list.add(0, root);
2981      }
2982    }
2983    if (diff)
2984      insertMissingSparseElements(list);
2985    genElement(defFile == null ? null : defFile+"#", gen, model.getRows(), list.get(0), list, profiles, diff, profileBaseFileName, null, snapshot, corePath, imagePath, true, logicalModel, profile.getDerivation() == TypeDerivationRule.CONSTRAINT && usesMustSupport(list), allInvariants, null);
2986    try {
2987      return gen.generate(model, imagePath, 0, outputTracker);
2988        } catch (org.hl7.fhir.exceptions.FHIRException e) {
2989                throw new FHIRException("Error generating table for profile " + profile.getUrl() + ": " + e.getMessage(), e);
2990        }
2991  }
2992
2993
2994  private void insertMissingSparseElements(List<ElementDefinition> list) {
2995    int i = 1;
2996    while (i < list.size()) {
2997      String[] pathCurrent = list.get(i).getPath().split("\\.");
2998      String[] pathLast = list.get(i-1).getPath().split("\\.");
2999      int firstDiff = 0; // the first entry must be a match 
3000      while (firstDiff < pathCurrent.length && firstDiff < pathLast.length && pathCurrent[firstDiff].equals(pathLast[firstDiff])) {
3001        firstDiff++;
3002      }
3003      if (!(isSibling(pathCurrent, pathLast, firstDiff) || isChild(pathCurrent, pathLast, firstDiff))) {
3004        // now work backwards down to lastMatch inserting missing path nodes
3005        for (int index = pathCurrent.length-2; index >= firstDiff; index--) {
3006          ElementDefinition root = new ElementDefinition().setPath(makePath(pathCurrent, index));
3007          root.setId(root.getPath());
3008          list.add(i, root);
3009        }
3010      } 
3011      i++;
3012    }
3013  }
3014
3015  private boolean isSibling(String[] pathCurrent, String[] pathLast, int firstDiff) {
3016    return pathCurrent.length == pathLast.length && firstDiff == pathCurrent.length-1;
3017  }
3018
3019
3020  private boolean isChild(String[] pathCurrent, String[] pathLast, int firstDiff) {
3021    return pathCurrent.length == pathLast.length+1 && firstDiff == pathLast.length;
3022  }
3023
3024
3025  private String makePath(String[] pathCurrent, int index) {
3026    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(".");
3027    for (int i = 0; i <= index; i++) {
3028      b.append(pathCurrent[i]);
3029    }
3030    return b.toString();
3031  }
3032
3033
3034  public XhtmlNode generateGrid(String defFile, StructureDefinition profile, String imageFolder, boolean inlineGraphics, String profileBaseFileName, String corePath, String imagePath, Set<String> outputTracker) throws IOException, FHIRException {
3035    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics, true);
3036    gen.setTranslator(getTranslator());
3037    TableModel model = gen.initGridTable(corePath, profile.getId());
3038    List<ElementDefinition> list = profile.getSnapshot().getElement();
3039    List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
3040    profiles.add(profile);
3041    genGridElement(defFile == null ? null : defFile+"#", gen, model.getRows(), list.get(0), list, profiles, true, profileBaseFileName, null, corePath, imagePath, true, profile.getDerivation() == TypeDerivationRule.CONSTRAINT && usesMustSupport(list));
3042    try {
3043      return gen.generate(model, imagePath, 1, outputTracker);
3044    } catch (org.hl7.fhir.exceptions.FHIRException e) {
3045      throw new FHIRException(e.getMessage(), e);
3046    }
3047  }
3048
3049
3050  private boolean usesMustSupport(List<ElementDefinition> list) {
3051    for (ElementDefinition ed : list)
3052      if (ed.hasMustSupport() && ed.getMustSupport())
3053        return true;
3054    return false;
3055  }
3056
3057
3058  private Row genElement(String defPath, HierarchicalTableGenerator gen, List<Row> rows, ElementDefinition element, List<ElementDefinition> all, List<StructureDefinition> profiles, boolean showMissing, String profileBaseFileName, Boolean extensions, boolean snapshot, String corePath, String imagePath, boolean root, boolean logicalModel, boolean isConstraintMode, boolean allInvariants, Row slicingRow) throws IOException, FHIRException {
3059    Row originalRow = slicingRow;
3060    StructureDefinition profile = profiles == null ? null : profiles.get(profiles.size()-1);
3061    String s = tail(element.getPath());
3062    if (element.hasSliceName())
3063      s = s +":"+element.getSliceName();
3064    Row typesRow = null;
3065    
3066    List<ElementDefinition> children = getChildren(all, element);
3067    boolean isExtension = (s.equals("extension") || s.equals("modifierExtension"));
3068//    if (!snapshot && isExtension && extensions != null && extensions != isExtension)
3069//      return;
3070
3071    if (!onlyInformationIsMapping(all, element)) {
3072      Row row = gen.new Row();
3073      row.setAnchor(element.getPath());
3074      row.setColor(getRowColor(element, isConstraintMode));
3075      if (element.hasSlicing())
3076        row.setLineColor(1);
3077      else if (element.hasSliceName())
3078        row.setLineColor(2);
3079      else
3080        row.setLineColor(0);
3081      boolean hasDef = element != null;
3082      boolean ext = false;
3083      if (tail(element.getPath()).equals("extension")) {
3084        if (element.hasType() && element.getType().get(0).hasProfile() && extensionIsComplex(element.getType().get(0).getProfile().get(0).getValue()))
3085          row.setIcon("icon_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
3086        else
3087          row.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
3088        ext = true;
3089      } else if (tail(element.getPath()).equals("modifierExtension")) {
3090        if (element.hasType() && element.getType().get(0).hasProfile() && extensionIsComplex(element.getType().get(0).getProfile().get(0).getValue()))
3091          row.setIcon("icon_modifier_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
3092        else
3093          row.setIcon("icon_modifier_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
3094      } else if (!hasDef || element.getType().size() == 0) {
3095        if (root && context.getResourceNames().contains(profile.getType())) {
3096          row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
3097        } else {
3098          row.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
3099        }
3100      } else if (hasDef && element.getType().size() > 1) {
3101        if (allAreReference(element.getType()))
3102          row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
3103        else {
3104          row.setIcon("icon_choice.gif", HierarchicalTableGenerator.TEXT_ICON_CHOICE);
3105          typesRow = row;
3106        }
3107      } else if (hasDef && element.getType().get(0).getWorkingCode() != null && element.getType().get(0).getWorkingCode().startsWith("@"))
3108        row.setIcon("icon_reuse.png", HierarchicalTableGenerator.TEXT_ICON_REUSE);
3109      else if (hasDef && isPrimitive(element.getType().get(0).getWorkingCode()))
3110        row.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
3111      else if (hasDef && element.getType().get(0).hasTarget())
3112        row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
3113      else if (hasDef && isDataType(element.getType().get(0).getWorkingCode()))
3114        row.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
3115      else
3116        row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
3117      String ref = defPath == null ? null : defPath + element.getId();
3118      UnusedTracker used = new UnusedTracker();
3119      used.used = true;
3120      if (logicalModel && element.hasRepresentation(PropertyRepresentation.XMLATTR))
3121        s = "@"+s;
3122      String hint = "";
3123      hint = checkAdd(hint, (element.hasSliceName() ? translate("sd.table", "Slice")+" "+element.getSliceName() : ""));
3124      hint = checkAdd(hint, (hasDef && element.hasSliceName() ? ": " : ""));
3125      hint = checkAdd(hint, !hasDef ? null : gt(element.getDefinitionElement()));
3126      Cell left = gen.new Cell(null, ref, s, hint, null);
3127      row.getCells().add(left);
3128      Cell gc = gen.new Cell();
3129      row.getCells().add(gc);
3130      if (element != null && element.getIsModifier())
3131        checkForNoChange(element.getIsModifierElement(), gc.addStyledText(translate("sd.table", "This element is a modifier element"), "?!", null, null, null, false));
3132      if (element != null && element.getMustSupport())
3133        checkForNoChange(element.getMustSupportElement(), gc.addStyledText(translate("sd.table", "This element must be supported"), "S", "white", "red", null, false));
3134      if (element != null && element.getIsSummary())
3135        checkForNoChange(element.getIsSummaryElement(), gc.addStyledText(translate("sd.table", "This element is included in summaries"), "\u03A3", null, null, null, false));
3136      if (element != null && (!element.getConstraint().isEmpty() || !element.getCondition().isEmpty()))
3137        gc.addStyledText(translate("sd.table", "This element has or is affected by some invariants ("+listConstraintsAndConditions(element)+")"), "I", null, null, null, false);
3138
3139      ExtensionContext extDefn = null;
3140      if (ext) {
3141        if (element != null && element.getType().size() == 1 && element.getType().get(0).hasProfile()) {
3142          String eurl = element.getType().get(0).getProfile().get(0).getValue();
3143          extDefn = locateExtension(StructureDefinition.class, eurl);
3144          if (extDefn == null) {
3145            genCardinality(gen, element, row, hasDef, used, null);
3146            row.getCells().add(gen.new Cell(null, null, "?? "+element.getType().get(0).getProfile(), null, null));
3147            generateDescription(gen, row, element, null, used.used, profile.getUrl(), eurl, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot);
3148          } else {
3149            String name = urltail(eurl);
3150            left.getPieces().get(0).setText(name);
3151            // left.getPieces().get(0).setReference((String) extDefn.getExtensionStructure().getTag("filename"));
3152            left.getPieces().get(0).setHint(translate("sd.table", "Extension URL")+" = "+extDefn.getUrl());
3153            genCardinality(gen, element, row, hasDef, used, extDefn.getElement());
3154            ElementDefinition valueDefn = extDefn.getExtensionValueDefinition();
3155            if (valueDefn != null && !"0".equals(valueDefn.getMax()))
3156               genTypes(gen, row, valueDefn, profileBaseFileName, profile, corePath, imagePath, root);
3157             else // if it's complex, we just call it nothing
3158                // genTypes(gen, row, extDefn.getSnapshot().getElement().get(0), profileBaseFileName, profile);
3159              row.getCells().add(gen.new Cell(null, null, "("+translate("sd.table", "Complex")+")", null, null));
3160            generateDescription(gen, row, element, extDefn.getElement(), used.used, null, extDefn.getUrl(), profile, corePath, imagePath, root, logicalModel, allInvariants, valueDefn, snapshot);
3161          }
3162        } else {
3163          genCardinality(gen, element, row, hasDef, used, null);
3164          if ("0".equals(element.getMax()))
3165            row.getCells().add(gen.new Cell());            
3166          else
3167            genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath, root);
3168          generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot);
3169        }
3170      } else {
3171        genCardinality(gen, element, row, hasDef, used, null);
3172        if (element.hasSlicing())
3173          row.getCells().add(gen.new Cell(null, corePath+"profiling.html#slicing", "(Slice Definition)", null, null));
3174        else if (hasDef && !"0".equals(element.getMax()) && typesRow == null)
3175          genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath, root);
3176        else
3177          row.getCells().add(gen.new Cell());
3178        generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot);
3179      }
3180      if (element.hasSlicing()) {
3181        if (standardExtensionSlicing(element)) {
3182          used.used = true; // doesn't matter whether we have a type, we're used if we're setting up slicing ... element.hasType() && element.getType().get(0).hasProfile();
3183          showMissing = false; //?
3184        } else {
3185          row.setIcon("icon_slice.png", HierarchicalTableGenerator.TEXT_ICON_SLICE);
3186          slicingRow = row;
3187          for (Cell cell : row.getCells())
3188            for (Piece p : cell.getPieces()) {
3189              p.addStyle("font-style: italic");
3190            }
3191        }
3192      } else if (element.hasSliceName()) {
3193        row.setIcon("icon_slice_item.png", HierarchicalTableGenerator.TEXT_ICON_SLICE_ITEM);
3194      }
3195      if (used.used || showMissing)
3196        rows.add(row);
3197      if (!used.used && !element.hasSlicing()) {
3198        for (Cell cell : row.getCells())
3199          for (Piece p : cell.getPieces()) {
3200            p.setStyle("text-decoration:line-through");
3201            p.setReference(null);
3202          }
3203      } else {
3204        if (slicingRow != originalRow && !children.isEmpty()) {
3205          // we've entered a slice; we're going to create a holder row for the slice children
3206          Row hrow = gen.new Row();
3207          hrow.setAnchor(element.getPath());
3208          hrow.setColor(getRowColor(element, isConstraintMode));
3209          hrow.setLineColor(1);
3210          hrow.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
3211          hrow.getCells().add(gen.new Cell(null, null, s+":All Slices", "", null));
3212          hrow.getCells().add(gen.new Cell());
3213          hrow.getCells().add(gen.new Cell());
3214          hrow.getCells().add(gen.new Cell());
3215          hrow.getCells().add(gen.new Cell(null, null, "Content/Rules for all slices", "", null));
3216          row.getSubRows().add(hrow);
3217          row = hrow;
3218        }
3219        if (typesRow != null && !children.isEmpty()) {
3220          // we've entered a typing slice; we're going to create a holder row for the all types children
3221          Row hrow = gen.new Row();
3222          hrow.setAnchor(element.getPath());
3223          hrow.setColor(getRowColor(element, isConstraintMode));
3224          hrow.setLineColor(1);
3225          hrow.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
3226          hrow.getCells().add(gen.new Cell(null, null, s+":All Types", "", null));
3227          hrow.getCells().add(gen.new Cell());
3228          hrow.getCells().add(gen.new Cell());
3229          hrow.getCells().add(gen.new Cell());
3230          hrow.getCells().add(gen.new Cell(null, null, "Content/Rules for all Types", "", null));
3231          row.getSubRows().add(hrow);
3232          row = hrow;
3233        }
3234          
3235        Row currRow = row; 
3236        for (ElementDefinition child : children) {
3237          if (!child.hasSliceName())
3238            currRow = row; 
3239          if (logicalModel || !child.getPath().endsWith(".id") || (child.getPath().endsWith(".id") && (profile != null) && (profile.getDerivation() == TypeDerivationRule.CONSTRAINT)))  
3240            currRow = genElement(defPath, gen, currRow.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, isExtension, snapshot, corePath, imagePath, false, logicalModel, isConstraintMode, allInvariants, currRow);
3241        }
3242//        if (!snapshot && (extensions == null || !extensions))
3243//          for (ElementDefinition child : children)
3244//            if (child.getPath().endsWith(".extension") || child.getPath().endsWith(".modifierExtension"))
3245//              genElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, true, false, corePath, imagePath, false, logicalModel, isConstraintMode, allInvariants);
3246      }
3247      if (typesRow != null) {
3248        makeChoiceRows(typesRow.getSubRows(), element, gen, corePath, profileBaseFileName);
3249      }
3250    }
3251    return slicingRow;
3252  }
3253
3254  private String checkAdd(String src, String app) {
3255    return app == null ? src : src + app;
3256  }
3257
3258
3259  private String listConstraintsAndConditions(ElementDefinition element) {
3260    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
3261    for (ElementDefinitionConstraintComponent con : element.getConstraint()) {
3262      b.append(con.getKey());
3263    }
3264    for (IdType id : element.getCondition()) {
3265      b.append(id.asStringValue());
3266    }
3267    return b.toString();
3268  }
3269
3270
3271  private void makeChoiceRows(List<Row> subRows, ElementDefinition element, HierarchicalTableGenerator gen, String corePath, String profileBaseFileName) {
3272    // create a child for each choice
3273    for (TypeRefComponent tr : element.getType()) {
3274      Row choicerow = gen.new Row();
3275      String t = tr.getWorkingCode();
3276      if (isReference(t)) {
3277        choicerow.getCells().add(gen.new Cell(null, null, tail(element.getPath()).replace("[x]", Utilities.capitalize(t)), null, null));
3278        choicerow.getCells().add(gen.new Cell());
3279        choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
3280        choicerow.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
3281        Cell c = gen.new Cell();
3282        choicerow.getCells().add(c);
3283        if (ADD_REFERENCE_TO_TABLE) {
3284          if (tr.getWorkingCode().equals("canonical"))
3285            c.getPieces().add(gen.new Piece(corePath+"datatypes.html#canonical", "canonical", null));
3286          else
3287            c.getPieces().add(gen.new Piece(corePath+"references.html#Reference", "Reference", null));
3288          c.getPieces().add(gen.new Piece(null, "(", null));
3289        }
3290        boolean first = true;
3291        for (CanonicalType rt : tr.getTargetProfile()) {
3292          if (!first)
3293            c.getPieces().add(gen.new Piece(null, " | ", null));
3294          genTargetLink(gen, profileBaseFileName, corePath, c, tr, rt.getValue());
3295          first = false;
3296        }
3297        if (first)
3298          c.getPieces().add(gen.new Piece(null, "Any", null));
3299          
3300        if (ADD_REFERENCE_TO_TABLE) 
3301          c.getPieces().add(gen.new Piece(null, ")", null));
3302        
3303      } else {
3304        StructureDefinition sd = context.fetchTypeDefinition(t);
3305        if (sd == null) {
3306          System.out.println("Unable to find "+t);
3307          sd = context.fetchTypeDefinition(t);
3308        } else if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE) {
3309          choicerow.getCells().add(gen.new Cell(null, null, tail(element.getPath()).replace("[x]",  Utilities.capitalize(t)), sd.getDescription(), null));
3310          choicerow.getCells().add(gen.new Cell());
3311          choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
3312          choicerow.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
3313          choicerow.getCells().add(gen.new Cell(null, corePath+"datatypes.html#"+t, sd.getType(), null, null));
3314          //      } else if (definitions.getConstraints().contthnsKey(t)) {
3315          //        ProfiledType pt = definitions.getConstraints().get(t);
3316          //        choicerow.getCells().add(gen.new Cell(null, null, e.getName().replace("[x]", Utilities.capitalize(pt.getBaseType())), definitions.getTypes().containsKey(t) ? definitions.getTypes().get(t).getDefinition() : null, null));
3317          //        choicerow.getCells().add(gen.new Cell());
3318          //        choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
3319          //        choicerow.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
3320          //        choicerow.getCells().add(gen.new Cell(null, definitions.getSrcFile(t)+".html#"+t.replace("*", "open"), t, null, null));
3321        } else {
3322          choicerow.getCells().add(gen.new Cell(null, null, tail(element.getPath()).replace("[x]",  Utilities.capitalize(t)), sd.getDescription(), null));
3323          choicerow.getCells().add(gen.new Cell());
3324          choicerow.getCells().add(gen.new Cell(null, null, "", null, null));
3325          choicerow.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
3326          choicerow.getCells().add(gen.new Cell(null, pkp.getLinkFor(corePath, t), sd.getType(), null, null));
3327        }
3328        if (tr.hasProfile()) {
3329          Cell typeCell = choicerow.getCells().get(3);
3330          typeCell.addPiece(gen.new Piece(null, "(", null));
3331          boolean first = true;
3332          for (CanonicalType pt : tr.getProfile()) {
3333            if (first) first = false; else typeCell.addPiece(gen.new Piece(null, " | ", null));
3334            StructureDefinition psd = context.fetchResource(StructureDefinition.class, pt.getValue());
3335            if (psd == null)
3336              typeCell.addPiece(gen.new Piece(null, "??", null));
3337            else
3338              typeCell.addPiece(gen.new Piece(psd.getUserString("path"), psd.getName(), psd.present()));
3339            
3340          }
3341          typeCell.addPiece(gen.new Piece(null, ")", null));
3342        }
3343      }    
3344      choicerow.getCells().add(gen.new Cell());
3345      subRows.add(choicerow);
3346    }
3347  }
3348
3349  private boolean isReference(String t) {
3350    return t.equals("Reference") || t.equals("canonical"); 
3351  }  
3352
3353
3354
3355  private void genGridElement(String defPath, HierarchicalTableGenerator gen, List<Row> rows, ElementDefinition element, List<ElementDefinition> all, List<StructureDefinition> profiles, boolean showMissing, String profileBaseFileName, Boolean extensions, String corePath, String imagePath, boolean root, boolean isConstraintMode) throws IOException, FHIRException {
3356    StructureDefinition profile = profiles == null ? null : profiles.get(profiles.size()-1);
3357    String s = tail(element.getPath());
3358    List<ElementDefinition> children = getChildren(all, element);
3359    boolean isExtension = (s.equals("extension") || s.equals("modifierExtension"));
3360
3361    if (!onlyInformationIsMapping(all, element)) {
3362      Row row = gen.new Row();
3363      row.setAnchor(element.getPath());
3364      row.setColor(getRowColor(element, isConstraintMode));
3365      if (element.hasSlicing())
3366        row.setLineColor(1);
3367      else if (element.hasSliceName())
3368        row.setLineColor(2);
3369      else
3370        row.setLineColor(0);
3371      boolean hasDef = element != null;
3372      String ref = defPath == null ? null : defPath + element.getId();
3373      UnusedTracker used = new UnusedTracker();
3374      used.used = true;
3375      Cell left = gen.new Cell();
3376      if (element.getType().size() == 1 && element.getType().get(0).isPrimitive())
3377        left.getPieces().add(gen.new Piece(ref, "\u00A0\u00A0" + s, !hasDef ? null : gt(element.getDefinitionElement())).addStyle("font-weight:bold"));
3378      else
3379        left.getPieces().add(gen.new Piece(ref, "\u00A0\u00A0" + s, !hasDef ? null : gt(element.getDefinitionElement())));
3380      if (element.hasSliceName()) {
3381        left.getPieces().add(gen.new Piece("br"));
3382        String indent = StringUtils.repeat('\u00A0', 1+2*(element.getPath().split("\\.").length));
3383        left.getPieces().add(gen.new Piece(null, indent + "("+element.getSliceName() + ")", null));
3384      }
3385      row.getCells().add(left);
3386
3387      ExtensionContext extDefn = null;
3388      genCardinality(gen, element, row, hasDef, used, null);
3389      if (hasDef && !"0".equals(element.getMax()))
3390        genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath, root);
3391      else
3392        row.getCells().add(gen.new Cell());
3393      generateGridDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, null);
3394/*      if (element.hasSlicing()) {
3395        if (standardExtensionSlicing(element)) {
3396          used.used = element.hasType() && element.getType().get(0).hasProfile();
3397          showMissing = false;
3398        } else {
3399          row.setIcon("icon_slice.png", HierarchicalTableGenerator.TEXT_ICON_SLICE);
3400          row.getCells().get(2).getPieces().clear();
3401          for (Cell cell : row.getCells())
3402            for (Piece p : cell.getPieces()) {
3403              p.addStyle("font-style: italic");
3404            }
3405        }
3406      }*/
3407      rows.add(row);
3408      for (ElementDefinition child : children)
3409        if (child.getMustSupport())
3410          genGridElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, isExtension, corePath, imagePath, false, isConstraintMode);
3411    }
3412  }
3413
3414
3415  private ExtensionContext locateExtension(Class<StructureDefinition> class1, String value)  {
3416    if (value.contains("#")) {
3417      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
3418      if (ext == null)
3419        return null;
3420      String tail = value.substring(value.indexOf("#")+1);
3421      ElementDefinition ed = null;
3422      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
3423        if (tail.equals(ted.getSliceName())) {
3424          ed = ted;
3425          return new ExtensionContext(ext, ed);
3426        }
3427      }
3428      return null;
3429    } else {
3430      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
3431      if (ext == null)
3432        return null;
3433      else 
3434        return new ExtensionContext(ext, ext.getSnapshot().getElement().get(0));
3435    }
3436  }
3437
3438
3439  private boolean extensionIsComplex(String value) {
3440    if (value.contains("#")) {
3441      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
3442    if (ext == null)
3443      return false;
3444      String tail = value.substring(value.indexOf("#")+1);
3445      ElementDefinition ed = null;
3446      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
3447        if (tail.equals(ted.getSliceName())) {
3448          ed = ted;
3449          break;
3450        }
3451      }
3452      if (ed == null)
3453        return false;
3454      int i = ext.getSnapshot().getElement().indexOf(ed);
3455      int j = i+1;
3456      while (j < ext.getSnapshot().getElement().size() && !ext.getSnapshot().getElement().get(j).getPath().equals(ed.getPath()))
3457        j++;
3458      return j - i > 5;
3459    } else {
3460      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
3461      return ext != null && ext.getSnapshot().getElement().size() > 5;
3462    }
3463  }
3464
3465
3466  private String getRowColor(ElementDefinition element, boolean isConstraintMode) {
3467    switch (element.getUserInt(UD_ERROR_STATUS)) {
3468    case STATUS_HINT: return ROW_COLOR_HINT;
3469    case STATUS_WARNING: return ROW_COLOR_WARNING;
3470    case STATUS_ERROR: return ROW_COLOR_ERROR;
3471    case STATUS_FATAL: return ROW_COLOR_FATAL;
3472    }
3473    if (isConstraintMode && !element.getMustSupport() && !element.getIsModifier() && element.getPath().contains("."))
3474      return null; // ROW_COLOR_NOT_MUST_SUPPORT;
3475    else
3476      return null;
3477  }
3478
3479
3480  private String urltail(String path) {
3481    if (path.contains("#"))
3482      return path.substring(path.lastIndexOf('#')+1);
3483    if (path.contains("/"))
3484      return path.substring(path.lastIndexOf('/')+1);
3485    else
3486      return path;
3487
3488  }
3489
3490  private boolean standardExtensionSlicing(ElementDefinition element) {
3491    String t = tail(element.getPath());
3492    return (t.equals("extension") || t.equals("modifierExtension"))
3493          && element.getSlicing().getRules() != SlicingRules.CLOSED && element.getSlicing().getDiscriminator().size() == 1 && element.getSlicing().getDiscriminator().get(0).getPath().equals("url") && element.getSlicing().getDiscriminator().get(0).getType().equals(DiscriminatorType.VALUE);
3494  }
3495
3496  private Cell generateDescription(HierarchicalTableGenerator gen, Row row, ElementDefinition definition, ElementDefinition fallback, boolean used, String baseURL, String url, StructureDefinition profile, String corePath, String imagePath, boolean root, boolean logicalModel, boolean allInvariants, boolean snapshot) throws IOException, FHIRException {
3497    return generateDescription(gen, row, definition, fallback, used, baseURL, url, profile, corePath, imagePath, root, logicalModel, allInvariants, null, snapshot);
3498  }
3499  
3500  private Cell generateDescription(HierarchicalTableGenerator gen, Row row, ElementDefinition definition, ElementDefinition fallback, boolean used, String baseURL, String url, StructureDefinition profile, String corePath, String imagePath, boolean root, boolean logicalModel, boolean allInvariants, ElementDefinition valueDefn, boolean snapshot) throws IOException, FHIRException {
3501    Cell c = gen.new Cell();
3502    row.getCells().add(c);
3503
3504    if (used) {
3505      if (logicalModel && ToolingExtensions.hasExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) {
3506        if (root) {
3507          c.getPieces().add(gen.new Piece(null, translate("sd.table", "XML Namespace")+": ", null).addStyle("font-weight:bold"));
3508          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
3509        } else if (!root && ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace") && 
3510            !ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace").equals(ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))) {
3511          c.getPieces().add(gen.new Piece(null, translate("sd.table", "XML Namespace")+": ", null).addStyle("font-weight:bold"));
3512          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
3513        }
3514      }
3515      
3516      if (definition.hasContentReference()) {
3517        ElementDefinition ed = getElementByName(profile.getSnapshot().getElement(), definition.getContentReference());
3518        if (ed == null)
3519          c.getPieces().add(gen.new Piece(null, translate("sd.table", "Unknown reference to %s", definition.getContentReference()), null));
3520        else
3521          c.getPieces().add(gen.new Piece("#"+ed.getPath(), translate("sd.table", "See %s", ed.getPath()), null));
3522      }
3523      if (definition.getPath().endsWith("url") && definition.hasFixed()) {
3524        c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "\""+buildJson(definition.getFixed())+"\"", null).addStyle("color: darkgreen")));
3525      } else {
3526        if (definition != null && definition.hasShort()) {
3527          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3528          c.addPiece(checkForNoChange(definition.getShortElement(), gen.new Piece(null, gt(definition.getShortElement()), null)));
3529        } else if (fallback != null && fallback.hasShort()) {
3530          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3531          c.addPiece(checkForNoChange(fallback.getShortElement(), gen.new Piece(null, gt(fallback.getShortElement()), null)));
3532        }
3533        if (url != null) {
3534          if (!c.getPieces().isEmpty()) 
3535            c.addPiece(gen.new Piece("br"));
3536          String fullUrl = url.startsWith("#") ? baseURL+url : url;
3537          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
3538          String ref = null;
3539          String ref2 = null;
3540          String fixedUrl = null;
3541          if (ed != null) {
3542            String p = ed.getUserString("path");
3543            if (p != null) {
3544              ref = p.startsWith("http:") || igmode ? p : Utilities.pathURL(corePath, p);
3545            }             
3546            fixedUrl = getFixedUrl(ed);
3547            if (fixedUrl != null) {// if its null, we guess that it's not a profiled extension? 
3548              if (fixedUrl.equals(url))
3549                fixedUrl = null;
3550              else {
3551                StructureDefinition ed2 = context.fetchResource(StructureDefinition.class, fixedUrl);
3552                if (ed2 != null) {
3553                  String p2 = ed2.getUserString("path");
3554                  if (p2 != null) {
3555                    ref2 = p2.startsWith("http:") || igmode ? p2 : Utilities.pathURL(corePath, p2);
3556                  }                              
3557                }
3558              }
3559            }
3560          }
3561          if (fixedUrl == null) {
3562            c.getPieces().add(gen.new Piece(null, translate("sd.table", "URL")+": ", null).addStyle("font-weight:bold"));
3563            c.getPieces().add(gen.new Piece(ref, fullUrl, null));
3564          } else { 
3565            // reference to a profile take on the extension show the base URL
3566            c.getPieces().add(gen.new Piece(null, translate("sd.table", "URL")+": ", null).addStyle("font-weight:bold"));
3567            c.getPieces().add(gen.new Piece(ref2, fixedUrl, null));
3568            c.getPieces().add(gen.new Piece(null, translate("sd.table", " profiled by ")+" ", null).addStyle("font-weight:bold"));
3569            c.getPieces().add(gen.new Piece(ref, fullUrl, null));
3570          
3571          }
3572        }
3573
3574        if (definition.hasSlicing()) {
3575          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3576          c.getPieces().add(gen.new Piece(null, translate("sd.table", "Slice")+": ", null).addStyle("font-weight:bold"));
3577          c.getPieces().add(gen.new Piece(null, describeSlice(definition.getSlicing()), null));
3578        }
3579        if (definition != null) {
3580          ElementDefinitionBindingComponent binding = null;
3581          if (valueDefn != null && valueDefn.hasBinding() && !valueDefn.getBinding().isEmpty())
3582            binding = valueDefn.getBinding();
3583          else if (definition.hasBinding())
3584            binding = definition.getBinding();
3585          if (binding!=null && !binding.isEmpty()) {
3586            if (!c.getPieces().isEmpty()) 
3587              c.addPiece(gen.new Piece("br"));
3588            BindingResolution br = pkp.resolveBinding(profile, binding, definition.getPath());
3589            c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, translate("sd.table", "Binding")+": ", null).addStyle("font-weight:bold")));
3590            c.getPieces().add(checkForNoChange(binding, gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url) || !pkp.prependLinks() ? br.url : corePath+br.url, br.display, null)));
3591            if (binding.hasStrength()) {
3592              c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, " (", null)));
3593              c.getPieces().add(checkForNoChange(binding, gen.new Piece(corePath+"terminologies.html#"+binding.getStrength().toCode(), egt(binding.getStrengthElement()), binding.getStrength().getDefinition())));
3594              
3595              c.getPieces().add(gen.new Piece(null, ")", null));
3596            }
3597            if (binding.hasExtension(ToolingExtensions.EXT_MAX_VALUESET)) {
3598              br = pkp.resolveBinding(profile, ToolingExtensions.readStringExtension(binding, ToolingExtensions.EXT_MAX_VALUESET), definition.getPath());
3599              c.addPiece(gen.new Piece("br"));
3600              c.getPieces().add(checkForNoChange(binding, gen.new Piece(corePath+"extension-elementdefinition-maxvalueset.html", translate("sd.table", "Max Binding")+": ", "Max Value Set Extension").addStyle("font-weight:bold")));             
3601              c.getPieces().add(checkForNoChange(binding, gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url) || !pkp.prependLinks() ? br.url : corePath+br.url, br.display, null)));
3602            }
3603            if (binding.hasExtension(ToolingExtensions.EXT_MIN_VALUESET)) {
3604              br = pkp.resolveBinding(profile, ToolingExtensions.readStringExtension(binding, ToolingExtensions.EXT_MIN_VALUESET), definition.getPath());
3605              c.addPiece(gen.new Piece("br"));
3606              c.getPieces().add(checkForNoChange(binding, gen.new Piece(corePath+"extension-elementdefinition-minvalueset.html", translate("sd.table", "Min Binding")+": ", "Min Value Set Extension").addStyle("font-weight:bold")));             
3607              c.getPieces().add(checkForNoChange(binding, gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url) || !pkp.prependLinks() ? br.url : corePath+br.url, br.display, null)));
3608            }
3609          }
3610          for (ElementDefinitionConstraintComponent inv : definition.getConstraint()) {
3611            if (!inv.hasSource() || inv.getSource().equals(profile.getUrl()) || allInvariants) {
3612              if (!c.getPieces().isEmpty()) 
3613                c.addPiece(gen.new Piece("br"));
3614              c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getKey()+": ", null).addStyle("font-weight:bold")));
3615              c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, gt(inv.getHumanElement()), null)));
3616            }
3617          }
3618          if ((definition.hasBase() && definition.getBase().getMax().equals("*")) || (definition.hasMax() && definition.getMax().equals("*"))) {
3619            if (c.getPieces().size() > 0)
3620              c.addPiece(gen.new Piece("br"));
3621            if (definition.hasOrderMeaning()) {
3622              c.getPieces().add(gen.new Piece(null, "This repeating element order: "+definition.getOrderMeaning(), null));
3623            } else {
3624              // don't show this, this it's important: c.getPieces().add(gen.new Piece(null, "This repeating element has no defined order", null));
3625            }           
3626          }
3627
3628          if (definition.hasFixed()) {
3629            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3630            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, translate("sd.table", "Fixed Value")+": ", null).addStyle("font-weight:bold")));
3631            if (!useTableForFixedValues || definition.getFixed().isPrimitive()) {
3632              String s = buildJson(definition.getFixed());
3633              String link = null;
3634              if (Utilities.isAbsoluteUrl(s))
3635                link = pkp.getLinkForUrl(corePath, s);
3636              c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(link, s, null).addStyle("color: darkgreen")));
3637            } else {
3638              c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "As shown", null).addStyle("color: darkgreen")));
3639              genFixedValue(gen, row, definition.getFixed(), snapshot, false, corePath);              
3640            }
3641            if (isCoded(definition.getFixed()) && !hasDescription(definition.getFixed())) {
3642              Piece p = describeCoded(gen, definition.getFixed());
3643              if (p != null)
3644                c.getPieces().add(p);
3645            }
3646          } else if (definition.hasPattern()) {
3647            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3648            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, translate("sd.table", "Required Pattern")+": ", null).addStyle("font-weight:bold")));
3649            if (!useTableForFixedValues || definition.getPattern().isPrimitive())
3650              c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, buildJson(definition.getPattern()), null).addStyle("color: darkgreen")));
3651            else {
3652              c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, "At least the following", null).addStyle("color: darkgreen")));
3653              genFixedValue(gen, row, definition.getPattern(), snapshot, true, corePath);
3654            }
3655          } else if (definition.hasExample()) {
3656            for (ElementDefinitionExampleComponent ex : definition.getExample()) {
3657              if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3658              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, translate("sd.table", "Example")+("".equals("General")? "" : " "+ex.getLabel())+": ", null).addStyle("font-weight:bold")));
3659              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, buildJson(ex.getValue()), null).addStyle("color: darkgreen")));
3660            }
3661          }
3662          if (definition.hasMaxLength() && definition.getMaxLength()!=0) {
3663            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3664            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, "Max Length: ", null).addStyle("font-weight:bold")));
3665            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, Integer.toString(definition.getMaxLength()), null).addStyle("color: darkgreen")));
3666          }
3667          if (profile != null) {
3668            for (StructureDefinitionMappingComponent md : profile.getMapping()) {
3669              if (md.hasExtension(ToolingExtensions.EXT_TABLE_NAME)) {
3670                ElementDefinitionMappingComponent map = null;
3671                for (ElementDefinitionMappingComponent m : definition.getMapping()) 
3672                  if (m.getIdentity().equals(md.getIdentity()))
3673                    map = m;
3674                if (map != null) {
3675                  for (int i = 0; i<definition.getMapping().size(); i++){
3676                    c.addPiece(gen.new Piece("br"));
3677                    c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(md, ToolingExtensions.EXT_TABLE_NAME)+": " + map.getMap(), null));
3678                  }
3679                }
3680              }
3681            }
3682          }
3683        }
3684      }
3685    }
3686    return c;
3687  }
3688
3689  private void genFixedValue(HierarchicalTableGenerator gen, Row erow, DataType value, boolean snapshot, boolean pattern, String corePath) {
3690    String ref = pkp.getLinkFor(corePath, value.fhirType());
3691    if (ref != null) {
3692      ref = ref.substring(0, ref.indexOf(".html"))+"-definitions.html#";
3693    } else {
3694      ref = "??";
3695    }
3696    StructureDefinition sd = context.fetchTypeDefinition(value.fhirType());
3697    
3698    for (org.hl7.fhir.r5.model.Property t : value.children()) {
3699      if (t.getValues().size() > 0 || snapshot) {
3700        ElementDefinition ed = findElementDefinition(sd, t.getName());
3701        if (t.getValues().size() == 0 || (t.getValues().size() == 1 && t.getValues().get(0).isEmpty())) {
3702          Row row = gen.new Row();
3703          erow.getSubRows().add(row);
3704          Cell c = gen.new Cell();
3705          row.getCells().add(c);
3706          c.addPiece(gen.new Piece((ed.getBase().getPath().equals(ed.getPath()) ? ref+ed.getPath() : corePath+(VersionUtilities.isThisOrLater("4.1", context.getVersion()) ? "types-definitions.html#"+ed.getBase().getPath() : "element-definitions.html#"+ed.getBase().getPath())), t.getName(), null));
3707          c = gen.new Cell();
3708          row.getCells().add(c);
3709          c.addPiece(gen.new Piece(null, null, null));
3710          c = gen.new Cell();
3711          row.getCells().add(c);
3712          if (!pattern) {
3713            c.addPiece(gen.new Piece(null, "0..0", null));
3714            row.setIcon("icon_fixed.gif", "Fixed Value" /*HierarchicalTableGenerator.TEXT_ICON_FIXED*/);
3715          } else if (isPrimitive(t.getTypeCode())) {
3716            row.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
3717            c.addPiece(gen.new Piece(null, "0.."+(t.getMaxCardinality() == 2147483647 ? "*": Integer.toString(t.getMaxCardinality())), null));
3718          } else if (isReference(t.getTypeCode())) { 
3719            row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
3720            c.addPiece(gen.new Piece(null, "0.."+(t.getMaxCardinality() == 2147483647 ? "*": Integer.toString(t.getMaxCardinality())), null));
3721          } else { 
3722            row.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
3723            c.addPiece(gen.new Piece(null, "0.."+(t.getMaxCardinality() == 2147483647 ? "*": Integer.toString(t.getMaxCardinality())), null));
3724          }
3725          c = gen.new Cell();
3726          row.getCells().add(c);
3727          if (t.getTypeCode().contains("(")) {
3728            String tc = t.getTypeCode();
3729            String tn = tc.substring(0, tc.indexOf("("));
3730            c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, tn), tn, null));
3731            c.addPiece(gen.new Piece(null, "(", null));
3732            String[] p = tc.substring(tc.indexOf("(")+1, tc.indexOf(")")).split("\\|");
3733            for (String s : p) {
3734              c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, s), s, null));
3735            }
3736            c.addPiece(gen.new Piece(null, ")", null));            
3737          } else {
3738            c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, t.getTypeCode()), t.getTypeCode(), null));
3739          }
3740          c = gen.new Cell();
3741          c.addPiece(gen.new Piece(null, ed.getShort(), null));
3742          row.getCells().add(c);
3743        } else {
3744          for (Base b : t.getValues()) {
3745            Row row = gen.new Row();
3746            erow.getSubRows().add(row);
3747            row.setIcon("icon_fixed.gif", "Fixed Value" /*HierarchicalTableGenerator.TEXT_ICON_FIXED*/);
3748
3749            Cell c = gen.new Cell();
3750            row.getCells().add(c);
3751            c.addPiece(gen.new Piece((ed.getBase().getPath().equals(ed.getPath()) ? ref+ed.getPath() : (VersionUtilities.isThisOrLater("4.1", context.getVersion()) ? corePath+"types-definitions.html#"+ed.getBase().getPath() : corePath+"element-definitions.html#"+ed.getBase().getPath())), t.getName(), null));
3752
3753            c = gen.new Cell();
3754            row.getCells().add(c);
3755            c.addPiece(gen.new Piece(null, null, null));
3756
3757            c = gen.new Cell();
3758            row.getCells().add(c);
3759            if (pattern)
3760              c.addPiece(gen.new Piece(null, "1.."+(t.getMaxCardinality() == 2147483647 ? "*" : Integer.toString(t.getMaxCardinality())), null));
3761            else
3762              c.addPiece(gen.new Piece(null, "1..1", null));
3763
3764            c = gen.new Cell();
3765            row.getCells().add(c);
3766            if (b.fhirType().contains("(")) {
3767              String tc = b.fhirType();
3768              String tn = tc.substring(0, tc.indexOf("("));
3769              c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, tn), tn, null));
3770              c.addPiece(gen.new Piece(null, "(", null));
3771              String[] p = tc.substring(tc.indexOf("(")+1, tc.indexOf(")")).split("\\|");
3772              for (String s : p) {
3773                c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, s), s, null));
3774              }
3775              c.addPiece(gen.new Piece(null, ")", null));            
3776            } else {
3777              c.addPiece(gen.new Piece(pkp.getLinkFor(corePath, b.fhirType()), b.fhirType(), null));
3778            }
3779
3780            if (b.isPrimitive()) {
3781              c = gen.new Cell();
3782              row.getCells().add(c);
3783              c.addPiece(gen.new Piece(null, ed.getShort(), null));
3784              c.addPiece(gen.new Piece("br"));
3785              c.getPieces().add(gen.new Piece(null, "Fixed Value: ", null).addStyle("font-weight: bold"));
3786              String s = b.primitiveValue();
3787              // ok. let's see if we can find a relevant link for this
3788              String link = null;
3789              if (Utilities.isAbsoluteUrl(s))
3790                link = pkp.getLinkForUrl(corePath, s);
3791              c.getPieces().add(gen.new Piece(link, s, null).addStyle("color: darkgreen"));
3792            } else {
3793              c = gen.new Cell();
3794              row.getCells().add(c);
3795              c.addPiece(gen.new Piece(null, ed.getShort(), null));
3796              c.addPiece(gen.new Piece("br"));
3797              c.getPieces().add(gen.new Piece(null, "Fixed Value: ", null).addStyle("font-weight: bold"));
3798              c.getPieces().add(gen.new Piece(null, "(complex)", null).addStyle("color: darkgreen"));
3799              genFixedValue(gen, row, (DataType) b, snapshot, pattern, corePath);
3800            }
3801          }
3802        }
3803      }
3804    }
3805  }
3806
3807
3808  private ElementDefinition findElementDefinition(StructureDefinition sd, String name) {
3809    String path = sd.getType()+"."+name;
3810    for (ElementDefinition ed : sd.getSnapshot().getElement()) {
3811      if (ed.getPath().equals(path))
3812        return ed;
3813    }
3814    throw new FHIRException("Unable to find element "+path);
3815  }
3816
3817
3818  private String getFixedUrl(StructureDefinition sd) {
3819    for (ElementDefinition ed : sd.getSnapshot().getElement()) {
3820      if (ed.getPath().equals("Extension.url")) {
3821        if (ed.hasFixed() && ed.getFixed() instanceof UriType)
3822          return ed.getFixed().primitiveValue();
3823      }
3824    }
3825    return null;
3826  }
3827
3828
3829  private Piece describeCoded(HierarchicalTableGenerator gen, DataType fixed) {
3830    if (fixed instanceof Coding) {
3831      Coding c = (Coding) fixed;
3832      ValidationResult vr = context.validateCode(terminologyServiceOptions , c.getSystem(), c.getCode(), c.getDisplay());
3833      if (vr.getDisplay() != null)
3834        return gen.new Piece(null, " ("+vr.getDisplay()+")", null).addStyle("color: darkgreen");
3835    } else if (fixed instanceof CodeableConcept) {
3836      CodeableConcept cc = (CodeableConcept) fixed;
3837      for (Coding c : cc.getCoding()) {
3838        ValidationResult vr = context.validateCode(terminologyServiceOptions, c.getSystem(), c.getCode(), c.getDisplay());
3839        if (vr.getDisplay() != null)
3840          return gen.new Piece(null, " ("+vr.getDisplay()+")", null).addStyle("color: darkgreen");
3841      }
3842    }
3843    return null;
3844  }
3845
3846
3847  private boolean hasDescription(DataType fixed) {
3848    if (fixed instanceof Coding) {
3849      return ((Coding) fixed).hasDisplay();
3850    } else if (fixed instanceof CodeableConcept) {
3851      CodeableConcept cc = (CodeableConcept) fixed;
3852      if (cc.hasText())
3853        return true;
3854      for (Coding c : cc.getCoding())
3855        if (c.hasDisplay())
3856         return true;
3857    } // (fixed instanceof CodeType) || (fixed instanceof Quantity);
3858    return false;
3859  }
3860
3861
3862  private boolean isCoded(DataType fixed) {
3863    return (fixed instanceof Coding) || (fixed instanceof CodeableConcept) || (fixed instanceof CodeType) || (fixed instanceof Quantity);
3864  }
3865
3866
3867  private Cell generateGridDescription(HierarchicalTableGenerator gen, Row row, ElementDefinition definition, ElementDefinition fallback, boolean used, String baseURL, String url, StructureDefinition profile, String corePath, String imagePath, boolean root, ElementDefinition valueDefn) throws IOException, FHIRException {
3868    Cell c = gen.new Cell();
3869    row.getCells().add(c);
3870
3871    if (used) {
3872      if (definition.hasContentReference()) {
3873        ElementDefinition ed = getElementByName(profile.getSnapshot().getElement(), definition.getContentReference());
3874        if (ed == null)
3875          c.getPieces().add(gen.new Piece(null, "Unknown reference to "+definition.getContentReference(), null));
3876        else
3877          c.getPieces().add(gen.new Piece("#"+ed.getPath(), "See "+ed.getPath(), null));
3878      }
3879      if (definition.getPath().endsWith("url") && definition.hasFixed()) {
3880        c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "\""+buildJson(definition.getFixed())+"\"", null).addStyle("color: darkgreen")));
3881      } else {
3882        if (url != null) {
3883          if (!c.getPieces().isEmpty()) 
3884            c.addPiece(gen.new Piece("br"));
3885          String fullUrl = url.startsWith("#") ? baseURL+url : url;
3886          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
3887          String ref = null;
3888          if (ed != null) {
3889            String p = ed.getUserString("path");
3890            if (p != null) {
3891              ref = p.startsWith("http:") || igmode ? p : Utilities.pathURL(corePath, p);
3892            }
3893          }
3894          c.getPieces().add(gen.new Piece(null, "URL: ", null).addStyle("font-weight:bold"));
3895          c.getPieces().add(gen.new Piece(ref, fullUrl, null));
3896        }
3897
3898        if (definition.hasSlicing()) {
3899          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3900          c.getPieces().add(gen.new Piece(null, "Slice: ", null).addStyle("font-weight:bold"));
3901          c.getPieces().add(gen.new Piece(null, describeSlice(definition.getSlicing()), null));
3902        }
3903        if (definition != null) {
3904          ElementDefinitionBindingComponent binding = null;
3905          if (valueDefn != null && valueDefn.hasBinding() && !valueDefn.getBinding().isEmpty())
3906            binding = valueDefn.getBinding();
3907          else if (definition.hasBinding())
3908            binding = definition.getBinding();
3909          if (binding!=null && !binding.isEmpty()) {
3910            if (!c.getPieces().isEmpty()) 
3911              c.addPiece(gen.new Piece("br"));
3912            BindingResolution br = pkp.resolveBinding(profile, binding, definition.getPath());
3913            c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, "Binding: ", null).addStyle("font-weight:bold")));
3914            c.getPieces().add(checkForNoChange(binding, gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url) || !pkp.prependLinks() ? br.url : corePath+br.url, br.display, null)));
3915            if (binding.hasStrength()) {
3916              c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, " (", null)));
3917              c.getPieces().add(checkForNoChange(binding, gen.new Piece(corePath+"terminologies.html#"+binding.getStrength().toCode(), binding.getStrength().toCode(), binding.getStrength().getDefinition())));              c.getPieces().add(gen.new Piece(null, ")", null));
3918            }
3919          }
3920          for (ElementDefinitionConstraintComponent inv : definition.getConstraint()) {
3921            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3922            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getKey()+": ", null).addStyle("font-weight:bold")));
3923            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getHuman(), null)));
3924          }
3925          if (definition.hasFixed()) {
3926            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3927            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "Fixed Value: ", null).addStyle("font-weight:bold")));
3928            String s = buildJson(definition.getFixed());
3929            String link = null;
3930            if (Utilities.isAbsoluteUrl(s))
3931              link = pkp.getLinkForUrl(corePath, s);
3932            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(link, s, null).addStyle("color: darkgreen")));
3933          } else if (definition.hasPattern()) {
3934            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3935            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, "Required Pattern: ", null).addStyle("font-weight:bold")));
3936            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, buildJson(definition.getPattern()), null).addStyle("color: darkgreen")));
3937          } else if (definition.hasExample()) {
3938            for (ElementDefinitionExampleComponent ex : definition.getExample()) {
3939              if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3940              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, "Example'"+("".equals("General")? "" : " "+ex.getLabel()+"'")+": ", null).addStyle("font-weight:bold")));
3941              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, buildJson(ex.getValue()), null).addStyle("color: darkgreen")));
3942            }
3943          }
3944          if (definition.hasMaxLength() && definition.getMaxLength()!=0) {
3945            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3946            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, "Max Length: ", null).addStyle("font-weight:bold")));
3947            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, Integer.toString(definition.getMaxLength()), null).addStyle("color: darkgreen")));
3948          }
3949          if (profile != null) {
3950            for (StructureDefinitionMappingComponent md : profile.getMapping()) {
3951              if (md.hasExtension(ToolingExtensions.EXT_TABLE_NAME)) {
3952                ElementDefinitionMappingComponent map = null;
3953                for (ElementDefinitionMappingComponent m : definition.getMapping()) 
3954                  if (m.getIdentity().equals(md.getIdentity()))
3955                    map = m;
3956                if (map != null) {
3957                  for (int i = 0; i<definition.getMapping().size(); i++){
3958                    c.addPiece(gen.new Piece("br"));
3959                    c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(md, ToolingExtensions.EXT_TABLE_NAME)+": " + map.getMap(), null));
3960                  }
3961                }
3962              }
3963            }
3964          }
3965          if (definition.hasDefinition()) {
3966            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3967            c.getPieces().add(gen.new Piece(null, "Definition: ", null).addStyle("font-weight:bold"));
3968            c.addPiece(gen.new Piece("br"));
3969            c.addMarkdown(definition.getDefinition());
3970//            c.getPieces().add(checkForNoChange(definition.getCommentElement(), gen.new Piece(null, definition.getComment(), null)));
3971          }
3972          if (definition.getComment()!=null) {
3973            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
3974            c.getPieces().add(gen.new Piece(null, "Comments: ", null).addStyle("font-weight:bold"));
3975            c.addPiece(gen.new Piece("br"));
3976            c.addMarkdown(definition.getComment());
3977//            c.getPieces().add(checkForNoChange(definition.getCommentElement(), gen.new Piece(null, definition.getComment(), null)));
3978          }
3979        }
3980      }
3981    }
3982    return c;
3983  }
3984
3985
3986
3987  private String buildJson(DataType value) throws IOException {
3988    if (value instanceof PrimitiveType)
3989      return ((PrimitiveType) value).asStringValue();
3990
3991    IParser json = context.newJsonParser();
3992    return json.composeString(value, null);
3993  }
3994
3995
3996  public String describeSlice(ElementDefinitionSlicingComponent slicing) {
3997    return translate("sd.table", "%s, %s by %s", slicing.getOrdered() ? translate("sd.table", "Ordered") : translate("sd.table", "Unordered"), describe(slicing.getRules()), commas(slicing.getDiscriminator()));
3998  }
3999
4000  private String commas(List<ElementDefinitionSlicingDiscriminatorComponent> list) {
4001    CommaSeparatedStringBuilder c = new CommaSeparatedStringBuilder();
4002    for (ElementDefinitionSlicingDiscriminatorComponent id : list)
4003      c.append(id.getType().toCode()+":"+id.getPath());
4004    return c.toString();
4005  }
4006
4007
4008  private String describe(SlicingRules rules) {
4009    if (rules == null)
4010      return translate("sd.table", "Unspecified");
4011    switch (rules) {
4012    case CLOSED : return translate("sd.table", "Closed");
4013    case OPEN : return translate("sd.table", "Open");
4014    case OPENATEND : return translate("sd.table", "Open At End");
4015    default:
4016      return "??";
4017    }
4018  }
4019
4020  private boolean onlyInformationIsMapping(List<ElementDefinition> list, ElementDefinition e) {
4021    return (!e.hasSliceName() && !e.hasSlicing() && (onlyInformationIsMapping(e))) &&
4022        getChildren(list, e).isEmpty();
4023  }
4024
4025  private boolean onlyInformationIsMapping(ElementDefinition d) {
4026    return !d.hasShort() && !d.hasDefinition() &&
4027        !d.hasRequirements() && !d.getAlias().isEmpty() && !d.hasMinElement() &&
4028        !d.hasMax() && !d.getType().isEmpty() && !d.hasContentReference() &&
4029        !d.hasExample() && !d.hasFixed() && !d.hasMaxLengthElement() &&
4030        !d.getCondition().isEmpty() && !d.getConstraint().isEmpty() && !d.hasMustSupportElement() &&
4031        !d.hasBinding();
4032  }
4033
4034  private boolean allAreReference(List<TypeRefComponent> types) {
4035    for (TypeRefComponent t : types) {
4036      if (!t.hasTarget())
4037        return false;
4038    }
4039    return true;
4040  }
4041
4042  private List<ElementDefinition> getChildren(List<ElementDefinition> all, ElementDefinition element) {
4043    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
4044    int i = all.indexOf(element)+1;
4045    while (i < all.size() && all.get(i).getPath().length() > element.getPath().length()) {
4046      if ((all.get(i).getPath().substring(0, element.getPath().length()+1).equals(element.getPath()+".")) && !all.get(i).getPath().substring(element.getPath().length()+1).contains("."))
4047        result.add(all.get(i));
4048      i++;
4049    }
4050    return result;
4051  }
4052
4053  private String tail(String path) {
4054    if (path.contains("."))
4055      return path.substring(path.lastIndexOf('.')+1);
4056    else
4057      return path;
4058  }
4059
4060  private boolean isDataType(String value) {
4061    StructureDefinition sd = context.fetchTypeDefinition(value);
4062    if (sd == null) // might be running before all SDs are available
4063      return Utilities.existsInList(value, "Address", "Age", "Annotation", "Attachment", "CodeableConcept", "Coding", "ContactPoint", "Count", "Distance", "Duration", "HumanName", "Identifier", "Money", "Period", "Quantity", "Range", "Ratio", "Reference", "SampledData", "Signature", "Timing", 
4064            "ContactDetail", "Contributor", "DataRequirement", "Expression", "ParameterDefinition", "RelatedArtifact", "TriggerDefinition", "UsageContext");
4065    else 
4066      return sd.getKind() == StructureDefinitionKind.COMPLEXTYPE && sd.getDerivation() == TypeDerivationRule.SPECIALIZATION;
4067  }
4068
4069  private boolean isConstrainedDataType(String value) {
4070    StructureDefinition sd = context.fetchTypeDefinition(value);
4071    if (sd == null) // might be running before all SDs are available
4072      return Utilities.existsInList(value, "SimpleQuantity", "MoneyQuantity");
4073    else 
4074      return sd.getKind() == StructureDefinitionKind.COMPLEXTYPE && sd.getDerivation() == TypeDerivationRule.CONSTRAINT;
4075  }
4076
4077  private String baseType(String value) {
4078    StructureDefinition sd = context.fetchTypeDefinition(value);
4079    if (sd != null) // might be running before all SDs are available
4080      return sd.getType();
4081    if (Utilities.existsInList(value, "SimpleQuantity", "MoneyQuantity"))
4082      return "Quantity";
4083     throw new Error("Internal error -  type not known "+value);
4084  }
4085
4086
4087  public boolean isPrimitive(String value) {
4088    StructureDefinition sd = context.fetchTypeDefinition(value);
4089    if (sd == null) // might be running before all SDs are available
4090      return Utilities.existsInList(value, "base64Binary", "boolean", "canonical", "code", "date", "dateTime", "decimal", "id", "instant", "integer", "integer64", "markdown", "oid", "positiveInt", "string", "time", "unsignedInt", "uri", "url", "uuid");
4091    else 
4092      return sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE;
4093  }
4094
4095//  private static String listStructures(StructureDefinition p) {
4096//    StringBuilder b = new StringBuilder();
4097//    boolean first = true;
4098//    for (ProfileStructureComponent s : p.getStructure()) {
4099//      if (first)
4100//        first = false;
4101//      else
4102//        b.append(", ");
4103//      if (pkp != null && pkp.hasLinkFor(s.getType()))
4104//        b.append("<a href=\""+pkp.getLinkFor(s.getType())+"\">"+s.getType()+"</a>");
4105//      else
4106//        b.append(s.getType());
4107//    }
4108//    return b.toString();
4109//  }
4110
4111
4112  public StructureDefinition getProfile(StructureDefinition source, String url) {
4113        StructureDefinition profile = null;
4114        String code = null;
4115        if (url.startsWith("#")) {
4116                profile = source;
4117                code = url.substring(1);
4118        } else if (context != null) {
4119                String[] parts = url.split("\\#");
4120                profile = context.fetchResource(StructureDefinition.class, parts[0]);
4121      code = parts.length == 1 ? null : parts[1];
4122        }         
4123        if (profile == null)
4124                return null;
4125        if (code == null)
4126                return profile;
4127        for (Resource r : profile.getContained()) {
4128                if (r instanceof StructureDefinition && r.getId().equals(code))
4129                        return (StructureDefinition) r;
4130        }
4131        return null;
4132  }
4133
4134
4135
4136  public static class ElementDefinitionHolder {
4137    private String name;
4138    private ElementDefinition self;
4139    private int baseIndex = 0;
4140    private List<ElementDefinitionHolder> children;
4141    private boolean placeHolder = false;
4142
4143    public ElementDefinitionHolder(ElementDefinition self, boolean isPlaceholder) {
4144      super();
4145      this.self = self;
4146      this.name = self.getPath();
4147      this.placeHolder = isPlaceholder;
4148      children = new ArrayList<ElementDefinitionHolder>();      
4149    }
4150
4151    public ElementDefinitionHolder(ElementDefinition self) {
4152      this(self, false);
4153    }
4154
4155    public ElementDefinition getSelf() {
4156      return self;
4157    }
4158
4159    public List<ElementDefinitionHolder> getChildren() {
4160      return children;
4161    }
4162
4163    public int getBaseIndex() {
4164      return baseIndex;
4165    }
4166
4167    public void setBaseIndex(int baseIndex) {
4168      this.baseIndex = baseIndex;
4169    }
4170
4171    public boolean isPlaceHolder() {
4172      return this.placeHolder;
4173    }
4174
4175    @Override
4176    public String toString() {
4177      if (self.hasSliceName())
4178        return self.getPath()+"("+self.getSliceName()+")";
4179      else
4180        return self.getPath();
4181    }
4182  }
4183
4184  public static class ElementDefinitionComparer implements Comparator<ElementDefinitionHolder> {
4185
4186    private boolean inExtension;
4187    private List<ElementDefinition> snapshot;
4188    private int prefixLength;
4189    private String base;
4190    private String name;
4191    private Set<String> errors = new HashSet<String>();
4192
4193    public ElementDefinitionComparer(boolean inExtension, List<ElementDefinition> snapshot, String base, int prefixLength, String name) {
4194      this.inExtension = inExtension;
4195      this.snapshot = snapshot;
4196      this.prefixLength = prefixLength;
4197      this.base = base;
4198      this.name = name;
4199    }
4200
4201    @Override
4202    public int compare(ElementDefinitionHolder o1, ElementDefinitionHolder o2) {
4203      if (o1.getBaseIndex() == 0)
4204        o1.setBaseIndex(find(o1.getSelf().getPath()));
4205      if (o2.getBaseIndex() == 0)
4206        o2.setBaseIndex(find(o2.getSelf().getPath()));
4207      return o1.getBaseIndex() - o2.getBaseIndex();
4208    }
4209
4210    private int find(String path) {
4211      String op = path;
4212      int lc = 0;
4213      String actual = base+path.substring(prefixLength);
4214      for (int i = 0; i < snapshot.size(); i++) {
4215        String p = snapshot.get(i).getPath();
4216        if (p.equals(actual)) {
4217          return i;
4218        }
4219        if (p.endsWith("[x]") && actual.startsWith(p.substring(0, p.length()-3)) && !(actual.endsWith("[x]")) && !actual.substring(p.length()-3).contains(".")) {
4220          return i;
4221        }
4222        if (actual.endsWith("[x]") && p.startsWith(actual.substring(0, actual.length()-3)) && !p.substring(actual.length()-3).contains(".")) {
4223          return i;
4224        }
4225        if (path.startsWith(p+".") && snapshot.get(i).hasContentReference()) {
4226          String ref = snapshot.get(i).getContentReference();
4227          if (ref.substring(1, 2).toUpperCase().equals(ref.substring(1,2))) {
4228            actual = base+(ref.substring(1)+"."+path.substring(p.length()+1)).substring(prefixLength);
4229            path = actual;
4230          } else {
4231            // Older versions of FHIR (e.g. 2016May) had reference of the style #parameter instead of #Parameters.parameter, so we have to handle that
4232            actual = base+(path.substring(0,  path.indexOf(".")+1) + ref.substring(1)+"."+path.substring(p.length()+1)).substring(prefixLength);
4233            path = actual;
4234          }
4235            
4236          i = 0;
4237          lc++;
4238          if (lc > MAX_RECURSION_LIMIT)
4239            throw new Error("Internal recursion detection: find() loop path recursion > "+MAX_RECURSION_LIMIT+" - check paths are valid (for path "+path+"/"+op+")");
4240        }
4241      }
4242      if (prefixLength == 0)
4243        errors.add("Differential contains path "+path+" which is not found in the base");
4244      else
4245        errors.add("Differential contains path "+path+" which is actually "+actual+", which is not found in the base");
4246      return 0;
4247    }
4248
4249    public void checkForErrors(List<String> errorList) {
4250      if (errors.size() > 0) {
4251//        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
4252//        for (String s : errors)
4253//          b.append("StructureDefinition "+name+": "+s);
4254//        throw new DefinitionException(b.toString());
4255        for (String s : errors)
4256          if (s.startsWith("!"))
4257            errorList.add("!StructureDefinition "+name+": "+s.substring(1));
4258          else
4259            errorList.add("StructureDefinition "+name+": "+s);
4260      }
4261    }
4262  }
4263
4264
4265  public void sortDifferential(StructureDefinition base, StructureDefinition diff, String name, List<String> errors) throws FHIRException  {
4266    final List<ElementDefinition> diffList = diff.getDifferential().getElement();
4267    int lastCount = diffList.size();
4268    // first, we move the differential elements into a tree
4269    if (diffList.isEmpty())
4270      return;
4271    
4272    ElementDefinitionHolder edh = null;
4273    int i = 0;
4274    if (diffList.get(0).getPath().contains(".")) {
4275      String newPath = diffList.get(0).getPath().split("\\.")[0];
4276      ElementDefinition e = new ElementDefinition(newPath);
4277      edh = new ElementDefinitionHolder(e, true);
4278    } else {
4279      edh = new ElementDefinitionHolder(diffList.get(0));
4280      i = 1;
4281    }
4282
4283    boolean hasSlicing = false;
4284    List<String> paths = new ArrayList<String>(); // in a differential, slicing may not be stated explicitly
4285    for(ElementDefinition elt : diffList) {
4286      if (elt.hasSlicing() || paths.contains(elt.getPath())) {
4287        hasSlicing = true;
4288        break;
4289      }
4290      paths.add(elt.getPath());
4291    }
4292    if(!hasSlicing) {
4293      // if Differential does not have slicing then safe to pre-sort the list
4294      // so elements and subcomponents are together
4295      Collections.sort(diffList, new ElementNameCompare());
4296    }
4297
4298    processElementsIntoTree(edh, i, diff.getDifferential().getElement());
4299
4300    // now, we sort the siblings throughout the tree
4301    ElementDefinitionComparer cmp = new ElementDefinitionComparer(true, base.getSnapshot().getElement(), "", 0, name);
4302    sortElements(edh, cmp, errors);
4303
4304    // now, we serialise them back to a list
4305    diffList.clear();
4306    writeElements(edh, diffList);
4307    
4308    if (lastCount != diffList.size())
4309      errors.add("Sort failed: counts differ; at least one of the paths in the differential is illegal");
4310  }
4311
4312  private int processElementsIntoTree(ElementDefinitionHolder edh, int i, List<ElementDefinition> list) {
4313    String path = edh.getSelf().getPath();
4314    final String prefix = path + ".";
4315    while (i < list.size() && list.get(i).getPath().startsWith(prefix)) {
4316      if (list.get(i).getPath().substring(prefix.length()+1).contains(".")) {
4317        String newPath = prefix + list.get(i).getPath().substring(prefix.length()).split("\\.")[0];
4318        ElementDefinition e = new ElementDefinition(newPath);
4319        ElementDefinitionHolder child = new ElementDefinitionHolder(e, true);
4320        edh.getChildren().add(child);
4321        i = processElementsIntoTree(child, i, list);
4322        
4323      } else {
4324        ElementDefinitionHolder child = new ElementDefinitionHolder(list.get(i));
4325        edh.getChildren().add(child);
4326        i = processElementsIntoTree(child, i+1, list);
4327      }
4328    }
4329    return i;
4330  }
4331
4332  private void sortElements(ElementDefinitionHolder edh, ElementDefinitionComparer cmp, List<String> errors) throws FHIRException {
4333    if (edh.getChildren().size() == 1)
4334      // special case - sort needsto allocate base numbers, but there'll be no sort if there's only 1 child. So in that case, we just go ahead and allocated base number directly
4335      edh.getChildren().get(0).baseIndex = cmp.find(edh.getChildren().get(0).getSelf().getPath());
4336    else
4337      Collections.sort(edh.getChildren(), cmp);
4338    cmp.checkForErrors(errors);
4339
4340    for (ElementDefinitionHolder child : edh.getChildren()) {
4341      if (child.getChildren().size() > 0) {
4342        ElementDefinitionComparer ccmp = getComparer(cmp, child);
4343        if (ccmp != null)
4344        sortElements(child, ccmp, errors);
4345      }
4346    }
4347  }
4348
4349
4350  public ElementDefinitionComparer getComparer(ElementDefinitionComparer cmp, ElementDefinitionHolder child) throws FHIRException, Error {
4351    // what we have to check for here is running off the base profile into a data type profile
4352    ElementDefinition ed = cmp.snapshot.get(child.getBaseIndex());
4353    ElementDefinitionComparer ccmp;
4354    if (ed.getType().isEmpty() || isAbstract(ed.getType().get(0).getWorkingCode()) || ed.getType().get(0).getWorkingCode().equals(ed.getPath())) {
4355      ccmp = new ElementDefinitionComparer(true, cmp.snapshot, cmp.base, cmp.prefixLength, cmp.name);
4356    } else if (ed.getType().get(0).getWorkingCode().equals("Extension") && child.getSelf().getType().size() == 1 && child.getSelf().getType().get(0).hasProfile()) {
4357      StructureDefinition profile = context.fetchResource(StructureDefinition.class, child.getSelf().getType().get(0).getProfile().get(0).getValue());
4358      if (profile==null)
4359        ccmp = null; // this might happen before everything is loaded. And we don't so much care about sot order in this case
4360      else
4361      ccmp = new ElementDefinitionComparer(true, profile.getSnapshot().getElement(), ed.getType().get(0).getWorkingCode(), child.getSelf().getPath().length(), cmp.name);
4362    } else if (ed.getType().size() == 1 && !ed.getType().get(0).getWorkingCode().equals("*")) {
4363      StructureDefinition profile = context.fetchResource(StructureDefinition.class, sdNs(ed.getType().get(0).getWorkingCode()));
4364      if (profile==null)
4365        throw new FHIRException("Unable to resolve profile " + sdNs(ed.getType().get(0).getWorkingCode()) + " in element " + ed.getPath());
4366      ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), ed.getType().get(0).getWorkingCode(), child.getSelf().getPath().length(), cmp.name);
4367    } else if (child.getSelf().getType().size() == 1) {
4368      StructureDefinition profile = context.fetchResource(StructureDefinition.class, sdNs(child.getSelf().getType().get(0).getWorkingCode()));
4369      if (profile==null)
4370        throw new FHIRException("Unable to resolve profile " + sdNs(ed.getType().get(0).getWorkingCode()) + " in element " + ed.getPath());
4371      ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), child.getSelf().getType().get(0).getWorkingCode(), child.getSelf().getPath().length(), cmp.name);
4372    } else if (ed.getPath().endsWith("[x]") && !child.getSelf().getPath().endsWith("[x]")) {
4373      String edLastNode = ed.getPath().replaceAll("(.*\\.)*(.*)", "$2");
4374      String childLastNode = child.getSelf().getPath().replaceAll("(.*\\.)*(.*)", "$2");
4375      String p = childLastNode.substring(edLastNode.length()-3);
4376      if (isPrimitive(Utilities.uncapitalize(p)))
4377        p = Utilities.uncapitalize(p);
4378      StructureDefinition sd = context.fetchResource(StructureDefinition.class, sdNs(p));
4379      if (sd == null)
4380        throw new Error("Unable to find profile '"+p+"' at "+ed.getId());
4381      ccmp = new ElementDefinitionComparer(false, sd.getSnapshot().getElement(), p, child.getSelf().getPath().length(), cmp.name);
4382    } else if (child.getSelf().hasType() && child.getSelf().getType().get(0).getWorkingCode().equals("Reference")) {
4383      for (TypeRefComponent t: child.getSelf().getType()) {
4384        if (!t.getWorkingCode().equals("Reference")) {
4385          throw new Error("Can't have children on an element with a polymorphic type - you must slice and constrain the types first (sortElements: "+ed.getPath()+":"+typeCode(ed.getType())+")");
4386        }
4387      }
4388      StructureDefinition profile = context.fetchResource(StructureDefinition.class, sdNs(ed.getType().get(0).getWorkingCode()));
4389      ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), ed.getType().get(0).getWorkingCode(), child.getSelf().getPath().length(), cmp.name);
4390    } else if (!child.getSelf().hasType() && ed.getType().get(0).getWorkingCode().equals("Reference")) {
4391      for (TypeRefComponent t: ed.getType()) {
4392        if (!t.getWorkingCode().equals("Reference")) {
4393          throw new Error("Not handled yet (sortElements: "+ed.getPath()+":"+typeCode(ed.getType())+")");
4394        }
4395      }
4396      StructureDefinition profile = context.fetchResource(StructureDefinition.class, sdNs(ed.getType().get(0).getWorkingCode()));
4397      ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), ed.getType().get(0).getWorkingCode(), child.getSelf().getPath().length(), cmp.name);
4398    } else {
4399      // this is allowed if we only profile the extensions
4400      StructureDefinition profile = context.fetchResource(StructureDefinition.class, sdNs("Element"));
4401      if (profile==null)
4402        throw new FHIRException("Unable to resolve profile " + sdNs(ed.getType().get(0).getWorkingCode()) + " in element " + ed.getPath());
4403      ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), "Element", child.getSelf().getPath().length(), cmp.name);
4404//      throw new Error("Not handled yet (sortElements: "+ed.getPath()+":"+typeCode(ed.getType())+")");
4405    }
4406    return ccmp;
4407  }
4408
4409  private static String sdNs(String type) {
4410    return sdNs(type, null);
4411  }
4412  
4413  public static String sdNs(String type, String overrideVersionNs) {
4414    if (Utilities.isAbsoluteUrl(type))
4415      return type;
4416    else if (overrideVersionNs != null)
4417      return Utilities.pathURL(overrideVersionNs, type);
4418    else
4419      return "http://hl7.org/fhir/StructureDefinition/"+type;
4420  }
4421
4422
4423  private boolean isAbstract(String code) {
4424    return code.equals("Element") || code.equals("BackboneElement") || code.equals("Resource") || code.equals("DomainResource");
4425  }
4426
4427
4428  private void writeElements(ElementDefinitionHolder edh, List<ElementDefinition> list) {
4429    if (!edh.isPlaceHolder())
4430      list.add(edh.getSelf());
4431    for (ElementDefinitionHolder child : edh.getChildren()) {
4432      writeElements(child, list);
4433    }
4434  }
4435
4436  /**
4437   * First compare element by path then by name if same
4438   */
4439  private static class ElementNameCompare implements Comparator<ElementDefinition> {
4440
4441    @Override
4442    public int compare(ElementDefinition o1, ElementDefinition o2) {
4443      String path1 = normalizePath(o1);
4444      String path2 = normalizePath(o2);
4445      int cmp = path1.compareTo(path2);
4446      if (cmp == 0) {
4447        String name1 = o1.hasSliceName() ? o1.getSliceName() : "";
4448        String name2 = o2.hasSliceName() ? o2.getSliceName() : "";
4449        cmp = name1.compareTo(name2);
4450      }
4451      return cmp;
4452    }
4453
4454    private static String normalizePath(ElementDefinition e) {
4455      if (!e.hasPath()) return "";
4456      String path = e.getPath();
4457      // if sorting element names make sure onset[x] appears before onsetAge, onsetDate, etc.
4458      // so strip off the [x] suffix when comparing the path names.
4459      if (path.endsWith("[x]")) {
4460        path = path.substring(0, path.length()-3);
4461      }
4462      return path;
4463    }
4464
4465  }
4466
4467
4468  // generate schematrons for the rules in a structure definition
4469  public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
4470    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
4471      throw new DefinitionException("not the right kind of structure to generate schematrons for");
4472    if (!structure.hasSnapshot())
4473      throw new DefinitionException("needs a snapshot");
4474
4475        StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
4476
4477        if (base != null) {
4478          SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
4479
4480          ElementDefinition ed = structure.getSnapshot().getElement().get(0);
4481          generateForChildren(sch, "f:"+ed.getPath(), ed, structure, base);
4482          sch.dump();
4483        }
4484  }
4485
4486  // generate a CSV representation of the structure definition
4487  public void generateCsvs(OutputStream dest, StructureDefinition structure, boolean asXml) throws IOException, DefinitionException, Exception {
4488    if (!structure.hasSnapshot())
4489      throw new DefinitionException("needs a snapshot");
4490
4491    CSVWriter csv = new CSVWriter(dest, structure, asXml);
4492
4493    for (ElementDefinition child : structure.getSnapshot().getElement()) {
4494      csv.processElement(child);
4495    }
4496    csv.dump();
4497  }
4498  
4499  // generate an Excel representation of the structure definition
4500  public void generateXlsx(OutputStream dest, StructureDefinition structure, boolean asXml, boolean hideMustSupportFalse) throws IOException, DefinitionException, Exception {
4501    if (structure == null) {
4502      System.out.println("no structure!");
4503    }
4504    if (!structure.hasSnapshot()) {
4505      throw new DefinitionException("needs a snapshot");
4506    }
4507
4508    XLSXWriter xlsx = new XLSXWriter(dest, structure, asXml, hideMustSupportFalse);
4509
4510    for (ElementDefinition child : structure.getSnapshot().getElement()) {
4511      xlsx.processElement(child);
4512    }
4513    xlsx.dump();
4514    xlsx.close();
4515  }
4516  
4517  private class Slicer extends ElementDefinitionSlicingComponent {
4518    String criteria = "";
4519    String name = "";   
4520    boolean check;
4521    public Slicer(boolean cantCheck) {
4522      super();
4523      this.check = cantCheck;
4524    }
4525  }
4526  
4527  private Slicer generateSlicer(ElementDefinition child, ElementDefinitionSlicingComponent slicing, StructureDefinition structure) {
4528    // given a child in a structure, it's sliced. figure out the slicing xpath
4529    if (child.getPath().endsWith(".extension")) {
4530      ElementDefinition ued = getUrlFor(structure, child);
4531      if ((ued == null || !ued.hasFixed()) && !(child.hasType() && (child.getType().get(0).hasProfile())))
4532        return new Slicer(false);
4533      else {
4534      Slicer s = new Slicer(true);
4535      String url = (ued == null || !ued.hasFixed()) ? child.getType().get(0).getProfile().get(0).getValue() : ((UriType) ued.getFixed()).asStringValue();
4536      s.name = " with URL = '"+url+"'";
4537      s.criteria = "[@url = '"+url+"']";
4538      return s;
4539      }
4540    } else
4541      return new Slicer(false);
4542  }
4543
4544  private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
4545    //    generateForChild(txt, structure, child);
4546    List<ElementDefinition> children = getChildList(structure, ed);
4547    String sliceName = null;
4548    ElementDefinitionSlicingComponent slicing = null;
4549    for (ElementDefinition child : children) {
4550      String name = tail(child.getPath());
4551      if (child.hasSlicing()) {
4552        sliceName = name;
4553        slicing = child.getSlicing();        
4554      } else if (!name.equals(sliceName))
4555        slicing = null;
4556      
4557      ElementDefinition based = getByPath(base, child.getPath());
4558      boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
4559      boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
4560      Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
4561      if (slicer.check) {
4562        if (doMin || doMax) {
4563          Section s = sch.section(xpath);
4564          Rule r = s.rule(xpath);
4565          if (doMin) 
4566            r.assrt("count(f:"+name+slicer.criteria+") >= "+Integer.toString(child.getMin()), name+slicer.name+": minimum cardinality of '"+name+"' is "+Integer.toString(child.getMin()));
4567          if (doMax) 
4568            r.assrt("count(f:"+name+slicer.criteria+") <= "+child.getMax(), name+slicer.name+": maximum cardinality of '"+name+"' is "+child.getMax());
4569          }
4570        }
4571      }
4572    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
4573      if (inv.hasXpath()) {
4574        Section s = sch.section(ed.getPath());
4575        Rule r = s.rule(xpath);
4576        r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId()+": " : "")+inv.getHuman()+(inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
4577      }
4578    }
4579    for (ElementDefinition child : children) {
4580      String name = tail(child.getPath());
4581      generateForChildren(sch, xpath+"/f:"+name, child, structure, base);
4582    }
4583  }
4584
4585
4586
4587
4588  private ElementDefinition getByPath(StructureDefinition base, String path) {
4589                for (ElementDefinition ed : base.getSnapshot().getElement()) {
4590                        if (ed.getPath().equals(path))
4591                                return ed;
4592                        if (ed.getPath().endsWith("[x]") && ed.getPath().length() <= path.length()-3 &&  ed.getPath().substring(0, ed.getPath().length()-3).equals(path.substring(0, ed.getPath().length()-3)))
4593                                return ed;
4594    }
4595          return null;
4596  }
4597
4598
4599  public void setIds(StructureDefinition sd, boolean checkFirst) throws DefinitionException  {
4600    if (!checkFirst || !sd.hasDifferential() || hasMissingIds(sd.getDifferential().getElement())) {
4601      if (!sd.hasDifferential())
4602        sd.setDifferential(new StructureDefinitionDifferentialComponent());
4603      generateIds(sd.getDifferential().getElement(), sd.getUrl());
4604    }
4605    if (!checkFirst || !sd.hasSnapshot() || hasMissingIds(sd.getSnapshot().getElement())) {
4606      if (!sd.hasSnapshot())
4607        sd.setSnapshot(new StructureDefinitionSnapshotComponent());
4608      generateIds(sd.getSnapshot().getElement(), sd.getUrl());
4609    }
4610  }
4611
4612
4613  private boolean hasMissingIds(List<ElementDefinition> list) {
4614    for (ElementDefinition ed : list) {
4615      if (!ed.hasId())
4616        return true;
4617    }    
4618    return false;
4619  }
4620
4621  public class SliceList {
4622
4623    private Map<String, String> slices = new HashMap<>();
4624    
4625    public void seeElement(ElementDefinition ed) {
4626      Iterator<Map.Entry<String,String>> iter = slices.entrySet().iterator();
4627      while (iter.hasNext()) {
4628        Map.Entry<String,String> entry = iter.next();
4629        if (entry.getKey().length() > ed.getPath().length() || entry.getKey().equals(ed.getPath()))
4630          iter.remove();
4631      }
4632      
4633      if (ed.hasSliceName()) 
4634        slices.put(ed.getPath(), ed.getSliceName());
4635    }
4636
4637    public String[] analyse(List<String> paths) {
4638      String s = paths.get(0);
4639      String[] res = new String[paths.size()];
4640      res[0] = null;
4641      for (int i = 1; i < paths.size(); i++) {
4642        s = s + "."+paths.get(i);
4643        if (slices.containsKey(s)) 
4644          res[i] = slices.get(s);
4645        else
4646          res[i] = null;
4647      }
4648      return res;
4649    }
4650
4651  }
4652
4653  private void generateIds(List<ElementDefinition> list, String name) throws DefinitionException  {
4654    if (list.isEmpty())
4655      return;
4656    
4657    Map<String, String> idMap = new HashMap<String, String>();
4658    Map<String, String> idList = new HashMap<String, String>();
4659    
4660    SliceList sliceInfo = new SliceList();
4661    // first pass, update the element ids
4662    for (ElementDefinition ed : list) {
4663      List<String> paths = new ArrayList<String>();
4664      if (!ed.hasPath())
4665        throw new DefinitionException("No path on element Definition "+Integer.toString(list.indexOf(ed))+" in "+name);
4666      sliceInfo.seeElement(ed);
4667      String[] pl = ed.getPath().split("\\.");
4668      for (int i = paths.size(); i < pl.length; i++) // -1 because the last path is in focus
4669        paths.add(pl[i]);
4670      String slices[] = sliceInfo.analyse(paths);
4671      
4672      StringBuilder b = new StringBuilder();
4673      b.append(paths.get(0));
4674      for (int i = 1; i < paths.size(); i++) {
4675        b.append(".");
4676        String s = paths.get(i);
4677        String p = slices[i];
4678        b.append(fixChars(s));
4679        if (p != null) {
4680          b.append(":");
4681          b.append(p);
4682        }
4683      }
4684      String bs = b.toString();
4685      idMap.put(ed.hasId() ? ed.getId() : ed.getPath(), bs);
4686      ed.setId(bs);
4687      if (idList.containsKey(bs)) {
4688        if (exception || messages == null) {
4689          throw new DefinitionException("Same id '"+bs+"'on multiple elements "+idList.get(bs)+"/"+ed.getPath()+" in "+name);
4690        } else
4691          messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, name+"."+bs, "Duplicate Element id "+bs, ValidationMessage.IssueSeverity.ERROR));
4692      }
4693      idList.put(bs, ed.getPath());
4694      if (ed.hasContentReference()) {
4695        String s = ed.getContentReference().substring(1);
4696        if (idMap.containsKey(s))
4697          ed.setContentReference("#"+idMap.get(s));
4698        
4699      }
4700    }  
4701    // second path - fix up any broken path based id references
4702    
4703  }
4704
4705
4706  private Object fixChars(String s) {
4707    return s.replace("_", "-");
4708  }
4709
4710
4711//  private String describeExtension(ElementDefinition ed) {
4712//    if (!ed.hasType() || !ed.getTypeFirstRep().hasProfile())
4713//      return "";
4714//    return "$"+urlTail(ed.getTypeFirstRep().getProfile());
4715//  }
4716//
4717
4718  private String urlTail(String profile) {
4719    return profile.contains("/") ? profile.substring(profile.lastIndexOf("/")+1) : profile;
4720  }
4721
4722
4723  private String checkName(String name) {
4724//    if (name.contains("."))
4725////      throw new Exception("Illegal name "+name+": no '.'");
4726//    if (name.contains(" "))
4727//      throw new Exception("Illegal name "+name+": no spaces");
4728    StringBuilder b = new StringBuilder();
4729    for (char c : name.toCharArray()) {
4730      if (!Utilities.existsInList(c, '.', ' ', ':', '"', '\'', '(', ')', '&', '[', ']'))
4731        b.append(c);
4732    }
4733    return b.toString().toLowerCase();
4734  }
4735
4736
4737  private int charCount(String path, char t) {
4738    int res = 0;
4739    for (char ch : path.toCharArray()) {
4740      if (ch == t)
4741        res++;
4742    }
4743    return res;
4744  }
4745
4746//
4747//private void generateForChild(TextStreamWriter txt,
4748//    StructureDefinition structure, ElementDefinition child) {
4749//  // TODO Auto-generated method stub
4750//
4751//}
4752
4753  private interface ExampleValueAccessor {
4754    DataType getExampleValue(ElementDefinition ed);
4755    String getId();
4756  }
4757
4758  private class BaseExampleValueAccessor implements ExampleValueAccessor {
4759    @Override
4760    public DataType getExampleValue(ElementDefinition ed) {
4761      if (ed.hasFixed())
4762        return ed.getFixed();
4763      if (ed.hasExample())
4764        return ed.getExample().get(0).getValue();
4765      else
4766        return null;
4767    }
4768
4769    @Override
4770    public String getId() {
4771      return "-genexample";
4772    }
4773  }
4774  
4775  private class ExtendedExampleValueAccessor implements ExampleValueAccessor {
4776    private String index;
4777
4778    public ExtendedExampleValueAccessor(String index) {
4779      this.index = index;
4780    }
4781    @Override
4782    public DataType getExampleValue(ElementDefinition ed) {
4783      if (ed.hasFixed())
4784        return ed.getFixed();
4785      for (Extension ex : ed.getExtension()) {
4786       String ndx = ToolingExtensions.readStringExtension(ex, "index");
4787       DataType value = ToolingExtensions.getExtension(ex, "exValue").getValue();
4788       if (index.equals(ndx) && value != null)
4789         return value;
4790      }
4791      return null;
4792    }
4793    @Override
4794    public String getId() {
4795      return "-genexample-"+index;
4796    }
4797  }
4798  
4799  public List<org.hl7.fhir.r5.elementmodel.Element> generateExamples(StructureDefinition sd, boolean evenWhenNoExamples) throws FHIRException {
4800    List<org.hl7.fhir.r5.elementmodel.Element> examples = new ArrayList<org.hl7.fhir.r5.elementmodel.Element>();
4801    if (sd.hasSnapshot()) {
4802      if (evenWhenNoExamples || hasAnyExampleValues(sd)) 
4803        examples.add(generateExample(sd, new BaseExampleValueAccessor()));
4804      for (int i = 1; i <= 50; i++) {
4805        if (hasAnyExampleValues(sd, Integer.toString(i))) 
4806          examples.add(generateExample(sd, new ExtendedExampleValueAccessor(Integer.toString(i))));
4807      }
4808    }
4809    return examples;
4810  }
4811
4812  private org.hl7.fhir.r5.elementmodel.Element generateExample(StructureDefinition profile, ExampleValueAccessor accessor) throws FHIRException {
4813    ElementDefinition ed = profile.getSnapshot().getElementFirstRep();
4814    org.hl7.fhir.r5.elementmodel.Element r = new org.hl7.fhir.r5.elementmodel.Element(ed.getPath(), new Property(context, ed, profile));
4815    List<ElementDefinition> children = getChildMap(profile, ed);
4816    for (ElementDefinition child : children) {
4817      if (child.getPath().endsWith(".id")) {
4818        org.hl7.fhir.r5.elementmodel.Element id = new org.hl7.fhir.r5.elementmodel.Element("id", new Property(context, child, profile));
4819        id.setValue(profile.getId()+accessor.getId());
4820        r.getChildren().add(id);
4821      } else { 
4822        org.hl7.fhir.r5.elementmodel.Element e = createExampleElement(profile, child, accessor);
4823        if (e != null)
4824          r.getChildren().add(e);
4825      }
4826    }
4827    return r;
4828  }
4829
4830  private org.hl7.fhir.r5.elementmodel.Element createExampleElement(StructureDefinition profile, ElementDefinition ed, ExampleValueAccessor accessor) throws FHIRException {
4831    DataType v = accessor.getExampleValue(ed);
4832    if (v != null) {
4833      return new ObjectConverter(context).convert(new Property(context, ed, profile), v);
4834    } else {
4835      org.hl7.fhir.r5.elementmodel.Element res = new org.hl7.fhir.r5.elementmodel.Element(tail(ed.getPath()), new Property(context, ed, profile));
4836      boolean hasValue = false;
4837      List<ElementDefinition> children = getChildMap(profile, ed);
4838      for (ElementDefinition child : children) {
4839        if (!child.hasContentReference()) {
4840        org.hl7.fhir.r5.elementmodel.Element e = createExampleElement(profile, child, accessor);
4841        if (e != null) {
4842          hasValue = true;
4843          res.getChildren().add(e);
4844        }
4845      }
4846      }
4847      if (hasValue)
4848        return res;
4849      else
4850        return null;
4851    }
4852  }
4853
4854  private boolean hasAnyExampleValues(StructureDefinition sd, String index) {
4855    for (ElementDefinition ed : sd.getSnapshot().getElement())
4856      for (Extension ex : ed.getExtension()) {
4857        String ndx = ToolingExtensions.readStringExtension(ex, "index");
4858        Extension exv = ToolingExtensions.getExtension(ex, "exValue");
4859        if (exv != null) {
4860          DataType value = exv.getValue();
4861        if (index.equals(ndx) && value != null)
4862          return true;
4863        }
4864       }
4865    return false;
4866  }
4867
4868
4869  private boolean hasAnyExampleValues(StructureDefinition sd) {
4870    for (ElementDefinition ed : sd.getSnapshot().getElement())
4871      if (ed.hasExample())
4872        return true;
4873    return false;
4874  }
4875
4876
4877  public void populateLogicalSnapshot(StructureDefinition sd) throws FHIRException {
4878    sd.getSnapshot().getElement().add(sd.getDifferential().getElementFirstRep().copy());
4879    
4880    if (sd.hasBaseDefinition()) {
4881    StructureDefinition base = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
4882    if (base == null)
4883      throw new FHIRException("Unable to find base definition for logical model: "+sd.getBaseDefinition()+" from "+sd.getUrl());
4884    copyElements(sd, base.getSnapshot().getElement());
4885    }
4886    copyElements(sd, sd.getDifferential().getElement());
4887  }
4888
4889
4890  private void copyElements(StructureDefinition sd, List<ElementDefinition> list) {
4891    for (ElementDefinition ed : list) {
4892      if (ed.getPath().contains(".")) {
4893        ElementDefinition n = ed.copy();
4894        n.setPath(sd.getSnapshot().getElementFirstRep().getPath()+"."+ed.getPath().substring(ed.getPath().indexOf(".")+1));
4895        sd.getSnapshot().addElement(n);
4896      }
4897    }
4898  }
4899
4900    
4901  public void cleanUpDifferential(StructureDefinition sd) {
4902    if (sd.getDifferential().getElement().size() > 1)
4903      cleanUpDifferential(sd, 1);
4904  }
4905  
4906  private void cleanUpDifferential(StructureDefinition sd, int start) {
4907    int level = Utilities.charCount(sd.getDifferential().getElement().get(start).getPath(), '.');
4908    int c = start;
4909    int len = sd.getDifferential().getElement().size();
4910    HashSet<String> paths = new HashSet<String>();
4911    while (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') == level) {
4912      ElementDefinition ed = sd.getDifferential().getElement().get(c);
4913      if (!paths.contains(ed.getPath())) {
4914        paths.add(ed.getPath());
4915        int ic = c+1; 
4916        while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') > level) 
4917          ic++;
4918        ElementDefinition slicer = null;
4919        List<ElementDefinition> slices = new ArrayList<ElementDefinition>();
4920        slices.add(ed);
4921        while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') == level) {
4922          ElementDefinition edi = sd.getDifferential().getElement().get(ic);
4923          if (ed.getPath().equals(edi.getPath())) {
4924            if (slicer == null) {
4925              slicer = new ElementDefinition();
4926              slicer.setPath(edi.getPath());
4927              slicer.getSlicing().setRules(SlicingRules.OPEN);
4928              sd.getDifferential().getElement().add(c, slicer);
4929              c++;
4930              ic++;
4931            }
4932            slices.add(edi);
4933          }
4934          ic++;
4935          while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') > level) 
4936            ic++;
4937        }
4938        // now we're at the end, we're going to figure out the slicing discriminator
4939        if (slicer != null)
4940          determineSlicing(slicer, slices);
4941      }
4942      c++;
4943      if (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') > level) {
4944        cleanUpDifferential(sd, c);
4945        c++;
4946        while (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') > level) 
4947          c++;
4948      }
4949  }
4950  }
4951
4952
4953  private void determineSlicing(ElementDefinition slicer, List<ElementDefinition> slices) {
4954    // first, name them
4955    int i = 0;
4956    for (ElementDefinition ed : slices) {
4957      if (ed.hasUserData("slice-name")) {
4958        ed.setSliceName(ed.getUserString("slice-name"));
4959      } else {
4960        i++;
4961        ed.setSliceName("slice-"+Integer.toString(i));
4962      }
4963    }
4964    // now, the hard bit, how are they differentiated? 
4965    // right now, we hard code this...
4966    if (slicer.getPath().endsWith(".extension") || slicer.getPath().endsWith(".modifierExtension"))
4967      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("url");
4968    else if (slicer.getPath().equals("DiagnosticReport.result"))
4969      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("reference.code");
4970    else if (slicer.getPath().equals("Observation.related"))
4971      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("target.reference.code");
4972    else if (slicer.getPath().equals("Bundle.entry"))
4973      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("resource.@profile");
4974    else  
4975      throw new Error("No slicing for "+slicer.getPath()); 
4976  }
4977
4978  public class SpanEntry {
4979    private List<SpanEntry> children = new ArrayList<SpanEntry>();
4980    private boolean profile;
4981    private String id;
4982    private String name;
4983    private String resType;
4984    private String cardinality;
4985    private String description;
4986    private String profileLink;
4987    private String resLink;
4988    private String type;
4989    
4990    public String getName() {
4991      return name;
4992    }
4993    public void setName(String name) {
4994      this.name = name;
4995    }
4996    public String getResType() {
4997      return resType;
4998    }
4999    public void setResType(String resType) {
5000      this.resType = resType;
5001    }
5002    public String getCardinality() {
5003      return cardinality;
5004    }
5005    public void setCardinality(String cardinality) {
5006      this.cardinality = cardinality;
5007    }
5008    public String getDescription() {
5009      return description;
5010    }
5011    public void setDescription(String description) {
5012      this.description = description;
5013    }
5014    public String getProfileLink() {
5015      return profileLink;
5016    }
5017    public void setProfileLink(String profileLink) {
5018      this.profileLink = profileLink;
5019    }
5020    public String getResLink() {
5021      return resLink;
5022    }
5023    public void setResLink(String resLink) {
5024      this.resLink = resLink;
5025    }
5026    public String getId() {
5027      return id;
5028    }
5029    public void setId(String id) {
5030      this.id = id;
5031    }
5032    public boolean isProfile() {
5033      return profile;
5034    }
5035    public void setProfile(boolean profile) {
5036      this.profile = profile;
5037    }
5038    public List<SpanEntry> getChildren() {
5039      return children;
5040    }
5041    public String getType() {
5042      return type;
5043    }
5044    public void setType(String type) {
5045      this.type = type;
5046    }
5047    
5048  }
5049
5050  public XhtmlNode generateSpanningTable(StructureDefinition profile, String imageFolder, boolean onlyConstraints, String constraintPrefix, Set<String> outputTracker) throws IOException, FHIRException {
5051    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, false, true);
5052    gen.setTranslator(getTranslator());
5053    TableModel model = initSpanningTable(gen, "", false, profile.getId());
5054    Set<String> processed = new HashSet<String>();
5055    SpanEntry span = buildSpanningTable("(focus)", "", profile, processed, onlyConstraints, constraintPrefix);
5056    
5057    genSpanEntry(gen, model.getRows(), span);
5058    return gen.generate(model, "", 0, outputTracker);
5059  }
5060
5061  private SpanEntry buildSpanningTable(String name, String cardinality, StructureDefinition profile, Set<String> processed, boolean onlyConstraints, String constraintPrefix) throws IOException {
5062    SpanEntry res = buildSpanEntryFromProfile(name, cardinality, profile);
5063    boolean wantProcess = !processed.contains(profile.getUrl());
5064    processed.add(profile.getUrl());
5065    if (wantProcess && profile.getDerivation() == TypeDerivationRule.CONSTRAINT) {
5066      for (ElementDefinition ed : profile.getSnapshot().getElement()) {
5067        if (!"0".equals(ed.getMax()) && ed.getType().size() > 0) {
5068          String card = getCardinality(ed, profile.getSnapshot().getElement());
5069          if (!card.endsWith(".0")) {
5070            List<String> refProfiles = listReferenceProfiles(ed);
5071            if (refProfiles.size() > 0) {
5072              String uri = refProfiles.get(0);
5073              if (uri != null) {
5074                StructureDefinition sd = context.fetchResource(StructureDefinition.class, uri);
5075                if (sd != null && (!onlyConstraints || (sd.getDerivation() == TypeDerivationRule.CONSTRAINT && (constraintPrefix == null || sd.getUrl().startsWith(constraintPrefix))))) {
5076                  res.getChildren().add(buildSpanningTable(nameForElement(ed), card, sd, processed, onlyConstraints, constraintPrefix));
5077                }
5078              }
5079            }
5080          }
5081        } 
5082      }
5083    }
5084    return res;
5085  }
5086
5087
5088  private String getCardinality(ElementDefinition ed, List<ElementDefinition> list) {
5089    int min = ed.getMin();
5090    int max = !ed.hasMax() || ed.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(ed.getMax());
5091    while (ed != null && ed.getPath().contains(".")) {
5092      ed = findParent(ed, list);
5093      if (ed.getMax().equals("0"))
5094        max = 0;
5095      else if (!ed.getMax().equals("1") && !ed.hasSlicing())
5096        max = Integer.MAX_VALUE;
5097      if (ed.getMin() == 0)
5098        min = 0;
5099    }
5100    return Integer.toString(min)+".."+(max == Integer.MAX_VALUE ? "*" : Integer.toString(max));
5101  }
5102
5103
5104  private ElementDefinition findParent(ElementDefinition ed, List<ElementDefinition> list) {
5105    int i = list.indexOf(ed)-1;
5106    while (i >= 0 && !ed.getPath().startsWith(list.get(i).getPath()+"."))
5107      i--;
5108    if (i == -1)
5109      return null;
5110    else
5111      return list.get(i);
5112  }
5113
5114
5115  private List<String> listReferenceProfiles(ElementDefinition ed) {
5116    List<String> res = new ArrayList<String>();
5117    for (TypeRefComponent tr : ed.getType()) {
5118      // code is null if we're dealing with "value" and profile is null if we just have Reference()
5119      if (tr.hasTarget() && tr.hasTargetProfile())
5120        for (UriType u : tr.getTargetProfile())
5121          res.add(u.getValue());
5122    }
5123    return res;
5124  }
5125
5126
5127  private String nameForElement(ElementDefinition ed) {
5128    return ed.getPath().substring(ed.getPath().indexOf(".")+1);
5129  }
5130
5131
5132  private SpanEntry buildSpanEntryFromProfile(String name, String cardinality, StructureDefinition profile) throws IOException {
5133    SpanEntry res = new SpanEntry();
5134    res.setName(name);
5135    res.setCardinality(cardinality);
5136    res.setProfileLink(profile.getUserString("path"));
5137    res.setResType(profile.getType());
5138    StructureDefinition base = context.fetchResource(StructureDefinition.class, res.getResType());
5139    if (base != null)
5140      res.setResLink(base.getUserString("path"));
5141    res.setId(profile.getId());
5142    res.setProfile(profile.getDerivation() == TypeDerivationRule.CONSTRAINT);
5143    StringBuilder b = new StringBuilder();
5144    b.append(res.getResType());
5145    boolean first = true;
5146    boolean open = false;
5147    if (profile.getDerivation() == TypeDerivationRule.CONSTRAINT) {
5148      res.setDescription(profile.getName());
5149      for (ElementDefinition ed : profile.getSnapshot().getElement()) {
5150        if (isKeyProperty(ed.getBase().getPath()) && ed.hasFixed()) {
5151          if (first) {
5152            open = true;
5153            first = false;
5154            b.append("[");
5155          } else {
5156            b.append(", ");
5157          }
5158          b.append(tail(ed.getBase().getPath()));
5159          b.append("=");
5160          b.append(summarize(ed.getFixed()));
5161        }
5162      }
5163      if (open)
5164        b.append("]");
5165    } else
5166      res.setDescription("Base FHIR "+profile.getName());
5167    res.setType(b.toString());
5168    return res ;
5169  }
5170
5171
5172  private String summarize(DataType value) throws IOException {
5173    if (value instanceof Coding)
5174      return summarizeCoding((Coding) value);
5175    else if (value instanceof CodeableConcept)
5176      return summarizeCodeableConcept((CodeableConcept) value);
5177    else
5178      return buildJson(value);
5179  }
5180
5181
5182  private String summarizeCoding(Coding value) {
5183    String uri = value.getSystem();
5184    String system = NarrativeGenerator.describeSystem(uri);
5185    if (Utilities.isURL(system)) {
5186      if (system.equals("http://cap.org/protocols"))
5187        system = "CAP Code";
5188    }
5189    return system+" "+value.getCode();
5190  }
5191
5192
5193  private String summarizeCodeableConcept(CodeableConcept value) {
5194    if (value.hasCoding())
5195      return summarizeCoding(value.getCodingFirstRep());
5196    else
5197      return value.getText();
5198  }
5199
5200
5201  private boolean isKeyProperty(String path) {
5202    return Utilities.existsInList(path, "Observation.code");
5203  }
5204
5205
5206  public TableModel initSpanningTable(HierarchicalTableGenerator gen, String prefix, boolean isLogical, String id) {
5207    TableModel model = gen.new TableModel(id, true);
5208    
5209    model.setDocoImg(prefix+"help16.png");
5210    model.setDocoRef(prefix+"formats.html#table"); // todo: change to graph definition
5211    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Property", "A profiled resource", null, 0));
5212    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Card.", "Minimum and Maximum # of times the the element can appear in the instance", null, 0));
5213    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Content", "What goes here", null, 0));
5214    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Description", "Description of the profile", null, 0));
5215    return model;
5216  }
5217
5218  private void genSpanEntry(HierarchicalTableGenerator gen, List<Row> rows, SpanEntry span) throws IOException {
5219    Row row = gen.new Row();
5220    rows.add(row);
5221    row.setAnchor(span.getId());
5222    //row.setColor(..?);
5223    if (span.isProfile()) {
5224      row.setIcon("icon_profile.png", HierarchicalTableGenerator.TEXT_ICON_PROFILE);
5225    } else {
5226      row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
5227    }
5228    
5229    row.getCells().add(gen.new Cell(null, null, span.getName(), null, null));
5230    row.getCells().add(gen.new Cell(null, null, span.getCardinality(), null, null));
5231    row.getCells().add(gen.new Cell(null, span.getProfileLink(), span.getType(), null, null));
5232    row.getCells().add(gen.new Cell(null, null, span.getDescription(), null, null));
5233
5234    for (SpanEntry child : span.getChildren()) {
5235      genSpanEntry(gen, row.getSubRows(), child);
5236    }
5237  }
5238
5239
5240  public static ElementDefinitionSlicingDiscriminatorComponent interpretR2Discriminator(String discriminator, boolean isExists) {
5241    if (discriminator.endsWith("@pattern"))
5242      return makeDiscriminator(DiscriminatorType.PATTERN, discriminator.length() == 8 ? "" : discriminator.substring(0,discriminator.length()-9)); 
5243    if (discriminator.endsWith("@profile"))
5244      return makeDiscriminator(DiscriminatorType.PROFILE, discriminator.length() == 8 ? "" : discriminator.substring(0,discriminator.length()-9)); 
5245    if (discriminator.endsWith("@type")) 
5246      return makeDiscriminator(DiscriminatorType.TYPE, discriminator.length() == 5 ? "" : discriminator.substring(0,discriminator.length()-6));
5247    if (discriminator.endsWith("@exists"))
5248      return makeDiscriminator(DiscriminatorType.EXISTS, discriminator.length() == 7 ? "" : discriminator.substring(0,discriminator.length()-8)); 
5249    if (isExists)
5250      return makeDiscriminator(DiscriminatorType.EXISTS, discriminator); 
5251    return new ElementDefinitionSlicingDiscriminatorComponent().setType(DiscriminatorType.VALUE).setPath(discriminator);
5252  }
5253
5254
5255  private static ElementDefinitionSlicingDiscriminatorComponent makeDiscriminator(DiscriminatorType dType, String str) {
5256    return new ElementDefinitionSlicingDiscriminatorComponent().setType(dType).setPath(Utilities.noString(str)? "$this" : str);
5257  }
5258
5259
5260  public static String buildR2Discriminator(ElementDefinitionSlicingDiscriminatorComponent t) throws FHIRException {
5261    switch (t.getType()) {
5262    case PROFILE: return t.getPath()+"/@profile";
5263    case PATTERN: return t.getPath()+"/@pattern";
5264    case TYPE: return t.getPath()+"/@type";
5265    case VALUE: return t.getPath();
5266    case EXISTS: return t.getPath(); // determination of value vs. exists is based on whether there's only 2 slices - one with minOccurs=1 and other with maxOccur=0
5267    default: throw new FHIRException("Unable to represent "+t.getType().toCode()+":"+t.getPath()+" in R2");    
5268    }
5269  }
5270
5271
5272  public static StructureDefinition makeExtensionForVersionedURL(IWorkerContext context, String url) {
5273    String epath = url.substring(54);
5274    if (!epath.contains("."))
5275      return null;
5276    String type = epath.substring(0, epath.indexOf("."));
5277    StructureDefinition sd = context.fetchTypeDefinition(type);
5278    if (sd == null)
5279      return null;
5280    ElementDefinition ed = null;
5281    for (ElementDefinition t : sd.getSnapshot().getElement()) {
5282      if (t.getPath().equals(epath)) {
5283        ed = t;
5284        break;
5285      }
5286    }
5287    if (ed == null)
5288      return null;
5289    if ("Element".equals(ed.typeSummary()) || "BackboneElement".equals(ed.typeSummary())) {
5290      return null;
5291    } else {
5292      StructureDefinition template = context.fetchResource(StructureDefinition.class, "http://fhir-registry.smarthealthit.org/StructureDefinition/capabilities");
5293      StructureDefinition ext = template.copy();
5294      ext.setUrl(url);
5295      ext.setId("extension-"+epath);
5296      ext.setName("Extension-"+epath);
5297      ext.setTitle("Extension for r4 "+epath);
5298      ext.setStatus(sd.getStatus());
5299      ext.setDate(sd.getDate());
5300      ext.getContact().clear();
5301      ext.getContact().addAll(sd.getContact());
5302      ext.setFhirVersion(sd.getFhirVersion());
5303      ext.setDescription(ed.getDefinition());
5304      ext.getContext().clear();
5305      ext.addContext().setType(ExtensionContextType.ELEMENT).setExpression(epath.substring(0, epath.lastIndexOf(".")));
5306      ext.getDifferential().getElement().clear();
5307      ext.getSnapshot().getElement().get(3).setFixed(new UriType(url));
5308      ext.getSnapshot().getElement().set(4, ed.copy());
5309      ext.getSnapshot().getElement().get(4).setPath("Extension.value"+Utilities.capitalize(ed.typeSummary()));
5310      return ext;      
5311    }
5312
5313  }
5314
5315
5316  public boolean isThrowException() {
5317    return exception;
5318  }
5319
5320
5321  public void setThrowException(boolean exception) {
5322    this.exception = exception;
5323  }
5324
5325
5326  public ValidationOptions getTerminologyServiceOptions() {
5327    return terminologyServiceOptions;
5328  }
5329
5330
5331  public void setTerminologyServiceOptions(ValidationOptions terminologyServiceOptions) {
5332    this.terminologyServiceOptions = terminologyServiceOptions;
5333  }
5334
5335
5336  public boolean isNewSlicingProcessing() {
5337    return newSlicingProcessing;
5338  }
5339
5340
5341  public void setNewSlicingProcessing(boolean newSlicingProcessing) {
5342    this.newSlicingProcessing = newSlicingProcessing;
5343  }
5344
5345
5346  public boolean isDebug() {
5347    return debug;
5348  }
5349
5350
5351  public void setDebug(boolean debug) {
5352    this.debug = debug;
5353  }
5354
5355
5356  public String getDefWebRoot() {
5357    return defWebRoot;
5358  }
5359
5360
5361  public void setDefWebRoot(String defWebRoot) {
5362    this.defWebRoot = defWebRoot;
5363    if (!this.defWebRoot.endsWith("/"))
5364      this.defWebRoot = this.defWebRoot + '/';
5365  }
5366
5367
5368  public static StructureDefinition makeBaseDefinition(FHIRVersion fhirVersion) {
5369    StructureDefinition base = new StructureDefinition();
5370    base.setId("Base");
5371    base.setUrl("http://hl7.org/fhir/StructureDefinition/Base");
5372    base.setVersion(fhirVersion.toCode());
5373    base.setName("Base"); 
5374    base.setStatus(PublicationStatus.ACTIVE);
5375    base.setDate(new Date());
5376    base.setFhirVersion(fhirVersion);
5377    base.setKind(StructureDefinitionKind.COMPLEXTYPE); 
5378    base.setAbstract(true); 
5379    base.setType("Base");
5380    ElementDefinition e = base.getSnapshot().getElementFirstRep();
5381    e.setId("Base");
5382    e.setPath("Base"); 
5383    e.setMin(0); 
5384    e.setMax("*"); 
5385    e.getBase().setPath("Base");
5386    e.getBase().setMin(0); 
5387    e.getBase().setMax("*"); 
5388    e.setIsModifier(false); 
5389    e = base.getDifferential().getElementFirstRep();
5390    e.setId("Base");
5391    e.setPath("Base"); 
5392    e.setMin(0); 
5393    e.setMax("*"); 
5394    return base;
5395  }
5396
5397
5398
5399  
5400}