001package org.hl7.fhir.dstu3.conformance;
002
003import java.io.FileNotFoundException;
004import java.io.FileOutputStream;
005import java.io.IOException;
006import java.io.OutputStream;
007import java.util.ArrayList;
008import java.util.Collections;
009import java.util.Comparator;
010import java.util.HashMap;
011import java.util.HashSet;
012import java.util.List;
013import java.util.Map;
014import java.util.Set;
015
016import org.apache.commons.lang3.StringUtils;
017import org.hl7.fhir.dstu3.conformance.ProfileUtilities.ProfileKnowledgeProvider.BindingResolution;
018import org.hl7.fhir.dstu3.context.IWorkerContext;
019import org.hl7.fhir.dstu3.context.IWorkerContext.ValidationResult;
020import org.hl7.fhir.dstu3.elementmodel.ObjectConverter;
021import org.hl7.fhir.dstu3.elementmodel.Property;
022import org.hl7.fhir.dstu3.formats.IParser;
023import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
024import org.hl7.fhir.dstu3.formats.XmlParser;
025import org.hl7.fhir.dstu3.model.Base;
026import org.hl7.fhir.dstu3.model.BooleanType;
027import org.hl7.fhir.dstu3.model.CodeType;
028import org.hl7.fhir.dstu3.model.CodeableConcept;
029import org.hl7.fhir.dstu3.model.Coding;
030import org.hl7.fhir.dstu3.model.Element;
031import org.hl7.fhir.dstu3.model.ElementDefinition;
032import org.hl7.fhir.dstu3.model.ElementDefinition.AggregationMode;
033import org.hl7.fhir.dstu3.model.ElementDefinition.DiscriminatorType;
034import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBaseComponent;
035import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionBindingComponent;
036import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionConstraintComponent;
037import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionExampleComponent;
038import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionMappingComponent;
039import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingComponent;
040import org.hl7.fhir.dstu3.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent;
041import org.hl7.fhir.dstu3.model.ElementDefinition.SlicingRules;
042import org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent;
043import org.hl7.fhir.dstu3.model.Enumeration;
044import org.hl7.fhir.dstu3.model.Enumerations.BindingStrength;
045import org.hl7.fhir.dstu3.model.Extension;
046import org.hl7.fhir.dstu3.model.IntegerType;
047import org.hl7.fhir.dstu3.model.PrimitiveType;
048import org.hl7.fhir.dstu3.model.Quantity;
049import org.hl7.fhir.dstu3.model.Reference;
050import org.hl7.fhir.dstu3.model.Resource;
051import org.hl7.fhir.dstu3.model.StringType;
052import org.hl7.fhir.dstu3.model.StructureDefinition;
053import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionDifferentialComponent;
054import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionKind;
055import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionMappingComponent;
056import org.hl7.fhir.dstu3.model.StructureDefinition.StructureDefinitionSnapshotComponent;
057import org.hl7.fhir.dstu3.model.StructureDefinition.TypeDerivationRule;
058import org.hl7.fhir.dstu3.model.Type;
059import org.hl7.fhir.dstu3.model.UriType;
060import org.hl7.fhir.dstu3.model.ValueSet;
061import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionComponent;
062import org.hl7.fhir.dstu3.model.ValueSet.ValueSetExpansionContainsComponent;
063import org.hl7.fhir.dstu3.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
064import org.hl7.fhir.dstu3.utils.NarrativeGenerator;
065import org.hl7.fhir.dstu3.utils.ToolingExtensions;
066import org.hl7.fhir.dstu3.utils.TranslatingUtilities;
067import org.hl7.fhir.dstu3.utils.formats.CSVWriter;
068import org.hl7.fhir.exceptions.DefinitionException;
069import org.hl7.fhir.exceptions.FHIRException;
070import org.hl7.fhir.exceptions.FHIRFormatError;
071import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
072import org.hl7.fhir.utilities.Utilities;
073import org.hl7.fhir.utilities.validation.ValidationMessage;
074import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
075import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator;
076import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell;
077import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece;
078import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row;
079import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel;
080import org.hl7.fhir.utilities.xhtml.XhtmlNode;
081import org.hl7.fhir.utilities.xml.SchematronWriter;
082import org.hl7.fhir.utilities.xml.SchematronWriter.Rule;
083import org.hl7.fhir.utilities.xml.SchematronWriter.SchematronType;
084import org.hl7.fhir.utilities.xml.SchematronWriter.Section;
085
086/**
087 * This class provides a set of utility operations for working with Profiles.
088 * Key functionality:
089 *  * getChildMap --?
090 *  * getChildList
091 *  * generateSnapshot: Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
092 *  * closeDifferential: fill out a differential by excluding anything not mentioned
093 *  * generateExtensionsTable: generate the HTML for a hierarchical table presentation of the extensions
094 *  * generateTable: generate  the HTML for a hierarchical table presentation of a structure
095 *  * generateSpanningTable: generate the HTML for a table presentation of a network of structures, starting at a nominated point
096 *  * summarise: describe the contents of a profile
097 *  
098 * 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
099 *  
100 * @author Grahame
101 *
102 */
103public class ProfileUtilities extends TranslatingUtilities {
104
105  private static int nextSliceId = 0;
106  
107  public class ExtensionContext {
108
109    private ElementDefinition element;
110    private StructureDefinition defn;
111
112    public ExtensionContext(StructureDefinition ext, ElementDefinition ed) {
113      this.defn = ext;
114      this.element = ed;
115    }
116
117    public ElementDefinition getElement() {
118      return element;
119    }
120
121    public StructureDefinition getDefn() {
122      return defn;
123    }
124
125    public String getUrl() {
126      if (element == defn.getSnapshot().getElement().get(0))
127        return defn.getUrl();
128      else
129        return element.getSliceName();
130    }
131
132    public ElementDefinition getExtensionValueDefinition() {
133      int i = defn.getSnapshot().getElement().indexOf(element)+1;
134      while (i < defn.getSnapshot().getElement().size()) {
135        ElementDefinition ed = defn.getSnapshot().getElement().get(i);
136        if (ed.getPath().equals(element.getPath()))
137          return null;
138        if (ed.getPath().startsWith(element.getPath()+".value"))
139          return ed;
140        i++;
141      }
142      return null;
143    }
144    
145  }
146
147  private static final String ROW_COLOR_ERROR = "#ffcccc";
148  private static final String ROW_COLOR_FATAL = "#ff9999";
149  private static final String ROW_COLOR_WARNING = "#ffebcc";
150  private static final String ROW_COLOR_HINT = "#ebf5ff";
151  private static final String ROW_COLOR_NOT_MUST_SUPPORT = "#d6eaf8";
152  public static final int STATUS_OK = 0;
153  public static final int STATUS_HINT = 1;
154  public static final int STATUS_WARNING = 2;
155  public static final int STATUS_ERROR = 3;
156  public static final int STATUS_FATAL = 4;
157
158
159  private static final String DERIVATION_EQUALS = "derivation.equals";
160  public static final String DERIVATION_POINTER = "derived.pointer";
161  public static final String IS_DERIVED = "derived.fact";
162  public static final String UD_ERROR_STATUS = "error-status";
163  private static final String GENERATED_IN_SNAPSHOT = "profileutilities.snapshot.processed";
164
165  // note that ProfileUtilities are used re-entrantly internally, so nothing with process state can be here
166  private final IWorkerContext context;
167  private List<ValidationMessage> messages;
168  private List<String> snapshotStack = new ArrayList<String>();
169  private ProfileKnowledgeProvider pkp;
170  private boolean igmode;
171
172  public ProfileUtilities(IWorkerContext context, List<ValidationMessage> messages, ProfileKnowledgeProvider pkp) {
173    super();
174    this.context = context;
175    this.messages = messages;
176    this.pkp = pkp;
177  }
178
179  private class UnusedTracker {
180    private boolean used;
181  }
182
183  public boolean isIgmode() {
184    return igmode;
185  }
186
187
188  public void setIgmode(boolean igmode) {
189    this.igmode = igmode;
190  }
191
192  public interface ProfileKnowledgeProvider {
193    public class BindingResolution {
194      public String display;
195      public String url;
196    }
197    boolean isDatatype(String typeSimple);
198    boolean isResource(String typeSimple);
199    boolean hasLinkFor(String typeSimple);
200    String getLinkFor(String corePath, String typeSimple);
201    BindingResolution resolveBinding(StructureDefinition def, ElementDefinitionBindingComponent binding, String path);
202    String getLinkForProfile(StructureDefinition profile, String url);
203    boolean prependLinks();
204  }
205
206
207
208  public static List<ElementDefinition> getChildMap(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
209    if (element.getContentReference()!=null) {
210      for (ElementDefinition e : profile.getSnapshot().getElement()) {
211        if (element.getContentReference().equals("#"+e.getId()))
212          return getChildMap(profile, e);
213      }
214      throw new DefinitionException("Unable to resolve name reference "+element.getContentReference()+" at path "+element.getPath());
215
216    } else {
217      List<ElementDefinition> res = new ArrayList<ElementDefinition>();
218      List<ElementDefinition> elements = profile.getSnapshot().getElement();
219      String path = element.getPath();
220      for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
221        ElementDefinition e = elements.get(index);
222        if (e.getPath().startsWith(path + ".")) {
223          // We only want direct children, not all descendants
224          if (!e.getPath().substring(path.length()+1).contains("."))
225            res.add(e);
226        } else
227          break;
228      }
229      return res;
230    }
231  }
232
233
234  public static List<ElementDefinition> getSliceList(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
235    if (!element.hasSlicing())
236      throw new Error("getSliceList should only be called when the element has slicing");
237
238    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
239    List<ElementDefinition> elements = profile.getSnapshot().getElement();
240    String path = element.getPath();
241    for (int index = elements.indexOf(element) + 1; index < elements.size(); index++) {
242      ElementDefinition e = elements.get(index);
243      if (e.getPath().startsWith(path + ".") || e.getPath().equals(path)) {
244        // We want elements with the same path (until we hit an element that doesn't start with the same path)
245        if (e.getPath().equals(element.getPath()))
246          res.add(e);
247      } else
248        break;
249    }
250    return res;
251  }
252
253
254  /**
255   * Given a Structure, navigate to the element given by the path and return the direct children of that element
256   *
257   * @param structure The structure to navigate into
258   * @param path The path of the element within the structure to get the children for
259   * @return A List containing the element children (all of them are Elements)
260   */
261  public static List<ElementDefinition> getChildList(StructureDefinition profile, String path, String id) {
262    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
263
264    boolean capturing = id==null;
265    if (id==null && !path.contains("."))
266      capturing = true;
267    
268    for (ElementDefinition e : profile.getSnapshot().getElement()) {
269      if (!capturing && id!=null && e.getId().equals(id)) {
270        capturing = true;
271      }
272      
273      // If our element is a slice, stop capturing children as soon as we see the next slice
274      if (capturing && e.hasId() && id!= null && !e.getId().equals(id) && e.getPath().equals(path))
275        break;
276      
277      if (capturing) {
278        String p = e.getPath();
279  
280        if (!Utilities.noString(e.getContentReference()) && path.startsWith(p)) {
281          if (path.length() > p.length())
282            return getChildList(profile, e.getContentReference()+"."+path.substring(p.length()+1), null);
283          else
284            return getChildList(profile, e.getContentReference(), null);
285          
286        } else if (p.startsWith(path+".") && !p.equals(path)) {
287          String tail = p.substring(path.length()+1);
288          if (!tail.contains(".")) {
289            res.add(e);
290          }
291        }
292      }
293    }
294
295    return res;
296  }
297
298
299  public static List<ElementDefinition> getChildList(StructureDefinition structure, ElementDefinition element) {
300    return getChildList(structure, element.getPath(), element.getId());
301        }
302
303  public void updateMaps(StructureDefinition base, StructureDefinition derived) throws DefinitionException {
304    if (base == null)
305        throw new DefinitionException("no base profile provided");
306    if (derived == null)
307      throw new DefinitionException("no derived structure provided");
308    
309    for (StructureDefinitionMappingComponent baseMap : base.getMapping()) {
310      boolean found = false;
311      for (StructureDefinitionMappingComponent derivedMap : derived.getMapping()) {
312        if (derivedMap.getUri().equals(baseMap.getUri())) {
313          found = true;
314          break;
315        }
316      }
317      if (!found)
318        derived.getMapping().add(baseMap);
319    }
320  }
321  
322  /**
323   * Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
324   *
325   * @param base - the base structure on which the differential will be applied
326   * @param differential - the differential to apply to the base
327   * @param url - where the base has relative urls for profile references, these need to be converted to absolutes by prepending this URL
328   * @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
329   * @return
330   * @throws FHIRException 
331   * @throws DefinitionException 
332   * @throws Exception
333   */
334  public void generateSnapshot(StructureDefinition base, StructureDefinition derived, String url, String profileName) throws DefinitionException, FHIRException {
335    if (base == null)
336      throw new DefinitionException("no base profile provided");
337    if (derived == null)
338      throw new DefinitionException("no derived structure provided");
339
340    if (snapshotStack.contains(derived.getUrl()))
341      throw new DefinitionException("Circular snapshot references detected; cannot generate snapshot (stack = "+snapshotStack.toString()+")");
342    snapshotStack.add(derived.getUrl());
343    
344
345    derived.setSnapshot(new StructureDefinitionSnapshotComponent());
346
347    // so we have two lists - the base list, and the differential list
348    // the differential list is only allowed to include things that are in the base list, but
349    // is allowed to include them multiple times - thereby slicing them
350
351    // our approach is to walk through the base list, and see whether the differential
352    // says anything about them.
353    int baseCursor = 0;
354    int diffCursor = 0; // we need a diff cursor because we can only look ahead, in the bound scoped by longer paths
355
356    if (derived.hasDifferential() && !derived.getDifferential().getElementFirstRep().getPath().contains(".") && !derived.getDifferential().getElementFirstRep().getType().isEmpty())
357      throw new Error("type on first differential element!");
358
359    for (ElementDefinition e : derived.getDifferential().getElement()) 
360      e.clearUserData(GENERATED_IN_SNAPSHOT);
361    
362    // we actually delegate the work to a subroutine so we can re-enter it with a different cursors
363    processPaths("", derived.getSnapshot(), base.getSnapshot(), derived.getDifferential(), baseCursor, diffCursor, base.getSnapshot().getElement().size()-1, 
364        derived.getDifferential().hasElement() ? derived.getDifferential().getElement().size()-1 : -1, url, derived.getId(), null, null, false, base.getUrl(), null, false);
365    if (!derived.getSnapshot().getElementFirstRep().getType().isEmpty())
366      throw new Error("type on first snapshot element for "+derived.getSnapshot().getElementFirstRep().getPath()+" in "+derived.getUrl()+" from "+base.getUrl());
367    updateMaps(base, derived);
368    setIds(derived, false);
369    
370    //Check that all differential elements have a corresponding snapshot element
371    for (ElementDefinition e : derived.getDifferential().getElement()) {
372      if (!e.hasUserData(GENERATED_IN_SNAPSHOT)) {
373        System.out.println("Error in snapshot generation: Snapshot for "+derived.getUrl()+" does not contain differential element with id: " + e.getId());
374        System.out.println("Differential: ");
375        for (ElementDefinition ed : derived.getDifferential().getElement())
376          System.out.println("  "+ed.getPath()+" : "+typeSummary(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  id = "+ed.getId());
377        System.out.println("Snapshot: ");
378        for (ElementDefinition ed : derived.getSnapshot().getElement())
379          System.out.println("  "+ed.getPath()+" : "+typeSummary(ed)+"["+ed.getMin()+".."+ed.getMax()+"]"+sliceSummary(ed)+"  id = "+ed.getId());
380        throw new DefinitionException("Snapshot for "+derived.getUrl()+" does not contain differential element with id: " + e.getId());
381//        System.out.println("**BAD Differential element: " + profileName + ":" + e.getId());
382      }
383    }
384  }
385
386  private String sliceSummary(ElementDefinition ed) {
387    if (!ed.hasSlicing() && !ed.hasSliceName())
388      return "";
389    if (ed.hasSliceName())
390      return " (slicename = "+ed.getSliceName()+")";
391    
392    StringBuilder b = new StringBuilder();
393    boolean first = true;
394    for (ElementDefinitionSlicingDiscriminatorComponent d : ed.getSlicing().getDiscriminator()) {
395      if (first) 
396        first = false;
397      else
398        b.append("|");
399      b.append(d.getPath());
400    }
401    return " (slicing by "+b.toString()+")";
402  }
403
404
405  private String typeSummary(ElementDefinition ed) {
406    StringBuilder b = new StringBuilder();
407    boolean first = true;
408    for (TypeRefComponent tr : ed.getType()) {
409      if (first) 
410        first = false;
411      else
412        b.append("|");
413      b.append(tr.getCode());
414    }
415    return b.toString();
416  }
417
418
419  private boolean findMatchingElement(String id, List<ElementDefinition> list) {
420    for (ElementDefinition ed : list) {
421      if (ed.getId().equals(id))
422        return true;
423      if (id.endsWith("[x]")) {
424        if (ed.getId().startsWith(id.substring(0, id.length()-3)) && !ed.getId().substring(id.length()-3).contains("."))
425          return true;
426      }
427    }
428    return false;
429  }
430
431
432  /**
433   * @param trimDifferential
434   * @throws DefinitionException, FHIRException 
435   * @throws Exception
436   */
437  private ElementDefinition processPaths(String indent, StructureDefinitionSnapshotComponent result, StructureDefinitionSnapshotComponent base, StructureDefinitionDifferentialComponent differential, int baseCursor, int diffCursor, int baseLimit,
438      int diffLimit, String url, String profileName, String contextPathSrc, String contextPathDst, boolean trimDifferential, String contextName, String resultPathBase, boolean slicingDone) throws DefinitionException, FHIRException {
439
440//    System.out.println(indent+"PP @ "+resultPathBase+": base = "+baseCursor+" to "+baseLimit+", diff = "+diffCursor+" to "+diffLimit+" (slicing = "+slicingDone+")");
441    ElementDefinition res = null; 
442    // just repeat processing entries until we run out of our allowed scope (1st entry, the allowed scope is all the entries)
443    while (baseCursor <= baseLimit) {
444      // get the current focus of the base, and decide what to do
445      ElementDefinition currentBase = base.getElement().get(baseCursor);
446      String cpath = fixedPath(contextPathSrc, currentBase.getPath());
447//      System.out.println(indent+" - "+cpath+": base = "+baseCursor+" to "+baseLimit+", diff = "+diffCursor+" to "+diffLimit+" (slicingDone = "+slicingDone+")");
448      List<ElementDefinition> diffMatches = getDiffMatches(differential, cpath, diffCursor, diffLimit, profileName, url); // get a list of matching elements in scope
449
450      // in the simple case, source is not sliced.
451      if (!currentBase.hasSlicing()) {
452        if (diffMatches.isEmpty()) { // the differential doesn't say anything about this item
453          // so we just copy it in
454          ElementDefinition outcome = updateURLs(url, currentBase.copy());
455          outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
456          updateFromBase(outcome, currentBase);
457          markDerived(outcome);
458          if (resultPathBase == null)
459            resultPathBase = outcome.getPath();
460          else if (!outcome.getPath().startsWith(resultPathBase))
461            throw new DefinitionException("Adding wrong path");
462          result.getElement().add(outcome);
463          if (hasInnerDiffMatches(differential, cpath, diffCursor, diffLimit, base.getElement())) {
464            // well, the profile walks into this, so we need to as well
465            if (outcome.getType().size() > 1) {
466              for (TypeRefComponent t : outcome.getType()) {
467                if (!t.getCode().equals("Reference"))
468                  throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
469              }
470            }
471            StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
472            if (dt == null)
473              throw new DefinitionException(cpath+" has children for type "+typeCode(outcome.getType())+" in profile "+profileName+", but can't find type");
474            contextName = dt.getUrl();
475            int start = diffCursor;
476            while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), cpath+"."))
477              diffCursor++;
478            processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start, dt.getSnapshot().getElement().size()-1,
479                diffCursor-1, url, profileName, cpath, outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
480          }
481          baseCursor++;
482        } else if (diffMatches.size() == 1 && (slicingDone || !(diffMatches.get(0).hasSlicing() || (isExtension(diffMatches.get(0)) && diffMatches.get(0).hasSliceName())))) {// one matching element in the differential
483          ElementDefinition template = null;
484          if (diffMatches.get(0).hasType() && diffMatches.get(0).getType().size() == 1 && diffMatches.get(0).getType().get(0).hasProfile() && !diffMatches.get(0).getType().get(0).getCode().equals("Reference")) {
485            String p = diffMatches.get(0).getType().get(0).getProfile();
486            StructureDefinition sd = context.fetchResource(StructureDefinition.class, p);
487            if (sd != null) {
488              if (!sd.hasSnapshot()) {
489                StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
490                if (sdb == null)
491                  throw new DefinitionException("no base for "+sd.getBaseDefinition());
492                generateSnapshot(sdb, sd, sd.getUrl(), sd.getName());
493              }
494              template = sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath());
495              template.setSliceName(null);
496              // temporary work around
497              if (!diffMatches.get(0).getType().get(0).getCode().equals("Extension")) {
498                template.setMin(currentBase.getMin());
499                template.setMax(currentBase.getMax());
500              }
501            }
502          } 
503          if (template == null)
504            template = currentBase.copy();
505          else
506            // some of what's in currentBase overrides template
507            template = overWriteWithCurrent(template, currentBase);
508          
509          ElementDefinition outcome = updateURLs(url, template);
510          outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
511          res = outcome;
512          updateFromBase(outcome, currentBase);
513          if (diffMatches.get(0).hasSliceName())
514            outcome.setSliceName(diffMatches.get(0).getSliceName());
515          outcome.setSlicing(null);
516          updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
517          if (outcome.getPath().endsWith("[x]") && outcome.getType().size() == 1 && !outcome.getType().get(0).getCode().equals("*")) // if the base profile allows multiple types, but the profile only allows one, rename it
518            outcome.setPath(outcome.getPath().substring(0, outcome.getPath().length()-3)+Utilities.capitalize(outcome.getType().get(0).getCode()));
519          if (resultPathBase == null)
520            resultPathBase = outcome.getPath();
521          else if (!outcome.getPath().startsWith(resultPathBase))
522            throw new DefinitionException("Adding wrong path");
523          result.getElement().add(outcome);
524          baseCursor++;
525          diffCursor = differential.getElement().indexOf(diffMatches.get(0))+1;
526          if (differential.getElement().size() > 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
527            if (pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+".") && !baseWalksInto(base.getElement(), baseCursor)) {
528              if (outcome.getType().size() > 1) {
529                for (TypeRefComponent t : outcome.getType()) {
530                  if (!t.getCode().equals("Reference"))
531                    throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
532                }
533              }
534              int start = diffCursor;
535              while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
536                diffCursor++;
537              if (outcome.hasContentReference()) {
538                ElementDefinition tgt = getElementById(base.getElement(), outcome.getContentReference());
539                if (tgt == null)
540                  throw new DefinitionException("Unable to resolve reference to "+outcome.getContentReference());
541                replaceFromContentReference(outcome, tgt);
542                int nbc = base.getElement().indexOf(tgt)+1;
543                int nbl = nbc;
544                while (nbl < base.getElement().size() && base.getElement().get(nbl).getPath().startsWith(tgt.getPath()+"."))
545                  nbl++;
546                processPaths(indent+"  ", result, base, differential, nbc, start - 1, nbl-1, diffCursor - 1, url, profileName, tgt.getPath(), diffMatches.get(0).getPath(), trimDifferential, contextName, resultPathBase, false);
547              } else {
548                StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
549                if (dt == null)
550                  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");
551                contextName = dt.getUrl();
552                processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start-1, dt.getSnapshot().getElement().size()-1,
553                    diffCursor - 1, url, profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
554              }
555            }
556          }
557        } else {
558          // ok, the differential slices the item. Let's check our pre-conditions to ensure that this is correct
559          if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))
560            // you can only slice an element that doesn't repeat if the sum total of your slices is limited to 1
561            // (but you might do that in order to split up constraints by type)
562            throw new DefinitionException("Attempt to a slice an element that does not repeat: "+currentBase.getPath()+"/"+currentBase.getSliceName()+" from "+contextName+" in "+url);
563          if (!diffMatches.get(0).hasSlicing() && !isExtension(currentBase)) // well, the diff has set up a slice, but hasn't defined it. this is an error
564            throw new DefinitionException("differential does not have a slice: "+currentBase.getPath()+" in profile "+url);
565
566          // well, if it passed those preconditions then we slice the dest.
567          int start = 0;
568          int nbl = findEndOfElement(base, baseCursor);
569          if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0))+1) {
570            int ndc = differential.getElement().indexOf(diffMatches.get(0));
571            int ndl = findEndOfElement(differential, ndc);
572            processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName+pathTail(diffMatches, 0), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true).setSlicing(diffMatches.get(0).getSlicing());
573            start++;
574          } else {
575            // we're just going to accept the differential slicing at face value
576            ElementDefinition outcome = updateURLs(url, currentBase.copy());
577            outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
578            updateFromBase(outcome, currentBase);
579
580            if (!diffMatches.get(0).hasSlicing())
581              outcome.setSlicing(makeExtensionSlicing());
582            else
583              outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
584            if (!outcome.getPath().startsWith(resultPathBase))
585              throw new DefinitionException("Adding wrong path");
586            result.getElement().add(outcome);
587
588            // 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.
589            if (!diffMatches.get(0).hasSliceName()) {
590              updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
591              if (!outcome.hasContentReference() && !outcome.hasType()) {
592                throw new DefinitionException("not done yet");
593              }
594              start++;
595              // result.getElement().remove(result.getElement().size()-1);
596            } else 
597              checkExtensionDoco(outcome);
598          }
599          // now, for each entry in the diff matches, we're going to process the base item
600          // our processing scope for base is all the children of the current path
601          int ndc = diffCursor;
602          int ndl = diffCursor;
603          for (int i = start; i < diffMatches.size(); i++) {
604            // our processing scope for the differential is the item in the list, and all the items before the next one in the list
605            ndc = differential.getElement().indexOf(diffMatches.get(i));
606            ndl = findEndOfElement(differential, ndc);
607/*            if (skipSlicingElement && i == 0) {
608              ndc = ndc + 1;
609              if (ndc > ndl)
610                continue;
611            }*/
612            // now we process the base scope repeatedly for each instance of the item in the differential list
613            processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName+pathTail(diffMatches, i), contextPathSrc, contextPathDst, trimDifferential, contextName, resultPathBase, true);
614          }
615          // ok, done with that - next in the base list
616          baseCursor = nbl+1;
617          diffCursor = ndl+1;
618        }
619      } else {
620        // the item is already sliced in the base profile.
621        // here's the rules
622        //  1. irrespective of whether the slicing is ordered or not, the definition order must be maintained
623        //  2. slice element names have to match.
624        //  3. new slices must be introduced at the end
625        // corallory: you can't re-slice existing slices. is that ok?
626
627        // we're going to need this:
628        String path = currentBase.getPath();
629        ElementDefinition original = currentBase;
630
631        if (diffMatches.isEmpty()) { // the differential doesn't say anything about this item
632          // copy across the currentbase, and all of its children and siblings
633          while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
634            ElementDefinition outcome = updateURLs(url, base.getElement().get(baseCursor).copy());
635            outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
636            if (!outcome.getPath().startsWith(resultPathBase))
637              throw new DefinitionException("Adding wrong path in profile " + profileName + ": "+outcome.getPath()+" vs " + resultPathBase);
638            result.getElement().add(outcome); // so we just copy it in
639            baseCursor++;
640          }
641        } else {
642          // first - check that the slicing is ok
643          boolean closed = currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
644          int diffpos = 0;
645          boolean isExtension = cpath.endsWith(".extension") || cpath.endsWith(".modifierExtension");
646          if (diffMatches.get(0).hasSlicing()) { // it might be null if the differential doesn't want to say anything about slicing
647            if (!isExtension)
648              diffpos++; // if there's a slice on the first, we'll ignore any content it has
649            ElementDefinitionSlicingComponent dSlice = diffMatches.get(0).getSlicing();
650            ElementDefinitionSlicingComponent bSlice = currentBase.getSlicing();
651            if (dSlice.hasOrderedElement() && bSlice.hasOrderedElement() && !orderMatches(dSlice.getOrderedElement(), bSlice.getOrderedElement()))
652              throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - order @ "+path+" ("+contextName+")");
653            if (!discriminatorMatches(dSlice.getDiscriminator(), bSlice.getDiscriminator()))
654             throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - disciminator @ "+path+" ("+contextName+")");
655            if (!ruleMatches(dSlice.getRules(), bSlice.getRules()))
656             throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - rule @ "+path+" ("+contextName+")");
657          }
658          if (diffMatches.size() > 1 && diffMatches.get(0).hasSlicing() && differential.getElement().indexOf(diffMatches.get(1)) > differential.getElement().indexOf(diffMatches.get(0))+1) {
659            throw new Error("Not done yet");
660          }
661          ElementDefinition outcome = updateURLs(url, currentBase.copy());
662          outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
663          updateFromBase(outcome, currentBase);
664          if (diffMatches.get(0).hasSlicing() /*&& !isExtension*/) {
665            updateFromSlicing(outcome.getSlicing(), diffMatches.get(0).getSlicing());
666            updateFromDefinition(outcome, diffMatches.get(0), profileName, closed, url); // if there's no slice, we don't want to update the unsliced description
667          } else if (!diffMatches.get(0).hasSliceName())
668            diffMatches.get(0).setUserData(GENERATED_IN_SNAPSHOT, true); // because of updateFromDefinition isn't called 
669          
670          result.getElement().add(outcome);
671
672          if (!diffMatches.get(0).hasSliceName()) { // it's not real content, just the slice
673            diffpos++; 
674          }
675
676          // now, we have two lists, base and diff. we're going to work through base, looking for matches in diff.
677          List<ElementDefinition> baseMatches = getSiblings(base.getElement(), currentBase);
678          for (ElementDefinition baseItem : baseMatches) {
679            baseCursor = base.getElement().indexOf(baseItem);
680            outcome = updateURLs(url, baseItem.copy());
681            updateFromBase(outcome, currentBase);
682            outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
683            outcome.setSlicing(null);
684            if (!outcome.getPath().startsWith(resultPathBase))
685              throw new DefinitionException("Adding wrong path");
686            if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getSliceName().equals(outcome.getSliceName())) {
687              // if there's a diff, we update the outcome with diff
688              // no? updateFromDefinition(outcome, diffMatches.get(diffpos), profileName, closed, url);
689              //then process any children
690              int nbl = findEndOfElement(base, baseCursor);
691              int ndc = differential.getElement().indexOf(diffMatches.get(diffpos));
692              int ndl = findEndOfElement(differential, ndc);
693              // now we process the base scope repeatedly for each instance of the item in the differential list
694              processPaths(indent+"  ", result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName+pathTail(diffMatches, diffpos), contextPathSrc, contextPathDst, closed, contextName, resultPathBase, true);
695              // ok, done with that - now set the cursors for if this is the end
696              baseCursor = nbl;
697              diffCursor = ndl+1;
698              diffpos++;
699            } else {
700              result.getElement().add(outcome);
701              baseCursor++;
702              // just copy any children on the base
703              while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
704                outcome = updateURLs(url, base.getElement().get(baseCursor).copy());
705                outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
706                if (!outcome.getPath().startsWith(resultPathBase))
707                  throw new DefinitionException("Adding wrong path");
708                result.getElement().add(outcome);
709                baseCursor++;
710              }
711              //Lloyd - add this for test T15
712              baseCursor--;
713            }
714          }
715          // finally, we process any remaining entries in diff, which are new (and which are only allowed if the base wasn't closed
716          if (closed && diffpos < diffMatches.size())
717            throw new DefinitionException("The base snapshot marks a slicing as closed, but the differential tries to extend it in "+profileName+" at "+path+" ("+cpath+")");
718          if (diffpos == diffMatches.size()) {
719            diffCursor++;
720          } else {
721            while (diffpos < diffMatches.size()) {
722              ElementDefinition diffItem = diffMatches.get(diffpos);
723              for (ElementDefinition baseItem : baseMatches)
724                if (baseItem.getSliceName().equals(diffItem.getSliceName()))
725                  throw new DefinitionException("Named items are out of order in the slice");
726              outcome = updateURLs(url, currentBase.copy());
727              //            outcome = updateURLs(url, diffItem.copy());
728              outcome.setPath(fixedPath(contextPathDst, outcome.getPath()));
729              updateFromBase(outcome, currentBase);
730              outcome.setSlicing(null);
731              if (!outcome.getPath().startsWith(resultPathBase))
732                throw new DefinitionException("Adding wrong path");
733              result.getElement().add(outcome);
734              updateFromDefinition(outcome, diffItem, profileName, trimDifferential, url);
735              // --- LM Added this
736              diffCursor = differential.getElement().indexOf(diffItem)+1;
737              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
738                if (!baseWalksInto(base.getElement(), baseCursor)) {
739                  if (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+".")) {
740                    if (outcome.getType().size() > 1)
741                      for (TypeRefComponent t : outcome.getType()) {
742                        if (!t.getCode().equals("Reference"))
743                          throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
744                      }
745                    TypeRefComponent t = outcome.getType().get(0);
746                    StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
747                    //                if (t.getCode().equals("Extension") && t.hasProfile() && !t.getProfile().contains(":")) {
748                    // lloydfix                  dt = 
749                    //                }
750                    if (dt == null)
751                      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");
752                    contextName = dt.getUrl();
753                    int start = diffCursor;
754                    while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
755                      diffCursor++;
756                    processPaths(indent+"  ", result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start-1, dt.getSnapshot().getElement().size()-1,
757                        diffCursor - 1, url, profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), outcome.getPath(), trimDifferential, contextName, resultPathBase, false);
758                  } else if (outcome.getType().get(0).getCode().equals("Extension")) {
759                    // Force URL to appear if we're dealing with an extension.  (This is a kludge - may need to drill down in other cases where we're slicing and the type has a profile declaration that could be setting the fixed value)
760                    StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
761                    for (ElementDefinition extEd : dt.getSnapshot().getElement()) {
762                      // We only want the children that aren't the root
763                      if (extEd.getPath().contains(".")) {
764                        ElementDefinition extUrlEd = updateURLs(url, extEd.copy());
765                        extUrlEd.setPath(fixedPath(outcome.getPath(), extUrlEd.getPath()));
766                        //                      updateFromBase(extUrlEd, currentBase);
767                        markDerived(extUrlEd);
768                        result.getElement().add(extUrlEd);
769                      }
770                    }                  
771                  }
772                }
773              }
774              // ---
775              diffpos++;
776            }
777          }
778          baseCursor++;
779        }
780      }
781    }
782    
783    int i = 0;
784    for (ElementDefinition e : result.getElement()) {
785      i++;
786      if (e.hasMinElement() && e.getMinElement().getValue()==null)
787        throw new Error("null min");
788    }
789    return res;
790  }
791
792
793  private void replaceFromContentReference(ElementDefinition outcome, ElementDefinition tgt) {
794    outcome.setContentReference(null);
795    outcome.getType().clear(); // though it should be clear anyway
796    outcome.getType().addAll(tgt.getType());    
797  }
798
799
800  private boolean baseWalksInto(List<ElementDefinition> elements, int cursor) {
801    if (cursor >= elements.size())
802      return false;
803    String path = elements.get(cursor).getPath();
804    String prevPath = elements.get(cursor - 1).getPath();
805    return path.startsWith(prevPath + ".");
806  }
807
808
809  private ElementDefinition overWriteWithCurrent(ElementDefinition profile, ElementDefinition usage) throws FHIRFormatError {
810    ElementDefinition res = profile.copy();
811    if (usage.hasSliceName())
812      res.setSliceName(usage.getSliceName());
813    if (usage.hasLabel())
814      res.setLabel(usage.getLabel());
815    for (Coding c : usage.getCode())
816      res.addCode(c);
817    
818    if (usage.hasDefinition())
819      res.setDefinition(usage.getDefinition());
820    if (usage.hasShort())
821      res.setShort(usage.getShort());
822    if (usage.hasComment())
823      res.setComment(usage.getComment());
824    if (usage.hasRequirements())
825      res.setRequirements(usage.getRequirements());
826    for (StringType c : usage.getAlias())
827      res.addAlias(c.getValue());
828    if (usage.hasMin())
829      res.setMin(usage.getMin());
830    if (usage.hasMax())
831      res.setMax(usage.getMax());
832     
833    if (usage.hasFixed())
834      res.setFixed(usage.getFixed());
835    if (usage.hasPattern())
836      res.setPattern(usage.getPattern());
837    if (usage.hasExample())
838      res.setExample(usage.getExample());
839    if (usage.hasMinValue())
840      res.setMinValue(usage.getMinValue());
841    if (usage.hasMaxValue())
842      res.setMaxValue(usage.getMaxValue());     
843    if (usage.hasMaxLength())
844      res.setMaxLength(usage.getMaxLength());
845    if (usage.hasMustSupport())
846      res.setMustSupport(usage.getMustSupport());
847    if (usage.hasBinding())
848      res.setBinding(usage.getBinding().copy());
849    for (ElementDefinitionConstraintComponent c : usage.getConstraint())
850      res.addConstraint(c);
851    
852    return res;
853  }
854
855
856  private boolean checkExtensionDoco(ElementDefinition base) {
857    // see task 3970. For an extension, there's no point copying across all the underlying definitional stuff
858    boolean isExtension = base.getPath().equals("Extension") || base.getPath().endsWith(".extension") || base.getPath().endsWith(".modifierExtension");
859    if (isExtension) {
860      base.setDefinition("An Extension");
861      base.setShort("Extension");
862      base.setCommentElement(null);
863      base.setRequirementsElement(null);
864      base.getAlias().clear();
865      base.getMapping().clear();
866    }
867    return isExtension;
868  }
869
870
871  private String pathTail(List<ElementDefinition> diffMatches, int i) {
872    
873    ElementDefinition d = diffMatches.get(i);
874    String s = d.getPath().contains(".") ? d.getPath().substring(d.getPath().lastIndexOf(".")+1) : d.getPath();
875    return "."+s + (d.hasType() && d.getType().get(0).hasProfile() ? "["+d.getType().get(0).getProfile()+"]" : "");
876  }
877
878
879  private void markDerived(ElementDefinition outcome) {
880    for (ElementDefinitionConstraintComponent inv : outcome.getConstraint())
881      inv.setUserData(IS_DERIVED, true);
882  }
883
884
885  private String summariseSlicing(ElementDefinitionSlicingComponent slice) {
886    StringBuilder b = new StringBuilder();
887    boolean first = true;
888    for (ElementDefinitionSlicingDiscriminatorComponent d : slice.getDiscriminator()) {
889      if (first)
890        first = false;
891      else
892        b.append(", ");
893      b.append(d);
894    }
895    b.append("(");
896    if (slice.hasOrdered())
897      b.append(slice.getOrderedElement().asStringValue());
898    b.append("/");
899    if (slice.hasRules())
900      b.append(slice.getRules().toCode());
901    b.append(")");
902    if (slice.hasDescription()) {
903      b.append(" \"");
904      b.append(slice.getDescription());
905      b.append("\"");
906    }
907    return b.toString();
908  }
909
910
911  private void updateFromBase(ElementDefinition derived, ElementDefinition base) {
912    if (base.hasBase()) {
913      if (!derived.hasBase())
914        derived.setBase(new ElementDefinitionBaseComponent());
915      derived.getBase().setPath(base.getBase().getPath());
916      derived.getBase().setMin(base.getBase().getMin());
917      derived.getBase().setMax(base.getBase().getMax());
918    } else {
919      if (!derived.hasBase())
920        derived.setBase(new ElementDefinitionBaseComponent());
921      derived.getBase().setPath(base.getPath());
922      derived.getBase().setMin(base.getMin());
923      derived.getBase().setMax(base.getMax());
924    }
925  }
926
927
928  private boolean pathStartsWith(String p1, String p2) {
929    return p1.startsWith(p2);
930  }
931
932  private boolean pathMatches(String p1, String p2) {
933    return p1.equals(p2) || (p2.endsWith("[x]") && p1.startsWith(p2.substring(0, p2.length()-3)) && !p1.substring(p2.length()-3).contains("."));
934  }
935
936
937  private String fixedPath(String contextPath, String pathSimple) {
938    if (contextPath == null)
939      return pathSimple;
940    return contextPath+"."+pathSimple.substring(pathSimple.indexOf(".")+1);
941  }
942
943
944  private StructureDefinition getProfileForDataType(TypeRefComponent type)  {
945    StructureDefinition sd = null;
946    if (type.hasProfile() && !type.getCode().equals("Reference"))  
947      sd = context.fetchResource(StructureDefinition.class, type.getProfile()); 
948    if (sd == null)
949      sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+type.getCode());
950    if (sd == null)
951      System.out.println("XX: failed to find profle for type: " + type.getCode()); // debug GJM
952    return sd;
953  }
954
955
956  public static String typeCode(List<TypeRefComponent> types) {
957    StringBuilder b = new StringBuilder();
958    boolean first = true;
959    for (TypeRefComponent type : types) {
960      if (first) first = false; else b.append(", ");
961      b.append(type.getCode());
962      if (type.hasTargetProfile())
963        b.append("{"+type.getTargetProfile()+"}");
964      else if (type.hasProfile())
965        b.append("{"+type.getProfile()+"}");
966    }
967    return b.toString();
968  }
969
970
971  private boolean isDataType(List<TypeRefComponent> types) {
972    if (types.isEmpty())
973      return false;
974    for (TypeRefComponent type : types) {
975      String t = type.getCode();
976      if (!isDataType(t) && !isPrimitive(t))
977        return false;
978    }
979    return true;
980  }
981
982
983  /**
984   * Finds internal references in an Element's Binding and StructureDefinition references (in TypeRef) and bases them on the given url
985   * @param url - the base url to use to turn internal references into absolute references
986   * @param element - the Element to update
987   * @return - the updated Element
988   */
989  private ElementDefinition updateURLs(String url, ElementDefinition element) {
990    if (element != null) {
991      ElementDefinition defn = element;
992      if (defn.hasBinding() && defn.getBinding().getValueSet() instanceof Reference && ((Reference)defn.getBinding().getValueSet()).getReference().startsWith("#"))
993        ((Reference)defn.getBinding().getValueSet()).setReference(url+((Reference)defn.getBinding().getValueSet()).getReference());
994      for (TypeRefComponent t : defn.getType()) {
995        if (t.hasProfile()) {
996          if (t.getProfile().startsWith("#"))
997            t.setProfile(url+t.getProfile());
998        }
999        if (t.hasTargetProfile()) {
1000          if (t.getTargetProfile().startsWith("#"))
1001            t.setTargetProfile(url+t.getTargetProfile());
1002        }
1003      }
1004    }
1005    return element;
1006  }
1007
1008  private List<ElementDefinition> getSiblings(List<ElementDefinition> list, ElementDefinition current) {
1009    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
1010    String path = current.getPath();
1011    int cursor = list.indexOf(current)+1;
1012    while (cursor < list.size() && list.get(cursor).getPath().length() >= path.length()) {
1013      if (pathMatches(list.get(cursor).getPath(), path))
1014        result.add(list.get(cursor));
1015      cursor++;
1016    }
1017    return result;
1018  }
1019
1020  private void updateFromSlicing(ElementDefinitionSlicingComponent dst, ElementDefinitionSlicingComponent src) {
1021    if (src.hasOrderedElement())
1022      dst.setOrderedElement(src.getOrderedElement().copy());
1023    if (src.hasDiscriminator()) {
1024      //    dst.getDiscriminator().addAll(src.getDiscriminator());  Can't use addAll because it uses object equality, not string equality
1025      for (ElementDefinitionSlicingDiscriminatorComponent s : src.getDiscriminator()) {
1026        boolean found = false;
1027        for (ElementDefinitionSlicingDiscriminatorComponent d : dst.getDiscriminator()) {
1028          if (matches(d, s)) {
1029            found = true;
1030            break;
1031          }
1032        }
1033        if (!found)
1034          dst.getDiscriminator().add(s);
1035      }
1036    }
1037    if (src.hasRulesElement())
1038      dst.setRulesElement(src.getRulesElement().copy());
1039  }
1040
1041  private boolean orderMatches(BooleanType diff, BooleanType base) {
1042    return (diff == null) || (base == null) || (diff.getValue() == base.getValue());
1043  }
1044
1045  private boolean discriminatorMatches(List<ElementDefinitionSlicingDiscriminatorComponent> diff, List<ElementDefinitionSlicingDiscriminatorComponent> base) {
1046    if (diff.isEmpty() || base.isEmpty())
1047        return true;
1048    if (diff.size() != base.size())
1049        return false;
1050    for (int i = 0; i < diff.size(); i++)
1051        if (!matches(diff.get(i), base.get(i)))
1052                return false;
1053    return true;
1054  }
1055
1056  private boolean matches(ElementDefinitionSlicingDiscriminatorComponent c1, ElementDefinitionSlicingDiscriminatorComponent c2) {
1057    return c1.getType().equals(c2.getType()) && c1.getPath().equals(c2.getPath());
1058  }
1059
1060
1061  private boolean ruleMatches(SlicingRules diff, SlicingRules base) {
1062    return (diff == null) || (base == null) || (diff == base) || (diff == SlicingRules.OPEN) ||
1063        ((diff == SlicingRules.OPENATEND && base == SlicingRules.CLOSED));
1064  }
1065
1066  private boolean isSlicedToOneOnly(ElementDefinition e) {
1067    return (e.hasSlicing() && e.hasMaxElement() && e.getMax().equals("1"));
1068  }
1069
1070  private ElementDefinitionSlicingComponent makeExtensionSlicing() {
1071        ElementDefinitionSlicingComponent slice = new ElementDefinitionSlicingComponent();
1072        nextSliceId++;
1073        slice.setId(Integer.toString(nextSliceId));
1074    slice.addDiscriminator().setPath("url").setType(DiscriminatorType.VALUE);
1075    slice.setOrdered(false);
1076    slice.setRules(SlicingRules.OPEN);
1077    return slice;
1078  }
1079
1080  private boolean isExtension(ElementDefinition currentBase) {
1081    return currentBase.getPath().endsWith(".extension") || currentBase.getPath().endsWith(".modifierExtension");
1082  }
1083
1084  private boolean hasInnerDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, List<ElementDefinition> base) throws DefinitionException {
1085    for (int i = start; i <= end; i++) {
1086      String statedPath = context.getElement().get(i).getPath();
1087      if (statedPath.startsWith(path+".") && !statedPath.substring(path.length()+1).contains(".")) {
1088        boolean found = false;
1089        for (ElementDefinition ed : base) {
1090          String ep = ed.getPath();
1091          if (ep.equals(statedPath) || (ep.endsWith("[x]") && statedPath.length() > ep.length() - 2 && statedPath.substring(0, ep.length()-3).equals(ep.substring(0, ep.length()-3)) && !statedPath.substring(ep.length()).contains(".")))
1092            found = true;
1093        }
1094        if (!found)
1095          return true;
1096      }
1097    }
1098    return false;
1099  }
1100
1101  private List<ElementDefinition> getDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, String profileName, String url) throws DefinitionException {
1102    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
1103    for (int i = start; i <= end; i++) {
1104      String statedPath = context.getElement().get(i).getPath();
1105      if (statedPath.equals(path) || (path.endsWith("[x]") && statedPath.length() > path.length() - 2 && statedPath.substring(0, path.length()-3).equals(path.substring(0, path.length()-3)) && (statedPath.length() < path.length() || !statedPath.substring(path.length()).contains(".")))) {
1106        /* 
1107         * Commenting this out because it raises warnings when profiling inherited elements.  For example,
1108         * Error: unknown element 'Bundle.meta.profile' (or it is out of order) in profile ... (looking for 'Bundle.entry')
1109         * Not sure we have enough information here to do the check properly.  Might be better done when we're sorting the profile?
1110
1111        if (i != start && result.isEmpty() && !path.startsWith(context.getElement().get(start).getPath()))
1112          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));
1113
1114         */
1115        result.add(context.getElement().get(i));
1116      }
1117    }
1118    return result;
1119  }
1120
1121  private int findEndOfElement(StructureDefinitionDifferentialComponent context, int cursor) {
1122            int result = cursor;
1123            String path = context.getElement().get(cursor).getPath()+".";
1124            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
1125              result++;
1126            return result;
1127          }
1128
1129  private int findEndOfElement(StructureDefinitionSnapshotComponent context, int cursor) {
1130            int result = cursor;
1131            String path = context.getElement().get(cursor).getPath()+".";
1132            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
1133              result++;
1134            return result;
1135          }
1136
1137  private boolean unbounded(ElementDefinition definition) {
1138    StringType max = definition.getMaxElement();
1139    if (max == null)
1140      return false; // this is not valid
1141    if (max.getValue().equals("1"))
1142      return false;
1143    if (max.getValue().equals("0"))
1144      return false;
1145    return true;
1146  }
1147
1148  private void updateFromDefinition(ElementDefinition dest, ElementDefinition source, String pn, boolean trimDifferential, String purl) throws DefinitionException, FHIRException {
1149    source.setUserData(GENERATED_IN_SNAPSHOT, true);
1150    // we start with a clone of the base profile ('dest') and we copy from the profile ('source')
1151    // over the top for anything the source has
1152    ElementDefinition base = dest;
1153    ElementDefinition derived = source;
1154    derived.setUserData(DERIVATION_POINTER, base);
1155
1156    // Before applying changes, apply them to what's in the profile
1157    // TODO: follow Chris's rules
1158    StructureDefinition profile = source.getType().size() == 1 && source.getTypeFirstRep().hasProfile() ? context.fetchResource(StructureDefinition.class, source.getTypeFirstRep().getProfile()) : null;
1159    if (profile != null) {
1160      ElementDefinition e = profile.getSnapshot().getElement().get(0);
1161      base.setDefinition(e.getDefinition());
1162      base.setShort(e.getShort());
1163      if (e.hasCommentElement())
1164        base.setCommentElement(e.getCommentElement());
1165      if (e.hasRequirementsElement())
1166        base.setRequirementsElement(e.getRequirementsElement());
1167      base.getAlias().clear();
1168      base.getAlias().addAll(e.getAlias());
1169      base.getMapping().clear();
1170      base.getMapping().addAll(e.getMapping());
1171    }
1172    
1173    if (derived != null) {
1174      boolean isExtension = checkExtensionDoco(base);
1175
1176      if (derived.hasSliceName()) {
1177        base.setSliceName(derived.getSliceName());
1178      }
1179      
1180      if (derived.hasShortElement()) {
1181        if (!Base.compareDeep(derived.getShortElement(), base.getShortElement(), false))
1182          base.setShortElement(derived.getShortElement().copy());
1183        else if (trimDifferential)
1184          derived.setShortElement(null);
1185        else if (derived.hasShortElement())
1186          derived.getShortElement().setUserData(DERIVATION_EQUALS, true);
1187      }
1188
1189      if (derived.hasDefinitionElement()) {
1190        if (derived.getDefinition().startsWith("..."))
1191          base.setDefinition(base.getDefinition()+"\r\n"+derived.getDefinition().substring(3));
1192        else if (!Base.compareDeep(derived.getDefinitionElement(), base.getDefinitionElement(), false))
1193          base.setDefinitionElement(derived.getDefinitionElement().copy());
1194        else if (trimDifferential)
1195          derived.setDefinitionElement(null);
1196        else if (derived.hasDefinitionElement())
1197          derived.getDefinitionElement().setUserData(DERIVATION_EQUALS, true);
1198      }
1199
1200      if (derived.hasCommentElement()) {
1201        if (derived.getComment().startsWith("..."))
1202          base.setComment(base.getComment()+"\r\n"+derived.getComment().substring(3));
1203        else if (derived.hasCommentElement()!= base.hasCommentElement() || !Base.compareDeep(derived.getCommentElement(), base.getCommentElement(), false))
1204          base.setCommentElement(derived.getCommentElement().copy());
1205        else if (trimDifferential)
1206          base.setCommentElement(derived.getCommentElement().copy());
1207        else if (derived.hasCommentElement())
1208          derived.getCommentElement().setUserData(DERIVATION_EQUALS, true);
1209      }
1210
1211      if (derived.hasLabelElement()) {
1212        if (derived.getLabel().startsWith("..."))
1213          base.setLabel(base.getLabel()+"\r\n"+derived.getLabel().substring(3));
1214        else if (!base.hasLabelElement() || !Base.compareDeep(derived.getLabelElement(), base.getLabelElement(), false))
1215          base.setLabelElement(derived.getLabelElement().copy());
1216        else if (trimDifferential)
1217          base.setLabelElement(derived.getLabelElement().copy());
1218        else if (derived.hasLabelElement())
1219          derived.getLabelElement().setUserData(DERIVATION_EQUALS, true);
1220      }
1221
1222      if (derived.hasRequirementsElement()) {
1223        if (derived.getRequirements().startsWith("..."))
1224          base.setRequirements(base.getRequirements()+"\r\n"+derived.getRequirements().substring(3));
1225        else if (!base.hasRequirementsElement() || !Base.compareDeep(derived.getRequirementsElement(), base.getRequirementsElement(), false))
1226          base.setRequirementsElement(derived.getRequirementsElement().copy());
1227        else if (trimDifferential)
1228          base.setRequirementsElement(derived.getRequirementsElement().copy());
1229        else if (derived.hasRequirementsElement())
1230          derived.getRequirementsElement().setUserData(DERIVATION_EQUALS, true);
1231      }
1232      // sdf-9
1233      if (derived.hasRequirements() && !base.getPath().contains("."))
1234        derived.setRequirements(null);
1235      if (base.hasRequirements() && !base.getPath().contains("."))
1236        base.setRequirements(null);
1237
1238      if (derived.hasAlias()) {
1239        if (!Base.compareDeep(derived.getAlias(), base.getAlias(), false))
1240          for (StringType s : derived.getAlias()) {
1241            if (!base.hasAlias(s.getValue()))
1242              base.getAlias().add(s.copy());
1243          }
1244        else if (trimDifferential)
1245          derived.getAlias().clear();
1246        else
1247          for (StringType t : derived.getAlias())
1248            t.setUserData(DERIVATION_EQUALS, true);
1249      }
1250
1251      if (derived.hasMinElement()) {
1252        if (!Base.compareDeep(derived.getMinElement(), base.getMinElement(), false)) {
1253          if (derived.getMin() < base.getMin())
1254            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+source.getPath(), "Derived min  ("+Integer.toString(derived.getMin())+") cannot be less than base min ("+Integer.toString(base.getMin())+")", ValidationMessage.IssueSeverity.ERROR));
1255          base.setMinElement(derived.getMinElement().copy());
1256        } else if (trimDifferential)
1257          derived.setMinElement(null);
1258        else
1259          derived.getMinElement().setUserData(DERIVATION_EQUALS, true);
1260      }
1261
1262      if (derived.hasMaxElement()) {
1263        if (!Base.compareDeep(derived.getMaxElement(), base.getMaxElement(), false)) {
1264          if (isLargerMax(derived.getMax(), base.getMax()))
1265            messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+source.getPath(), "Derived max ("+derived.getMax()+") cannot be greater than base max ("+base.getMax()+")", ValidationMessage.IssueSeverity.ERROR));
1266          base.setMaxElement(derived.getMaxElement().copy());
1267        } else if (trimDifferential)
1268          derived.setMaxElement(null);
1269        else
1270          derived.getMaxElement().setUserData(DERIVATION_EQUALS, true);
1271      }
1272
1273      if (derived.hasFixed()) {
1274        if (!Base.compareDeep(derived.getFixed(), base.getFixed(), true)) {
1275          base.setFixed(derived.getFixed().copy());
1276        } else if (trimDifferential)
1277          derived.setFixed(null);
1278        else
1279          derived.getFixed().setUserData(DERIVATION_EQUALS, true);
1280      }
1281
1282      if (derived.hasPattern()) {
1283        if (!Base.compareDeep(derived.getPattern(), base.getPattern(), false)) {
1284          base.setPattern(derived.getPattern().copy());
1285        } else
1286          if (trimDifferential)
1287            derived.setPattern(null);
1288          else
1289            derived.getPattern().setUserData(DERIVATION_EQUALS, true);
1290      }
1291
1292      for (ElementDefinitionExampleComponent ex : derived.getExample()) {
1293        boolean found = false;
1294        for (ElementDefinitionExampleComponent exS : base.getExample())
1295          if (Base.compareDeep(ex, exS, false))
1296            found = true;
1297        if (!found)
1298          base.addExample(ex.copy());
1299        else if (trimDifferential)
1300          derived.getExample().remove(ex);
1301        else
1302          ex.setUserData(DERIVATION_EQUALS, true);
1303      }
1304
1305      if (derived.hasMaxLengthElement()) {
1306        if (!Base.compareDeep(derived.getMaxLengthElement(), base.getMaxLengthElement(), false))
1307          base.setMaxLengthElement(derived.getMaxLengthElement().copy());
1308        else if (trimDifferential)
1309          derived.setMaxLengthElement(null);
1310        else
1311          derived.getMaxLengthElement().setUserData(DERIVATION_EQUALS, true);
1312      }
1313
1314      // todo: what to do about conditions?
1315      // condition : id 0..*
1316
1317      if (derived.hasMustSupportElement()) {
1318        if (!(base.hasMustSupportElement() && Base.compareDeep(derived.getMustSupportElement(), base.getMustSupportElement(), false)))
1319          base.setMustSupportElement(derived.getMustSupportElement().copy());
1320        else if (trimDifferential)
1321          derived.setMustSupportElement(null);
1322        else
1323          derived.getMustSupportElement().setUserData(DERIVATION_EQUALS, true);
1324      }
1325
1326
1327      // profiles cannot change : isModifier, defaultValue, meaningWhenMissing
1328      // but extensions can change isModifier
1329      if (isExtension) {
1330        if (derived.hasIsModifierElement() && !(base.hasIsModifierElement() && Base.compareDeep(derived.getIsModifierElement(), base.getIsModifierElement(), false)))
1331          base.setIsModifierElement(derived.getIsModifierElement().copy());
1332        else if (trimDifferential)
1333          derived.setIsModifierElement(null);
1334        else if (derived.hasIsModifierElement())
1335          derived.getIsModifierElement().setUserData(DERIVATION_EQUALS, true);
1336      }
1337
1338      if (derived.hasBinding()) {
1339        if (!base.hasBinding() || !Base.compareDeep(derived.getBinding(), base.getBinding(), false)) {
1340          if (base.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && derived.getBinding().getStrength() != BindingStrength.REQUIRED)
1341            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));
1342//            throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": illegal attempt to change a binding from "+base.getBinding().getStrength().toCode()+" to "+derived.getBinding().getStrength().toCode());
1343          else if (base.hasBinding() && derived.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && base.getBinding().hasValueSetReference() && derived.getBinding().hasValueSetReference()) {
1344            ValueSetExpansionOutcome expBase = context.expandVS(context.fetchResource(ValueSet.class, base.getBinding().getValueSetReference().getReference()), true, false);
1345            ValueSetExpansionOutcome expDerived = context.expandVS(context.fetchResource(ValueSet.class, derived.getBinding().getValueSetReference().getReference()), true, false);
1346            if (expBase.getValueset() == null)
1347              messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+base.getPath(), "Binding "+base.getBinding().getValueSetReference().getReference()+" could not be expanded", ValidationMessage.IssueSeverity.WARNING));
1348            else if (expDerived.getValueset() == null)
1349              messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSetReference().getReference()+" could not be expanded", ValidationMessage.IssueSeverity.WARNING));
1350            else if (!isSubset(expBase.getValueset(), expDerived.getValueset()))
1351              messages.add(new ValidationMessage(Source.ProfileValidator, ValidationMessage.IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSetReference().getReference()+" is not a subset of binding "+base.getBinding().getValueSetReference().getReference(), ValidationMessage.IssueSeverity.ERROR));
1352          }
1353          base.setBinding(derived.getBinding().copy());
1354        } else if (trimDifferential)
1355          derived.setBinding(null);
1356        else
1357          derived.getBinding().setUserData(DERIVATION_EQUALS, true);
1358      } // else if (base.hasBinding() && doesn't have bindable type )
1359        //  base
1360
1361      if (derived.hasIsSummaryElement()) {
1362        if (!Base.compareDeep(derived.getIsSummaryElement(), base.getIsSummaryElement(), false)) {
1363          if (base.hasIsSummary())
1364            throw new Error("Error in profile "+pn+" at "+derived.getPath()+": Base isSummary = "+base.getIsSummaryElement().asStringValue()+", derived isSummary = "+derived.getIsSummaryElement().asStringValue());
1365          base.setIsSummaryElement(derived.getIsSummaryElement().copy());
1366        } else if (trimDifferential)
1367          derived.setIsSummaryElement(null);
1368        else
1369          derived.getIsSummaryElement().setUserData(DERIVATION_EQUALS, true);
1370      }
1371
1372      if (derived.hasType()) {
1373        if (!Base.compareDeep(derived.getType(), base.getType(), false)) {
1374          if (base.hasType()) {
1375            for (TypeRefComponent ts : derived.getType()) {
1376              boolean ok = false;
1377              CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1378              for (TypeRefComponent td : base.getType()) {;
1379                b.append(td.getCode());
1380                if (td.hasCode() && (td.getCode().equals(ts.getCode()) || td.getCode().equals("Extension") ||
1381                    td.getCode().equals("Element") || td.getCode().equals("*") ||
1382                    ((td.getCode().equals("Resource") || (td.getCode().equals("DomainResource")) && pkp.isResource(ts.getCode())))))
1383                  ok = true;
1384              }
1385              if (!ok)
1386                throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": illegal constrained type "+ts.getCode()+" from "+b.toString());
1387            }
1388          }
1389          base.getType().clear();
1390          for (TypeRefComponent t : derived.getType()) {
1391            TypeRefComponent tt = t.copy();
1392//            tt.setUserData(DERIVATION_EQUALS, true);
1393            base.getType().add(tt);
1394          }
1395        }
1396        else if (trimDifferential)
1397          derived.getType().clear();
1398        else
1399          for (TypeRefComponent t : derived.getType())
1400            t.setUserData(DERIVATION_EQUALS, true);
1401      }
1402
1403      if (derived.hasMapping()) {
1404        // todo: mappings are not cumulative - one replaces another
1405        if (!Base.compareDeep(derived.getMapping(), base.getMapping(), false)) {
1406          for (ElementDefinitionMappingComponent s : derived.getMapping()) {
1407            boolean found = false;
1408            for (ElementDefinitionMappingComponent d : base.getMapping()) {
1409              found = found || (d.getIdentity().equals(s.getIdentity()) && d.getMap().equals(s.getMap()));
1410            }
1411            if (!found)
1412              base.getMapping().add(s);
1413          }
1414        }
1415        else if (trimDifferential)
1416          derived.getMapping().clear();
1417        else
1418          for (ElementDefinitionMappingComponent t : derived.getMapping())
1419            t.setUserData(DERIVATION_EQUALS, true);
1420      }
1421
1422      // todo: constraints are cumulative. there is no replacing
1423      for (ElementDefinitionConstraintComponent s : base.getConstraint()) { 
1424        s.setUserData(IS_DERIVED, true);
1425        if (!s.hasSource())
1426          s.setSource(base.getId());
1427      }
1428      if (derived.hasConstraint()) {
1429        for (ElementDefinitionConstraintComponent s : derived.getConstraint()) {
1430          ElementDefinitionConstraintComponent inv = s.copy();
1431          base.getConstraint().add(inv);
1432        }
1433      }
1434      
1435      // now, check that we still have a bindable type; if not, delete the binding - see task 8477
1436      if (dest.hasBinding() && !hasBindableType(dest))
1437        dest.setBinding(null);
1438        
1439      // finally, we copy any extensions from source to dest
1440      for (Extension ex : base.getExtension()) {
1441        StructureDefinition sd  = context.fetchResource(StructureDefinition.class, ex.getUrl());
1442        if (sd == null || sd.getSnapshot() == null || sd.getSnapshot().getElementFirstRep().getMax().equals("1"))
1443          ToolingExtensions.removeExtension(dest, ex.getUrl());
1444        dest.addExtension(ex);
1445      }
1446    }
1447  }
1448
1449  private boolean hasBindableType(ElementDefinition ed) {
1450    for (TypeRefComponent tr : ed.getType()) {
1451      if (Utilities.existsInList(tr.getCode(), "Coding", "CodeableConcept", "Quantity", "url", "string", "code"))
1452        return true;
1453    }
1454    return false;
1455  }
1456
1457
1458  private boolean isLargerMax(String derived, String base) {
1459    if ("*".equals(base))
1460      return false;
1461    if ("*".equals(derived))
1462      return true;
1463    return Integer.parseInt(derived) > Integer.parseInt(base);
1464  }
1465
1466
1467  private boolean isSubset(ValueSet expBase, ValueSet expDerived) {
1468    return codesInExpansion(expDerived.getExpansion().getContains(), expBase.getExpansion());
1469  }
1470
1471
1472  private boolean codesInExpansion(List<ValueSetExpansionContainsComponent> contains, ValueSetExpansionComponent expansion) {
1473    for (ValueSetExpansionContainsComponent cc : contains) {
1474      if (!inExpansion(cc, expansion.getContains()))
1475        return false;
1476      if (!codesInExpansion(cc.getContains(), expansion))
1477        return false;
1478    }
1479    return true;
1480  }
1481
1482
1483  private boolean inExpansion(ValueSetExpansionContainsComponent cc, List<ValueSetExpansionContainsComponent> contains) {
1484    for (ValueSetExpansionContainsComponent cc1 : contains) {
1485      if (cc.getSystem().equals(cc1.getSystem()) && cc.getCode().equals(cc1.getCode()))
1486        return true;
1487      if (inExpansion(cc,  cc1.getContains()))
1488        return true;
1489    }
1490    return false;
1491  }
1492
1493  public void closeDifferential(StructureDefinition base, StructureDefinition derived) throws FHIRException {
1494    for (ElementDefinition edb : base.getSnapshot().getElement()) {
1495      if (isImmediateChild(edb) && !edb.getPath().endsWith(".id")) {
1496        ElementDefinition edm = getMatchInDerived(edb, derived.getDifferential().getElement());
1497        if (edm == null) {
1498          ElementDefinition edd = derived.getDifferential().addElement();
1499          edd.setPath(edb.getPath());
1500          edd.setMax("0");
1501        } else if (edb.hasSlicing()) {
1502          closeChildren(base, edb, derived, edm);
1503        }
1504      }
1505    }
1506    sortDifferential(base, derived, derived.getName(), new ArrayList<String>());
1507  }
1508
1509  private void closeChildren(StructureDefinition base, ElementDefinition edb, StructureDefinition derived, ElementDefinition edm) {
1510    String path = edb.getPath()+".";
1511    int baseStart = base.getSnapshot().getElement().indexOf(edb);
1512    int baseEnd = findEnd(base.getSnapshot().getElement(), edb, baseStart+1);
1513    int diffStart = derived.getDifferential().getElement().indexOf(edm);
1514    int diffEnd = findEnd(derived.getDifferential().getElement(), edm, diffStart+1);
1515    
1516    for (int cBase = baseStart; cBase < baseEnd; cBase++) {
1517      ElementDefinition edBase = base.getSnapshot().getElement().get(cBase);
1518      if (isImmediateChild(edBase, edb)) {
1519        ElementDefinition edMatch = getMatchInDerived(edBase, derived.getDifferential().getElement(), diffStart, diffEnd);
1520        if (edMatch == null) {
1521          ElementDefinition edd = derived.getDifferential().addElement();
1522          edd.setPath(edBase.getPath());
1523          edd.setMax("0");
1524        } else {
1525          closeChildren(base, edBase, derived, edMatch);
1526        }        
1527      }
1528    }
1529  }
1530
1531
1532
1533
1534  private int findEnd(List<ElementDefinition> list, ElementDefinition ed, int cursor) {
1535    String path = ed.getPath()+".";
1536    while (cursor < list.size() && list.get(cursor).getPath().startsWith(path))
1537      cursor++;
1538    return cursor;
1539  }
1540
1541
1542  private ElementDefinition getMatchInDerived(ElementDefinition ed, List<ElementDefinition> list) {
1543    for (ElementDefinition t : list)
1544      if (t.getPath().equals(ed.getPath()))
1545        return t;
1546    return null;
1547  }
1548
1549  private ElementDefinition getMatchInDerived(ElementDefinition ed, List<ElementDefinition> list, int start, int end) {
1550    for (int i = start; i < end; i++) {
1551      ElementDefinition t = list.get(i);
1552      if (t.getPath().equals(ed.getPath()))
1553        return t;
1554    }
1555    return null;
1556  }
1557
1558
1559  private boolean isImmediateChild(ElementDefinition ed) {
1560    String p = ed.getPath();
1561    if (!p.contains("."))
1562      return false;
1563    p = p.substring(p.indexOf(".")+1);
1564    return !p.contains(".");
1565  }
1566
1567  private boolean isImmediateChild(ElementDefinition candidate, ElementDefinition base) {
1568    String p = candidate.getPath();
1569    if (!p.contains("."))
1570      return false;
1571    if (!p.startsWith(base.getPath()+"."))
1572      return false;
1573    p = p.substring(base.getPath().length()+1);
1574    return !p.contains(".");
1575  }
1576
1577  public XhtmlNode generateExtensionTable(String defFile, StructureDefinition ed, String imageFolder, boolean inlineGraphics, boolean full, String corePath, String imagePath, Set<String> outputTracker) throws IOException, FHIRException {
1578    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics);
1579    gen.setTranslator(getTranslator());
1580    TableModel model = gen.initNormalTable(corePath, false);
1581
1582    boolean deep = false;
1583    String m = "";
1584    boolean vdeep = false;
1585    if (ed.getSnapshot().getElementFirstRep().getIsModifier())
1586      m = "modifier_";
1587    for (ElementDefinition eld : ed.getSnapshot().getElement()) {
1588      deep = deep || eld.getPath().contains("Extension.extension.");
1589      vdeep = vdeep || eld.getPath().contains("Extension.extension.extension.");
1590    }
1591    Row r = gen.new Row();
1592    model.getRows().add(r);
1593    r.getCells().add(gen.new Cell(null, defFile == null ? "" : defFile+"-definitions.html#extension."+ed.getName(), ed.getSnapshot().getElement().get(0).getIsModifier() ? "modifierExtension" : "extension", null, null));
1594    r.getCells().add(gen.new Cell());
1595    r.getCells().add(gen.new Cell(null, null, describeCardinality(ed.getSnapshot().getElement().get(0), null, new UnusedTracker()), null, null));
1596
1597    ElementDefinition ved = null;
1598    if (full || vdeep) {
1599      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
1600
1601      r.setIcon(deep ? "icon_"+m+"extension_complex.png" : "icon_extension_simple.png", deep ? HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX : HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
1602      List<ElementDefinition> children = getChildren(ed.getSnapshot().getElement(), ed.getSnapshot().getElement().get(0));
1603      for (ElementDefinition child : children)
1604        if (!child.getPath().endsWith(".id"))
1605          genElement(defFile == null ? "" : defFile+"-definitions.html#extension.", gen, r.getSubRows(), child, ed.getSnapshot().getElement(), null, true, defFile, true, full, corePath, imagePath, true, false, false, false);
1606    } else if (deep) {
1607      List<ElementDefinition> children = new ArrayList<ElementDefinition>();
1608      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
1609        if (ted.getPath().equals("Extension.extension"))
1610          children.add(ted);
1611      }
1612
1613      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
1614      r.setIcon("icon_"+m+"extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
1615      
1616      for (ElementDefinition c : children) {
1617        ved = getValueFor(ed, c);
1618        ElementDefinition ued = getUrlFor(ed, c);
1619        if (ved != null && ued != null) {
1620          Row r1 = gen.new Row();
1621          r.getSubRows().add(r1);
1622          r1.getCells().add(gen.new Cell(null, defFile == null ? "" : defFile+"-definitions.html#extension."+ed.getName(), ((UriType) ued.getFixed()).getValue(), null, null));
1623          r1.getCells().add(gen.new Cell());
1624          r1.getCells().add(gen.new Cell(null, null, describeCardinality(c, null, new UnusedTracker()), null, null));
1625          genTypes(gen, r1, ved, defFile, ed, corePath, imagePath);
1626          r1.getCells().add(gen.new Cell(null, null, c.getDefinition(), null, null));
1627          r1.setIcon("icon_"+m+"extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
1628        }
1629      }
1630    } else  {
1631      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
1632        if (ted.getPath().startsWith("Extension.value"))
1633          ved = ted;
1634      }
1635
1636      genTypes(gen, r, ved, defFile, ed, corePath, imagePath);
1637
1638      r.setIcon("icon_"+m+"extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
1639    }
1640    Cell c = gen.new Cell("", "", "URL = "+ed.getUrl(), null, null);
1641    c.addPiece(gen.new Piece("br")).addPiece(gen.new Piece(null, ed.getName()+": "+ed.getDescription(), null));
1642    if (!full && !(deep || vdeep) && ved != null && ved.hasBinding()) {  
1643        c.addPiece(gen.new Piece("br"));
1644      BindingResolution br = pkp.resolveBinding(ed, ved.getBinding(), ved.getPath());
1645      c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(null, translate("sd.table", "Binding")+": ", null).addStyle("font-weight:bold")));
1646      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)));
1647      if (ved.getBinding().hasStrength()) {
1648        c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(null, " (", null)));
1649        c.getPieces().add(checkForNoChange(ved.getBinding(), gen.new Piece(corePath+"terminologies.html#"+ved.getBinding().getStrength().toCode(), egt(ved.getBinding().getStrengthElement()), ved.getBinding().getStrength().getDefinition())));              
1650        c.getPieces().add(gen.new Piece(null, ")", null));
1651      }
1652    }
1653    c.addPiece(gen.new Piece("br")).addPiece(gen.new Piece(null, describeExtensionContext(ed), null));
1654    r.getCells().add(c);
1655    
1656    try {
1657      return gen.generate(model, corePath, 0, outputTracker);
1658        } catch (org.hl7.fhir.exceptions.FHIRException e) {
1659                throw new FHIRException(e.getMessage(), e);
1660        }
1661  }
1662
1663  private ElementDefinition getUrlFor(StructureDefinition ed, ElementDefinition c) {
1664    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
1665    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
1666      if (ed.getSnapshot().getElement().get(i).getPath().equals(c.getPath()+".url"))
1667        return ed.getSnapshot().getElement().get(i);
1668      i++;
1669    }
1670    return null;
1671  }
1672
1673  private ElementDefinition getValueFor(StructureDefinition ed, ElementDefinition c) {
1674    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
1675    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
1676      if (ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".value"))
1677        return ed.getSnapshot().getElement().get(i);
1678      i++;
1679    }
1680    return null;
1681  }
1682
1683
1684  private static final int AGG_NONE = 0;
1685  private static final int AGG_IND = 1;
1686  private static final int AGG_GR = 2;
1687  private Cell genTypes(HierarchicalTableGenerator gen, Row r, ElementDefinition e, String profileBaseFileName, StructureDefinition profile, String corePath, String imagePath) {
1688    Cell c = gen.new Cell();
1689    r.getCells().add(c);
1690    List<TypeRefComponent> types = e.getType();
1691    if (!e.hasType()) {
1692      if (e.hasContentReference()) {
1693        return c;
1694      } else {
1695      ElementDefinition d = (ElementDefinition) e.getUserData(DERIVATION_POINTER);
1696      if (d != null && d.hasType()) {
1697        types = new ArrayList<ElementDefinition.TypeRefComponent>();
1698        for (TypeRefComponent tr : d.getType()) {
1699          TypeRefComponent tt = tr.copy();
1700          tt.setUserData(DERIVATION_EQUALS, true);
1701          types.add(tt);
1702        }
1703      } else
1704        return c;
1705    }
1706    }
1707
1708    boolean first = true;
1709    Element source = types.get(0); // either all types are the same, or we don't consider any of them the same
1710    int aggMode = AGG_NONE;
1711
1712    boolean allReference = !types.isEmpty();
1713    Set<AggregationMode> aggs = new HashSet<ElementDefinition.AggregationMode>();
1714    for (TypeRefComponent t : types) {
1715      if (t.getCode()!=null && t.getCode().equals("Reference") && t.hasProfile()) {
1716        for (Enumeration<AggregationMode> en : t.getAggregation())
1717          aggs.add(en.getValue());
1718      } else
1719        allReference = false;
1720      
1721    }
1722    if (allReference) {
1723      if (aggs.size() > 0) {
1724        boolean allSame = true;
1725        for (TypeRefComponent t : types) {
1726          for (AggregationMode agg : aggs) {
1727            boolean found = false;
1728            for (Enumeration<AggregationMode> en : t.getAggregation())
1729              if (en.getValue() == agg)
1730                found = true;
1731            if (!found)
1732              allSame = false;
1733          }
1734        }
1735        aggMode = allSame ? AGG_GR : AGG_IND;
1736        if (aggMode != AGG_GR)
1737          allReference = false;
1738      }
1739    } else 
1740      aggMode = aggs.size() == 0 ? AGG_NONE : AGG_IND;
1741
1742    if (allReference) {
1743      c.getPieces().add(gen.new Piece(corePath+"references.html", "Reference", null));
1744      c.getPieces().add(gen.new Piece(null, "(", null));
1745    }
1746    TypeRefComponent tl = null;
1747    for (TypeRefComponent t : types) {
1748      if (first)
1749        first = false;
1750      else if (allReference)
1751        c.addPiece(checkForNoChange(tl, gen.new Piece(null," | ", null)));
1752      else
1753        c.addPiece(checkForNoChange(tl, gen.new Piece(null,", ", null)));
1754      tl = t;
1755      if (t.getCode()!= null && t.getCode().equals("Reference")) {
1756        if (!allReference) {
1757          c.getPieces().add(gen.new Piece(corePath+"references.html", "Reference", null));
1758          c.getPieces().add(gen.new Piece(null, "(", null));
1759        }
1760        if (t.hasTargetProfile() && t.getTargetProfile().startsWith("http://hl7.org/fhir/StructureDefinition/")) {
1761          StructureDefinition sd = context.fetchResource(StructureDefinition.class, t.getTargetProfile());
1762          if (sd != null) {
1763            String disp = sd.hasTitle() ? sd.getTitle() : sd.getName();
1764            c.addPiece(checkForNoChange(t, gen.new Piece(checkPrepend(corePath, sd.getUserString("path")), disp, null)));
1765          } else {
1766            String rn = t.getTargetProfile().substring(40);
1767            c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, rn), rn, null)));
1768          }
1769        } else if (t.hasTargetProfile() && Utilities.isAbsoluteUrl(t.getTargetProfile())) {
1770          StructureDefinition sd = context.fetchResource(StructureDefinition.class, t.getTargetProfile());
1771          if (sd != null) {
1772            String disp = sd.hasTitle() ? sd.getTitle() : sd.getName();
1773            String ref = pkp.getLinkForProfile(null, sd.getUrl());
1774            if (ref.contains("|"))
1775              ref = ref.substring(0,  ref.indexOf("|"));
1776            c.addPiece(checkForNoChange(t, gen.new Piece(ref, disp, null)));
1777          } else
1778            c.addPiece(checkForNoChange(t, gen.new Piece(null, t.getTargetProfile(), null)));
1779        } else if (t.hasTargetProfile() && t.getTargetProfile().startsWith("#"))
1780          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+profileBaseFileName+"."+t.getTargetProfile().substring(1).toLowerCase()+".html", t.getTargetProfile(), null)));
1781        else if (t.hasTargetProfile())
1782          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+t.getTargetProfile(), t.getTargetProfile(), null)));
1783        if (!allReference) {
1784          c.getPieces().add(gen.new Piece(null, ")", null));
1785          if (t.getAggregation().size() > 0) {
1786            c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", " {", null));
1787            boolean firstA = true;
1788            for (Enumeration<AggregationMode> a : t.getAggregation()) {
1789              if (firstA = true)
1790                firstA = false;
1791              else
1792                c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", ", ", null));
1793              c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", codeForAggregation(a.getValue()), null));
1794            }
1795            c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", "}", null));
1796          }
1797        }
1798      } else if (t.hasProfile() && (!t.getCode().equals("Extension") || t.getProfile().contains(":"))) { // a profiled type
1799        String ref;
1800        ref = pkp.getLinkForProfile(profile, t.getProfile());
1801        if (ref != null) {
1802          String[] parts = ref.split("\\|");
1803          if (parts[0].startsWith("http:") || parts[0].startsWith("https:"))
1804            c.addPiece(checkForNoChange(t, gen.new Piece(parts[0], parts[1], t.getCode())));
1805          else
1806            c.addPiece(checkForNoChange(t, gen.new Piece((t.getProfile().startsWith(corePath)? corePath: "")+parts[0], parts[1], t.getCode())));
1807        } else
1808          c.addPiece(checkForNoChange(t, gen.new Piece((t.getProfile().startsWith(corePath)? corePath: "")+ref, t.getCode(), null)));
1809      } else if (pkp.hasLinkFor(t.getCode())) {
1810        c.addPiece(checkForNoChange(t, gen.new Piece(pkp.getLinkFor(corePath, t.getCode()), t.getCode(), null)));
1811      } else
1812        c.addPiece(checkForNoChange(t, gen.new Piece(null, t.getCode(), null)));
1813    }
1814    if (allReference) {
1815      c.getPieces().add(gen.new Piece(null, ")", null));
1816      if (aggs.size() > 0) {
1817        c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", " {", null));
1818        boolean firstA = true;
1819        for (AggregationMode a : aggs) {
1820          if (firstA = true)
1821            firstA = false;
1822          else
1823            c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", ", ", null));
1824          c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", codeForAggregation(a), null));
1825        }
1826        c.getPieces().add(gen.new Piece(corePath+"valueset-resource-aggregation-mode.html", "}", null));
1827      }
1828    }
1829    return c;
1830  }
1831
1832  private String codeForAggregation(AggregationMode a) {
1833    switch (a) {
1834    case BUNDLED : return "b";
1835    case CONTAINED : return "c";
1836    case REFERENCED: return "r";
1837         default: return "?";
1838    }
1839  }
1840
1841
1842  private String checkPrepend(String corePath, String path) {
1843    if (pkp.prependLinks() && !(path.startsWith("http:") || path.startsWith("https:")))
1844      return corePath+path;
1845    else 
1846      return path;
1847  }
1848
1849
1850  private ElementDefinition getElementByName(List<ElementDefinition> elements, String contentReference) {
1851    for (ElementDefinition ed : elements)
1852      if (ed.hasSliceName() && ("#"+ed.getSliceName()).equals(contentReference))
1853        return ed;
1854    return null;
1855  }
1856
1857  private ElementDefinition getElementById(List<ElementDefinition> elements, String contentReference) {
1858    for (ElementDefinition ed : elements)
1859      if (ed.hasId() && ("#"+ed.getId()).equals(contentReference))
1860        return ed;
1861    return null;
1862  }
1863
1864
1865  public static String describeExtensionContext(StructureDefinition ext) {
1866    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1867    for (StringType t : ext.getContext())
1868      b.append(t.getValue());
1869    if (!ext.hasContextType())
1870      throw new Error("no context type on "+ext.getUrl());
1871    switch (ext.getContextType()) {
1872    case DATATYPE: return "Use on data type: "+b.toString();
1873    case EXTENSION: return "Use on extension: "+b.toString();
1874    case RESOURCE: return "Use on element: "+b.toString();
1875    default:
1876      return "??";
1877    }
1878  }
1879
1880  private String describeCardinality(ElementDefinition definition, ElementDefinition fallback, UnusedTracker tracker) {
1881    IntegerType min = definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
1882    StringType max = definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
1883    if (min.isEmpty() && fallback != null)
1884      min = fallback.getMinElement();
1885    if (max.isEmpty() && fallback != null)
1886      max = fallback.getMaxElement();
1887
1888    tracker.used = !max.isEmpty() && !max.getValue().equals("0");
1889
1890    if (min.isEmpty() && max.isEmpty())
1891      return null;
1892    else
1893      return (!min.hasValue() ? "" : Integer.toString(min.getValue())) + ".." + (!max.hasValue() ? "" : max.getValue());
1894  }
1895
1896  private void genCardinality(HierarchicalTableGenerator gen, ElementDefinition definition, Row row, boolean hasDef, UnusedTracker tracker, ElementDefinition fallback) {
1897    IntegerType min = !hasDef ? new IntegerType() : definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
1898    StringType max = !hasDef ? new StringType() : definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
1899    if (min.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
1900      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
1901      if (base.hasMinElement()) {
1902        min = base.getMinElement().copy();
1903        min.setUserData(DERIVATION_EQUALS, true);
1904      }
1905    }
1906    if (max.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
1907      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
1908      if (base.hasMaxElement()) {
1909        max = base.getMaxElement().copy();
1910        max.setUserData(DERIVATION_EQUALS, true);
1911      }
1912    }
1913    if (min.isEmpty() && fallback != null)
1914      min = fallback.getMinElement();
1915    if (max.isEmpty() && fallback != null)
1916      max = fallback.getMaxElement();
1917
1918    if (!max.isEmpty())
1919      tracker.used = !max.getValue().equals("0");
1920
1921    Cell cell = gen.new Cell(null, null, null, null, null);
1922    row.getCells().add(cell);
1923    if (!min.isEmpty() || !max.isEmpty()) {
1924      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !min.hasValue() ? "" : Integer.toString(min.getValue()), null)));
1925      cell.addPiece(checkForNoChange(min, max, gen.new Piece(null, "..", null)));
1926      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !max.hasValue() ? "" : max.getValue(), null)));
1927    }
1928  }
1929
1930
1931  private Piece checkForNoChange(Element source, Piece piece) {
1932    if (source.hasUserData(DERIVATION_EQUALS)) {
1933      piece.addStyle("opacity: 0.4");
1934    }
1935    return piece;
1936  }
1937
1938  private Piece checkForNoChange(Element src1, Element src2, Piece piece) {
1939    if (src1.hasUserData(DERIVATION_EQUALS) && src2.hasUserData(DERIVATION_EQUALS)) {
1940      piece.addStyle("opacity: 0.5");
1941    }
1942    return piece;
1943  }
1944
1945  public XhtmlNode generateTable(String defFile, StructureDefinition profile, boolean diff, String imageFolder, boolean inlineGraphics, String profileBaseFileName, boolean snapshot, String corePath, String imagePath, boolean logicalModel, boolean allInvariants, Set<String> outputTracker) throws IOException, FHIRException {
1946    assert(diff != snapshot);// check it's ok to get rid of one of these
1947    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics);
1948    gen.setTranslator(getTranslator());
1949    TableModel model = gen.initNormalTable(corePath, false);
1950    List<ElementDefinition> list = diff ? profile.getDifferential().getElement() : profile.getSnapshot().getElement();
1951    List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
1952    profiles.add(profile);
1953    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);
1954    try {
1955      return gen.generate(model, imagePath, 0, outputTracker);
1956        } catch (org.hl7.fhir.exceptions.FHIRException e) {
1957                throw new FHIRException(e.getMessage(), e);
1958        }
1959  }
1960
1961
1962  public XhtmlNode generateGrid(String defFile, StructureDefinition profile, String imageFolder, boolean inlineGraphics, String profileBaseFileName, String corePath, String imagePath, Set<String> outputTracker) throws IOException, FHIRException {
1963    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics);
1964    gen.setTranslator(getTranslator());
1965    TableModel model = gen.initGridTable(corePath);
1966    List<ElementDefinition> list = profile.getSnapshot().getElement();
1967    List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
1968    profiles.add(profile);
1969    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));
1970    try {
1971      return gen.generate(model, imagePath, 1, outputTracker);
1972    } catch (org.hl7.fhir.exceptions.FHIRException e) {
1973      throw new FHIRException(e.getMessage(), e);
1974    }
1975  }
1976
1977
1978  private boolean usesMustSupport(List<ElementDefinition> list) {
1979    for (ElementDefinition ed : list)
1980      if (ed.hasMustSupport() && ed.getMustSupport())
1981        return true;
1982    return false;
1983  }
1984
1985
1986  private void 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) throws IOException {
1987    StructureDefinition profile = profiles == null ? null : profiles.get(profiles.size()-1);
1988    String s = tail(element.getPath());
1989    List<ElementDefinition> children = getChildren(all, element);
1990    boolean isExtension = (s.equals("extension") || s.equals("modifierExtension"));
1991    if (!snapshot && isExtension && extensions != null && extensions != isExtension)
1992      return;
1993
1994    if (!onlyInformationIsMapping(all, element)) {
1995      Row row = gen.new Row();
1996      row.setAnchor(element.getPath());
1997      row.setColor(getRowColor(element, isConstraintMode));
1998      boolean hasDef = element != null;
1999      boolean ext = false;
2000      if (s.equals("extension")) {
2001        if (element.hasType() && element.getType().get(0).hasProfile() && extensionIsComplex(element.getType().get(0).getProfile()))
2002          row.setIcon("icon_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
2003        else
2004          row.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
2005        ext = true;
2006      } else if (s.equals("modifierExtension")) {
2007        if (element.hasType() && element.getType().get(0).hasProfile() && extensionIsComplex(element.getType().get(0).getProfile()))
2008          row.setIcon("icon_modifier_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
2009        else
2010          row.setIcon("icon_modifier_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
2011      } else if (!hasDef || element.getType().size() == 0)
2012        row.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
2013      else if (hasDef && element.getType().size() > 1) {
2014        if (allTypesAre(element.getType(), "Reference"))
2015          row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
2016        else
2017          row.setIcon("icon_choice.gif", HierarchicalTableGenerator.TEXT_ICON_CHOICE);
2018      } else if (hasDef && element.getType().get(0).getCode() != null && element.getType().get(0).getCode().startsWith("@"))
2019        row.setIcon("icon_reuse.png", HierarchicalTableGenerator.TEXT_ICON_REUSE);
2020      else if (hasDef && isPrimitive(element.getType().get(0).getCode()))
2021        row.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
2022      else if (hasDef && isReference(element.getType().get(0).getCode()))
2023        row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
2024      else if (hasDef && isDataType(element.getType().get(0).getCode()))
2025        row.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
2026      else
2027        row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
2028      String ref = defPath == null ? null : defPath + element.getId();
2029      UnusedTracker used = new UnusedTracker();
2030      used.used = true;
2031      Cell left = gen.new Cell(null, ref, s, (element.hasSliceName() ? translate("sd.table", "Slice")+" "+element.getSliceName() : "")+(hasDef && element.hasSliceName() ? ": " : "")+(!hasDef ? null : gt(element.getDefinitionElement())), null);
2032      row.getCells().add(left);
2033      Cell gc = gen.new Cell();
2034      row.getCells().add(gc);
2035      if (element != null && element.getIsModifier())
2036        checkForNoChange(element.getIsModifierElement(), gc.addStyledText(translate("sd.table", "This element is a modifier element"), "?!", null, null, null, false));
2037      if (element != null && element.getMustSupport())
2038        checkForNoChange(element.getMustSupportElement(), gc.addStyledText(translate("sd.table", "This element must be supported"), "S", "white", "red", null, false));
2039      if (element != null && element.getIsSummary())
2040        checkForNoChange(element.getIsSummaryElement(), gc.addStyledText(translate("sd.table", "This element is included in summaries"), "Σ", null, null, null, false));
2041      if (element != null && (!element.getConstraint().isEmpty() || !element.getCondition().isEmpty()))
2042        gc.addStyledText(translate("sd.table", "This element has or is affected by some invariants"), "I", null, null, null, false);
2043
2044      ExtensionContext extDefn = null;
2045      if (ext) {
2046        if (element != null && element.getType().size() == 1 && element.getType().get(0).hasProfile()) {
2047        extDefn = locateExtension(StructureDefinition.class, element.getType().get(0).getProfile());
2048          if (extDefn == null) {
2049            genCardinality(gen, element, row, hasDef, used, null);
2050            row.getCells().add(gen.new Cell(null, null, "?? "+element.getType().get(0).getProfile(), null, null));
2051            generateDescription(gen, row, element, null, used.used, profile.getUrl(), element.getType().get(0).getProfile(), profile, corePath, imagePath, root, logicalModel, allInvariants);
2052          } else {
2053            String name = urltail(element.getType().get(0).getProfile());
2054            left.getPieces().get(0).setText(name);
2055            // left.getPieces().get(0).setReference((String) extDefn.getExtensionStructure().getTag("filename"));
2056            left.getPieces().get(0).setHint(translate("sd.table", "Extension URL")+" = "+extDefn.getUrl());
2057            genCardinality(gen, element, row, hasDef, used, extDefn.getElement());
2058            ElementDefinition valueDefn = extDefn.getExtensionValueDefinition();
2059            if (valueDefn != null && !"0".equals(valueDefn.getMax()))
2060               genTypes(gen, row, valueDefn, profileBaseFileName, profile, corePath, imagePath);
2061             else // if it's complex, we just call it nothing
2062                // genTypes(gen, row, extDefn.getSnapshot().getElement().get(0), profileBaseFileName, profile);
2063              row.getCells().add(gen.new Cell(null, null, "("+translate("sd.table", "Complex")+")", null, null));
2064            generateDescription(gen, row, element, extDefn.getElement(), used.used, null, extDefn.getUrl(), profile, corePath, imagePath, root, logicalModel, allInvariants, valueDefn);
2065          }
2066        } else {
2067          genCardinality(gen, element, row, hasDef, used, null);
2068          if ("0".equals(element.getMax()))
2069            row.getCells().add(gen.new Cell());            
2070          else
2071            genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath);
2072          generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants);
2073        }
2074      } else {
2075        genCardinality(gen, element, row, hasDef, used, null);
2076        if (hasDef && !"0".equals(element.getMax()))
2077          genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath);
2078        else
2079          row.getCells().add(gen.new Cell());
2080        generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants);
2081      }
2082      if (element.hasSlicing()) {
2083        if (standardExtensionSlicing(element)) {
2084          used.used = element.hasType() && element.getType().get(0).hasProfile();
2085          showMissing = false;
2086        } else {
2087          row.setIcon("icon_slice.png", HierarchicalTableGenerator.TEXT_ICON_SLICE);
2088          row.getCells().get(2).getPieces().clear();
2089          for (Cell cell : row.getCells())
2090            for (Piece p : cell.getPieces()) {
2091              p.addStyle("font-style: italic");
2092            }
2093        }
2094      }
2095      if (used.used || showMissing)
2096        rows.add(row);
2097      if (!used.used && !element.hasSlicing()) {
2098        for (Cell cell : row.getCells())
2099          for (Piece p : cell.getPieces()) {
2100            p.setStyle("text-decoration:line-through");
2101            p.setReference(null);
2102          }
2103      } else{
2104        for (ElementDefinition child : children)
2105          if (logicalModel || !child.getPath().endsWith(".id") || (child.getPath().endsWith(".id") && (profile != null) && (profile.getDerivation() == TypeDerivationRule.CONSTRAINT)))  
2106            genElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, isExtension, snapshot, corePath, imagePath, false, logicalModel, isConstraintMode, allInvariants);
2107        if (!snapshot && (extensions == null || !extensions))
2108          for (ElementDefinition child : children)
2109            if (child.getPath().endsWith(".extension") || child.getPath().endsWith(".modifierExtension"))
2110              genElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, true, false, corePath, imagePath, false, logicalModel, isConstraintMode, allInvariants);
2111      }
2112    }
2113  }
2114
2115  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 {
2116    StructureDefinition profile = profiles == null ? null : profiles.get(profiles.size()-1);
2117    String s = tail(element.getPath());
2118    List<ElementDefinition> children = getChildren(all, element);
2119    boolean isExtension = (s.equals("extension") || s.equals("modifierExtension"));
2120
2121    if (!onlyInformationIsMapping(all, element)) {
2122      Row row = gen.new Row();
2123      row.setAnchor(element.getPath());
2124      row.setColor(getRowColor(element, isConstraintMode));
2125      boolean hasDef = element != null;
2126      String ref = defPath == null ? null : defPath + element.getId();
2127      UnusedTracker used = new UnusedTracker();
2128      used.used = true;
2129      Cell left = gen.new Cell();
2130      if (element.getType().size() == 1 && element.getType().get(0).isPrimitive())
2131        left.getPieces().add(gen.new Piece(ref, "\u00A0\u00A0" + s, !hasDef ? null : gt(element.getDefinitionElement())).addStyle("font-weight:bold"));
2132      else
2133        left.getPieces().add(gen.new Piece(ref, "\u00A0\u00A0" + s, !hasDef ? null : gt(element.getDefinitionElement())));
2134      if (element.hasSliceName()) {
2135        left.getPieces().add(gen.new Piece("br"));
2136        String indent = StringUtils.repeat('\u00A0', 1+2*(element.getPath().split("\\.").length));
2137        left.getPieces().add(gen.new Piece(null, indent + "("+element.getSliceName() + ")", null));
2138      }
2139      row.getCells().add(left);
2140
2141      ExtensionContext extDefn = null;
2142      genCardinality(gen, element, row, hasDef, used, null);
2143      if (hasDef && !"0".equals(element.getMax()))
2144        genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath);
2145      else
2146        row.getCells().add(gen.new Cell());
2147      generateGridDescription(gen, row, element, null, used.used, null, null, profile, corePath, imagePath, root, null);
2148/*      if (element.hasSlicing()) {
2149        if (standardExtensionSlicing(element)) {
2150          used.used = element.hasType() && element.getType().get(0).hasProfile();
2151          showMissing = false;
2152        } else {
2153          row.setIcon("icon_slice.png", HierarchicalTableGenerator.TEXT_ICON_SLICE);
2154          row.getCells().get(2).getPieces().clear();
2155          for (Cell cell : row.getCells())
2156            for (Piece p : cell.getPieces()) {
2157              p.addStyle("font-style: italic");
2158            }
2159        }
2160      }*/
2161      rows.add(row);
2162      for (ElementDefinition child : children)
2163        if (child.getMustSupport())
2164          genGridElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, isExtension, corePath, imagePath, false, isConstraintMode);
2165    }
2166  }
2167
2168
2169  private ExtensionContext locateExtension(Class<StructureDefinition> class1, String value)  {
2170    if (value.contains("#")) {
2171      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
2172      if (ext == null)
2173        return null;
2174      String tail = value.substring(value.indexOf("#")+1);
2175      ElementDefinition ed = null;
2176      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
2177        if (tail.equals(ted.getSliceName())) {
2178          ed = ted;
2179          return new ExtensionContext(ext, ed);
2180        }
2181      }
2182      return null;
2183    } else {
2184      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
2185      if (ext == null)
2186        return null;
2187      else 
2188        return new ExtensionContext(ext, ext.getSnapshot().getElement().get(0));
2189    }
2190  }
2191
2192
2193  private boolean extensionIsComplex(String value) {
2194    if (value.contains("#")) {
2195      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
2196    if (ext == null)
2197      return false;
2198      String tail = value.substring(value.indexOf("#")+1);
2199      ElementDefinition ed = null;
2200      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
2201        if (tail.equals(ted.getSliceName())) {
2202          ed = ted;
2203          break;
2204        }
2205      }
2206      if (ed == null)
2207        return false;
2208      int i = ext.getSnapshot().getElement().indexOf(ed);
2209      int j = i+1;
2210      while (j < ext.getSnapshot().getElement().size() && !ext.getSnapshot().getElement().get(j).getPath().equals(ed.getPath()))
2211        j++;
2212      return j - i > 5;
2213    } else {
2214      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
2215      return ext != null && ext.getSnapshot().getElement().size() > 5;
2216    }
2217  }
2218
2219
2220  private String getRowColor(ElementDefinition element, boolean isConstraintMode) {
2221    switch (element.getUserInt(UD_ERROR_STATUS)) {
2222    case STATUS_HINT: return ROW_COLOR_HINT;
2223    case STATUS_WARNING: return ROW_COLOR_WARNING;
2224    case STATUS_ERROR: return ROW_COLOR_ERROR;
2225    case STATUS_FATAL: return ROW_COLOR_FATAL;
2226    }
2227    if (isConstraintMode && !element.getMustSupport() && !element.getIsModifier() && element.getPath().contains("."))
2228      return null; // ROW_COLOR_NOT_MUST_SUPPORT;
2229    else
2230      return null;
2231  }
2232
2233
2234  private String urltail(String path) {
2235    if (path.contains("#"))
2236      return path.substring(path.lastIndexOf('#')+1);
2237    if (path.contains("/"))
2238      return path.substring(path.lastIndexOf('/')+1);
2239    else
2240      return path;
2241
2242  }
2243
2244  private boolean standardExtensionSlicing(ElementDefinition element) {
2245    String t = tail(element.getPath());
2246    return (t.equals("extension") || t.equals("modifierExtension"))
2247          && 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);
2248  }
2249
2250  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) throws IOException {
2251    return generateDescription(gen, row, definition, fallback, used, baseURL, url, profile, corePath, imagePath, root, logicalModel, allInvariants, null);
2252  }
2253  
2254  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) throws IOException {
2255    Cell c = gen.new Cell();
2256    row.getCells().add(c);
2257
2258    if (used) {
2259      if (logicalModel && ToolingExtensions.hasExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) {
2260        if (root) {
2261          c.getPieces().add(gen.new Piece(null, translate("sd.table", "XML Namespace")+": ", null).addStyle("font-weight:bold"));
2262          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
2263        } else if (!root && ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace") && 
2264            !ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace").equals(ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))) {
2265          c.getPieces().add(gen.new Piece(null, translate("sd.table", "XML Namespace")+": ", null).addStyle("font-weight:bold"));
2266          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
2267        }
2268      }
2269      
2270      if (definition.hasContentReference()) {
2271        ElementDefinition ed = getElementByName(profile.getSnapshot().getElement(), definition.getContentReference());
2272        if (ed == null)
2273          c.getPieces().add(gen.new Piece(null, translate("sd.table", "Unknown reference to %s", definition.getContentReference()), null));
2274        else
2275          c.getPieces().add(gen.new Piece("#"+ed.getPath(), translate("sd.table", "See %s", ed.getPath()), null));
2276      }
2277      if (definition.getPath().endsWith("url") && definition.hasFixed()) {
2278        c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "\""+buildJson(definition.getFixed())+"\"", null).addStyle("color: darkgreen")));
2279      } else {
2280        if (definition != null && definition.hasShort()) {
2281          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2282          c.addPiece(checkForNoChange(definition.getShortElement(), gen.new Piece(null, gt(definition.getShortElement()), null)));
2283        } else if (fallback != null && fallback.hasShort()) {
2284          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2285          c.addPiece(checkForNoChange(fallback.getShortElement(), gen.new Piece(null, gt(fallback.getShortElement()), null)));
2286        }
2287        if (url != null) {
2288          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2289          String fullUrl = url.startsWith("#") ? baseURL+url : url;
2290          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
2291          String ref = null;
2292          if (ed != null) {
2293            String p = ed.getUserString("path");
2294            if (p != null) {
2295              ref = p.startsWith("http:") || igmode ? p : Utilities.pathURL(corePath, p);
2296            }
2297          }
2298          c.getPieces().add(gen.new Piece(null, translate("sd.table", "URL")+": ", null).addStyle("font-weight:bold"));
2299          c.getPieces().add(gen.new Piece(ref, fullUrl, null));
2300        }
2301
2302        if (definition.hasSlicing()) {
2303          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2304          c.getPieces().add(gen.new Piece(null, translate("sd.table", "Slice")+": ", null).addStyle("font-weight:bold"));
2305          c.getPieces().add(gen.new Piece(null, describeSlice(definition.getSlicing()), null));
2306        }
2307        if (definition != null) {
2308          ElementDefinitionBindingComponent binding = null;
2309          if (valueDefn != null && valueDefn.hasBinding() && !valueDefn.getBinding().isEmpty())
2310            binding = valueDefn.getBinding();
2311          else if (definition.hasBinding())
2312            binding = definition.getBinding();
2313          if (binding!=null && !binding.isEmpty()) {
2314            if (!c.getPieces().isEmpty()) 
2315              c.addPiece(gen.new Piece("br"));
2316            BindingResolution br = pkp.resolveBinding(profile, binding, definition.getPath());
2317            c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, translate("sd.table", "Binding")+": ", null).addStyle("font-weight:bold")));
2318            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)));
2319            if (binding.hasStrength()) {
2320              c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, " (", null)));
2321              c.getPieces().add(checkForNoChange(binding, gen.new Piece(corePath+"terminologies.html#"+binding.getStrength().toCode(), egt(binding.getStrengthElement()), binding.getStrength().getDefinition())));              
2322              c.getPieces().add(gen.new Piece(null, ")", null));
2323            }
2324          }
2325          for (ElementDefinitionConstraintComponent inv : definition.getConstraint()) {
2326            if (!inv.hasSource() || allInvariants) {
2327              if (!c.getPieces().isEmpty()) 
2328                c.addPiece(gen.new Piece("br"));
2329              c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getKey()+": ", null).addStyle("font-weight:bold")));
2330              c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, gt(inv.getHumanElement()), null)));
2331            }
2332          }
2333          if ((definition.hasBase() && definition.getBase().getMax().equals("*")) || (definition.hasMax() && definition.getMax().equals("*"))) {
2334            if (c.getPieces().size() > 0)
2335              c.addPiece(gen.new Piece("br"));
2336            if (definition.hasOrderMeaning()) {
2337              c.getPieces().add(gen.new Piece(null, "This repeating element order: "+definition.getOrderMeaning(), null));
2338            } else {
2339              // don't show this, this it's important: c.getPieces().add(gen.new Piece(null, "This repeating element has no defined order", null));
2340            }           
2341          }
2342
2343          if (definition.hasFixed()) {
2344            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2345            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, translate("sd.table", "Fixed Value")+": ", null).addStyle("font-weight:bold")));
2346            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, buildJson(definition.getFixed()), null).addStyle("color: darkgreen")));
2347            if (isCoded(definition.getFixed()) && !hasDescription(definition.getFixed())) {
2348              Piece p = describeCoded(gen, definition.getFixed());
2349              if (p != null)
2350                c.getPieces().add(p);
2351            }
2352          } else if (definition.hasPattern()) {
2353            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2354            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, translate("sd.table", "Required Pattern")+": ", null).addStyle("font-weight:bold")));
2355            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, buildJson(definition.getPattern()), null).addStyle("color: darkgreen")));
2356          } else if (definition.hasExample()) {
2357            for (ElementDefinitionExampleComponent ex : definition.getExample()) {
2358              if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2359              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, translate("sd.table", "Example")+("".equals("General")? "" : " "+ex.getLabel()+"'")+": ", null).addStyle("font-weight:bold")));
2360              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, buildJson(ex.getValue()), null).addStyle("color: darkgreen")));
2361            }
2362          }
2363          if (definition.hasMaxLength() && definition.getMaxLength()!=0) {
2364            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2365            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, "Max Length: ", null).addStyle("font-weight:bold")));
2366            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, Integer.toString(definition.getMaxLength()), null).addStyle("color: darkgreen")));
2367          }
2368          if (profile != null) {
2369            for (StructureDefinitionMappingComponent md : profile.getMapping()) {
2370              if (md.hasExtension(ToolingExtensions.EXT_TABLE_NAME)) {
2371                ElementDefinitionMappingComponent map = null;
2372                for (ElementDefinitionMappingComponent m : definition.getMapping()) 
2373                  if (m.getIdentity().equals(md.getIdentity()))
2374                    map = m;
2375                if (map != null) {
2376                  for (int i = 0; i<definition.getMapping().size(); i++){
2377                    c.addPiece(gen.new Piece("br"));
2378                    c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(md, ToolingExtensions.EXT_TABLE_NAME)+": " + map.getMap(), null));
2379                  }
2380                }
2381              }
2382            }
2383          }
2384        }
2385      }
2386    }
2387    return c;
2388  }
2389
2390  private Piece describeCoded(HierarchicalTableGenerator gen, Type fixed) {
2391    if (fixed instanceof Coding) {
2392      Coding c = (Coding) fixed;
2393      ValidationResult vr = context.validateCode(c.getSystem(), c.getCode(), c.getDisplay());
2394      if (vr.getDisplay() != null)
2395        return gen.new Piece(null, " ("+vr.getDisplay()+")", null).addStyle("color: darkgreen");
2396    } else if (fixed instanceof CodeableConcept) {
2397      CodeableConcept cc = (CodeableConcept) fixed;
2398      for (Coding c : cc.getCoding()) {
2399        ValidationResult vr = context.validateCode(c.getSystem(), c.getCode(), c.getDisplay());
2400        if (vr.getDisplay() != null)
2401          return gen.new Piece(null, " ("+vr.getDisplay()+")", null).addStyle("color: darkgreen");
2402      }
2403    }
2404    return null;
2405  }
2406
2407
2408  private boolean hasDescription(Type fixed) {
2409    if (fixed instanceof Coding) {
2410      return ((Coding) fixed).hasDisplay();
2411    } else if (fixed instanceof CodeableConcept) {
2412      CodeableConcept cc = (CodeableConcept) fixed;
2413      if (cc.hasText())
2414        return true;
2415      for (Coding c : cc.getCoding())
2416        if (c.hasDisplay())
2417         return true;
2418    } // (fixed instanceof CodeType) || (fixed instanceof Quantity);
2419    return false;
2420  }
2421
2422
2423  private boolean isCoded(Type fixed) {
2424    return (fixed instanceof Coding) || (fixed instanceof CodeableConcept) || (fixed instanceof CodeType) || (fixed instanceof Quantity);
2425  }
2426
2427
2428  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 {
2429    Cell c = gen.new Cell();
2430    row.getCells().add(c);
2431
2432    if (used) {
2433      if (definition.hasContentReference()) {
2434        ElementDefinition ed = getElementByName(profile.getSnapshot().getElement(), definition.getContentReference());
2435        if (ed == null)
2436          c.getPieces().add(gen.new Piece(null, "Unknown reference to "+definition.getContentReference(), null));
2437        else
2438          c.getPieces().add(gen.new Piece("#"+ed.getPath(), "See "+ed.getPath(), null));
2439      }
2440      if (definition.getPath().endsWith("url") && definition.hasFixed()) {
2441        c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "\""+buildJson(definition.getFixed())+"\"", null).addStyle("color: darkgreen")));
2442      } else {
2443        if (url != null) {
2444          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2445          String fullUrl = url.startsWith("#") ? baseURL+url : url;
2446          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
2447          String ref = null;
2448          if (ed != null) {
2449            String p = ed.getUserString("path");
2450            if (p != null) {
2451              ref = p.startsWith("http:") || igmode ? p : Utilities.pathURL(corePath, p);
2452            }
2453          }
2454          c.getPieces().add(gen.new Piece(null, "URL: ", null).addStyle("font-weight:bold"));
2455          c.getPieces().add(gen.new Piece(ref, fullUrl, null));
2456        }
2457
2458        if (definition.hasSlicing()) {
2459          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2460          c.getPieces().add(gen.new Piece(null, "Slice: ", null).addStyle("font-weight:bold"));
2461          c.getPieces().add(gen.new Piece(null, describeSlice(definition.getSlicing()), null));
2462        }
2463        if (definition != null) {
2464          ElementDefinitionBindingComponent binding = null;
2465          if (valueDefn != null && valueDefn.hasBinding() && !valueDefn.getBinding().isEmpty())
2466            binding = valueDefn.getBinding();
2467          else if (definition.hasBinding())
2468            binding = definition.getBinding();
2469          if (binding!=null && !binding.isEmpty()) {
2470            if (!c.getPieces().isEmpty()) 
2471              c.addPiece(gen.new Piece("br"));
2472            BindingResolution br = pkp.resolveBinding(profile, binding, definition.getPath());
2473            c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, "Binding: ", null).addStyle("font-weight:bold")));
2474            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)));
2475            if (binding.hasStrength()) {
2476              c.getPieces().add(checkForNoChange(binding, gen.new Piece(null, " (", null)));
2477              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));
2478            }
2479          }
2480          for (ElementDefinitionConstraintComponent inv : definition.getConstraint()) {
2481            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2482            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getKey()+": ", null).addStyle("font-weight:bold")));
2483            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getHuman(), null)));
2484          }
2485          if (definition.hasFixed()) {
2486            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2487            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "Fixed Value: ", null).addStyle("font-weight:bold")));
2488            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, buildJson(definition.getFixed()), null).addStyle("color: darkgreen")));
2489          } else if (definition.hasPattern()) {
2490            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2491            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, "Required Pattern: ", null).addStyle("font-weight:bold")));
2492            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, buildJson(definition.getPattern()), null).addStyle("color: darkgreen")));
2493          } else if (definition.hasExample()) {
2494            for (ElementDefinitionExampleComponent ex : definition.getExample()) {
2495              if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2496              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, "Example'"+("".equals("General")? "" : " "+ex.getLabel()+"'")+": ", null).addStyle("font-weight:bold")));
2497              c.getPieces().add(checkForNoChange(ex, gen.new Piece(null, buildJson(ex.getValue()), null).addStyle("color: darkgreen")));
2498            }
2499          }
2500          if (definition.hasMaxLength() && definition.getMaxLength()!=0) {
2501            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2502            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, "Max Length: ", null).addStyle("font-weight:bold")));
2503            c.getPieces().add(checkForNoChange(definition.getMaxLengthElement(), gen.new Piece(null, Integer.toString(definition.getMaxLength()), null).addStyle("color: darkgreen")));
2504          }
2505          if (profile != null) {
2506            for (StructureDefinitionMappingComponent md : profile.getMapping()) {
2507              if (md.hasExtension(ToolingExtensions.EXT_TABLE_NAME)) {
2508                ElementDefinitionMappingComponent map = null;
2509                for (ElementDefinitionMappingComponent m : definition.getMapping()) 
2510                  if (m.getIdentity().equals(md.getIdentity()))
2511                    map = m;
2512                if (map != null) {
2513                  for (int i = 0; i<definition.getMapping().size(); i++){
2514                    c.addPiece(gen.new Piece("br"));
2515                    c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(md, ToolingExtensions.EXT_TABLE_NAME)+": " + map.getMap(), null));
2516                  }
2517                }
2518              }
2519            }
2520          }
2521          if (definition.getComment()!=null) {
2522            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
2523            c.getPieces().add(gen.new Piece(null, "Comments: ", null).addStyle("font-weight:bold"));
2524            c.addPiece(gen.new Piece("br"));
2525            c.addMarkdown(definition.getComment());
2526//            c.getPieces().add(checkForNoChange(definition.getCommentElement(), gen.new Piece(null, definition.getComment(), null)));
2527          }
2528        }
2529      }
2530    }
2531    return c;
2532  }
2533  /*
2534  private List<Piece> markdownToPieces(String markdown) throws FHIRException {
2535    String htmlString = Processor.process(markdown);
2536    XhtmlParser parser = new XhtmlParser();
2537    try {
2538      XhtmlNode node = parser.parseFragment(htmlString);
2539      return htmlToPieces(node);
2540    } catch (IOException e) {
2541    }
2542    return null;
2543  }
2544
2545  private List<Piece> htmlToPieces(XhtmlNode n) {
2546    
2547  }*/
2548
2549  private String buildJson(Type value) throws IOException {
2550    if (value instanceof PrimitiveType)
2551      return ((PrimitiveType) value).asStringValue();
2552
2553    IParser json = context.newJsonParser();
2554    return json.composeString(value, null);
2555  }
2556
2557
2558  public String describeSlice(ElementDefinitionSlicingComponent slicing) {
2559    return translate("sd.table", "%s, %s by %s", slicing.getOrdered() ? translate("sd.table", "Ordered") : translate("sd.table", "Unordered"), describe(slicing.getRules()), commas(slicing.getDiscriminator()));
2560  }
2561
2562  private String commas(List<ElementDefinitionSlicingDiscriminatorComponent> list) {
2563    CommaSeparatedStringBuilder c = new CommaSeparatedStringBuilder();
2564    for (ElementDefinitionSlicingDiscriminatorComponent id : list)
2565      c.append(id.getType().toCode()+":"+id.getPath());
2566    return c.toString();
2567  }
2568
2569
2570  private String describe(SlicingRules rules) {
2571    if (rules == null)
2572      return translate("sd.table", "Unspecified");
2573    switch (rules) {
2574    case CLOSED : return translate("sd.table", "Closed");
2575    case OPEN : return translate("sd.table", "Open");
2576    case OPENATEND : return translate("sd.table", "Open At End");
2577    default:
2578      return "??";
2579    }
2580  }
2581
2582  private boolean onlyInformationIsMapping(List<ElementDefinition> list, ElementDefinition e) {
2583    return (!e.hasSliceName() && !e.hasSlicing() && (onlyInformationIsMapping(e))) &&
2584        getChildren(list, e).isEmpty();
2585  }
2586
2587  private boolean onlyInformationIsMapping(ElementDefinition d) {
2588    return !d.hasShort() && !d.hasDefinition() &&
2589        !d.hasRequirements() && !d.getAlias().isEmpty() && !d.hasMinElement() &&
2590        !d.hasMax() && !d.getType().isEmpty() && !d.hasContentReference() &&
2591        !d.hasExample() && !d.hasFixed() && !d.hasMaxLengthElement() &&
2592        !d.getCondition().isEmpty() && !d.getConstraint().isEmpty() && !d.hasMustSupportElement() &&
2593        !d.hasBinding();
2594  }
2595
2596  private boolean allTypesAre(List<TypeRefComponent> types, String name) {
2597    for (TypeRefComponent t : types) {
2598      if (!t.getCode().equals(name))
2599        return false;
2600    }
2601    return true;
2602  }
2603
2604  private List<ElementDefinition> getChildren(List<ElementDefinition> all, ElementDefinition element) {
2605    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
2606    int i = all.indexOf(element)+1;
2607    while (i < all.size() && all.get(i).getPath().length() > element.getPath().length()) {
2608      if ((all.get(i).getPath().substring(0, element.getPath().length()+1).equals(element.getPath()+".")) && !all.get(i).getPath().substring(element.getPath().length()+1).contains("."))
2609        result.add(all.get(i));
2610      i++;
2611    }
2612    return result;
2613  }
2614
2615  private String tail(String path) {
2616    if (path.contains("."))
2617      return path.substring(path.lastIndexOf('.')+1);
2618    else
2619      return path;
2620  }
2621
2622  private boolean isDataType(String value) {
2623    StructureDefinition sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+value);
2624    return sd != null && sd.getKind() == StructureDefinitionKind.COMPLEXTYPE;
2625  }
2626
2627  private boolean isReference(String value) {
2628    return "Reference".equals(value);
2629  }
2630
2631  public boolean isPrimitive(String value) {
2632    StructureDefinition sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+value);
2633    return sd != null && sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE;
2634  }
2635
2636//  private static String listStructures(StructureDefinition p) {
2637//    StringBuilder b = new StringBuilder();
2638//    boolean first = true;
2639//    for (ProfileStructureComponent s : p.getStructure()) {
2640//      if (first)
2641//        first = false;
2642//      else
2643//        b.append(", ");
2644//      if (pkp != null && pkp.hasLinkFor(s.getType()))
2645//        b.append("<a href=\""+pkp.getLinkFor(s.getType())+"\">"+s.getType()+"</a>");
2646//      else
2647//        b.append(s.getType());
2648//    }
2649//    return b.toString();
2650//  }
2651
2652
2653  public StructureDefinition getProfile(StructureDefinition source, String url) {
2654        StructureDefinition profile = null;
2655        String code = null;
2656        if (url.startsWith("#")) {
2657                profile = source;
2658                code = url.substring(1);
2659        } else if (context != null) {
2660                String[] parts = url.split("\\#");
2661                profile = context.fetchResource(StructureDefinition.class, parts[0]);
2662      code = parts.length == 1 ? null : parts[1];
2663        }         
2664        if (profile == null)
2665                return null;
2666        if (code == null)
2667                return profile;
2668        for (Resource r : profile.getContained()) {
2669                if (r instanceof StructureDefinition && r.getId().equals(code))
2670                        return (StructureDefinition) r;
2671        }
2672        return null;
2673  }
2674
2675
2676
2677  public static class ElementDefinitionHolder {
2678    private String name;
2679    private ElementDefinition self;
2680    private int baseIndex = 0;
2681    private List<ElementDefinitionHolder> children;
2682
2683    public ElementDefinitionHolder(ElementDefinition self) {
2684      super();
2685      this.self = self;
2686      this.name = self.getPath();
2687      children = new ArrayList<ElementDefinitionHolder>();
2688    }
2689
2690    public ElementDefinition getSelf() {
2691      return self;
2692    }
2693
2694    public List<ElementDefinitionHolder> getChildren() {
2695      return children;
2696    }
2697
2698    public int getBaseIndex() {
2699      return baseIndex;
2700    }
2701
2702    public void setBaseIndex(int baseIndex) {
2703      this.baseIndex = baseIndex;
2704    }
2705
2706    @Override
2707    public String toString() {
2708      if (self.hasSliceName())
2709        return self.getPath()+"("+self.getSliceName()+")";
2710      else
2711        return self.getPath();
2712    }
2713  }
2714
2715  public static class ElementDefinitionComparer implements Comparator<ElementDefinitionHolder> {
2716
2717    private boolean inExtension;
2718    private List<ElementDefinition> snapshot;
2719    private int prefixLength;
2720    private String base;
2721    private String name;
2722    private Set<String> errors = new HashSet<String>();
2723
2724    public ElementDefinitionComparer(boolean inExtension, List<ElementDefinition> snapshot, String base, int prefixLength, String name) {
2725      this.inExtension = inExtension;
2726      this.snapshot = snapshot;
2727      this.prefixLength = prefixLength;
2728      this.base = base;
2729      this.name = name;
2730    }
2731
2732    @Override
2733    public int compare(ElementDefinitionHolder o1, ElementDefinitionHolder o2) {
2734      if (o1.getBaseIndex() == 0)
2735        o1.setBaseIndex(find(o1.getSelf().getPath()));
2736      if (o2.getBaseIndex() == 0)
2737        o2.setBaseIndex(find(o2.getSelf().getPath()));
2738      return o1.getBaseIndex() - o2.getBaseIndex();
2739    }
2740
2741    private int find(String path) {
2742      String actual = base+path.substring(prefixLength);
2743      for (int i = 0; i < snapshot.size(); i++) {
2744        String p = snapshot.get(i).getPath();
2745        if (p.equals(actual)) {
2746          return i;
2747        }
2748        if (p.endsWith("[x]") && actual.startsWith(p.substring(0, p.length()-3)) && !(actual.endsWith("[x]")) && !actual.substring(p.length()-3).contains(".")) {
2749          return i;
2750        }
2751        if (path.startsWith(p+".") && snapshot.get(i).hasContentReference()) {
2752          actual = base+(snapshot.get(i).getContentReference().substring(1)+"."+path.substring(p.length()+1)).substring(prefixLength);
2753          i = 0;
2754        }
2755      }
2756      if (prefixLength == 0)
2757        errors.add("Differential contains path "+path+" which is not found in the base");
2758      else
2759        errors.add("Differential contains path "+path+" which is actually "+actual+", which is not found in the base");
2760      return 0;
2761    }
2762
2763    public void checkForErrors(List<String> errorList) {
2764      if (errors.size() > 0) {
2765//        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
2766//        for (String s : errors)
2767//          b.append("StructureDefinition "+name+": "+s);
2768//        throw new DefinitionException(b.toString());
2769        for (String s : errors)
2770          if (s.startsWith("!"))
2771            errorList.add("!StructureDefinition "+name+": "+s.substring(1));
2772          else
2773            errorList.add("StructureDefinition "+name+": "+s);
2774      }
2775    }
2776  }
2777
2778
2779  public void sortDifferential(StructureDefinition base, StructureDefinition diff, String name, List<String> errors) throws FHIRException  {
2780
2781    final List<ElementDefinition> diffList = diff.getDifferential().getElement();
2782    // first, we move the differential elements into a tree
2783    if (diffList.isEmpty())
2784      return;
2785    ElementDefinitionHolder edh = new ElementDefinitionHolder(diffList.get(0));
2786
2787    boolean hasSlicing = false;
2788    List<String> paths = new ArrayList<String>(); // in a differential, slicing may not be stated explicitly
2789    for(ElementDefinition elt : diffList) {
2790      if (elt.hasSlicing() || paths.contains(elt.getPath())) {
2791        hasSlicing = true;
2792        break;
2793      }
2794      paths.add(elt.getPath());
2795    }
2796    if(!hasSlicing) {
2797      // if Differential does not have slicing then safe to pre-sort the list
2798      // so elements and subcomponents are together
2799      Collections.sort(diffList, new ElementNameCompare());
2800    }
2801
2802    int i = 1;
2803    processElementsIntoTree(edh, i, diff.getDifferential().getElement());
2804
2805    // now, we sort the siblings throughout the tree
2806    ElementDefinitionComparer cmp = new ElementDefinitionComparer(true, base.getSnapshot().getElement(), "", 0, name);
2807    sortElements(edh, cmp, errors);
2808
2809    // now, we serialise them back to a list
2810    diffList.clear();
2811    writeElements(edh, diffList);
2812  }
2813
2814  private int processElementsIntoTree(ElementDefinitionHolder edh, int i, List<ElementDefinition> list) {
2815    String path = edh.getSelf().getPath();
2816    final String prefix = path + ".";
2817    while (i < list.size() && list.get(i).getPath().startsWith(prefix)) {
2818      ElementDefinitionHolder child = new ElementDefinitionHolder(list.get(i));
2819      edh.getChildren().add(child);
2820      i = processElementsIntoTree(child, i+1, list);
2821    }
2822    return i;
2823  }
2824
2825  private void sortElements(ElementDefinitionHolder edh, ElementDefinitionComparer cmp, List<String> errors) throws FHIRException {
2826    if (edh.getChildren().size() == 1)
2827      // 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
2828      edh.getChildren().get(0).baseIndex = cmp.find(edh.getChildren().get(0).getSelf().getPath());
2829    else
2830      Collections.sort(edh.getChildren(), cmp);
2831    cmp.checkForErrors(errors);
2832
2833    for (ElementDefinitionHolder child : edh.getChildren()) {
2834      if (child.getChildren().size() > 0) {
2835        // what we have to check for here is running off the base profile into a data type profile
2836        ElementDefinition ed = cmp.snapshot.get(child.getBaseIndex());
2837        ElementDefinitionComparer ccmp;
2838        if (ed.getType().isEmpty() || isAbstract(ed.getType().get(0).getCode()) || ed.getType().get(0).getCode().equals(ed.getPath())) {
2839          ccmp = new ElementDefinitionComparer(true, cmp.snapshot, cmp.base, cmp.prefixLength, cmp.name);
2840        } else if (ed.getType().get(0).getCode().equals("Extension") && child.getSelf().getType().size() == 1 && child.getSelf().getType().get(0).hasProfile()) {
2841          StructureDefinition profile = context.fetchResource(StructureDefinition.class, child.getSelf().getType().get(0).getProfile());
2842          if (profile==null)
2843            ccmp = null; // this might happen before everything is loaded. And we don't so much care about sot order in this case
2844          else
2845          ccmp = new ElementDefinitionComparer(true, profile.getSnapshot().getElement(), ed.getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2846        } else if (ed.getType().size() == 1 && !ed.getType().get(0).getCode().equals("*")) {
2847          StructureDefinition profile = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+ed.getType().get(0).getCode());
2848          if (profile==null)
2849            throw new FHIRException("Unable to resolve profile " + "http://hl7.org/fhir/StructureDefinition/"+ed.getType().get(0).getCode() + " in element " + ed.getPath());
2850          ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), ed.getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2851        } else if (child.getSelf().getType().size() == 1) {
2852          StructureDefinition profile = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+child.getSelf().getType().get(0).getCode());
2853          if (profile==null)
2854            throw new FHIRException("Unable to resolve profile " + "http://hl7.org/fhir/StructureDefinition/"+ed.getType().get(0).getCode() + " in element " + ed.getPath());
2855          ccmp = new ElementDefinitionComparer(false, profile.getSnapshot().getElement(), child.getSelf().getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2856        } else if (ed.getPath().endsWith("[x]") && !child.getSelf().getPath().endsWith("[x]")) {
2857          String edLastNode = ed.getPath().replaceAll("(.*\\.)*(.*)", "$2");
2858          String childLastNode = child.getSelf().getPath().replaceAll("(.*\\.)*(.*)", "$2");
2859          String p = childLastNode.substring(edLastNode.length()-3);
2860          StructureDefinition sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+p);
2861          if (sd == null)
2862            throw new Error("Unable to find profile "+p);
2863          ccmp = new ElementDefinitionComparer(false, sd.getSnapshot().getElement(), p, child.getSelf().getPath().length(), cmp.name);
2864        } else {
2865          throw new Error("Not handled yet (sortElements: "+ed.getPath()+":"+typeCode(ed.getType())+")");
2866        }
2867        if (ccmp != null)
2868        sortElements(child, ccmp, errors);
2869      }
2870    }
2871  }
2872
2873  private boolean isAbstract(String code) {
2874    return code.equals("Element") || code.equals("BackboneElement") || code.equals("Resource") || code.equals("DomainResource");
2875  }
2876
2877
2878  private void writeElements(ElementDefinitionHolder edh, List<ElementDefinition> list) {
2879    list.add(edh.getSelf());
2880    for (ElementDefinitionHolder child : edh.getChildren()) {
2881      writeElements(child, list);
2882    }
2883  }
2884
2885  /**
2886   * First compare element by path then by name if same
2887   */
2888  private static class ElementNameCompare implements Comparator<ElementDefinition> {
2889
2890    @Override
2891    public int compare(ElementDefinition o1, ElementDefinition o2) {
2892      String path1 = normalizePath(o1);
2893      String path2 = normalizePath(o2);
2894      int cmp = path1.compareTo(path2);
2895      if (cmp == 0) {
2896        String name1 = o1.hasSliceName() ? o1.getSliceName() : "";
2897        String name2 = o2.hasSliceName() ? o2.getSliceName() : "";
2898        cmp = name1.compareTo(name2);
2899      }
2900      return cmp;
2901    }
2902
2903    private static String normalizePath(ElementDefinition e) {
2904      if (!e.hasPath()) return "";
2905      String path = e.getPath();
2906      // if sorting element names make sure onset[x] appears before onsetAge, onsetDate, etc.
2907      // so strip off the [x] suffix when comparing the path names.
2908      if (path.endsWith("[x]")) {
2909        path = path.substring(0, path.length()-3);
2910      }
2911      return path;
2912    }
2913
2914  }
2915
2916
2917  // generate schematrons for the rules in a structure definition
2918  public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
2919    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
2920      throw new DefinitionException("not the right kind of structure to generate schematrons for");
2921    if (!structure.hasSnapshot())
2922      throw new DefinitionException("needs a snapshot");
2923
2924        StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
2925
2926        SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
2927
2928    ElementDefinition ed = structure.getSnapshot().getElement().get(0);
2929    generateForChildren(sch, "f:"+ed.getPath(), ed, structure, base);
2930    sch.dump();
2931  }
2932
2933  // generate a CSV representation of the structure definition
2934  public void generateCsvs(OutputStream dest, StructureDefinition structure, boolean asXml) throws IOException, DefinitionException, Exception {
2935    if (!structure.hasSnapshot())
2936      throw new DefinitionException("needs a snapshot");
2937
2938    CSVWriter csv = new CSVWriter(dest, structure, asXml);
2939
2940    for (ElementDefinition child : structure.getSnapshot().getElement()) {
2941      csv.processElement(child);
2942    }
2943    csv.dump();
2944  }
2945  
2946  private class Slicer extends ElementDefinitionSlicingComponent {
2947    String criteria = "";
2948    String name = "";   
2949    boolean check;
2950    public Slicer(boolean cantCheck) {
2951      super();
2952      this.check = cantCheck;
2953    }
2954  }
2955  
2956  private Slicer generateSlicer(ElementDefinition child, ElementDefinitionSlicingComponent slicing, StructureDefinition structure) {
2957    // given a child in a structure, it's sliced. figure out the slicing xpath
2958    if (child.getPath().endsWith(".extension")) {
2959      ElementDefinition ued = getUrlFor(structure, child);
2960      if ((ued == null || !ued.hasFixed()) && !(child.hasType() && (child.getType().get(0).hasProfile())))
2961        return new Slicer(false);
2962      else {
2963      Slicer s = new Slicer(true);
2964      String url = (ued == null || !ued.hasFixed()) ? child.getType().get(0).getProfile() : ((UriType) ued.getFixed()).asStringValue();
2965      s.name = " with URL = '"+url+"'";
2966      s.criteria = "[@url = '"+url+"']";
2967      return s;
2968      }
2969    } else
2970      return new Slicer(false);
2971  }
2972
2973  private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
2974    //    generateForChild(txt, structure, child);
2975    List<ElementDefinition> children = getChildList(structure, ed);
2976    String sliceName = null;
2977    ElementDefinitionSlicingComponent slicing = null;
2978    for (ElementDefinition child : children) {
2979      String name = tail(child.getPath());
2980      if (child.hasSlicing()) {
2981        sliceName = name;
2982        slicing = child.getSlicing();        
2983      } else if (!name.equals(sliceName))
2984        slicing = null;
2985      
2986      ElementDefinition based = getByPath(base, child.getPath());
2987      boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
2988      boolean doMax = child.hasMax() && !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
2989      Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
2990      if (slicer.check) {
2991        if (doMin || doMax) {
2992          Section s = sch.section(xpath);
2993          Rule r = s.rule(xpath);
2994          if (doMin) 
2995            r.assrt("count(f:"+name+slicer.criteria+") >= "+Integer.toString(child.getMin()), name+slicer.name+": minimum cardinality of '"+name+"' is "+Integer.toString(child.getMin()));
2996          if (doMax) 
2997            r.assrt("count(f:"+name+slicer.criteria+") <= "+child.getMax(), name+slicer.name+": maximum cardinality of '"+name+"' is "+child.getMax());
2998          }
2999        }
3000      }
3001    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
3002      if (inv.hasXpath()) {
3003        Section s = sch.section(ed.getPath());
3004        Rule r = s.rule(xpath);
3005        r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId()+": " : "")+inv.getHuman()+(inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
3006      }
3007    }
3008    for (ElementDefinition child : children) {
3009      String name = tail(child.getPath());
3010      generateForChildren(sch, xpath+"/f:"+name, child, structure, base);
3011    }
3012  }
3013
3014
3015
3016
3017  private ElementDefinition getByPath(StructureDefinition base, String path) {
3018                for (ElementDefinition ed : base.getSnapshot().getElement()) {
3019                        if (ed.getPath().equals(path))
3020                                return ed;
3021                        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)))
3022                                return ed;
3023    }
3024          return null;
3025  }
3026
3027
3028  public void setIds(StructureDefinition sd, boolean checkFirst) throws DefinitionException  {
3029    if (!checkFirst || !sd.hasDifferential() || hasMissingIds(sd.getDifferential().getElement())) {
3030      if (!sd.hasDifferential())
3031        sd.setDifferential(new StructureDefinitionDifferentialComponent());
3032      generateIds(sd.getDifferential().getElement(), sd.getName());
3033    }
3034    if (!checkFirst || !sd.hasSnapshot() || hasMissingIds(sd.getSnapshot().getElement())) {
3035      if (!sd.hasSnapshot())
3036        sd.setSnapshot(new StructureDefinitionSnapshotComponent());
3037      generateIds(sd.getSnapshot().getElement(), sd.getName());
3038    }
3039  }
3040
3041
3042  private boolean hasMissingIds(List<ElementDefinition> list) {
3043    for (ElementDefinition ed : list) {
3044      if (!ed.hasId())
3045        return true;
3046    }    
3047    return false;
3048  }
3049
3050
3051  private void generateIds(List<ElementDefinition> list, String name) throws DefinitionException  {
3052    if (list.isEmpty())
3053      return;
3054    
3055    Map<String, String> idMap = new HashMap<String, String>();
3056    
3057    List<String> paths = new ArrayList<String>();
3058    // first pass, update the element ids
3059    for (ElementDefinition ed : list) {
3060      if (!ed.hasPath())
3061        throw new DefinitionException("No path on element Definition "+Integer.toString(list.indexOf(ed))+" in "+name);
3062      int depth = charCount(ed.getPath(), '.');
3063      String tail = tail(ed.getPath());
3064
3065      if (depth > paths.size()) {
3066        // this means that we've jumped into a sparse thing. 
3067        String[] pl = ed.getPath().split("\\.");
3068        for (int i = paths.size(); i < pl.length-1; i++) // -1 because the last path is in focus
3069          paths.add(pl[i]);
3070      }
3071      while (depth < paths.size() && paths.size() > 0)
3072        paths.remove(paths.size() - 1);
3073      
3074      String t = ed.hasSliceName() ? tail+":"+checkName(ed.getSliceName()) : /* why do this? name != null ? tail + ":"+checkName(name) : */ tail;
3075//      if (isExtension(ed))
3076//        t = t + describeExtension(ed);
3077      name = null;
3078      StringBuilder b = new StringBuilder();
3079      for (String s : paths) {
3080        b.append(s);
3081        b.append(".");
3082      }
3083      b.append(t);
3084      String bs = b.toString();
3085      idMap.put(ed.hasId() ? ed.getId() : ed.getPath(), bs);
3086      ed.setId(bs);
3087      paths.add(t);
3088      if (ed.hasContentReference()) {
3089        String s = ed.getContentReference().substring(1);
3090        if (idMap.containsKey(s))
3091          ed.setContentReference("#"+idMap.get(s));
3092        
3093      }
3094    }  
3095    // second path - fix up any broken path based id references
3096    
3097  }
3098
3099
3100//  private String describeExtension(ElementDefinition ed) {
3101//    if (!ed.hasType() || !ed.getTypeFirstRep().hasProfile())
3102//      return "";
3103//    return "$"+urlTail(ed.getTypeFirstRep().getProfile());
3104//  }
3105//
3106
3107  private String urlTail(String profile) {
3108    return profile.contains("/") ? profile.substring(profile.lastIndexOf("/")+1) : profile;
3109  }
3110
3111
3112  private String checkName(String name) {
3113//    if (name.contains("."))
3114////      throw new Exception("Illegal name "+name+": no '.'");
3115//    if (name.contains(" "))
3116//      throw new Exception("Illegal name "+name+": no spaces");
3117    StringBuilder b = new StringBuilder();
3118    for (char c : name.toCharArray()) {
3119      if (!Utilities.existsInList(c, '.', ' ', ':', '"', '\'', '(', ')', '&', '[', ']'))
3120        b.append(c);
3121    }
3122    return b.toString().toLowerCase();
3123  }
3124
3125
3126  private int charCount(String path, char t) {
3127    int res = 0;
3128    for (char ch : path.toCharArray()) {
3129      if (ch == t)
3130        res++;
3131    }
3132    return res;
3133  }
3134
3135//
3136//private void generateForChild(TextStreamWriter txt,
3137//    StructureDefinition structure, ElementDefinition child) {
3138//  // TODO Auto-generated method stub
3139//
3140//}
3141
3142  private interface ExampleValueAccessor {
3143    Type getExampleValue(ElementDefinition ed);
3144    String getId();
3145  }
3146
3147  private class BaseExampleValueAccessor implements ExampleValueAccessor {
3148    @Override
3149    public Type getExampleValue(ElementDefinition ed) {
3150      if (ed.hasFixed())
3151        return ed.getFixed();
3152      if (ed.hasExample())
3153        return ed.getExample().get(0).getValue();
3154      else
3155        return null;
3156    }
3157
3158    @Override
3159    public String getId() {
3160      return "-genexample";
3161    }
3162  }
3163  
3164  private class ExtendedExampleValueAccessor implements ExampleValueAccessor {
3165    private String index;
3166
3167    public ExtendedExampleValueAccessor(String index) {
3168      this.index = index;
3169    }
3170    @Override
3171    public Type getExampleValue(ElementDefinition ed) {
3172      if (ed.hasFixed())
3173        return ed.getFixed();
3174      for (Extension ex : ed.getExtension()) {
3175       String ndx = ToolingExtensions.readStringExtension(ex, "index");
3176       Type value = ToolingExtensions.getExtension(ex, "exValue").getValue();
3177       if (index.equals(ndx) && value != null)
3178         return value;
3179      }
3180      return null;
3181    }
3182    @Override
3183    public String getId() {
3184      return "-genexample-"+index;
3185    }
3186  }
3187  
3188  public List<org.hl7.fhir.dstu3.elementmodel.Element> generateExamples(StructureDefinition sd, boolean evenWhenNoExamples) throws FHIRException {
3189    List<org.hl7.fhir.dstu3.elementmodel.Element> examples = new ArrayList<org.hl7.fhir.dstu3.elementmodel.Element>();
3190    if (sd.hasSnapshot()) {
3191      if (evenWhenNoExamples || hasAnyExampleValues(sd)) 
3192        examples.add(generateExample(sd, new BaseExampleValueAccessor()));
3193      for (int i = 1; i <= 50; i++) {
3194        if (hasAnyExampleValues(sd, Integer.toString(i))) 
3195          examples.add(generateExample(sd, new ExtendedExampleValueAccessor(Integer.toString(i))));
3196      }
3197    }
3198    return examples;
3199  }
3200
3201  private org.hl7.fhir.dstu3.elementmodel.Element generateExample(StructureDefinition profile, ExampleValueAccessor accessor) throws FHIRException {
3202    ElementDefinition ed = profile.getSnapshot().getElementFirstRep();
3203    org.hl7.fhir.dstu3.elementmodel.Element r = new org.hl7.fhir.dstu3.elementmodel.Element(ed.getPath(), new Property(context, ed, profile));
3204    List<ElementDefinition> children = getChildMap(profile, ed);
3205    for (ElementDefinition child : children) {
3206      if (child.getPath().endsWith(".id")) {
3207        org.hl7.fhir.dstu3.elementmodel.Element id = new org.hl7.fhir.dstu3.elementmodel.Element("id", new Property(context, child, profile));
3208        id.setValue(profile.getId()+accessor.getId());
3209        r.getChildren().add(id);
3210      } else { 
3211        org.hl7.fhir.dstu3.elementmodel.Element e = createExampleElement(profile, child, accessor);
3212        if (e != null)
3213          r.getChildren().add(e);
3214      }
3215    }
3216    return r;
3217  }
3218
3219  private org.hl7.fhir.dstu3.elementmodel.Element createExampleElement(StructureDefinition profile, ElementDefinition ed, ExampleValueAccessor accessor) throws FHIRException {
3220    Type v = accessor.getExampleValue(ed);
3221    if (v != null) {
3222      return new ObjectConverter(context).convert(new Property(context, ed, profile), v);
3223    } else {
3224      org.hl7.fhir.dstu3.elementmodel.Element res = new org.hl7.fhir.dstu3.elementmodel.Element(tail(ed.getPath()), new Property(context, ed, profile));
3225      boolean hasValue = false;
3226      List<ElementDefinition> children = getChildMap(profile, ed);
3227      for (ElementDefinition child : children) {
3228        if (!child.hasContentReference()) {
3229        org.hl7.fhir.dstu3.elementmodel.Element e = createExampleElement(profile, child, accessor);
3230        if (e != null) {
3231          hasValue = true;
3232          res.getChildren().add(e);
3233        }
3234      }
3235      }
3236      if (hasValue)
3237        return res;
3238      else
3239        return null;
3240    }
3241  }
3242
3243  private boolean hasAnyExampleValues(StructureDefinition sd, String index) {
3244    for (ElementDefinition ed : sd.getSnapshot().getElement())
3245      for (Extension ex : ed.getExtension()) {
3246        String ndx = ToolingExtensions.readStringExtension(ex, "index");
3247        Extension exv = ToolingExtensions.getExtension(ex, "exValue");
3248        if (exv != null) {
3249          Type value = exv.getValue();
3250        if (index.equals(ndx) && value != null)
3251          return true;
3252        }
3253       }
3254    return false;
3255  }
3256
3257
3258  private boolean hasAnyExampleValues(StructureDefinition sd) {
3259    for (ElementDefinition ed : sd.getSnapshot().getElement())
3260      if (ed.hasExample())
3261        return true;
3262    return false;
3263  }
3264
3265
3266  public void populateLogicalSnapshot(StructureDefinition sd) throws FHIRException {
3267    sd.getSnapshot().getElement().add(sd.getDifferential().getElementFirstRep().copy());
3268    
3269    if (sd.hasBaseDefinition()) {
3270    StructureDefinition base = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
3271    if (base == null)
3272      throw new FHIRException("Unable to find base definition for logical model: "+sd.getBaseDefinition()+" from "+sd.getUrl());
3273    copyElements(sd, base.getSnapshot().getElement());
3274    }
3275    copyElements(sd, sd.getDifferential().getElement());
3276  }
3277
3278
3279  private void copyElements(StructureDefinition sd, List<ElementDefinition> list) {
3280    for (ElementDefinition ed : list) {
3281      if (ed.getPath().contains(".")) {
3282        ElementDefinition n = ed.copy();
3283        n.setPath(sd.getSnapshot().getElementFirstRep().getPath()+"."+ed.getPath().substring(ed.getPath().indexOf(".")+1));
3284        sd.getSnapshot().addElement(n);
3285      }
3286    }
3287  }
3288
3289    
3290  public void cleanUpDifferential(StructureDefinition sd) {
3291    if (sd.getDifferential().getElement().size() > 1)
3292      cleanUpDifferential(sd, 1);
3293  }
3294  
3295  private void cleanUpDifferential(StructureDefinition sd, int start) {
3296    int level = Utilities.charCount(sd.getDifferential().getElement().get(start).getPath(), '.');
3297    int c = start;
3298    int len = sd.getDifferential().getElement().size();
3299    HashSet<String> paths = new HashSet<String>();
3300    while (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') == level) {
3301      ElementDefinition ed = sd.getDifferential().getElement().get(c);
3302      if (!paths.contains(ed.getPath())) {
3303        paths.add(ed.getPath());
3304        int ic = c+1; 
3305        while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') > level) 
3306          ic++;
3307        ElementDefinition slicer = null;
3308        List<ElementDefinition> slices = new ArrayList<ElementDefinition>();
3309        slices.add(ed);
3310        while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') == level) {
3311          ElementDefinition edi = sd.getDifferential().getElement().get(ic);
3312          if (ed.getPath().equals(edi.getPath())) {
3313            if (slicer == null) {
3314              slicer = new ElementDefinition();
3315              slicer.setPath(edi.getPath());
3316              slicer.getSlicing().setRules(SlicingRules.OPEN);
3317              sd.getDifferential().getElement().add(c, slicer);
3318              c++;
3319              ic++;
3320            }
3321            slices.add(edi);
3322          }
3323          ic++;
3324          while (ic < len && Utilities.charCount(sd.getDifferential().getElement().get(ic).getPath(), '.') > level) 
3325            ic++;
3326        }
3327        // now we're at the end, we're going to figure out the slicing discriminator
3328        if (slicer != null)
3329          determineSlicing(slicer, slices);
3330      }
3331      c++;
3332      if (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') > level) {
3333        cleanUpDifferential(sd, c);
3334        c++;
3335        while (c < len && Utilities.charCount(sd.getDifferential().getElement().get(c).getPath(), '.') > level) 
3336          c++;
3337      }
3338  }
3339  }
3340
3341
3342  private void determineSlicing(ElementDefinition slicer, List<ElementDefinition> slices) {
3343    // first, name them
3344    int i = 0;
3345    for (ElementDefinition ed : slices) {
3346      if (ed.hasUserData("slice-name")) {
3347        ed.setSliceName(ed.getUserString("slice-name"));
3348      } else {
3349        i++;
3350        ed.setSliceName("slice-"+Integer.toString(i));
3351      }
3352    }
3353    // now, the hard bit, how are they differentiated? 
3354    // right now, we hard code this...
3355    if (slicer.getPath().endsWith(".extension") || slicer.getPath().endsWith(".modifierExtension"))
3356      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("url");
3357    else if (slicer.getPath().equals("DiagnosticReport.result"))
3358      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("reference.code");
3359    else if (slicer.getPath().equals("Observation.related"))
3360      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("target.reference.code");
3361    else if (slicer.getPath().equals("Bundle.entry"))
3362      slicer.getSlicing().addDiscriminator().setType(DiscriminatorType.VALUE).setPath("resource.@profile");
3363    else  
3364      throw new Error("No slicing for "+slicer.getPath()); 
3365  }
3366
3367  public class SpanEntry {
3368    private List<SpanEntry> children = new ArrayList<SpanEntry>();
3369    private boolean profile;
3370    private String id;
3371    private String name;
3372    private String resType;
3373    private String cardinality;
3374    private String description;
3375    private String profileLink;
3376    private String resLink;
3377    private String type;
3378    
3379    public String getName() {
3380      return name;
3381    }
3382    public void setName(String name) {
3383      this.name = name;
3384    }
3385    public String getResType() {
3386      return resType;
3387    }
3388    public void setResType(String resType) {
3389      this.resType = resType;
3390    }
3391    public String getCardinality() {
3392      return cardinality;
3393    }
3394    public void setCardinality(String cardinality) {
3395      this.cardinality = cardinality;
3396    }
3397    public String getDescription() {
3398      return description;
3399    }
3400    public void setDescription(String description) {
3401      this.description = description;
3402    }
3403    public String getProfileLink() {
3404      return profileLink;
3405    }
3406    public void setProfileLink(String profileLink) {
3407      this.profileLink = profileLink;
3408    }
3409    public String getResLink() {
3410      return resLink;
3411    }
3412    public void setResLink(String resLink) {
3413      this.resLink = resLink;
3414    }
3415    public String getId() {
3416      return id;
3417    }
3418    public void setId(String id) {
3419      this.id = id;
3420    }
3421    public boolean isProfile() {
3422      return profile;
3423    }
3424    public void setProfile(boolean profile) {
3425      this.profile = profile;
3426    }
3427    public List<SpanEntry> getChildren() {
3428      return children;
3429    }
3430    public String getType() {
3431      return type;
3432    }
3433    public void setType(String type) {
3434      this.type = type;
3435    }
3436    
3437  }
3438
3439  public XhtmlNode generateSpanningTable(StructureDefinition profile, String imageFolder, boolean onlyConstraints, String constraintPrefix, Set<String> outputTracker) throws IOException, FHIRException {
3440    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, false);
3441    gen.setTranslator(getTranslator());
3442    TableModel model = initSpanningTable(gen, "", false);
3443    Set<String> processed = new HashSet<String>();
3444    SpanEntry span = buildSpanningTable("(focus)", "", profile, processed, onlyConstraints, constraintPrefix);
3445    
3446    genSpanEntry(gen, model.getRows(), span);
3447    return gen.generate(model, "", 0, outputTracker);
3448  }
3449
3450  private SpanEntry buildSpanningTable(String name, String cardinality, StructureDefinition profile, Set<String> processed, boolean onlyConstraints, String constraintPrefix) throws IOException {
3451    SpanEntry res = buildSpanEntryFromProfile(name, cardinality, profile);
3452    boolean wantProcess = !processed.contains(profile.getUrl());
3453    processed.add(profile.getUrl());
3454    if (wantProcess && profile.getDerivation() == TypeDerivationRule.CONSTRAINT) {
3455      for (ElementDefinition ed : profile.getSnapshot().getElement()) {
3456        if (!"0".equals(ed.getMax()) && ed.getType().size() > 0) {
3457          String card = getCardinality(ed, profile.getSnapshot().getElement());
3458          if (!card.endsWith(".0")) {
3459            List<String> refProfiles = listReferenceProfiles(ed);
3460            if (refProfiles.size() > 0) {
3461              String uri = refProfiles.get(0);
3462              if (uri != null) {
3463                StructureDefinition sd = context.fetchResource(StructureDefinition.class, uri);
3464                if (sd != null && (!onlyConstraints || (sd.getDerivation() == TypeDerivationRule.CONSTRAINT && (constraintPrefix == null || sd.getUrl().startsWith(constraintPrefix))))) {
3465                  res.getChildren().add(buildSpanningTable(nameForElement(ed), card, sd, processed, onlyConstraints, constraintPrefix));
3466                }
3467              }
3468            }
3469          }
3470        } 
3471      }
3472    }
3473    return res;
3474  }
3475
3476
3477  private String getCardinality(ElementDefinition ed, List<ElementDefinition> list) {
3478    int min = ed.getMin();
3479    int max = !ed.hasMax() || ed.getMax().equals("*") ? Integer.MAX_VALUE : Integer.parseInt(ed.getMax());
3480    while (ed != null && ed.getPath().contains(".")) {
3481      ed = findParent(ed, list);
3482      if (ed.getMax().equals("0"))
3483        max = 0;
3484      else if (!ed.getMax().equals("1") && !ed.hasSlicing())
3485        max = Integer.MAX_VALUE;
3486      if (ed.getMin() == 0)
3487        min = 0;
3488    }
3489    return Integer.toString(min)+".."+(max == Integer.MAX_VALUE ? "*" : Integer.toString(max));
3490  }
3491
3492
3493  private ElementDefinition findParent(ElementDefinition ed, List<ElementDefinition> list) {
3494    int i = list.indexOf(ed)-1;
3495    while (i >= 0 && !ed.getPath().startsWith(list.get(i).getPath()+"."))
3496      i--;
3497    if (i == -1)
3498      return null;
3499    else
3500      return list.get(i);
3501  }
3502
3503
3504  private List<String> listReferenceProfiles(ElementDefinition ed) {
3505    List<String> res = new ArrayList<String>();
3506    for (TypeRefComponent tr : ed.getType()) {
3507      // code is null if we're dealing with "value" and profile is null if we just have Reference()
3508      if (tr.getCode()!= null && "Reference".equals(tr.getCode()) && tr.getTargetProfile() != null)
3509        res.add(tr.getTargetProfile());
3510    }
3511    return res ;
3512  }
3513
3514
3515  private String nameForElement(ElementDefinition ed) {
3516    return ed.getPath().substring(ed.getPath().indexOf(".")+1);
3517  }
3518
3519
3520  private SpanEntry buildSpanEntryFromProfile(String name, String cardinality, StructureDefinition profile) throws IOException {
3521    SpanEntry res = new SpanEntry();
3522    res.setName(name);
3523    res.setCardinality(cardinality);
3524    res.setProfileLink(profile.getUserString("path"));
3525    res.setResType(profile.getType());
3526    StructureDefinition base = context.fetchResource(StructureDefinition.class, res.getResType());
3527    if (base != null)
3528      res.setResLink(base.getUserString("path"));
3529    res.setId(profile.getId());
3530    res.setProfile(profile.getDerivation() == TypeDerivationRule.CONSTRAINT);
3531    StringBuilder b = new StringBuilder();
3532    b.append(res.getResType());
3533    boolean first = true;
3534    boolean open = false;
3535    if (profile.getDerivation() == TypeDerivationRule.CONSTRAINT) {
3536      res.setDescription(profile.getName());
3537      for (ElementDefinition ed : profile.getSnapshot().getElement()) {
3538        if (isKeyProperty(ed.getBase().getPath()) && ed.hasFixed()) {
3539          if (first) {
3540            open = true;
3541            first = false;
3542            b.append("[");
3543          } else {
3544            b.append(", ");
3545          }
3546          b.append(tail(ed.getBase().getPath()));
3547          b.append("=");
3548          b.append(summarise(ed.getFixed()));
3549        }
3550      }
3551      if (open)
3552        b.append("]");
3553    } else
3554      res.setDescription("Base FHIR "+profile.getName());
3555    res.setType(b.toString());
3556    return res ;
3557  }
3558
3559
3560  private String summarise(Type value) throws IOException {
3561    if (value instanceof Coding)
3562      return summariseCoding((Coding) value);
3563    else if (value instanceof CodeableConcept)
3564      return summariseCodeableConcept((CodeableConcept) value);
3565    else
3566      return buildJson(value);
3567  }
3568
3569
3570  private String summariseCoding(Coding value) {
3571    String uri = value.getSystem();
3572    String system = NarrativeGenerator.describeSystem(uri);
3573    if (Utilities.isURL(system)) {
3574      if (system.equals("http://cap.org/protocols"))
3575        system = "CAP Code";
3576    }
3577    return system+" "+value.getCode();
3578  }
3579
3580
3581  private String summariseCodeableConcept(CodeableConcept value) {
3582    if (value.hasCoding())
3583      return summariseCoding(value.getCodingFirstRep());
3584    else
3585      return value.getText();
3586  }
3587
3588
3589  private boolean isKeyProperty(String path) {
3590    return Utilities.existsInList(path, "Observation.code");
3591  }
3592
3593
3594  public TableModel initSpanningTable(HierarchicalTableGenerator gen, String prefix, boolean isLogical) {
3595    TableModel model = gen.new TableModel();
3596    
3597    model.setDocoImg(prefix+"help16.png");
3598    model.setDocoRef(prefix+"formats.html#table"); // todo: change to graph definition
3599    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Property", "A profiled resource", null, 0));
3600    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));
3601    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Content", "What goes here", null, 0));
3602    model.getTitles().add(gen.new Title(null, model.getDocoRef(), "Description", "Description of the profile", null, 0));
3603    return model;
3604  }
3605
3606  private void genSpanEntry(HierarchicalTableGenerator gen, List<Row> rows, SpanEntry span) throws IOException {
3607    Row row = gen.new Row();
3608    rows.add(row);
3609    row.setAnchor(span.getId());
3610    //row.setColor(..?);
3611    if (span.isProfile()) 
3612      row.setIcon("icon_profile.png", HierarchicalTableGenerator.TEXT_ICON_PROFILE);
3613    else
3614      row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
3615    
3616    row.getCells().add(gen.new Cell(null, null, span.getName(), null, null));
3617    row.getCells().add(gen.new Cell(null, null, span.getCardinality(), null, null));
3618    row.getCells().add(gen.new Cell(null, span.getProfileLink(), span.getType(), null, null));
3619    row.getCells().add(gen.new Cell(null, null, span.getDescription(), null, null));
3620
3621    for (SpanEntry child : span.getChildren())
3622      genSpanEntry(gen, row.getSubRows(), child);
3623  }
3624
3625
3626  public static ElementDefinitionSlicingDiscriminatorComponent interpretR2Discriminator(String discriminator) {
3627    if (discriminator.endsWith("@profile"))
3628      return makeDiscriminator(DiscriminatorType.PROFILE, discriminator.length() == 8 ? "" : discriminator.substring(discriminator.length()-9)); 
3629    if (discriminator.endsWith("@type")) 
3630      return makeDiscriminator(DiscriminatorType.TYPE, discriminator.length() == 5 ? "" : discriminator.substring(discriminator.length()-6)); 
3631    return new ElementDefinitionSlicingDiscriminatorComponent().setType(DiscriminatorType.VALUE).setPath(discriminator);
3632  }
3633
3634
3635  public static ElementDefinitionSlicingDiscriminatorComponent makeDiscriminator(DiscriminatorType profile, String str) {
3636    return new ElementDefinitionSlicingDiscriminatorComponent().setType(DiscriminatorType.VALUE).setPath(Utilities.noString(str)? "$this" : str);
3637  }
3638
3639
3640  public static String buildR2Discriminator(ElementDefinitionSlicingDiscriminatorComponent t) throws FHIRException {
3641    switch (t.getType()) {
3642    case PROFILE: return t.getPath()+"/@profile";
3643    case TYPE: return t.getPath()+"/@type";
3644    case VALUE: return t.getPath();
3645    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
3646    default: throw new FHIRException("Unable to represent "+t.getType().toCode()+":"+t.getPath()+" in R2");    
3647    }
3648  }
3649
3650
3651
3652
3653
3654
3655}