001package org.hl7.fhir.dstu2016may.utils;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033import java.io.IOException;
034import java.io.OutputStream;
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.Comparator;
038import java.util.HashSet;
039import java.util.List;
040import java.util.Set;
041
042import org.hl7.fhir.dstu2016may.formats.IParser;
043import org.hl7.fhir.dstu2016may.model.Base;
044import org.hl7.fhir.dstu2016may.model.BooleanType;
045import org.hl7.fhir.dstu2016may.model.Coding;
046import org.hl7.fhir.dstu2016may.model.Element;
047import org.hl7.fhir.dstu2016may.model.ElementDefinition;
048import org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionBindingComponent;
049import org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionConstraintComponent;
050import org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionMappingComponent;
051import org.hl7.fhir.dstu2016may.model.ElementDefinition.ElementDefinitionSlicingComponent;
052import org.hl7.fhir.dstu2016may.model.ElementDefinition.SlicingRules;
053import org.hl7.fhir.dstu2016may.model.ElementDefinition.TypeRefComponent;
054import org.hl7.fhir.dstu2016may.model.Enumerations.BindingStrength;
055import org.hl7.fhir.dstu2016may.model.IntegerType;
056import org.hl7.fhir.dstu2016may.model.PrimitiveType;
057import org.hl7.fhir.dstu2016may.model.Reference;
058import org.hl7.fhir.dstu2016may.model.Resource;
059import org.hl7.fhir.dstu2016may.model.StringType;
060import org.hl7.fhir.dstu2016may.model.StructureDefinition;
061import org.hl7.fhir.dstu2016may.model.StructureDefinition.StructureDefinitionDifferentialComponent;
062import org.hl7.fhir.dstu2016may.model.StructureDefinition.StructureDefinitionSnapshotComponent;
063import org.hl7.fhir.dstu2016may.model.StructureDefinition.TypeDerivationRule;
064import org.hl7.fhir.dstu2016may.model.Type;
065import org.hl7.fhir.dstu2016may.model.UriType;
066import org.hl7.fhir.dstu2016may.model.ValueSet;
067import org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionComponent;
068import org.hl7.fhir.dstu2016may.model.ValueSet.ValueSetExpansionContainsComponent;
069import org.hl7.fhir.dstu2016may.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
070import org.hl7.fhir.dstu2016may.utils.ProfileUtilities.ProfileKnowledgeProvider.BindingResolution;
071import org.hl7.fhir.exceptions.DefinitionException;
072import org.hl7.fhir.exceptions.FHIRException;
073import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
074import org.hl7.fhir.utilities.Utilities;
075import org.hl7.fhir.utilities.validation.ValidationMessage;
076import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
077import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
078import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
079import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator;
080import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell;
081import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece;
082import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Row;
083import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel;
084import org.hl7.fhir.utilities.xhtml.XhtmlNode;
085import org.hl7.fhir.utilities.xml.SchematronWriter;
086import org.hl7.fhir.utilities.xml.SchematronWriter.Rule;
087import org.hl7.fhir.utilities.xml.SchematronWriter.SchematronType;
088import org.hl7.fhir.utilities.xml.SchematronWriter.Section;
089
090/**
091 * This class provides a set of utility operations for working with Profiles.
092 * Key functionality:
093 *  * getChildMap --?
094 *  * getChildList
095 *  * generateSnapshot: Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
096 *  * generateExtensionsTable: generate the HTML for a hierarchical table presentation of the extensions
097 *  * generateTable: generate  the HTML for a hierarchical table presentation of a structure
098 *  * summarise: describe the contents of a profile
099 * @author Grahame
100 *
101 */
102public class ProfileUtilities {
103
104  public class ExtensionContext {
105
106    private ElementDefinition element;
107    private StructureDefinition defn;
108
109    public ExtensionContext(StructureDefinition ext, ElementDefinition ed) {
110      this.defn = ext;
111      this.element = ed;
112    }
113
114    public ElementDefinition getElement() {
115      return element;
116    }
117
118    public StructureDefinition getDefn() {
119      return defn;
120    }
121
122    public String getUrl() {
123      if (element == defn.getSnapshot().getElement().get(0))
124        return defn.getUrl();
125      else
126        return element.getName();
127    }
128
129    public ElementDefinition getExtensionValueDefinition() {
130      int i = defn.getSnapshot().getElement().indexOf(element)+1;
131      while (i < defn.getSnapshot().getElement().size()) {
132        ElementDefinition ed = defn.getSnapshot().getElement().get(i);
133        if (ed.getPath().equals(element.getPath()))
134          return null;
135        if (ed.getPath().startsWith(element.getPath()+".value"))
136          return ed;
137        i++;
138      }
139      return null;
140    }
141    
142  }
143
144
145
146
147  private final boolean ADD_REFERENCE_TO_TABLE = true;
148
149
150  private static final String ROW_COLOR_ERROR = "#ffcccc";
151  private static final String ROW_COLOR_FATAL = "#ff9999";
152  private static final String ROW_COLOR_WARNING = "#ffebcc";
153  private static final String ROW_COLOR_HINT = "#ebf5ff";
154  public static final int STATUS_OK = 0;
155  public static final int STATUS_HINT = 1;
156  public static final int STATUS_WARNING = 2;
157  public static final int STATUS_ERROR = 3;
158  public static final int STATUS_FATAL = 4;
159
160
161  private static final String DERIVATION_EQUALS = "derivation.equals";
162  public static final String DERIVATION_POINTER = "derived.pointer";
163  public static final String IS_DERIVED = "derived.fact";
164  public static final String UD_ERROR_STATUS = "error-status";
165
166  // note that ProfileUtilities are used re-entrantly internally, so nothing with process state can be here
167  private final IWorkerContext context;
168  private List<ValidationMessage> messages;
169  private List<String> snapshotStack = new ArrayList<String>();
170  private ProfileKnowledgeProvider pkp;
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 interface ProfileKnowledgeProvider {
184    public class BindingResolution {
185      public String display;
186      public String url;
187    }
188    boolean isDatatype(String typeSimple);
189    boolean isResource(String typeSimple);
190    boolean hasLinkFor(String typeSimple);
191    String getLinkFor(String typeSimple);
192    BindingResolution resolveBinding(ElementDefinitionBindingComponent binding);
193    String getLinkForProfile(StructureDefinition profile, String url);
194  }
195
196
197/**
198 * Given a Structure, navigate to the element given by the path and return the direct children of that element
199 *
200 * @param structure The structure to navigate into
201 * @param path The path of the element within the structure to get the children for
202 * @return A Map containing the name of the element child (not the path) and the child itself (an Element)
203 * @throws DefinitionException 
204 * @throws Exception
205 */
206  public static List<ElementDefinition> getChildMap(StructureDefinition profile, String name, String path, String contentReference) throws DefinitionException {
207    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
208
209    // if we have a name reference, we have to find it, and iterate it's children
210    if (contentReference != null) {
211        boolean found = false;
212      for (ElementDefinition e : profile.getSnapshot().getElement()) {
213        if (contentReference.equals("#"+e.getId())) {
214                found = true;
215                path = e.getPath();
216        }
217      }
218      if (!found)
219        throw new DefinitionException("Unable to resolve name reference "+contentReference+" at path "+path);
220    }
221
222    for (ElementDefinition e : profile.getSnapshot().getElement())
223    {
224      String p = e.getPath();
225
226      if (path != null && !Utilities.noString(e.getContentReference()) && path.startsWith(p))
227      {
228        /* The path we are navigating to is on or below this element, but the element defers its definition to another named part of the
229         * structure.
230         */
231        if (path.length() > p.length())
232        {
233          // The path navigates further into the referenced element, so go ahead along the path over there
234          return getChildMap(profile, name, e.getContentReference()+"."+path.substring(p.length()+1), null);
235        }
236        else
237        {
238          // The path we are looking for is actually this element, but since it defers it definition, go get the referenced element
239          return getChildMap(profile, name, e.getContentReference(), null);
240        }
241      }
242      else if (p.startsWith(path+"."))
243      {
244          // The path of the element is a child of the path we're looking for (i.e. the parent),
245          // so add this element to the result.
246          String tail = p.substring(path.length()+1);
247
248          // Only add direct children, not any deeper paths
249          if (!tail.contains(".")) {
250            res.add(e);
251          }
252        }
253      }
254
255    return res;
256  }
257
258
259  public static List<ElementDefinition> getChildMap(StructureDefinition profile, ElementDefinition element) throws DefinitionException {
260                return getChildMap(profile, element.getName(), element.getPath(), element.getContentReference());
261  }
262
263
264  /**
265   * Given a Structure, navigate to the element given by the path and return the direct children of that element
266   *
267   * @param structure The structure to navigate into
268   * @param path The path of the element within the structure to get the children for
269   * @return A List containing the element children (all of them are Elements)
270   */
271  public static List<ElementDefinition> getChildList(StructureDefinition profile, String path) {
272    List<ElementDefinition> res = new ArrayList<ElementDefinition>();
273
274    for (ElementDefinition e : profile.getSnapshot().getElement())
275    {
276      String p = e.getPath();
277
278      if (!Utilities.noString(e.getContentReference()) && path.startsWith(p))
279      {
280        if (path.length() > p.length())
281          return getChildList(profile, e.getContentReference()+"."+path.substring(p.length()+1));
282        else
283          return getChildList(profile, e.getContentReference());
284      }
285      else if (p.startsWith(path+".") && !p.equals(path))
286      {
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());
301          }
302
303  /**
304   * Given a base (snapshot) profile structure, and a differential profile, generate a new snapshot profile
305   *
306   * @param base - the base structure on which the differential will be applied
307   * @param differential - the differential to apply to the base
308   * @param url - where the base has relative urls for profile references, these need to be converted to absolutes by prepending this URL
309   * @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
310   * @return
311   * @throws FHIRException 
312   * @throws DefinitionException 
313   * @throws Exception
314   */
315  public void generateSnapshot(StructureDefinition base, StructureDefinition derived, String url, String profileName) throws DefinitionException, FHIRException {
316    if (base == null)
317      throw new DefinitionException("no base profile provided");
318    if (derived == null)
319      throw new DefinitionException("no derived structure provided");
320
321    if (snapshotStack.contains(derived.getUrl()))
322      throw new DefinitionException("Circular snapshot references detected; cannot generate snapshot (stack = "+snapshotStack.toString()+")");
323    snapshotStack.add(derived.getUrl());
324    
325//    System.out.println("Generate Snapshot for "+derived.getUrl());
326
327    derived.setSnapshot(new StructureDefinitionSnapshotComponent());
328
329    // so we have two lists - the base list, and the differential list
330    // the differential list is only allowed to include things that are in the base list, but
331    // is allowed to include them multiple times - thereby slicing them
332
333    // our approach is to walk through the base list, and see whether the differential
334    // says anything about them.
335    int baseCursor = 0;
336    int diffCursor = 0; // we need a diff cursor because we can only look ahead, in the bound scoped by longer paths
337
338    // we actually delegate the work to a subroutine so we can re-enter it with a different cursors
339    processPaths(derived.getSnapshot(), base.getSnapshot(), derived.getDifferential(), baseCursor, diffCursor, base.getSnapshot().getElement().size()-1, derived.getDifferential().getElement().size()-1, url, derived.getId(), null, false, base.getUrl(), null, false);
340  }
341
342  /**
343   * @param trimDifferential
344   * @throws DefinitionException, FHIRException 
345   * @throws Exception
346   */
347  private void processPaths(StructureDefinitionSnapshotComponent result, StructureDefinitionSnapshotComponent base, StructureDefinitionDifferentialComponent differential, int baseCursor, int diffCursor, int baseLimit,
348      int diffLimit, String url, String profileName, String contextPath, boolean trimDifferential, String contextName, String resultPathBase, boolean slicingDone) throws DefinitionException, FHIRException {
349
350    // just repeat processing entries until we run out of our allowed scope (1st entry, the allowed scope is all the entries)
351    while (baseCursor <= baseLimit) {
352      // get the current focus of the base, and decide what to do
353      ElementDefinition currentBase = base.getElement().get(baseCursor);
354      String cpath = fixedPath(contextPath, currentBase.getPath());
355      List<ElementDefinition> diffMatches = getDiffMatches(differential, cpath, diffCursor, diffLimit, profileName); // get a list of matching elements in scope
356
357      // in the simple case, source is not sliced.
358      if (!currentBase.hasSlicing()) {
359        if (diffMatches.isEmpty()) { // the differential doesn't say anything about this item
360          // so we just copy it in
361          ElementDefinition outcome = updateURLs(url, currentBase.copy());
362          outcome.setPath(fixedPath(contextPath, outcome.getPath()));
363          updateFromBase(outcome, currentBase);
364          markDerived(outcome);
365          if (resultPathBase == null)
366            resultPathBase = outcome.getPath();
367          else if (!outcome.getPath().startsWith(resultPathBase))
368            throw new DefinitionException("Adding wrong path");
369          result.getElement().add(outcome);
370          baseCursor++;
371        } else if (diffMatches.size() == 1 && (slicingDone || (!diffMatches.get(0).hasSlicing() && !(isExtension(diffMatches.get(0)) && !diffMatches.get(0).hasName())))) {// one matching element in the differential
372          ElementDefinition template = null;
373          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")) {
374            String p = diffMatches.get(0).getType().get(0).getProfile().get(0).asStringValue();
375            StructureDefinition sd = context.fetchResource(StructureDefinition.class, p);
376            if (sd != null) {
377              if (!sd.hasSnapshot()) {
378                StructureDefinition sdb = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
379                if (sdb == null)
380                  throw new DefinitionException("no base for "+sd.getBaseDefinition());
381                generateSnapshot(sdb, sd, sd.getUrl(), sd.getName());
382              }
383              template = sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath());
384              // temporary work around
385              if (!diffMatches.get(0).getType().get(0).getCode().equals("Extension")) {
386                template.setMin(currentBase.getMin());
387                template.setMax(currentBase.getMax());
388              }
389            }
390          } 
391          if (template == null)
392            template = currentBase.copy();
393          else
394            // some of what's in currentBase overrides template
395            template = overWriteWithCurrent(template, currentBase);
396          ElementDefinition outcome = updateURLs(url, template);
397          outcome.setPath(fixedPath(contextPath, outcome.getPath()));
398          updateFromBase(outcome, currentBase);
399          if (diffMatches.get(0).hasName())
400          outcome.setName(diffMatches.get(0).getName());
401          outcome.setSlicing(null);
402          updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
403          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
404            outcome.setPath(outcome.getPath().substring(0, outcome.getPath().length()-3)+Utilities.capitalize(outcome.getType().get(0).getCode()));
405          if (resultPathBase == null)
406            resultPathBase = outcome.getPath();
407          else if (!outcome.getPath().startsWith(resultPathBase))
408            throw new DefinitionException("Adding wrong path");
409          result.getElement().add(outcome);
410          baseCursor++;
411          diffCursor = differential.getElement().indexOf(diffMatches.get(0))+1;
412          if (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
413            if (pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+".")) {
414              if (outcome.getType().size() > 1)
415                throw new DefinitionException(diffMatches.get(0).getPath()+" has children ("+differential.getElement().get(diffCursor).getPath()+") and multiple types ("+typeCode(outcome.getType())+") in profile "+profileName);
416              StructureDefinition dt = getProfileForDataType(outcome.getType().get(0));
417              if (dt == null)
418                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");
419              contextName = dt.getUrl();
420              int start = diffCursor;
421              while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(), diffMatches.get(0).getPath()+"."))
422                diffCursor++;
423              processPaths(result, dt.getSnapshot(), differential, 1 /* starting again on the data type, but skip the root */, start-1, dt.getSnapshot().getElement().size()-1,
424                  diffCursor - 1, url, profileName+pathTail(diffMatches, 0), diffMatches.get(0).getPath(), trimDifferential, contextName, resultPathBase, false);
425            }
426          }
427        } else {
428          // ok, the differential slices the item. Let's check our pre-conditions to ensure that this is correct
429          if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))
430            // you can only slice an element that doesn't repeat if the sum total of your slices is limited to 1
431            // (but you might do that in order to split up constraints by type)
432            throw new DefinitionException("Attempt to a slice an element that does not repeat: "+currentBase.getPath()+"/"+currentBase.getName()+" from "+contextName);
433          if (!diffMatches.get(0).hasSlicing() && !isExtension(currentBase)) // well, the diff has set up a slice, but hasn't defined it. this is an error
434            throw new DefinitionException("differential does not have a slice: "+currentBase.getPath());
435
436          // well, if it passed those preconditions then we slice the dest.
437          // we're just going to accept the differential slicing at face value
438          ElementDefinition outcome = updateURLs(url, currentBase.copy());
439          outcome.setPath(fixedPath(contextPath, outcome.getPath()));
440          updateFromBase(outcome, currentBase);
441
442          if (!diffMatches.get(0).hasSlicing())
443            outcome.setSlicing(makeExtensionSlicing());
444          else
445            outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
446          if (!outcome.getPath().startsWith(resultPathBase))
447            throw new DefinitionException("Adding wrong path");
448          result.getElement().add(outcome);
449
450          // 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.
451          int start = 0;
452          if (!diffMatches.get(0).hasName()) {
453            updateFromDefinition(outcome, diffMatches.get(0), profileName, trimDifferential, url);
454            if (!outcome.hasType()) {
455              throw new DefinitionException("not done yet");
456            }
457            start = 1;
458          } else 
459            checkExtensionDoco(outcome);
460
461          // now, for each entry in the diff matches, we're going to process the base item
462          // our processing scope for base is all the children of the current path
463          int nbl = findEndOfElement(base, baseCursor);
464          int ndc = diffCursor;
465          int ndl = diffCursor;
466          for (int i = start; i < diffMatches.size(); i++) {
467            // our processing scope for the differential is the item in the list, and all the items before the next one in the list
468            ndc = differential.getElement().indexOf(diffMatches.get(i));
469            ndl = findEndOfElement(differential, ndc);
470            // now we process the base scope repeatedly for each instance of the item in the differential list
471            processPaths(result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName+pathTail(diffMatches, i), contextPath, trimDifferential, contextName, resultPathBase, true);
472          }
473          // ok, done with that - next in the base list
474          baseCursor = nbl+1;
475          diffCursor = ndl+1;
476        }
477      } else {
478        // the item is already sliced in the base profile.
479        // here's the rules
480        //  1. irrespective of whether the slicing is ordered or not, the definition order must be maintained
481        //  2. slice element names have to match.
482        //  3. new slices must be introduced at the end
483        // corallory: you can't re-slice existing slices. is that ok?
484
485        // we're going to need this:
486        String path = currentBase.getPath();
487        ElementDefinition original = currentBase;
488
489        if (diffMatches.isEmpty()) { // the differential doesn't say anything about this item
490          // copy across the currentbase, and all of it's children and siblings
491          while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
492            ElementDefinition outcome = updateURLs(url, base.getElement().get(baseCursor).copy());
493            if (!outcome.getPath().startsWith(resultPathBase))
494              throw new DefinitionException("Adding wrong path: "+outcome.getPath()+" vs " + resultPathBase);
495            result.getElement().add(outcome); // so we just copy it in
496            baseCursor++;
497          }
498        } else {
499          // first - check that the slicing is ok
500          boolean closed = currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
501          int diffpos = 0;
502          boolean isExtension = cpath.endsWith(".extension") || cpath.endsWith(".modifierExtension");
503          if (diffMatches.get(0).hasSlicing()) { // it might be null if the differential doesn't want to say anything about slicing
504            if (!isExtension)
505            diffpos++; // if there's a slice on the first, we'll ignore any content it has
506            ElementDefinitionSlicingComponent dSlice = diffMatches.get(0).getSlicing();
507            ElementDefinitionSlicingComponent bSlice = currentBase.getSlicing();
508            if (!orderMatches(dSlice.getOrderedElement(), bSlice.getOrderedElement()))
509              throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - order @ "+path+" ("+contextName+")");
510            if (!discriiminatorMatches(dSlice.getDiscriminator(), bSlice.getDiscriminator()))
511             throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - disciminator @ "+path+" ("+contextName+")");
512            if (!ruleMatches(dSlice.getRules(), bSlice.getRules()))
513             throw new DefinitionException("Slicing rules on differential ("+summariseSlicing(dSlice)+") do not match those on base ("+summariseSlicing(bSlice)+") - rule @ "+path+" ("+contextName+")");
514          }
515          ElementDefinition outcome = updateURLs(url, currentBase.copy());
516          outcome.setPath(fixedPath(contextPath, outcome.getPath()));
517          updateFromBase(outcome, currentBase);
518          if (diffMatches.get(0).hasSlicing() && !isExtension) {
519            updateFromSlicing(outcome.getSlicing(), diffMatches.get(0).getSlicing());
520            updateFromDefinition(outcome, diffMatches.get(0), profileName, closed, url); // if there's no slice, we don't want to update the unsliced description
521          }
522          if (diffMatches.get(0).hasSlicing() && !diffMatches.get(0).hasName())
523            diffpos++;
524            
525          result.getElement().add(outcome);
526
527          // now, we have two lists, base and diff. we're going to work through base, looking for matches in diff.
528          List<ElementDefinition> baseMatches = getSiblings(base.getElement(), currentBase);
529          for (ElementDefinition baseItem : baseMatches) {
530            baseCursor = base.getElement().indexOf(baseItem);
531            outcome = updateURLs(url, baseItem.copy());
532            updateFromBase(outcome, currentBase);
533            outcome.setPath(fixedPath(contextPath, outcome.getPath()));
534            outcome.setSlicing(null);
535            if (!outcome.getPath().startsWith(resultPathBase))
536              throw new DefinitionException("Adding wrong path");
537            if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getName().equals(outcome.getName())) {
538              // if there's a diff, we update the outcome with diff
539              // no? updateFromDefinition(outcome, diffMatches.get(diffpos), profileName, closed, url);
540              //then process any children
541              int nbl = findEndOfElement(base, baseCursor);
542              int ndc = differential.getElement().indexOf(diffMatches.get(diffpos));
543              int ndl = findEndOfElement(differential, ndc);
544              // now we process the base scope repeatedly for each instance of the item in the differential list
545              processPaths(result, base, differential, baseCursor, ndc, nbl, ndl, url, profileName+pathTail(diffMatches, diffpos), contextPath, closed, contextName, resultPathBase, true);
546              // ok, done with that - now set the cursors for if this is the end
547              baseCursor = nbl+1;
548              diffCursor = ndl+1;
549              diffpos++;
550            } else {
551              result.getElement().add(outcome);
552              baseCursor++;
553              // just copy any children on the base
554              while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
555                outcome = updateURLs(url, currentBase.copy());
556                outcome.setPath(fixedPath(contextPath, outcome.getPath()));
557                if (!outcome.getPath().startsWith(resultPathBase))
558                  throw new DefinitionException("Adding wrong path");
559                result.getElement().add(outcome);
560                baseCursor++;
561              }
562            }
563          }
564          // finally, we process any remaining entries in diff, which are new (and which are only allowed if the base wasn't closed
565          if (closed && diffpos < diffMatches.size())
566            throw new DefinitionException("The base snapshot marks a slicing as closed, but the differential tries to extend it in "+profileName+" at "+path+" ("+cpath+")");
567          while (diffpos < diffMatches.size()) {
568            ElementDefinition diffItem = diffMatches.get(diffpos);
569            for (ElementDefinition baseItem : baseMatches)
570              if (baseItem.getName().equals(diffItem.getName()))
571                throw new DefinitionException("Named items are out of order in the slice");
572            outcome = updateURLs(url, original.copy());
573            outcome.setPath(fixedPath(contextPath, outcome.getPath()));
574            updateFromBase(outcome, currentBase);
575            outcome.setSlicing(null);
576            if (!outcome.getPath().startsWith(resultPathBase))
577              throw new DefinitionException("Adding wrong path");
578            result.getElement().add(outcome);
579            updateFromDefinition(outcome, diffItem, profileName, trimDifferential, url);
580            diffpos++;
581          }
582        }
583      }
584    }
585  }
586
587
588  private ElementDefinition overWriteWithCurrent(ElementDefinition profile, ElementDefinition usage) {
589    ElementDefinition res = profile.copy();
590    if (usage.hasName())
591      res.setName(usage.getName());
592    if (usage.hasLabel())
593      res.setLabel(usage.getLabel());
594    for (Coding c : usage.getCode())
595      res.addCode(c);
596    
597    if (usage.hasDefinition())
598      res.setDefinition(usage.getDefinition());
599    if (usage.hasShort())
600      res.setShort(usage.getShort());
601    if (usage.hasComments())
602      res.setComments(usage.getComments());
603    if (usage.hasRequirements())
604      res.setRequirements(usage.getRequirements());
605    for (StringType c : usage.getAlias())
606      res.addAlias(c.getValue());
607    if (usage.hasMin())
608      res.setMin(usage.getMin());
609    if (usage.hasMax())
610      res.setMax(usage.getMax());
611     
612    if (usage.hasFixed())
613      res.setFixed(usage.getFixed());
614    if (usage.hasPattern())
615      res.setPattern(usage.getPattern());
616    if (usage.hasExample())
617      res.setExample(usage.getExample());
618    if (usage.hasMinValue())
619      res.setMinValue(usage.getMinValue());
620    if (usage.hasMaxValue())
621      res.setMaxValue(usage.getMaxValue());     
622    if (usage.hasMaxLength())
623      res.setMaxLength(usage.getMaxLength());
624    if (usage.hasMustSupport())
625      res.setMustSupport(usage.getMustSupport());
626    if (usage.hasBinding())
627      res.setBinding(usage.getBinding().copy());
628    for (ElementDefinitionConstraintComponent c : usage.getConstraint())
629      res.addConstraint(c);
630    
631    return res;
632  }
633
634
635  private boolean checkExtensionDoco(ElementDefinition base) {
636    // see task 3970. For an extension, there's no point copying across all the underlying definitional stuff
637    boolean isExtension = base.getPath().equals("Extension") || base.getPath().endsWith(".extension") || base.getPath().endsWith(".modifierExtension");
638    if (isExtension) {
639      base.setDefinition("An Extension");
640      base.setShort("Extension");
641      base.setCommentsElement(null);
642      base.setRequirementsElement(null);
643      base.getAlias().clear();
644      base.getMapping().clear();
645    }
646    return isExtension;
647  }
648
649
650  private String pathTail(List<ElementDefinition> diffMatches, int i) {
651    
652    ElementDefinition d = diffMatches.get(i);
653    String s = d.getPath().contains(".") ? d.getPath().substring(d.getPath().lastIndexOf(".")+1) : d.getPath();
654    return "."+s + (d.hasType() && d.getType().get(0).hasProfile() ? "["+d.getType().get(0).getProfile().get(0).asStringValue()+"]" : "");
655  }
656
657
658  private void markDerived(ElementDefinition outcome) {
659    for (ElementDefinitionConstraintComponent inv : outcome.getConstraint())
660      inv.setUserData(IS_DERIVED, true);
661  }
662
663
664  private String summariseSlicing(ElementDefinitionSlicingComponent slice) {
665    StringBuilder b = new StringBuilder();
666    boolean first = true;
667    for (StringType d : slice.getDiscriminator()) {
668      if (first)
669        first = false;
670      else
671        b.append(", ");
672      b.append(d);
673    }
674    b.append("(");
675    if (slice.hasOrdered())
676      b.append(slice.getOrderedElement().asStringValue());
677    b.append("/");
678    if (slice.hasRules())
679      b.append(slice.getRules().toCode());
680    b.append(")");
681    if (slice.hasDescription()) {
682      b.append(" \"");
683      b.append(slice.getDescription());
684      b.append("\"");
685    }
686    return b.toString();
687  }
688
689
690  private void updateFromBase(ElementDefinition derived, ElementDefinition base) {
691    if (base.hasBase()) {
692      derived.getBase().setPath(base.getBase().getPath());
693      derived.getBase().setMin(base.getBase().getMin());
694      derived.getBase().setMax(base.getBase().getMax());
695    } else {
696      derived.getBase().setPath(base.getPath());
697      derived.getBase().setMin(base.getMin());
698      derived.getBase().setMax(base.getMax());
699    }
700  }
701
702
703  private boolean pathStartsWith(String p1, String p2) {
704    return p1.startsWith(p2);
705  }
706
707  private boolean pathMatches(String p1, String p2) {
708    return p1.equals(p2) || (p2.endsWith("[x]") && p1.startsWith(p2.substring(0, p2.length()-3)) && !p1.substring(p2.length()-3).contains("."));
709  }
710
711
712  private String fixedPath(String contextPath, String pathSimple) {
713    if (contextPath == null)
714      return pathSimple;
715    return contextPath+"."+pathSimple.substring(pathSimple.indexOf(".")+1);
716  }
717
718
719  private StructureDefinition getProfileForDataType(TypeRefComponent type)  {
720    StructureDefinition sd = null;
721    if (type.hasProfile())  
722      sd = context.fetchResource(StructureDefinition.class, type.getProfile().get(0).asStringValue()); 
723    if (sd == null) 
724      sd = context.fetchTypeDefinition(type.getCode());
725    if (sd == null)
726      System.out.println("XX: failed to find profle for type: " + type.getCode()); // debug GJM
727    return sd;
728  }
729
730
731  public static String typeCode(List<TypeRefComponent> types) {
732    StringBuilder b = new StringBuilder();
733    boolean first = true;
734    for (TypeRefComponent type : types) {
735      if (first) first = false; else b.append(", ");
736      b.append(type.getCode());
737      if (type.hasProfile())
738        b.append("{"+type.getProfile()+"}");
739    }
740    return b.toString();
741  }
742
743
744  private boolean isDataType(List<TypeRefComponent> types) {
745    if (types.isEmpty())
746      return false;
747    for (TypeRefComponent type : types) {
748      String t = type.getCode();
749      if (!isDataType(t) && !t.equals("Reference") && !t.equals("Narrative") && !t.equals("Extension") && !t.equals("ElementDefinition") && !isPrimitive(t))
750        return false;
751    }
752    return true;
753  }
754
755
756  /**
757   * Finds internal references in an Element's Binding and StructureDefinition references (in TypeRef) and bases them on the given url
758   * @param url - the base url to use to turn internal references into absolute references
759   * @param element - the Element to update
760   * @return - the updated Element
761   */
762  private ElementDefinition updateURLs(String url, ElementDefinition element) {
763    if (element != null) {
764      ElementDefinition defn = element;
765      if (defn.hasBinding() && defn.getBinding().getValueSet() instanceof Reference && ((Reference)defn.getBinding().getValueSet()).getReference().startsWith("#"))
766        ((Reference)defn.getBinding().getValueSet()).setReference(url+((Reference)defn.getBinding().getValueSet()).getReference());
767      for (TypeRefComponent t : defn.getType()) {
768        for (UriType tp : t.getProfile()) {
769                if (tp.getValue().startsWith("#"))
770            tp.setValue(url+t.getProfile());
771        }
772      }
773    }
774    return element;
775  }
776
777  private List<ElementDefinition> getSiblings(List<ElementDefinition> list, ElementDefinition current) {
778    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
779    String path = current.getPath();
780    int cursor = list.indexOf(current)+1;
781    while (cursor < list.size() && list.get(cursor).getPath().length() >= path.length()) {
782      if (pathMatches(list.get(cursor).getPath(), path))
783        result.add(list.get(cursor));
784      cursor++;
785    }
786    return result;
787  }
788
789  private void updateFromSlicing(ElementDefinitionSlicingComponent dst, ElementDefinitionSlicingComponent src) {
790    if (src.hasOrderedElement())
791      dst.setOrderedElement(src.getOrderedElement().copy());
792    if (src.hasDiscriminator())
793      dst.getDiscriminator().addAll(src.getDiscriminator());
794    if (src.hasRulesElement())
795      dst.setRulesElement(src.getRulesElement().copy());
796  }
797
798  private boolean orderMatches(BooleanType diff, BooleanType base) {
799    return (diff == null) || (base == null) || (diff.getValue() == base.getValue());
800  }
801
802  private boolean discriiminatorMatches(List<StringType> diff, List<StringType> base) {
803    if (diff.isEmpty() || base.isEmpty())
804        return true;
805    if (diff.size() != base.size())
806        return false;
807    for (int i = 0; i < diff.size(); i++)
808        if (!diff.get(i).getValue().equals(base.get(i).getValue()))
809                return false;
810    return true;
811  }
812
813  private boolean ruleMatches(SlicingRules diff, SlicingRules base) {
814    return (diff == null) || (base == null) || (diff == base) || (diff == SlicingRules.OPEN) ||
815        ((diff == SlicingRules.OPENATEND && base == SlicingRules.CLOSED));
816  }
817
818  private boolean isSlicedToOneOnly(ElementDefinition e) {
819    return (e.hasSlicing() && e.hasMaxElement() && e.getMax().equals("1"));
820  }
821
822  private ElementDefinitionSlicingComponent makeExtensionSlicing() {
823        ElementDefinitionSlicingComponent slice = new ElementDefinitionSlicingComponent();
824    slice.addDiscriminator("url");
825    slice.setOrdered(false);
826    slice.setRules(SlicingRules.OPEN);
827    return slice;
828  }
829
830  private boolean isExtension(ElementDefinition currentBase) {
831    return currentBase.getPath().endsWith(".extension") || currentBase.getPath().endsWith(".modifierExtension");
832  }
833
834  private List<ElementDefinition> getDiffMatches(StructureDefinitionDifferentialComponent context, String path, int start, int end, String profileName) {
835    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
836    for (int i = start; i <= end; i++) {
837      String statedPath = context.getElement().get(i).getPath();
838      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.substring(path.length()).contains("."))) {
839        result.add(context.getElement().get(i));
840      } else if (result.isEmpty()) {
841//        System.out.println("ignoring "+statedPath+" in differential of "+profileName);
842      }
843    }
844    return result;
845  }
846
847  private int findEndOfElement(StructureDefinitionDifferentialComponent context, int cursor) {
848            int result = cursor;
849            String path = context.getElement().get(cursor).getPath()+".";
850            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
851              result++;
852            return result;
853          }
854
855  private int findEndOfElement(StructureDefinitionSnapshotComponent context, int cursor) {
856            int result = cursor;
857            String path = context.getElement().get(cursor).getPath()+".";
858            while (result < context.getElement().size()- 1 && context.getElement().get(result+1).getPath().startsWith(path))
859              result++;
860            return result;
861          }
862
863  private boolean unbounded(ElementDefinition definition) {
864    StringType max = definition.getMaxElement();
865    if (max == null)
866      return false; // this is not valid
867    if (max.getValue().equals("1"))
868      return false;
869    if (max.getValue().equals("0"))
870      return false;
871    return true;
872  }
873
874  private void updateFromDefinition(ElementDefinition dest, ElementDefinition source, String pn, boolean trimDifferential, String purl) throws DefinitionException, FHIRException {
875    // we start with a clone of the base profile ('dest') and we copy from the profile ('source')
876    // over the top for anything the source has
877    ElementDefinition base = dest;
878    ElementDefinition derived = source;
879    derived.setUserData(DERIVATION_POINTER, base);
880
881    if (derived != null) {
882      boolean isExtension = checkExtensionDoco(base);
883
884      if (derived.hasShortElement()) {
885        if (!Base.compareDeep(derived.getShortElement(), base.getShortElement(), false))
886          base.setShortElement(derived.getShortElement().copy());
887        else if (trimDifferential)
888          derived.setShortElement(null);
889        else if (derived.hasShortElement())
890          derived.getShortElement().setUserData(DERIVATION_EQUALS, true);
891      }
892
893      if (derived.hasDefinitionElement()) {
894        if (derived.getDefinition().startsWith("..."))
895          base.setDefinition(base.getDefinition()+"\r\n"+derived.getDefinition().substring(3));
896        else if (!Base.compareDeep(derived.getDefinitionElement(), base.getDefinitionElement(), false))
897          base.setDefinitionElement(derived.getDefinitionElement().copy());
898        else if (trimDifferential)
899          derived.setDefinitionElement(null);
900        else if (derived.hasDefinitionElement())
901          derived.getDefinitionElement().setUserData(DERIVATION_EQUALS, true);
902      }
903
904      if (derived.hasCommentsElement()) {
905        if (derived.getComments().startsWith("..."))
906          base.setComments(base.getComments()+"\r\n"+derived.getComments().substring(3));
907        else if (!Base.compareDeep(derived.getCommentsElement(), base.getCommentsElement(), false))
908          base.setCommentsElement(derived.getCommentsElement().copy());
909        else if (trimDifferential)
910          base.setCommentsElement(derived.getCommentsElement().copy());
911        else if (derived.hasCommentsElement())
912          derived.getCommentsElement().setUserData(DERIVATION_EQUALS, true);
913      }
914
915      if (derived.hasLabelElement()) {
916        if (derived.getLabel().startsWith("..."))
917          base.setLabel(base.getLabel()+"\r\n"+derived.getLabel().substring(3));
918        else if (!Base.compareDeep(derived.getLabelElement(), base.getLabelElement(), false))
919          base.setLabelElement(derived.getLabelElement().copy());
920        else if (trimDifferential)
921          base.setLabelElement(derived.getLabelElement().copy());
922        else if (derived.hasLabelElement())
923          derived.getLabelElement().setUserData(DERIVATION_EQUALS, true);
924      }
925
926      if (derived.hasRequirementsElement()) {
927        if (derived.getRequirements().startsWith("..."))
928          base.setRequirements(base.getRequirements()+"\r\n"+derived.getRequirements().substring(3));
929        else if (!Base.compareDeep(derived.getRequirementsElement(), base.getRequirementsElement(), false))
930          base.setRequirementsElement(derived.getRequirementsElement().copy());
931        else if (trimDifferential)
932          base.setRequirementsElement(derived.getRequirementsElement().copy());
933        else if (derived.hasRequirementsElement())
934          derived.getRequirementsElement().setUserData(DERIVATION_EQUALS, true);
935      }
936      // sdf-9
937      if (derived.hasRequirements() && !base.getPath().contains("."))
938        derived.setRequirements(null);
939      if (base.hasRequirements() && !base.getPath().contains("."))
940        base.setRequirements(null);
941
942      if (derived.hasAlias()) {
943        if (!Base.compareDeep(derived.getAlias(), base.getAlias(), false))
944          for (StringType s : derived.getAlias()) {
945            if (!base.hasAlias(s.getValue()))
946              base.getAlias().add(s.copy());
947          }
948        else if (trimDifferential)
949          derived.getAlias().clear();
950        else
951          for (StringType t : derived.getAlias())
952            t.setUserData(DERIVATION_EQUALS, true);
953      }
954
955      if (derived.hasMinElement()) {
956        if (!Base.compareDeep(derived.getMinElement(), base.getMinElement(), false)) {
957          if (derived.getMin() < base.getMin())
958            messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Derived min  ("+Integer.toString(derived.getMin())+") cannot be less than base min ("+Integer.toString(base.getMin())+")", IssueSeverity.ERROR));
959          base.setMinElement(derived.getMinElement().copy());
960        } else if (trimDifferential)
961          derived.setMinElement(null);
962        else
963          derived.getMinElement().setUserData(DERIVATION_EQUALS, true);
964      }
965
966      if (derived.hasMaxElement()) {
967        if (!Base.compareDeep(derived.getMaxElement(), base.getMaxElement(), false)) {
968          if (isLargerMax(derived.getMax(), base.getMax()))
969            messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Derived max ("+derived.getMax()+") cannot be greater than base max ("+base.getMax()+")", IssueSeverity.ERROR));
970          base.setMaxElement(derived.getMaxElement().copy());
971        } else if (trimDifferential)
972          derived.setMaxElement(null);
973        else
974          derived.getMaxElement().setUserData(DERIVATION_EQUALS, true);
975      }
976
977      if (derived.hasFixed()) {
978        if (!Base.compareDeep(derived.getFixed(), base.getFixed(), true)) {
979          base.setFixed(derived.getFixed().copy());
980        } else if (trimDifferential)
981          derived.setFixed(null);
982        else
983          derived.getFixed().setUserData(DERIVATION_EQUALS, true);
984      }
985
986      if (derived.hasPattern()) {
987        if (!Base.compareDeep(derived.getPattern(), base.getPattern(), false)) {
988          base.setPattern(derived.getPattern().copy());
989        } else
990          if (trimDifferential)
991            derived.setPattern(null);
992          else
993            derived.getPattern().setUserData(DERIVATION_EQUALS, true);
994      }
995
996      if (derived.hasExample()) {
997        if (!Base.compareDeep(derived.getExample(), base.getExample(), false))
998          base.setExample(derived.getExample().copy());
999        else if (trimDifferential)
1000          derived.setExample(null);
1001        else
1002          derived.getExample().setUserData(DERIVATION_EQUALS, true);
1003      }
1004
1005      if (derived.hasMaxLengthElement()) {
1006        if (!Base.compareDeep(derived.getMaxLengthElement(), base.getMaxLengthElement(), false))
1007          base.setMaxLengthElement(derived.getMaxLengthElement().copy());
1008        else if (trimDifferential)
1009          derived.setMaxLengthElement(null);
1010        else
1011          derived.getMaxLengthElement().setUserData(DERIVATION_EQUALS, true);
1012      }
1013
1014      // todo: what to do about conditions?
1015      // condition : id 0..*
1016
1017      if (derived.hasMustSupportElement()) {
1018        if (!Base.compareDeep(derived.getMustSupportElement(), base.getMustSupportElement(), false))
1019          base.setMustSupportElement(derived.getMustSupportElement().copy());
1020        else if (trimDifferential)
1021          derived.setMustSupportElement(null);
1022        else
1023          derived.getMustSupportElement().setUserData(DERIVATION_EQUALS, true);
1024      }
1025
1026
1027      // profiles cannot change : isModifier, defaultValue, meaningWhenMissing
1028      // but extensions can change isModifier
1029      if (isExtension) {
1030        if (!Base.compareDeep(derived.getIsModifierElement(), base.getIsModifierElement(), false))
1031          base.setIsModifierElement(derived.getIsModifierElement().copy());
1032        else if (trimDifferential)
1033          derived.setIsModifierElement(null);
1034        else
1035          derived.getIsModifierElement().setUserData(DERIVATION_EQUALS, true);
1036      }
1037
1038      if (derived.hasBinding()) {
1039        if (!Base.compareDeep(derived.getBinding(), base.getBinding(), false)) {
1040          if (base.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && derived.getBinding().getStrength() != BindingStrength.REQUIRED)
1041            messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "illegal attempt to change a binding from "+base.getBinding().getStrength().toCode()+" to "+derived.getBinding().getStrength().toCode(), IssueSeverity.ERROR));
1042//            throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": illegal attempt to change a binding from "+base.getBinding().getStrength().toCode()+" to "+derived.getBinding().getStrength().toCode());
1043          else if (base.hasBinding() && derived.hasBinding() && base.getBinding().getStrength() == BindingStrength.REQUIRED && base.getBinding().hasValueSetReference() && derived.getBinding().hasValueSetReference()) {
1044            ValueSetExpansionOutcome expBase = context.expandVS(context.fetchResource(ValueSet.class, base.getBinding().getValueSetReference().getReference()), true);
1045            ValueSetExpansionOutcome expDerived = context.expandVS(context.fetchResource(ValueSet.class, derived.getBinding().getValueSetReference().getReference()), true);
1046            if (expBase.getValueset() == null)
1047              messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+base.getPath(), "Binding "+base.getBinding().getValueSetReference().getReference()+" could not be expanded", IssueSeverity.WARNING));
1048            else if (expDerived.getValueset() == null)
1049              messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSetReference().getReference()+" could not be expanded", IssueSeverity.WARNING));
1050            else if (!isSubset(expBase.getValueset(), expDerived.getValueset()))
1051              messages.add(new ValidationMessage(Source.ProfileValidator, IssueType.BUSINESSRULE, pn+"."+derived.getPath(), "Binding "+derived.getBinding().getValueSetReference().getReference()+" is not a subset of binding "+base.getBinding().getValueSetReference().getReference(), IssueSeverity.ERROR));
1052          }
1053          base.setBinding(derived.getBinding().copy());
1054        } else if (trimDifferential)
1055          derived.setBinding(null);
1056        else
1057          derived.getBinding().setUserData(DERIVATION_EQUALS, true);
1058      } // else if (base.hasBinding() && doesn't have bindable type )
1059        //  base
1060
1061      if (derived.hasIsSummaryElement()) {
1062        if (!Base.compareDeep(derived.getIsSummaryElement(), base.getIsSummaryElement(), false))
1063          base.setIsSummaryElement(derived.getIsSummaryElement().copy());
1064        else if (trimDifferential)
1065          derived.setIsSummaryElement(null);
1066        else
1067          derived.getIsSummaryElement().setUserData(DERIVATION_EQUALS, true);
1068      }
1069
1070      if (derived.hasType()) {
1071        if (!Base.compareDeep(derived.getType(), base.getType(), false)) {
1072          if (base.hasType()) {
1073            for (TypeRefComponent ts : derived.getType()) {
1074              boolean ok = false;
1075              CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1076              for (TypeRefComponent td : base.getType()) {
1077                b.append(td.getCode());
1078                if (td.hasCode() && (td.getCode().equals(ts.getCode()) || td.getCode().equals("Extension") ||
1079                    td.getCode().equals("Element") || td.getCode().equals("*") ||
1080                    ((td.getCode().equals("Resource") || (td.getCode().equals("DomainResource")) && pkp.isResource(ts.getCode())))))
1081                  ok = true;
1082              }
1083              if (!ok)
1084                throw new DefinitionException("StructureDefinition "+pn+" at "+derived.getPath()+": illegal constrained type "+ts.getCode()+" from "+b.toString());
1085            }
1086          }
1087          base.getType().clear();
1088          for (TypeRefComponent t : derived.getType()) {
1089            TypeRefComponent tt = t.copy();
1090//            tt.setUserData(DERIVATION_EQUALS, true);
1091            base.getType().add(tt);
1092          }
1093        }
1094        else if (trimDifferential)
1095          derived.getType().clear();
1096        else
1097          for (TypeRefComponent t : derived.getType())
1098            t.setUserData(DERIVATION_EQUALS, true);
1099      }
1100
1101      if (derived.hasMapping()) {
1102        // todo: mappings are not cumulative - one replaces another
1103        if (!Base.compareDeep(derived.getMapping(), base.getMapping(), false)) {
1104          for (ElementDefinitionMappingComponent s : derived.getMapping()) {
1105            boolean found = false;
1106            for (ElementDefinitionMappingComponent d : base.getMapping()) {
1107              found = found || (d.getIdentity().equals(s.getIdentity()) && d.getMap().equals(s.getMap()));
1108            }
1109            if (!found)
1110              base.getMapping().add(s);
1111          }
1112        }
1113        else if (trimDifferential)
1114          derived.getMapping().clear();
1115        else
1116          for (ElementDefinitionMappingComponent t : derived.getMapping())
1117            t.setUserData(DERIVATION_EQUALS, true);
1118      }
1119
1120      // todo: constraints are cumulative. there is no replacing
1121      for (ElementDefinitionConstraintComponent s : base.getConstraint()) 
1122        s.setUserData(IS_DERIVED, true);
1123      if (derived.hasConstraint()) {
1124        for (ElementDefinitionConstraintComponent s : derived.getConstraint()) {
1125          base.getConstraint().add(s.copy());
1126        }
1127      }
1128    }
1129  }
1130
1131  private boolean isLargerMax(String derived, String base) {
1132    if ("*".equals(base))
1133      return false;
1134    if ("*".equals(derived))
1135      return true;
1136    return Integer.parseInt(derived) > Integer.parseInt(base);
1137  }
1138
1139
1140  private boolean isSubset(ValueSet expBase, ValueSet expDerived) {
1141    return codesInExpansion(expDerived.getExpansion().getContains(), expBase.getExpansion());
1142  }
1143
1144
1145  private boolean codesInExpansion(List<ValueSetExpansionContainsComponent> contains, ValueSetExpansionComponent expansion) {
1146    for (ValueSetExpansionContainsComponent cc : contains) {
1147      if (!inExpansion(cc, expansion.getContains()))
1148        return false;
1149      if (!codesInExpansion(cc.getContains(), expansion))
1150        return false;
1151    }
1152    return true;
1153  }
1154
1155
1156  private boolean inExpansion(ValueSetExpansionContainsComponent cc, List<ValueSetExpansionContainsComponent> contains) {
1157    for (ValueSetExpansionContainsComponent cc1 : contains) {
1158      if (cc.getSystem().equals(cc1.getSystem()) && cc.getCode().equals(cc1.getCode()))
1159        return true;
1160      if (inExpansion(cc,  cc1.getContains()))
1161        return true;
1162    }
1163    return false;
1164  }
1165
1166
1167  public XhtmlNode generateExtensionTable(String defFile, StructureDefinition ed, String imageFolder, boolean inlineGraphics, boolean full, String corePath, Set<String> outputTracker) throws IOException, FHIRException {
1168    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics);
1169    TableModel model = gen.initNormalTable(corePath, false, true, ed.getId(), false);
1170
1171    boolean deep = false;
1172    boolean vdeep = false;
1173    for (ElementDefinition eld : ed.getSnapshot().getElement()) {
1174      deep = deep || eld.getPath().contains("Extension.extension.");
1175      vdeep = vdeep || eld.getPath().contains("Extension.extension.extension.");
1176    }
1177    Row r = gen.new Row();
1178    model.getRows().add(r);
1179    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));
1180    r.getCells().add(gen.new Cell());
1181    r.getCells().add(gen.new Cell(null, null, describeCardinality(ed.getSnapshot().getElement().get(0), null, new UnusedTracker()), null, null));
1182
1183    if (full || vdeep) {
1184      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
1185
1186      r.setIcon(deep ? "icon_extension_complex.png" : "icon_extension_simple.png", deep ? HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX : HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
1187      List<ElementDefinition> children = getChildren(ed.getSnapshot().getElement(), ed.getSnapshot().getElement().get(0));
1188      for (ElementDefinition child : children)
1189        if (!child.getPath().endsWith(".id"))
1190          genElement(defFile == null ? "" : defFile+"-definitions.html#extension.", gen, r.getSubRows(), child, ed.getSnapshot().getElement(), null, true, defFile, true, full, corePath, true, false);
1191    } else if (deep) {
1192      List<ElementDefinition> children = new ArrayList<ElementDefinition>();
1193      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
1194        if (ted.getPath().equals("Extension.extension"))
1195          children.add(ted);
1196      }
1197
1198      r.getCells().add(gen.new Cell("", "", "Extension", null, null));
1199      r.setIcon("icon_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
1200      
1201      for (ElementDefinition c : children) {
1202        ElementDefinition ved = getValueFor(ed, c);
1203        ElementDefinition ued = getUrlFor(ed, c);
1204        if (ved != null && ued != null) {
1205          Row r1 = gen.new Row();
1206          r.getSubRows().add(r1);
1207          r1.getCells().add(gen.new Cell(null, defFile == null ? "" : defFile+"-definitions.html#extension."+ed.getName(), ((UriType) ued.getFixed()).getValue(), null, null));
1208          r1.getCells().add(gen.new Cell());
1209          r1.getCells().add(gen.new Cell(null, null, describeCardinality(c, null, new UnusedTracker()), null, null));
1210          genTypes(gen, r1, ved, defFile, ed, corePath);
1211          r1.getCells().add(gen.new Cell(null, null, c.getDefinition(), null, null));
1212          r1.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
1213        }
1214      }
1215    } else  {
1216      ElementDefinition ved = null;
1217      for (ElementDefinition ted : ed.getSnapshot().getElement()) {
1218        if (ted.getPath().startsWith("Extension.value"))
1219          ved = ted;
1220      }
1221
1222      genTypes(gen, r, ved, defFile, ed, corePath);
1223
1224      r.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);      
1225    }
1226    Cell c = gen.new Cell("", "", "URL = "+ed.getUrl(), null, null);
1227    c.addPiece(gen.new Piece("br")).addPiece(gen.new Piece(null, ed.getName()+": "+ed.getDescription(), null));
1228    c.addPiece(gen.new Piece("br")).addPiece(gen.new Piece(null, describeExtensionContext(ed), null));
1229    r.getCells().add(c);
1230
1231
1232    try {
1233                return gen.generate(model, corePath, 0, outputTracker);
1234        } catch (org.hl7.fhir.exceptions.FHIRException e) {
1235                throw new FHIRException(e.getMessage(), e);
1236        }
1237    }
1238
1239  private ElementDefinition getUrlFor(StructureDefinition ed, ElementDefinition c) {
1240    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
1241    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
1242      if (ed.getSnapshot().getElement().get(i).getPath().equals(c.getPath()+".url"))
1243        return ed.getSnapshot().getElement().get(i);
1244      i++;
1245    }
1246    return null;
1247  }
1248
1249  private ElementDefinition getValueFor(StructureDefinition ed, ElementDefinition c) {
1250    int i = ed.getSnapshot().getElement().indexOf(c) + 1;
1251    while (i < ed.getSnapshot().getElement().size() && ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".")) {
1252      if (ed.getSnapshot().getElement().get(i).getPath().startsWith(c.getPath()+".value"))
1253        return ed.getSnapshot().getElement().get(i);
1254      i++;
1255    }
1256    return null;
1257  }
1258
1259
1260  private Cell genTypes(HierarchicalTableGenerator gen, Row r, ElementDefinition e, String profileBaseFileName, StructureDefinition profile, String corePath) {
1261    Cell c = gen.new Cell();
1262    r.getCells().add(c);
1263    List<TypeRefComponent> types = e.getType();
1264    if (!e.hasType()) {
1265      if (e.hasContentReference()) {
1266        return c;
1267      } else {
1268        ElementDefinition d = (ElementDefinition) e.getUserData(DERIVATION_POINTER);
1269        if (d != null && d.hasType()) {
1270          types = new ArrayList<ElementDefinition.TypeRefComponent>();
1271          for (TypeRefComponent tr : d.getType()) {
1272            TypeRefComponent tt = tr.copy();
1273            tt.setUserData(DERIVATION_EQUALS, true);
1274            types.add(tt);
1275          }
1276        } else
1277          return c;
1278      }
1279    }
1280
1281    boolean first = true;
1282    Element source = types.get(0); // either all types are the same, or we don't consider any of them the same
1283
1284    boolean allReference = ADD_REFERENCE_TO_TABLE && !types.isEmpty();
1285    for (TypeRefComponent t : types) {
1286      if (!(t.getCode().equals("Reference") && t.hasProfile()))
1287        allReference = false;
1288    }
1289    if (allReference) {
1290      c.getPieces().add(gen.new Piece(corePath+"references.html", "Reference", null));
1291      c.getPieces().add(gen.new Piece(null, "(", null));
1292    }
1293    TypeRefComponent tl = null;
1294    for (TypeRefComponent t : types) {
1295      if (first)
1296        first = false;
1297      else if (allReference)
1298        c.addPiece(checkForNoChange(tl, gen.new Piece(null," | ", null)));
1299      else
1300        c.addPiece(checkForNoChange(tl, gen.new Piece(null,", ", null)));
1301      tl = t;
1302      if (t.getCode().equals("Reference") || (t.getCode().equals("Resource") && t.hasProfile())) {
1303        if (ADD_REFERENCE_TO_TABLE && !allReference) {
1304          c.getPieces().add(gen.new Piece(corePath+"references.html", "Reference", null));
1305          c.getPieces().add(gen.new Piece(null, "(", null));
1306        }
1307        if (t.hasProfile() && t.getProfile().get(0).getValue().startsWith("http://hl7.org/fhir/StructureDefinition/")) {
1308          StructureDefinition sd = context.fetchResource(StructureDefinition.class, t.getProfile().get(0).getValue());
1309          if (sd != null) {
1310            String disp = sd.hasDisplay() ? sd.getDisplay() : sd.getName();
1311            c.addPiece(checkForNoChange(t, gen.new Piece(corePath+sd.getUserString("path"), disp, null)));
1312          } else {
1313            String rn = t.getProfile().get(0).getValue().substring(40);
1314            c.addPiece(checkForNoChange(t, gen.new Piece(corePath+pkp.getLinkFor(rn), rn, null)));
1315          }
1316        } else if (t.getProfile().size() == 0) {
1317          c.addPiece(checkForNoChange(t, gen.new Piece(null, t.getCode(), null)));
1318        } else if (t.getProfile().get(0).getValue().startsWith("#"))
1319          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+profileBaseFileName+"."+t.getProfile().get(0).getValue().substring(1).toLowerCase()+".html", t.getProfile().get(0).getValue(), null)));
1320        else
1321          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+t.getProfile().get(0).getValue(), t.getProfile().get(0).getValue(), null)));
1322        if (ADD_REFERENCE_TO_TABLE && !allReference) {
1323          c.getPieces().add(gen.new Piece(null, ")", null));
1324        }
1325      } else if (t.hasProfile()) { // a profiled type
1326        String ref;
1327        ref = pkp.getLinkForProfile(profile, t.getProfile().get(0).getValue());
1328        if (ref != null) {
1329          String[] parts = ref.split("\\|");
1330          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+parts[0], parts[1], t.getCode())));
1331        } else
1332          c.addPiece(checkForNoChange(t, gen.new Piece(corePath+ref, t.getCode(), null)));
1333      } else if (pkp.hasLinkFor(t.getCode())) {
1334        c.addPiece(checkForNoChange(t, gen.new Piece(corePath+pkp.getLinkFor(t.getCode()), t.getCode(), null)));
1335      } else
1336        c.addPiece(checkForNoChange(t, gen.new Piece(null, t.getCode(), null)));
1337    }
1338    if (allReference) {
1339      c.getPieces().add(gen.new Piece(null, ")", null));
1340    }
1341    return c;
1342  }
1343
1344  private ElementDefinition getElementByName(List<ElementDefinition> elements, String contentReference) {
1345    for (ElementDefinition ed : elements)
1346      if (ed.hasName() && ("#"+ed.getName()).equals(contentReference))
1347        return ed;
1348    return null;
1349  }
1350
1351
1352  public static String describeExtensionContext(StructureDefinition ext) {
1353    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1354    for (StringType t : ext.getContext())
1355      b.append(t.getValue());
1356    if (!ext.hasContextType())
1357      throw new Error("no context type on "+ext.getUrl());
1358    switch (ext.getContextType()) {
1359    case DATATYPE: return "Use on data type: "+b.toString();
1360    case EXTENSION: return "Use on extension: "+b.toString();
1361    case RESOURCE: return "Use on element: "+b.toString();
1362    default:
1363      return "??";
1364    }
1365  }
1366
1367  private String describeCardinality(ElementDefinition definition, ElementDefinition fallback, UnusedTracker tracker) {
1368    IntegerType min = definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
1369    StringType max = definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
1370    if (min.isEmpty() && fallback != null)
1371      min = fallback.getMinElement();
1372    if (max.isEmpty() && fallback != null)
1373      max = fallback.getMaxElement();
1374
1375    tracker.used = !max.isEmpty() && !max.getValue().equals("0");
1376
1377    if (min.isEmpty() && max.isEmpty())
1378      return null;
1379    else
1380      return (!min.hasValue() ? "" : Integer.toString(min.getValue())) + ".." + (!max.hasValue() ? "" : max.getValue());
1381  }
1382
1383  private void genCardinality(HierarchicalTableGenerator gen, ElementDefinition definition, Row row, boolean hasDef, UnusedTracker tracker, ElementDefinition fallback) {
1384    IntegerType min = !hasDef ? new IntegerType() : definition.hasMinElement() ? definition.getMinElement() : new IntegerType();
1385    StringType max = !hasDef ? new StringType() : definition.hasMaxElement() ? definition.getMaxElement() : new StringType();
1386    if (min.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
1387      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
1388      min = base.getMinElement().copy();
1389      min.setUserData(DERIVATION_EQUALS, true);
1390    }
1391    if (max.isEmpty() && definition.getUserData(DERIVATION_POINTER) != null) {
1392      ElementDefinition base = (ElementDefinition) definition.getUserData(DERIVATION_POINTER);
1393      max = base.getMaxElement().copy();
1394      max.setUserData(DERIVATION_EQUALS, true);
1395    }
1396    if (min.isEmpty() && fallback != null)
1397      min = fallback.getMinElement();
1398    if (max.isEmpty() && fallback != null)
1399      max = fallback.getMaxElement();
1400
1401    if (!max.isEmpty())
1402      tracker.used = !max.getValue().equals("0");
1403
1404    Cell cell = gen.new Cell(null, null, null, null, null);
1405    row.getCells().add(cell);
1406    if (!min.isEmpty() || !max.isEmpty()) {
1407      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !min.hasValue() ? "" : Integer.toString(min.getValue()), null)));
1408      cell.addPiece(checkForNoChange(min, max, gen.new Piece(null, "..", null)));
1409      cell.addPiece(checkForNoChange(min, gen.new Piece(null, !max.hasValue() ? "" : max.getValue(), null)));
1410    }
1411  }
1412
1413
1414  private Piece checkForNoChange(Element source, Piece piece) {
1415    if (source.hasUserData(DERIVATION_EQUALS)) {
1416      piece.addStyle("opacity: 0.4");
1417    }
1418    return piece;
1419  }
1420
1421  private Piece checkForNoChange(Element src1, Element src2, Piece piece) {
1422    if (src1.hasUserData(DERIVATION_EQUALS) && src2.hasUserData(DERIVATION_EQUALS)) {
1423      piece.addStyle("opacity: 0.5");
1424    }
1425    return piece;
1426  }
1427
1428  public XhtmlNode generateTable(String defFile, StructureDefinition profile, boolean diff, String imageFolder, boolean inlineGraphics, String profileBaseFileName, boolean snapshot, String corePath, boolean logicalModel, Set<String> outputTracker) throws IOException, FHIRException {
1429    assert(diff != snapshot);// check it's ok to get rid of one of these
1430    HierarchicalTableGenerator gen = new HierarchicalTableGenerator(imageFolder, inlineGraphics);
1431    TableModel model = gen.initNormalTable(corePath, false, true, profile.getId()+(diff ? "d" : "s"), false);
1432    List<ElementDefinition> list = diff ? profile.getDifferential().getElement() : profile.getSnapshot().getElement();
1433    List<StructureDefinition> profiles = new ArrayList<StructureDefinition>();
1434    profiles.add(profile);
1435    genElement(defFile == null ? null : defFile+"#"+profile.getId()+".", gen, model.getRows(), list.get(0), list, profiles, diff, profileBaseFileName, null, snapshot, corePath, true, logicalModel);
1436    try {
1437                return gen.generate(model, corePath, 0, outputTracker);
1438        } catch (org.hl7.fhir.exceptions.FHIRException e) {
1439                throw new FHIRException(e.getMessage(), e);
1440        }
1441  }
1442
1443  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, boolean root, boolean logicalModel) throws IOException {
1444    StructureDefinition profile = profiles == null ? null : profiles.get(profiles.size()-1);
1445    String s = tail(element.getPath());
1446    List<ElementDefinition> children = getChildren(all, element);
1447    boolean isExtension = (s.equals("extension") || s.equals("modifierExtension"));
1448    if (!snapshot && isExtension && extensions != null && extensions != isExtension)
1449      return;
1450
1451    if (!onlyInformationIsMapping(all, element)) {
1452      Row row = gen.new Row();
1453      row.setAnchor(element.getPath());
1454      row.setColor(getRowColor(element));
1455      boolean hasDef = element != null;
1456      boolean ext = false;
1457      if (s.equals("extension") || s.equals("modifierExtension")) {
1458        if (element.hasType() && element.getType().get(0).hasProfile() && extensionIsComplex(element.getType().get(0).getProfile().get(0).getValue()))
1459          row.setIcon("icon_extension_complex.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_COMPLEX);
1460        else
1461          row.setIcon("icon_extension_simple.png", HierarchicalTableGenerator.TEXT_ICON_EXTENSION_SIMPLE);
1462        ext = true;
1463      } else if (!hasDef || element.getType().size() == 0)
1464        row.setIcon("icon_element.gif", HierarchicalTableGenerator.TEXT_ICON_ELEMENT);
1465      else if (hasDef && element.getType().size() > 1) {
1466        if (allTypesAre(element.getType(), "Reference"))
1467          row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
1468        else
1469          row.setIcon("icon_choice.gif", HierarchicalTableGenerator.TEXT_ICON_CHOICE);
1470      } else if (hasDef && element.getType().get(0).getCode() != null && element.getType().get(0).getCode().startsWith("@"))
1471        row.setIcon("icon_reuse.png", HierarchicalTableGenerator.TEXT_ICON_REUSE);
1472      else if (hasDef && isPrimitive(element.getType().get(0).getCode()))
1473        row.setIcon("icon_primitive.png", HierarchicalTableGenerator.TEXT_ICON_PRIMITIVE);
1474      else if (hasDef && isReference(element.getType().get(0).getCode()))
1475        row.setIcon("icon_reference.png", HierarchicalTableGenerator.TEXT_ICON_REFERENCE);
1476      else if (hasDef && isDataType(element.getType().get(0).getCode()))
1477        row.setIcon("icon_datatype.gif", HierarchicalTableGenerator.TEXT_ICON_DATATYPE);
1478      else
1479        row.setIcon("icon_resource.png", HierarchicalTableGenerator.TEXT_ICON_RESOURCE);
1480      String ref = defPath == null ? null : defPath + makePathLink(element);
1481      UnusedTracker used = new UnusedTracker();
1482      used.used = true;
1483      Cell left = gen.new Cell(null, ref, s, !hasDef ? null : element.getDefinition(), null);
1484      row.getCells().add(left);
1485      Cell gc = gen.new Cell();
1486      row.getCells().add(gc);
1487      if (element != null && element.getIsModifier())
1488        checkForNoChange(element.getIsModifierElement(), gc.addStyledText("This element is a modifier element", "?!", null, null, null, false));
1489      if (element != null && element.getMustSupport())
1490        checkForNoChange(element.getMustSupportElement(), gc.addStyledText("This element must be supported", "S", null, null, null, false));
1491      if (element != null && element.getIsSummary())
1492        checkForNoChange(element.getIsSummaryElement(), gc.addStyledText("This element is included in summaries", "∑", null, null, null, false));
1493      if (element != null && (!element.getConstraint().isEmpty() || !element.getCondition().isEmpty()))
1494        gc.addStyledText("This element has or is affected by some invariants", "I", null, null, null, false);
1495
1496      ExtensionContext extDefn = null;
1497      if (ext) {
1498        if (element != null && element.getType().size() == 1 && element.getType().get(0).hasProfile()) {
1499        extDefn = locateExtension(StructureDefinition.class, element.getType().get(0).getProfile().get(0).getValue());
1500          if (extDefn == null) {
1501            genCardinality(gen, element, row, hasDef, used, null);
1502            row.getCells().add(gen.new Cell(null, null, "?? "+element.getType().get(0).getProfile(), null, null));
1503            generateDescription(gen, row, element, null, used.used, profile.getUrl(), element.getType().get(0).getProfile().get(0).getValue(), profile, corePath, root, logicalModel);
1504          } else {
1505            String name = urltail(element.getType().get(0).getProfile().get(0).getValue());
1506            left.getPieces().get(0).setText(name);
1507            // left.getPieces().get(0).setReference((String) extDefn.getExtensionStructure().getTag("filename"));
1508            left.getPieces().get(0).setHint("Extension URL = "+extDefn.getUrl());
1509            genCardinality(gen, element, row, hasDef, used, extDefn.getElement());
1510            ElementDefinition valueDefn = extDefn.getExtensionValueDefinition();
1511            if (valueDefn != null && !"0".equals(valueDefn.getMax()))
1512               genTypes(gen, row, valueDefn, profileBaseFileName, profile, corePath);
1513             else // if it's complex, we just call it nothing
1514                // genTypes(gen, row, extDefn.getSnapshot().getElement().get(0), profileBaseFileName, profile);
1515              row.getCells().add(gen.new Cell(null, null, "(Complex)", null, null));
1516            generateDescription(gen, row, element, extDefn.getElement(), used.used, null, extDefn.getUrl(), profile, corePath, root, logicalModel);
1517          }
1518        } else {
1519          genCardinality(gen, element, row, hasDef, used, null);
1520          if ("0".equals(element.getMax()))
1521            row.getCells().add(gen.new Cell());            
1522          else
1523            genTypes(gen, row, element, profileBaseFileName, profile, corePath);
1524          generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, root, logicalModel);
1525        }
1526      } else {
1527        genCardinality(gen, element, row, hasDef, used, null);
1528        if (hasDef && !"0".equals(element.getMax()))
1529          genTypes(gen, row, element, profileBaseFileName, profile, corePath);
1530        else
1531          row.getCells().add(gen.new Cell());
1532        generateDescription(gen, row, element, null, used.used, null, null, profile, corePath, root, logicalModel);
1533      }
1534      if (element.hasSlicing()) {
1535        if (standardExtensionSlicing(element)) {
1536          used.used = element.hasType() && element.getType().get(0).hasProfile();
1537          showMissing = false;
1538        } else {
1539          row.setIcon("icon_slice.png", HierarchicalTableGenerator.TEXT_ICON_SLICE);
1540          row.getCells().get(2).getPieces().clear();
1541          for (Cell cell : row.getCells())
1542            for (Piece p : cell.getPieces()) {
1543              p.addStyle("font-style: italic");
1544            }
1545        }
1546      }
1547      if (used.used || showMissing)
1548        rows.add(row);
1549      if (!used.used && !element.hasSlicing()) {
1550        for (Cell cell : row.getCells())
1551          for (Piece p : cell.getPieces()) {
1552            p.setStyle("text-decoration:line-through");
1553            p.setReference(null);
1554          }
1555      } else{
1556        for (ElementDefinition child : children)
1557          if (logicalModel || !child.getPath().endsWith(".id"))
1558            genElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, isExtension, snapshot, corePath, false, logicalModel);
1559        if (!snapshot && (extensions == null || !extensions))
1560          for (ElementDefinition child : children)
1561            if (child.getPath().endsWith(".extension"))
1562              genElement(defPath, gen, row.getSubRows(), child, all, profiles, showMissing, profileBaseFileName, true, false, corePath, false, logicalModel);
1563      }
1564    }
1565  }
1566
1567  private ExtensionContext locateExtension(Class<StructureDefinition> class1, String value)  {
1568    if (value.contains("#")) {
1569      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
1570      if (ext == null)
1571        return null;
1572      String tail = value.substring(value.indexOf("#")+1);
1573      ElementDefinition ed = null;
1574      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
1575        if (tail.equals(ted.getName())) {
1576          ed = ted;
1577          return new ExtensionContext(ext, ed);
1578        }
1579      }
1580      return null;
1581    } else {
1582      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
1583      if (ext == null)
1584        return null;
1585      else 
1586        return new ExtensionContext(ext, ext.getSnapshot().getElement().get(0));
1587    }
1588  }
1589
1590
1591  private boolean extensionIsComplex(String value) {
1592    if (value.contains("#")) {
1593      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value.substring(0, value.indexOf("#")));
1594    if (ext == null)
1595      return false;
1596      String tail = value.substring(value.indexOf("#")+1);
1597      ElementDefinition ed = null;
1598      for (ElementDefinition ted : ext.getSnapshot().getElement()) {
1599        if (tail.equals(ted.getName())) {
1600          ed = ted;
1601          break;
1602        }
1603      }
1604      if (ed == null)
1605        return false;
1606      int i = ext.getSnapshot().getElement().indexOf(ed);
1607      int j = i+1;
1608      while (j < ext.getSnapshot().getElement().size() && !ext.getSnapshot().getElement().get(j).getPath().equals(ed.getPath()))
1609        j++;
1610      return j - i > 5;
1611    } else {
1612      StructureDefinition ext = context.fetchResource(StructureDefinition.class, value);
1613      return ext != null && ext.getSnapshot().getElement().size() > 5;
1614    }
1615  }
1616
1617
1618  private String getRowColor(ElementDefinition element) {
1619    switch (element.getUserInt(UD_ERROR_STATUS)) {
1620    case STATUS_OK: return null;
1621    case STATUS_HINT: return ROW_COLOR_HINT;
1622    case STATUS_WARNING: return ROW_COLOR_WARNING;
1623    case STATUS_ERROR: return ROW_COLOR_ERROR;
1624    case STATUS_FATAL: return ROW_COLOR_FATAL;
1625    default: return null;
1626    }
1627  }
1628
1629
1630  private String urltail(String path) {
1631    if (path.contains("#"))
1632      return path.substring(path.lastIndexOf('#')+1);
1633    if (path.contains("/"))
1634      return path.substring(path.lastIndexOf('/')+1);
1635    else
1636      return path;
1637
1638  }
1639
1640  private boolean standardExtensionSlicing(ElementDefinition element) {
1641    String t = tail(element.getPath());
1642    return (t.equals("extension") || t.equals("modifierExtension"))
1643          && element.getSlicing().getRules() != SlicingRules.CLOSED && element.getSlicing().getDiscriminator().size() == 1 && element.getSlicing().getDiscriminator().get(0).getValue().equals("url");
1644  }
1645
1646
1647  private String makePathLink(ElementDefinition element) {
1648    if (!element.hasName())
1649      return element.getPath();
1650    if (!element.getPath().contains("."))
1651      return element.getName();
1652    return element.getPath().substring(0, element.getPath().lastIndexOf("."))+"."+element.getName();
1653
1654  }
1655
1656  private Cell generateDescription(HierarchicalTableGenerator gen, Row row, ElementDefinition definition, ElementDefinition fallback, boolean used, String baseURL, String url, StructureDefinition profile, String corePath, boolean root, boolean logicalModel) throws IOException {
1657    Cell c = gen.new Cell();
1658    row.getCells().add(c);
1659
1660    if (used) {
1661      if (logicalModel && ToolingExtensions.hasExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) {
1662        if (root) {
1663          c.getPieces().add(gen.new Piece(null, "XML Namespace: ", null).addStyle("font-weight:bold"));
1664          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
1665        } else if (!root && ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace") && 
1666            !ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace").equals(ToolingExtensions.readStringExtension(profile, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))) {
1667          c.getPieces().add(gen.new Piece(null, "XML Namespace: ", null).addStyle("font-weight:bold"));
1668          c.getPieces().add(gen.new Piece(null, ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"), null));        
1669        }
1670      }
1671      
1672      if (definition.hasContentReference()) {
1673        ElementDefinition ed = getElementByName(profile.getSnapshot().getElement(), definition.getContentReference());
1674        if (ed == null)
1675          c.getPieces().add(gen.new Piece(null, "Unknown reference to "+definition.getContentReference(), null));
1676        else
1677          c.getPieces().add(gen.new Piece("#"+ed.getPath(), "See "+ed.getPath(), null));
1678      }
1679      if (definition.getPath().endsWith("url") && definition.hasFixed()) {
1680        c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "\""+buildJson(definition.getFixed())+"\"", null).addStyle("color: darkgreen")));
1681      } else {
1682        if (definition != null && definition.hasShort()) {
1683          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1684          c.addPiece(checkForNoChange(definition.getShortElement(), gen.new Piece(null, definition.getShort(), null)));
1685        } else if (fallback != null && fallback != null && fallback.hasShort()) {
1686          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1687          c.addPiece(checkForNoChange(fallback.getShortElement(), gen.new Piece(null, fallback.getShort(), null)));
1688        }
1689        if (url != null) {
1690          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1691          String fullUrl = url.startsWith("#") ? baseURL+url : url;
1692          StructureDefinition ed = context.fetchResource(StructureDefinition.class, url);
1693          String ref = ed == null ? null : (String) corePath+ed.getUserData("path");
1694          c.getPieces().add(gen.new Piece(null, "URL: ", null).addStyle("font-weight:bold"));
1695          c.getPieces().add(gen.new Piece(ref, fullUrl, null));
1696        }
1697
1698        if (definition.hasSlicing()) {
1699          if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1700          c.getPieces().add(gen.new Piece(null, "Slice: ", null).addStyle("font-weight:bold"));
1701          c.getPieces().add(gen.new Piece(null, describeSlice(definition.getSlicing()), null));
1702        }
1703        if (definition != null) {
1704          if (definition.hasBinding()) {
1705            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1706            BindingResolution br = pkp.resolveBinding(definition.getBinding());
1707            c.getPieces().add(checkForNoChange(definition.getBinding(), gen.new Piece(null, "Binding: ", null).addStyle("font-weight:bold")));
1708            c.getPieces().add(checkForNoChange(definition.getBinding(), gen.new Piece(br.url == null ? null : Utilities.isAbsoluteUrl(br.url)? br.url : corePath+br.url, br.display, null)));
1709            if (definition.getBinding().hasStrength()) {
1710              c.getPieces().add(checkForNoChange(definition.getBinding(), gen.new Piece(null, " (", null)));
1711              c.getPieces().add(checkForNoChange(definition.getBinding(), gen.new Piece(corePath+"terminologies.html#"+definition.getBinding().getStrength().toCode(), definition.getBinding().getStrength().toCode(), definition.getBinding().getStrength().getDefinition())));
1712              c.getPieces().add(gen.new Piece(null, ")", null));
1713            }
1714          }
1715          for (ElementDefinitionConstraintComponent inv : definition.getConstraint()) {
1716            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1717            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getKey()+": ", null).addStyle("font-weight:bold")));
1718            c.getPieces().add(checkForNoChange(inv, gen.new Piece(null, inv.getHuman(), null)));
1719          }
1720          if (definition.hasFixed()) {
1721            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1722            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, "Fixed Value: ", null).addStyle("font-weight:bold")));
1723            c.getPieces().add(checkForNoChange(definition.getFixed(), gen.new Piece(null, buildJson(definition.getFixed()), null).addStyle("color: darkgreen")));
1724          } else if (definition.hasPattern()) {
1725            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1726            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, "Required Pattern: ", null).addStyle("font-weight:bold")));
1727            c.getPieces().add(checkForNoChange(definition.getPattern(), gen.new Piece(null, buildJson(definition.getPattern()), null).addStyle("color: darkgreen")));
1728          } else if (definition.hasExample()) {
1729            if (!c.getPieces().isEmpty()) c.addPiece(gen.new Piece("br"));
1730            c.getPieces().add(checkForNoChange(definition.getExample(), gen.new Piece(null, "Example: ", null).addStyle("font-weight:bold")));
1731            c.getPieces().add(checkForNoChange(definition.getExample(), gen.new Piece(null, buildJson(definition.getExample()), null).addStyle("color: darkgreen")));
1732          }
1733        }
1734      }
1735    }
1736    return c;
1737  }
1738
1739  private String buildJson(Type value) throws IOException {
1740    if (value instanceof PrimitiveType)
1741      return ((PrimitiveType) value).asStringValue();
1742
1743    IParser json = context.newJsonParser();
1744    return json.composeString(value, null);
1745  }
1746
1747
1748  public String describeSlice(ElementDefinitionSlicingComponent slicing) {
1749    return (slicing.getOrdered() ? "Ordered, " : "Unordered, ")+describe(slicing.getRules())+", by "+commas(slicing.getDiscriminator());
1750  }
1751
1752  private String commas(List<StringType> discriminator) {
1753    CommaSeparatedStringBuilder c = new CommaSeparatedStringBuilder();
1754    for (StringType id : discriminator)
1755      c.append(id.asStringValue());
1756    return c.toString();
1757  }
1758
1759
1760  private String describe(SlicingRules rules) {
1761    switch (rules) {
1762    case CLOSED : return "Closed";
1763    case OPEN : return "Open";
1764    case OPENATEND : return "Open At End";
1765    default:
1766      return "??";
1767    }
1768  }
1769
1770  private boolean onlyInformationIsMapping(List<ElementDefinition> list, ElementDefinition e) {
1771    return (!e.hasName() && !e.hasSlicing() && (onlyInformationIsMapping(e))) &&
1772        getChildren(list, e).isEmpty();
1773  }
1774
1775  private boolean onlyInformationIsMapping(ElementDefinition d) {
1776    return !d.hasShort() && !d.hasDefinition() &&
1777        !d.hasRequirements() && !d.getAlias().isEmpty() && !d.hasMinElement() &&
1778        !d.hasMax() && !d.getType().isEmpty() && !d.hasContentReference() &&
1779        !d.hasExample() && !d.hasFixed() && !d.hasMaxLengthElement() &&
1780        !d.getCondition().isEmpty() && !d.getConstraint().isEmpty() && !d.hasMustSupportElement() &&
1781        !d.hasBinding();
1782  }
1783
1784  private boolean allTypesAre(List<TypeRefComponent> types, String name) {
1785    for (TypeRefComponent t : types) {
1786      if (!t.getCode().equals(name))
1787        return false;
1788    }
1789    return true;
1790  }
1791
1792  private List<ElementDefinition> getChildren(List<ElementDefinition> all, ElementDefinition element) {
1793    List<ElementDefinition> result = new ArrayList<ElementDefinition>();
1794    int i = all.indexOf(element)+1;
1795    while (i < all.size() && all.get(i).getPath().length() > element.getPath().length()) {
1796      if ((all.get(i).getPath().substring(0, element.getPath().length()+1).equals(element.getPath()+".")) && !all.get(i).getPath().substring(element.getPath().length()+1).contains("."))
1797        result.add(all.get(i));
1798      i++;
1799    }
1800    return result;
1801  }
1802
1803  private String tail(String path) {
1804    if (path.contains("."))
1805      return path.substring(path.lastIndexOf('.')+1);
1806    else
1807      return path;
1808  }
1809
1810  private boolean isDataType(String value) {
1811    return Utilities.existsInList(value, "Identifier", "HumanName", "Address", "ContactPoint", "Timing", "SimpleQuantity", "Quantity", "Attachment", "Range",
1812          "Period", "Ratio", "CodeableConcept", "Coding", "SampledData", "Age", "Distance", "Duration", "Count", "Money");
1813  }
1814
1815  private boolean isReference(String value) {
1816    return value.equals("Reference");
1817  }
1818
1819  public static boolean isPrimitive(String value) {
1820    return value == null || Utilities.existsInListNC(value, "boolean", "integer", "decimal", "base64Binary", "instant", "string", "date", "dateTime", "code", "oid", "uuid", "id", "uri");
1821  }
1822
1823//  private static String listStructures(StructureDefinition p) {
1824//    StringBuilder b = new StringBuilder();
1825//    boolean first = true;
1826//    for (ProfileStructureComponent s : p.getStructure()) {
1827//      if (first)
1828//        first = false;
1829//      else
1830//        b.append(", ");
1831//      if (pkp != null && pkp.hasLinkFor(s.getType()))
1832//        b.append("<a href=\""+pkp.getLinkFor(s.getType())+"\">"+s.getType()+"</a>");
1833//      else
1834//        b.append(s.getType());
1835//    }
1836//    return b.toString();
1837//  }
1838
1839
1840  public StructureDefinition getProfile(StructureDefinition source, String url) {
1841        StructureDefinition profile;
1842        String code;
1843        if (url.startsWith("#")) {
1844                profile = source;
1845                code = url.substring(1);
1846        } else {
1847                String[] parts = url.split("\\#");
1848                profile = context.fetchResource(StructureDefinition.class, parts[0]);
1849      code = parts.length == 1 ? null : parts[1];
1850        }
1851        if (profile == null)
1852                return null;
1853        if (code == null)
1854                return profile;
1855        for (Resource r : profile.getContained()) {
1856                if (r instanceof StructureDefinition && r.getId().equals(code))
1857                        return (StructureDefinition) r;
1858        }
1859        return null;
1860  }
1861
1862
1863
1864  public static class ElementDefinitionHolder {
1865    private String name;
1866    private ElementDefinition self;
1867    private int baseIndex = 0;
1868    private List<ElementDefinitionHolder> children;
1869
1870    public ElementDefinitionHolder(ElementDefinition self) {
1871      super();
1872      this.self = self;
1873      this.name = self.getPath();
1874      children = new ArrayList<ElementDefinitionHolder>();
1875    }
1876
1877    public ElementDefinition getSelf() {
1878      return self;
1879    }
1880
1881    public List<ElementDefinitionHolder> getChildren() {
1882      return children;
1883    }
1884
1885    public int getBaseIndex() {
1886      return baseIndex;
1887    }
1888
1889    public void setBaseIndex(int baseIndex) {
1890      this.baseIndex = baseIndex;
1891    }
1892
1893  }
1894
1895  public static class ElementDefinitionComparer implements Comparator<ElementDefinitionHolder> {
1896
1897    private boolean inExtension;
1898    private List<ElementDefinition> snapshot;
1899    private int prefixLength;
1900    private String base;
1901    private String name;
1902    private Set<String> errors = new HashSet<String>();
1903
1904    public ElementDefinitionComparer(boolean inExtension, List<ElementDefinition> snapshot, String base, int prefixLength, String name) {
1905      this.inExtension = inExtension;
1906      this.snapshot = snapshot;
1907      this.prefixLength = prefixLength;
1908      this.base = base;
1909      this.name = name;
1910    }
1911
1912    @Override
1913    public int compare(ElementDefinitionHolder o1, ElementDefinitionHolder o2) {
1914      if (o1.getBaseIndex() == 0)
1915        o1.setBaseIndex(find(o1.getSelf().getPath()));
1916      if (o2.getBaseIndex() == 0)
1917        o2.setBaseIndex(find(o2.getSelf().getPath()));
1918      return o1.getBaseIndex() - o2.getBaseIndex();
1919    }
1920
1921    private int find(String path) {
1922      String actual = base+path.substring(prefixLength);
1923      for (int i = 0; i < snapshot.size(); i++) {
1924        String p = snapshot.get(i).getPath();
1925        if (p.equals(actual))
1926          return i;
1927        if (p.endsWith("[x]") && actual.startsWith(p.substring(0, p.length()-3)) && !(actual.endsWith("[x]")) && !actual.substring(p.length()-3).contains("."))
1928          return i;
1929      }
1930      if (prefixLength == 0)
1931        errors.add("Differential contains path "+path+" which is not found in the base");
1932      else
1933        errors.add("Differential contains path "+path+" which is actually "+actual+", which is not found in the base");
1934      return 0;
1935    }
1936
1937    public void checkForErrors(List<String> errorList) {
1938      if (errors.size() > 0) {
1939//        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1940//        for (String s : errors)
1941//          b.append("StructureDefinition "+name+": "+s);
1942//        throw new DefinitionException(b.toString());
1943        for (String s : errors)
1944          if (s.startsWith("!"))
1945            errorList.add("!StructureDefinition "+name+": "+s.substring(1));
1946          else
1947            errorList.add("StructureDefinition "+name+": "+s);
1948      }
1949    }
1950  }
1951
1952
1953  public void sortDifferential(StructureDefinition base, StructureDefinition diff, String name, List<String> errors)  {
1954
1955    final List<ElementDefinition> diffList = diff.getDifferential().getElement();
1956    // first, we move the differential elements into a tree
1957    ElementDefinitionHolder edh = new ElementDefinitionHolder(diffList.get(0));
1958
1959    boolean hasSlicing = false;
1960    List<String> paths = new ArrayList<String>(); // in a differential, slicing may not be stated explicitly
1961    for(ElementDefinition elt : diffList) {
1962      if (elt.hasSlicing() || paths.contains(elt.getPath())) {
1963        hasSlicing = true;
1964        break;
1965      }
1966      paths.add(elt.getPath());
1967    }
1968    if(!hasSlicing) {
1969      // if Differential does not have slicing then safe to pre-sort the list
1970      // so elements and subcomponents are together
1971      Collections.sort(diffList, new ElementNameCompare());
1972    }
1973
1974    int i = 1;
1975    processElementsIntoTree(edh, i, diff.getDifferential().getElement());
1976
1977    // now, we sort the siblings throughout the tree
1978    ElementDefinitionComparer cmp = new ElementDefinitionComparer(true, base.getSnapshot().getElement(), "", 0, name);
1979    sortElements(edh, cmp, errors);
1980
1981    // now, we serialise them back to a list
1982    diffList.clear();
1983    writeElements(edh, diffList);
1984  }
1985
1986  private int processElementsIntoTree(ElementDefinitionHolder edh, int i, List<ElementDefinition> list) {
1987    String path = edh.getSelf().getPath();
1988    final String prefix = path + ".";
1989    while (i < list.size() && list.get(i).getPath().startsWith(prefix)) {
1990      ElementDefinitionHolder child = new ElementDefinitionHolder(list.get(i));
1991      edh.getChildren().add(child);
1992      i = processElementsIntoTree(child, i+1, list);
1993    }
1994    return i;
1995  }
1996
1997  private void sortElements(ElementDefinitionHolder edh, ElementDefinitionComparer cmp, List<String> errors) {
1998    if (edh.getChildren().size() == 1)
1999      // 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
2000      edh.getChildren().get(0).baseIndex = cmp.find(edh.getChildren().get(0).getSelf().getPath());
2001    else
2002      Collections.sort(edh.getChildren(), cmp);
2003    cmp.checkForErrors(errors);
2004
2005    for (ElementDefinitionHolder child : edh.getChildren()) {
2006      if (child.getChildren().size() > 0) {
2007        // what we have to check for here is running off the base profile into a data type profile
2008        ElementDefinition ed = cmp.snapshot.get(child.getBaseIndex());
2009        ElementDefinitionComparer ccmp;
2010        if (ed.getType().isEmpty() || isAbstract(ed.getType().get(0).getCode()) || ed.getType().get(0).getCode().equals(ed.getPath())) {
2011          ccmp = new ElementDefinitionComparer(true, cmp.snapshot, cmp.base, cmp.prefixLength, cmp.name);
2012        } else if (ed.getType().get(0).getCode().equals("Extension") && child.getSelf().getType().size() == 1 && child.getSelf().getType().get(0).hasProfile()) {
2013          ccmp = new ElementDefinitionComparer(true, context.fetchResource(StructureDefinition.class, child.getSelf().getType().get(0).getProfile().get(0).getValue()).getSnapshot().getElement(), ed.getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2014        } else if (ed.getType().size() == 1 && !ed.getType().get(0).getCode().equals("*")) {
2015          ccmp = new ElementDefinitionComparer(false, context.fetchTypeDefinition(ed.getType().get(0).getCode()).getSnapshot().getElement(), ed.getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2016        } else if (child.getSelf().getType().size() == 1) {
2017          ccmp = new ElementDefinitionComparer(false, context.fetchTypeDefinition(child.getSelf().getType().get(0).getCode()).getSnapshot().getElement(), child.getSelf().getType().get(0).getCode(), child.getSelf().getPath().length(), cmp.name);
2018        } else if (ed.getPath().endsWith("[x]") && !child.getSelf().getPath().endsWith("[x]")) {
2019          String p = child.getSelf().getPath().substring(ed.getPath().length()-3);
2020          StructureDefinition sd = context.fetchTypeDefinition(p);
2021          if (sd == null)
2022            throw new Error("Unable to find profile "+p);
2023          ccmp = new ElementDefinitionComparer(false, sd.getSnapshot().getElement(), p, child.getSelf().getPath().length(), cmp.name);
2024        } else {
2025          throw new Error("Not handled yet (sortElements: "+ed.getPath()+":"+typeCode(ed.getType())+")");
2026        }
2027        sortElements(child, ccmp, errors);
2028      }
2029    }
2030  }
2031
2032  private boolean isAbstract(String code) {
2033    return code.equals("Element") || code.equals("BackboneElement") || code.equals("Resource") || code.equals("DomainResource");
2034  }
2035
2036
2037  private void writeElements(ElementDefinitionHolder edh, List<ElementDefinition> list) {
2038    list.add(edh.getSelf());
2039    for (ElementDefinitionHolder child : edh.getChildren()) {
2040      writeElements(child, list);
2041    }
2042  }
2043
2044  /**
2045   * First compare element by path then by name if same
2046   */
2047  private static class ElementNameCompare implements Comparator<ElementDefinition> {
2048
2049    @Override
2050    public int compare(ElementDefinition o1, ElementDefinition o2) {
2051      String path1 = normalizePath(o1);
2052      String path2 = normalizePath(o2);
2053      int cmp = path1.compareTo(path2);
2054      if (cmp == 0) {
2055        String name1 = o1.hasName() ? o1.getName() : "";
2056        String name2 = o2.hasName() ? o2.getName() : "";
2057        cmp = name1.compareTo(name2);
2058      }
2059      return cmp;
2060    }
2061
2062    private static String normalizePath(ElementDefinition e) {
2063      if (!e.hasPath()) return "";
2064      String path = e.getPath();
2065      // if sorting element names make sure onset[x] appears before onsetAge, onsetDate, etc.
2066      // so strip off the [x] suffix when comparing the path names.
2067      if (path.endsWith("[x]")) {
2068        path = path.substring(0, path.length()-3);
2069      }
2070      return path;
2071    }
2072
2073  }
2074
2075  // generate schematroins for the rules in a structure definition
2076
2077  public void generateSchematrons(OutputStream dest, StructureDefinition structure) throws IOException, DefinitionException {
2078    if (structure.getDerivation() != TypeDerivationRule.CONSTRAINT)
2079      throw new DefinitionException("not the right kind of structure to generate schematrons for");
2080    if (!structure.hasSnapshot())
2081      throw new DefinitionException("needs a snapshot");
2082
2083        StructureDefinition base = context.fetchResource(StructureDefinition.class, structure.getBaseDefinition());
2084
2085        SchematronWriter sch = new SchematronWriter(dest, SchematronType.PROFILE, base.getName());
2086
2087    ElementDefinition ed = structure.getSnapshot().getElement().get(0);
2088    generateForChildren(sch, "f:"+ed.getPath(), ed, structure, base);
2089    sch.dump();
2090  }
2091
2092  private class Slicer extends ElementDefinitionSlicingComponent {
2093    String criteria = "";
2094    String name = "";   
2095    boolean check;
2096    public Slicer(boolean cantCheck) {
2097      super();
2098      this.check = cantCheck;
2099    }
2100  }
2101  
2102  private Slicer generateSlicer(ElementDefinition child, ElementDefinitionSlicingComponent slicing, StructureDefinition structure) {
2103    // given a child in a structure, it's sliced. figure out the slicing xpath
2104    if (child.getPath().endsWith(".extension")) {
2105      ElementDefinition ued = getUrlFor(structure, child);
2106      if ((ued == null || !ued.hasFixed()) && !(child.getType().get(0).hasProfile()))
2107        return new Slicer(false);
2108      else {
2109      Slicer s = new Slicer(true);
2110      String url = (ued == null || !ued.hasFixed()) ? child.getType().get(0).getProfile().get(0).asStringValue() : ((UriType) ued.getFixed()).asStringValue();
2111      s.name = " with URL = '"+url+"'";
2112      s.criteria = "[@url = '"+url+"']";
2113      return s;
2114      }
2115    } else
2116      return new Slicer(false);
2117  }
2118
2119  private void generateForChildren(SchematronWriter sch, String xpath, ElementDefinition ed, StructureDefinition structure, StructureDefinition base) throws IOException {
2120    //    generateForChild(txt, structure, child);
2121    List<ElementDefinition> children = getChildList(structure, ed);
2122    String sliceName = null;
2123    ElementDefinitionSlicingComponent slicing = null;
2124    for (ElementDefinition child : children) {
2125      String name = tail(child.getPath());
2126      if (child.hasSlicing()) {
2127        sliceName = name;
2128        slicing = child.getSlicing();        
2129      } else if (!name.equals(sliceName))
2130        slicing = null;
2131      
2132      ElementDefinition based = getByPath(base, child.getPath());
2133      boolean doMin = (child.getMin() > 0) && (based == null || (child.getMin() != based.getMin()));
2134      boolean doMax =  !child.getMax().equals("*") && (based == null || (!child.getMax().equals(based.getMax())));
2135      Slicer slicer = slicing == null ? new Slicer(true) : generateSlicer(child, slicing, structure);
2136      if (slicer.check) {
2137        if (doMin || doMax) {
2138          Section s = sch.section(xpath);
2139          Rule r = s.rule(xpath);
2140          if (doMin) 
2141            r.assrt("count(f:"+name+slicer.criteria+") >= "+Integer.toString(child.getMin()), name+slicer.name+": minimum cardinality of '"+name+"' is "+Integer.toString(child.getMin()));
2142          if (doMax) 
2143            r.assrt("count(f:"+name+slicer.criteria+") <= "+child.getMax(), name+slicer.name+": maximum cardinality of '"+name+"' is "+child.getMax());
2144          }
2145        }
2146      }
2147    for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
2148      if (inv.hasXpath()) {
2149        Section s = sch.section(ed.getPath());
2150        Rule r = s.rule(xpath);
2151        r.assrt(inv.getXpath(), (inv.hasId() ? inv.getId()+": " : "")+inv.getHuman()+(inv.hasUserData(IS_DERIVED) ? " (inherited)" : ""));
2152      }
2153    }
2154    for (ElementDefinition child : children) {
2155      String name = tail(child.getPath());
2156      generateForChildren(sch, xpath+"/f:"+name, child, structure, base);
2157    }
2158  }
2159
2160
2161
2162
2163  private ElementDefinition getByPath(StructureDefinition base, String path) {
2164                for (ElementDefinition ed : base.getSnapshot().getElement()) {
2165                        if (ed.getPath().equals(path))
2166                                return ed;
2167                        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)))
2168                                return ed;
2169    }
2170          return null;
2171  }
2172
2173//
2174//private void generateForChild(TextStreamWriter txt,
2175//    StructureDefinition structure, ElementDefinition child) {
2176//  // TODO Auto-generated method stub
2177//
2178//}
2179
2180
2181}