001package org.hl7.fhir.r5.conformance;
002
003import java.io.BufferedOutputStream;
004
005/*-
006 * #%L
007 * org.hl7.fhir.r5
008 * %%
009 * Copyright (C) 2014 - 2019 Health Level 7
010 * %%
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 *      http://www.apache.org/licenses/LICENSE-2.0
016 * 
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 * #L%
023 */
024
025
026import java.io.File;
027import java.io.FileInputStream;
028import java.io.FileOutputStream;
029import java.io.IOException;
030import java.io.InputStream;
031import java.io.OutputStream;
032import java.net.URL;
033import java.net.URLConnection;
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040import java.util.UUID;
041import java.util.zip.ZipEntry;
042import java.util.zip.ZipInputStream;
043
044import org.hl7.fhir.exceptions.DefinitionException;
045import org.hl7.fhir.exceptions.FHIRException;
046import org.hl7.fhir.exceptions.FHIRFormatError;
047import org.hl7.fhir.r5.conformance.ProfileComparer.ProfileComparison;
048import org.hl7.fhir.r5.conformance.ProfileUtilities.ProfileKnowledgeProvider;
049import org.hl7.fhir.r5.context.IWorkerContext;
050import org.hl7.fhir.r5.formats.IParser;
051import org.hl7.fhir.r5.model.Base;
052import org.hl7.fhir.r5.model.Coding;
053import org.hl7.fhir.r5.model.ElementDefinition;
054import org.hl7.fhir.r5.model.ElementDefinition.DiscriminatorType;
055import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent;
056import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent;
057import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionMappingComponent;
058import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingComponent;
059import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent;
060import org.hl7.fhir.r5.model.ElementDefinition.SlicingRules;
061import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
062import org.hl7.fhir.r5.model.Enumerations.BindingStrength;
063import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
064import org.hl7.fhir.r5.model.IntegerType;
065import org.hl7.fhir.r5.model.PrimitiveType;
066import org.hl7.fhir.r5.model.StringType;
067import org.hl7.fhir.r5.model.StructureDefinition;
068import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule;
069import org.hl7.fhir.r5.model.DataType;
070import org.hl7.fhir.r5.model.ValueSet;
071import org.hl7.fhir.r5.model.ValueSet.ConceptReferenceComponent;
072import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent;
073import org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent;
074import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
075import org.hl7.fhir.r5.utils.DefinitionNavigator;
076import org.hl7.fhir.r5.utils.KeyGenerator;
077import org.hl7.fhir.r5.utils.NarrativeGenerator;
078import org.hl7.fhir.r5.utils.ToolingExtensions;
079import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
080import org.hl7.fhir.utilities.Logger.LogMessageType;
081import org.hl7.fhir.utilities.TextFile;
082import org.hl7.fhir.utilities.Utilities;
083import org.hl7.fhir.utilities.validation.ValidationMessage;
084import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
085import org.hl7.fhir.utilities.validation.ValidationMessage.Source;
086import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
087
088/**
089 * A engine that generates difference analysis between two sets of structure 
090 * definitions, typically from 2 different implementation guides. 
091 * 
092 * How this class works is that you create it with access to a bunch of underying
093 * resources that includes all the structure definitions from both implementation 
094 * guides 
095 * 
096 * Once the class is created, you repeatedly pass pairs of structure definitions,
097 * one from each IG, building up a web of difference analyses. This class will
098 * automatically process any internal comparisons that it encounters
099 * 
100 * When all the comparisons have been performed, you can then generate a variety
101 * of output formats
102 * 
103 * @author Grahame Grieve
104 *
105 */
106public class ProfileComparer implements ProfileKnowledgeProvider {
107
108  private IWorkerContext context;
109  private KeyGenerator keygen;
110  private String folder;
111  
112  public ProfileComparer(IWorkerContext context, KeyGenerator keygen, String folder) throws IOException {
113    super();
114    this.context = context;
115    this.keygen = keygen;
116    this.folder = folder;
117    if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) {
118      String f = Utilities.path(folder, "comparison.zip");
119      download("http://www.fhir.org/archive/comparison.zip", f);
120      unzip(f, folder);
121    }
122  }
123
124  private void download(String address, String filename) throws IOException {
125//    System.out.print("Download "+address+" to "+filename);
126    URL url = new URL(address);
127    URLConnection c = url.openConnection();
128    InputStream s = c.getInputStream();
129    FileOutputStream f = new FileOutputStream(filename);
130    transfer(s, f, 1024);
131    f.close();   
132//    System.out.println(" ... "+new File(filename).length()+" bytes");
133  }
134
135
136  public static void transfer(InputStream in, OutputStream out, int buffer) throws IOException {
137    byte[] read = new byte[buffer]; // Your buffer size.
138    while (0 < (buffer = in.read(read)))
139      out.write(read, 0, buffer);
140  }
141
142  /**
143   * Size of the buffer to read/write data
144   */
145  private static final int BUFFER_SIZE = 4096;
146  /**
147   * Extracts a zip file specified by the zipFilePath to a directory specified by
148   * destDirectory (will be created if does not exists)
149   * @param zipFilePath
150   * @param destDirectory
151   * @throws IOException
152   */
153  public void unzip(String zipFilePath, String destDirectory) throws IOException {
154      File destDir = new File(destDirectory);
155      if (!destDir.exists()) {
156          destDir.mkdir();
157      }
158      ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
159      ZipEntry entry = zipIn.getNextEntry();
160      // iterates over entries in the zip file
161      while (entry != null) {
162          String filePath = destDirectory + File.separator + entry.getName();
163          if (!entry.isDirectory()) {
164              // if the entry is a file, extracts it
165              extractFile(zipIn, filePath);
166          } else {
167              // if the entry is a directory, make the directory
168              File dir = new File(filePath);
169              dir.mkdir();
170          }
171          zipIn.closeEntry();
172          entry = zipIn.getNextEntry();
173      }
174      zipIn.close();
175  }
176  /**
177   * Extracts a zip entry (file entry)
178   * @param zipIn
179   * @param filePath
180   * @throws IOException
181   */
182  private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
183      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
184      byte[] bytesIn = new byte[BUFFER_SIZE];
185      int read = 0;
186      while ((read = zipIn.read(bytesIn)) != -1) {
187          bos.write(bytesIn, 0, read);
188      }
189      bos.close();
190  }
191
192
193  public ProfileComparer(IWorkerContext context, String folder) throws IOException {
194    super();
195    this.context = context;
196    this.folder = folder;
197    if (!new File(Utilities.path(folder, "conparison-zip-marker.bin")).exists()) {
198      String f = Utilities.path(folder, "comparison.zip");
199      download("https://www.fhir.org/archive/comparison.zip", f);
200      unzip(f, folder);
201    }
202  }
203
204  private static final int BOTH_NULL = 0;
205  private static final int EITHER_NULL = 1;
206
207  public class ProfileComparison {
208    private String id;
209    /**
210     * the first of two structures that were compared to generate this comparison
211     * 
212     *   In a few cases - selection of example content and value sets - left gets 
213     *   preference over right
214     */
215    private StructureDefinition left;
216
217    /**
218     * the second of two structures that were compared to generate this comparison
219     * 
220     *   In a few cases - selection of example content and value sets - left gets 
221     *   preference over right
222     */
223    private StructureDefinition right;
224
225    
226    public String getId() {
227      return id;
228    }
229    private String leftName() {
230      return left.getName();
231    }
232    private String rightName() {
233      return right.getName();
234    }
235
236    /**
237     * messages generated during the comparison. There are 4 grades of messages:
238     *   information - a list of differences between structures
239     *   warnings - notifies that the comparer is unable to fully compare the structures (constraints differ, open value sets)
240     *   errors - where the structures are incompatible
241     *   fatal errors - some error that prevented full analysis 
242     * 
243     * @return
244     */
245    private List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
246
247    /**
248     * The structure that describes all instances that will conform to both structures 
249     */
250    private StructureDefinition subset;
251
252    /**
253     * The structure that describes all instances that will conform to either structures 
254     */
255    private StructureDefinition superset;
256
257    public StructureDefinition getLeft() {
258      return left;
259    }
260
261    public StructureDefinition getRight() {
262      return right;
263    }
264
265    public List<ValidationMessage> getMessages() {
266      return messages;
267    }
268
269    public StructureDefinition getSubset() {
270      return subset;
271    }
272
273    public StructureDefinition getSuperset() {
274      return superset;
275    }
276    
277    private boolean ruleEqual(String path, ElementDefinition ed, String vLeft, String vRight, String description, boolean nullOK) {
278      if (vLeft == null && vRight == null && nullOK)
279        return true;
280      if (vLeft == null && vRight == null) {
281        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, description+" and not null (null/null)", ValidationMessage.IssueSeverity.ERROR));
282        if (ed != null)
283          status(ed, ProfileUtilities.STATUS_ERROR);
284      }
285      if (vLeft == null || !vLeft.equals(vRight)) {
286        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, description+" ("+vLeft+"/"+vRight+")", ValidationMessage.IssueSeverity.ERROR));
287        if (ed != null)
288          status(ed, ProfileUtilities.STATUS_ERROR);
289      }
290      return true;
291    }
292    
293    private boolean ruleCompares(ElementDefinition ed, DataType vLeft, DataType vRight, String path, int nullStatus) throws IOException {
294      if (vLeft == null && vRight == null && nullStatus == BOTH_NULL)
295        return true;
296      if (vLeft == null && vRight == null) {
297        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Must be the same and not null (null/null)", ValidationMessage.IssueSeverity.ERROR));
298        status(ed, ProfileUtilities.STATUS_ERROR);
299      }
300      if (vLeft == null && nullStatus == EITHER_NULL)
301        return true;
302      if (vRight == null && nullStatus == EITHER_NULL)
303        return true;
304      if (vLeft == null || vRight == null || !Base.compareDeep(vLeft, vRight, false)) {
305        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Must be the same ("+toString(vLeft)+"/"+toString(vRight)+")", ValidationMessage.IssueSeverity.ERROR));
306        status(ed, ProfileUtilities.STATUS_ERROR);
307      }
308      return true;
309    }
310
311    private boolean rule(ElementDefinition ed, boolean test, String path, String message) {
312      if (!test)  {
313        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, message, ValidationMessage.IssueSeverity.ERROR));
314        status(ed, ProfileUtilities.STATUS_ERROR);
315      }
316      return test;
317    }
318
319    private boolean ruleEqual(ElementDefinition ed, boolean vLeft, boolean vRight, String path, String elementName) {
320      if (vLeft != vRight) {
321        messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, elementName+" must be the same ("+vLeft+"/"+vRight+")", ValidationMessage.IssueSeverity.ERROR));
322        status(ed, ProfileUtilities.STATUS_ERROR);
323      }
324      return true;
325    }
326
327    private String toString(DataType val) throws IOException {
328      if (val instanceof PrimitiveType) 
329        return "\"" + ((PrimitiveType) val).getValueAsString()+"\"";
330      
331      IParser jp = context.newJsonParser();
332      return jp.composeString(val, "value");
333    }
334    
335    public int getErrorCount() {
336      int c = 0;
337      for (ValidationMessage vm : messages)
338        if (vm.getLevel() == ValidationMessage.IssueSeverity.ERROR)
339          c++;
340      return c;
341    }
342
343    public int getWarningCount() {
344      int c = 0;
345      for (ValidationMessage vm : messages)
346        if (vm.getLevel() == ValidationMessage.IssueSeverity.WARNING)
347          c++;
348      return c;
349    }
350    
351    public int getHintCount() {
352      int c = 0;
353      for (ValidationMessage vm : messages)
354        if (vm.getLevel() == ValidationMessage.IssueSeverity.INFORMATION)
355          c++;
356      return c;
357    }
358  }
359  
360  /**
361   * Value sets used in the subset and superset
362   */
363  private List<ValueSet> valuesets = new ArrayList<ValueSet>();
364  private List<ProfileComparison> comparisons = new ArrayList<ProfileComparison>();
365  private String id; 
366  private String title;
367  private String leftLink;
368  private String leftName;
369  private String rightLink;
370  private String rightName;
371  
372  
373  public List<ValueSet> getValuesets() {
374    return valuesets;
375  }
376
377  public void status(ElementDefinition ed, int value) {
378    ed.setUserData(ProfileUtilities.UD_ERROR_STATUS, Math.max(value, ed.getUserInt("error-status")));
379  }
380
381  public List<ProfileComparison> getComparisons() {
382    return comparisons;
383  }
384
385  /**
386   * Compare left and right structure definitions to see whether they are consistent or not
387   * 
388   * Note that left and right are arbitrary choices. In one respect, left 
389   * is 'preferred' - the left's example value and data sets will be selected 
390   * over the right ones in the common structure definition
391   * @throws DefinitionException 
392   * @throws IOException 
393   * @throws FHIRFormatError 
394   *  
395   * @
396   */
397  public ProfileComparison compareProfiles(StructureDefinition left, StructureDefinition right) throws DefinitionException, IOException, FHIRFormatError {
398    ProfileComparison outcome = new ProfileComparison();
399    outcome.left = left;
400    outcome.right = right;
401    
402    if (left == null)
403      throw new DefinitionException("No StructureDefinition provided (left)");
404    if (right == null)
405      throw new DefinitionException("No StructureDefinition provided (right)");
406    if (!left.hasSnapshot())
407      throw new DefinitionException("StructureDefinition has no snapshot (left: "+outcome.leftName()+")");
408    if (!right.hasSnapshot())
409      throw new DefinitionException("StructureDefinition has no snapshot (right: "+outcome.rightName()+")");
410    if (left.getSnapshot().getElement().isEmpty())
411      throw new DefinitionException("StructureDefinition snapshot is empty (left: "+outcome.leftName()+")");
412    if (right.getSnapshot().getElement().isEmpty())
413      throw new DefinitionException("StructureDefinition snapshot is empty (right: "+outcome.rightName()+")");
414
415    for (ProfileComparison pc : comparisons) 
416      if (pc.left.getUrl().equals(left.getUrl()) && pc.right.getUrl().equals(right.getUrl()))
417        return pc;
418
419    outcome.id = Integer.toString(comparisons.size()+1);
420    comparisons.add(outcome);
421    
422    DefinitionNavigator ln = new DefinitionNavigator(context, left);
423    DefinitionNavigator rn = new DefinitionNavigator(context, right);
424    
425    // from here on in, any issues go in messages
426    outcome.superset = new StructureDefinition();
427    outcome.subset = new StructureDefinition();
428    keygen.genId(outcome.subset);
429    keygen.genId(outcome.superset);
430    if (outcome.ruleEqual(ln.path(), null,ln.path(), rn.path(), "Base Type is not compatible", false)) {
431      if (compareElements(outcome, ln.path(), ln, rn, null)) {
432        outcome.subset.setName("intersection of "+outcome.leftName()+" and "+outcome.rightName());
433        outcome.subset.setStatus(PublicationStatus.DRAFT);
434        outcome.subset.setKind(outcome.left.getKind());
435        outcome.subset.setType(outcome.left.getType());
436        outcome.subset.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/"+outcome.subset.getType());
437        outcome.subset.setDerivation(TypeDerivationRule.CONSTRAINT);
438        outcome.subset.setAbstract(false);
439        outcome.superset.setName("union of "+outcome.leftName()+" and "+outcome.rightName());
440        outcome.superset.setStatus(PublicationStatus.DRAFT);
441        outcome.superset.setKind(outcome.left.getKind());
442        outcome.superset.setType(outcome.left.getType());
443        outcome.superset.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/"+outcome.subset.getType());
444        outcome.superset.setAbstract(false);
445        outcome.superset.setDerivation(TypeDerivationRule.CONSTRAINT);
446      } else {
447        outcome.subset = null;
448        outcome.superset = null;
449      }
450    }
451    return outcome;
452  }
453
454  /**
455   * left and right refer to the same element. Are they compatible?   
456   * @param outcome 
457   * @param outcome
458   * @param path
459   * @param left
460   * @param right
461   * @- if there's a problem that needs fixing in this code
462   * @throws DefinitionException 
463   * @throws IOException 
464   * @throws FHIRFormatError 
465   */
466  private boolean compareElements(ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right, String sliceName) throws DefinitionException, IOException, FHIRFormatError {
467//    preconditions:
468    assert(path != null);  
469    assert(left != null);
470    assert(right != null);
471    assert(left.path().equals(right.path()));
472    
473    // we ignore slicing right now - we're going to clone the root one anyway, and then think about clones 
474    // simple stuff
475    ElementDefinition subset = new ElementDefinition();
476    subset.setPath(left.path());
477    if (sliceName != null)
478      subset.setSliceName(sliceName);
479    
480    // not allowed to be different: 
481    subset.getRepresentation().addAll(left.current().getRepresentation()); // can't be bothered even testing this one
482    if (!outcome.ruleCompares(subset, left.current().getDefaultValue(), right.current().getDefaultValue(), path+".defaultValue[x]", BOTH_NULL))
483      return false;
484    subset.setDefaultValue(left.current().getDefaultValue());
485    if (!outcome.ruleEqual(path, subset, left.current().getMeaningWhenMissing(), right.current().getMeaningWhenMissing(), "meaningWhenMissing Must be the same", true))
486      return false;
487    subset.setMeaningWhenMissing(left.current().getMeaningWhenMissing());
488    if (!outcome.ruleEqual(subset, left.current().getIsModifier(), right.current().getIsModifier(), path, "isModifier"))
489      return false;
490    subset.setIsModifier(left.current().getIsModifier());
491    if (!outcome.ruleEqual(subset, left.current().getIsSummary(), right.current().getIsSummary(), path, "isSummary"))
492      return false;
493    subset.setIsSummary(left.current().getIsSummary());
494    
495    // descriptive properties from ElementDefinition - merge them:
496    subset.setLabel(mergeText(subset, outcome, path, "label", left.current().getLabel(), right.current().getLabel()));
497    subset.setShort(mergeText(subset, outcome, path, "short", left.current().getShort(), right.current().getShort()));
498    subset.setDefinition(mergeText(subset, outcome, path, "definition", left.current().getDefinition(), right.current().getDefinition()));
499    subset.setComment(mergeText(subset, outcome, path, "comments", left.current().getComment(), right.current().getComment()));
500    subset.setRequirements(mergeText(subset, outcome, path, "requirements", left.current().getRequirements(), right.current().getRequirements()));
501    subset.getCode().addAll(mergeCodings(left.current().getCode(), right.current().getCode()));
502    subset.getAlias().addAll(mergeStrings(left.current().getAlias(), right.current().getAlias()));
503    subset.getMapping().addAll(mergeMappings(left.current().getMapping(), right.current().getMapping()));
504    // left will win for example
505    subset.setExample(left.current().hasExample() ? left.current().getExample() : right.current().getExample());
506
507    subset.setMustSupport(left.current().getMustSupport() || right.current().getMustSupport());
508    ElementDefinition superset = subset.copy();
509
510
511    // compare and intersect
512    superset.setMin(unionMin(left.current().getMin(), right.current().getMin()));
513    superset.setMax(unionMax(left.current().getMax(), right.current().getMax()));
514    subset.setMin(intersectMin(left.current().getMin(), right.current().getMin()));
515    subset.setMax(intersectMax(left.current().getMax(), right.current().getMax()));
516    outcome.rule(subset, subset.getMax().equals("*") || Integer.parseInt(subset.getMax()) >= subset.getMin(), path, "Cardinality Mismatch: "+card(left)+"/"+card(right));
517    
518    superset.getType().addAll(unionTypes(path, left.current().getType(), right.current().getType()));
519    subset.getType().addAll(intersectTypes(subset, outcome, path, left.current().getType(), right.current().getType()));
520    outcome.rule(subset, !subset.getType().isEmpty() || (!left.current().hasType() && !right.current().hasType()), path, "Type Mismatch:\r\n  "+typeCode(left)+"\r\n  "+typeCode(right));
521//    <fixed[x]><!-- ?? 0..1 * Value must be exactly this --></fixed[x]>
522//    <pattern[x]><!-- ?? 0..1 * Value must have at least these property values --></pattern[x]>
523    superset.setMaxLengthElement(unionMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
524    subset.setMaxLengthElement(intersectMaxLength(left.current().getMaxLength(), right.current().getMaxLength()));
525    if (left.current().hasBinding() || right.current().hasBinding()) {
526      compareBindings(outcome, subset, superset, path, left.current(), right.current());
527    }
528
529    // note these are backwards
530    superset.getConstraint().addAll(intersectConstraints(path, left.current().getConstraint(), right.current().getConstraint()));
531    subset.getConstraint().addAll(unionConstraints(subset, outcome, path, left.current().getConstraint(), right.current().getConstraint()));
532
533    // add the children
534    outcome.subset.getSnapshot().getElement().add(subset);
535    outcome.superset.getSnapshot().getElement().add(superset);
536    boolean ret = compareChildren(subset, outcome, path, left, right);
537    
538    // now process the slices
539    if (left.current().hasSlicing() || right.current().hasSlicing()) {
540      assert sliceName == null;
541      if (isExtension(left.path()))
542        return compareExtensions(outcome, path, superset, subset, left, right);
543//      return true;
544      else {
545        ElementDefinitionSlicingComponent slicingL = left.current().getSlicing();
546        ElementDefinitionSlicingComponent slicingR = right.current().getSlicing();
547        // well, this is tricky. If one is sliced, and the other is not, then in general, the union just ignores the slices, and the intersection is the slices.
548        if (left.current().hasSlicing() && !right.current().hasSlicing()) { 
549          // the super set is done. Any restrictions in the slices are irrelevant to what the super set says, except that we're going sum up the value sets if we can (for documentation purposes) (todo)
550          // the minimum set is the slicing specified in the slicer
551          subset.setSlicing(slicingL);
552          // stick everything from the right to do with the slices to the subset 
553          copySlices(outcome.subset.getSnapshot().getElement(), left.getStructure().getSnapshot().getElement(), left.slices());
554        } else if (!left.current().hasSlicing() && right.current().hasSlicing()) { 
555          // the super set is done. Any restrictions in the slices are irrelevant to what the super set says, except that we're going sum up the value sets if we can (for documentation purposes) (todo)
556          // the minimum set is the slicing specified in the slicer
557          subset.setSlicing(slicingR);
558          // stick everything from the right to do with the slices to the subset 
559          copySlices(outcome.subset.getSnapshot().getElement(), right.getStructure().getSnapshot().getElement(), right.slices());
560        } else if (isTypeSlicing(slicingL) || isTypeSlicing(slicingR)) {
561          superset.getSlicing().setRules(SlicingRules.OPEN).setOrdered(false).addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
562          subset.getSlicing().setRules(slicingL.getRules() == SlicingRules.CLOSED || slicingR.getRules() == SlicingRules.CLOSED ? SlicingRules.OPEN : SlicingRules.CLOSED).setOrdered(false).addDiscriminator().setType(DiscriminatorType.TYPE).setPath("$this");
563
564          // the superset is the union of the types 
565          // the subset is the intersection of them 
566          List<DefinitionNavigator> handled = new ArrayList<>();
567          for (DefinitionNavigator t : left.slices()) {
568            DefinitionNavigator r = findMatchingSlice(right.slices(), t);
569            if (r == null) {
570              copySlice(outcome.superset.getSnapshot().getElement(), left.getStructure().getSnapshot().getElement(), t);              
571            } else {
572              handled.add(r);
573              ret = compareElements(outcome, path+":"+t.current().getSliceName(), t, r, t.current().getSliceName()) && ret;
574            }
575          }
576          for (DefinitionNavigator t : right.slices()) {
577            if (!handled.contains(t)) {
578              copySlice(outcome.superset.getSnapshot().getElement(), right.getStructure().getSnapshot().getElement(), t);
579            }
580          }
581        } else if (slicingMatches(slicingL, slicingR)) {
582          // if it's the same, we can try matching the slices - though we might have to give up without getting matches correct
583          // there amy be implied consistency we can't reason about 
584          throw new DefinitionException("Slicing matches but is not handled yet at "+left.current().getId()+": ("+ProfileUtilities.summarizeSlicing(slicingL)+")");
585        } else  {
586          // if the slicing is different, we can't compare them - or can we?
587          throw new DefinitionException("Slicing doesn't match at "+left.current().getId()+": ("+ProfileUtilities.summarizeSlicing(slicingL)+" / "+ProfileUtilities.summarizeSlicing(slicingR)+")");
588        }
589      }
590    // todo: name 
591    }
592    return ret;
593  }
594
595
596  private DefinitionNavigator findMatchingSlice(List<DefinitionNavigator> slices, DefinitionNavigator tgt) {
597    for (DefinitionNavigator t : slices) {
598      if (sliceMatchesByType(t, tgt)) 
599        return t;
600    }
601    return null;
602  }
603
604  private boolean sliceMatchesByType(DefinitionNavigator t, DefinitionNavigator tgt) {
605    return t.current().typeSummary().equals(tgt.current().typeSummary());
606  }
607
608  private void copySlices(List<ElementDefinition> target, List<ElementDefinition> source, List<DefinitionNavigator> list) {
609    for (DefinitionNavigator slice : list) {
610      copySlice(target, source, slice);
611    }    
612  }
613
614  public void copySlice(List<ElementDefinition> target, List<ElementDefinition> source, DefinitionNavigator slice) {
615    target.add(slice.current().copy());
616    int i = source.indexOf(slice.current())+1;
617    while (i < source.size() && source.get(i).getPath().startsWith(slice.current().getPath()+".")) {
618      target.add(source.get(i).copy());
619      i++;
620    }
621  }
622
623  private boolean isTypeSlicing(ElementDefinitionSlicingComponent slicing) {
624    if (slicing.getDiscriminator().size() == 1 && slicing.getDiscriminatorFirstRep().getType() == DiscriminatorType.TYPE && "$this".equals(slicing.getDiscriminatorFirstRep().getPath()))
625      return true;
626    return false;
627  }
628
629  private boolean slicingMatches(ElementDefinitionSlicingComponent l, ElementDefinitionSlicingComponent r) {
630    if (l.getDiscriminator().size() != r.getDiscriminator().size())
631      return false;
632    for (int i = 0; i < l.getDiscriminator().size(); i++) {
633      if (!slicingMatches(l.getDiscriminator().get(i), r.getDiscriminator().get(i)))
634        return false;
635    }
636    return l.getOrdered() == r.getOrdered();
637  }
638
639  private boolean slicingMatches(ElementDefinitionSlicingDiscriminatorComponent l,  ElementDefinitionSlicingDiscriminatorComponent r) {
640    return l.getType() == r.getType() && l.getPath().equals(r.getPath());
641  }
642
643  private class ExtensionUsage {
644    private DefinitionNavigator defn;
645    private int minSuperset;
646    private int minSubset;
647    private String maxSuperset;
648    private String maxSubset;
649    private boolean both = false;
650    
651    public ExtensionUsage(DefinitionNavigator defn, int min, String max) {
652      super();
653      this.defn = defn;
654      this.minSubset = min;
655      this.minSuperset = min;
656      this.maxSubset = max;
657      this.maxSuperset = max;
658    }
659    
660  }
661  private boolean compareExtensions(ProfileComparison outcome, String path, ElementDefinition superset, ElementDefinition subset, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException {
662    // for now, we don't handle sealed (or ordered) extensions
663    
664    // for an extension the superset is all extensions, and the subset is.. all extensions - well, unless thay are sealed. 
665    // but it's not useful to report that. instead, we collate the defined ones, and just adjust the cardinalities
666    Map<String, ExtensionUsage> map = new HashMap<String, ExtensionUsage>();
667    
668    if (left.slices() != null)
669      for (DefinitionNavigator ex : left.slices()) {
670        String url = ex.current().getType().get(0).getProfile().get(0).getValue();
671        if (map.containsKey(url))
672          throw new DefinitionException("Duplicate Extension "+url+" at "+path);
673        else
674          map.put(url, new ExtensionUsage(ex, ex.current().getMin(), ex.current().getMax()));
675      }
676    if (right.slices() != null)
677      for (DefinitionNavigator ex : right.slices()) {
678        String url = ex.current().getType().get(0).getProfile().get(0).getValue();
679        if (map.containsKey(url)) {
680          ExtensionUsage exd = map.get(url);
681          exd.minSuperset = unionMin(exd.defn.current().getMin(), ex.current().getMin());
682          exd.maxSuperset = unionMax(exd.defn.current().getMax(), ex.current().getMax());
683          exd.minSubset = intersectMin(exd.defn.current().getMin(), ex.current().getMin());
684          exd.maxSubset = intersectMax(exd.defn.current().getMax(), ex.current().getMax());
685          exd.both = true;
686          outcome.rule(subset, exd.maxSubset.equals("*") || Integer.parseInt(exd.maxSubset) >= exd.minSubset, path, "Cardinality Mismatch on extension: "+card(exd.defn)+"/"+card(ex));
687        } else {
688          map.put(url, new ExtensionUsage(ex, ex.current().getMin(), ex.current().getMax()));
689        }
690      }
691    List<String> names = new ArrayList<String>();
692    names.addAll(map.keySet());
693    Collections.sort(names);
694    for (String name : names) {
695      ExtensionUsage exd = map.get(name);
696      if (exd.both)
697        outcome.subset.getSnapshot().getElement().add(exd.defn.current().copy().setMin(exd.minSubset).setMax(exd.maxSubset));
698      outcome.superset.getSnapshot().getElement().add(exd.defn.current().copy().setMin(exd.minSuperset).setMax(exd.maxSuperset));
699    }    
700    return true;
701  }
702
703  private boolean isExtension(String path) {
704    return path.endsWith(".extension") || path.endsWith(".modifierExtension");
705  }
706
707  private boolean compareChildren(ElementDefinition ed, ProfileComparison outcome, String path, DefinitionNavigator left, DefinitionNavigator right) throws DefinitionException, IOException, FHIRFormatError {
708    List<DefinitionNavigator> lc = left.children();
709    List<DefinitionNavigator> rc = right.children();
710    // it's possible that one of these profiles walks into a data type and the other doesn't
711    // if it does, we have to load the children for that data into the profile that doesn't 
712    // walk into it
713    if (lc.isEmpty() && !rc.isEmpty() && right.current().getType().size() == 1 && left.hasTypeChildren(right.current().getType().get(0)))
714      lc = left.childrenFromType(right.current().getType().get(0));
715    if (rc.isEmpty() && !lc.isEmpty() && left.current().getType().size() == 1 && right.hasTypeChildren(left.current().getType().get(0)))
716      rc = right.childrenFromType(left.current().getType().get(0));
717    if (lc.size() != rc.size()) {
718      outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Different number of children at "+path+" ("+Integer.toString(lc.size())+"/"+Integer.toString(rc.size())+")", ValidationMessage.IssueSeverity.ERROR));
719      status(ed, ProfileUtilities.STATUS_ERROR);
720      return false;      
721    } else {
722      for (int i = 0; i < lc.size(); i++) {
723        DefinitionNavigator l = lc.get(i);
724        DefinitionNavigator r = rc.get(i);
725        String cpath = comparePaths(l.path(), r.path(), path, l.nameTail(), r.nameTail());
726        if (cpath != null) {
727          if (!compareElements(outcome, cpath, l, r, null))
728            return false;
729        } else {
730          outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Different path at "+path+"["+Integer.toString(i)+"] ("+l.path()+"/"+r.path()+")", ValidationMessage.IssueSeverity.ERROR));
731          status(ed, ProfileUtilities.STATUS_ERROR);
732          return false;
733        }
734      }
735    }
736    return true;
737  }
738
739  private String comparePaths(String path1, String path2, String path, String tail1, String tail2) {
740    if (tail1.equals(tail2)) {
741      return path+"."+tail1;
742    } else if (tail1.endsWith("[x]") && tail2.startsWith(tail1.substring(0, tail1.length()-3))) {
743      return path+"."+tail1;
744    } else if (tail2.endsWith("[x]") && tail1.startsWith(tail2.substring(0, tail2.length()-3))) {
745      return path+"."+tail2;
746    } else 
747      return null;
748  }
749
750  private boolean compareBindings(ProfileComparison outcome, ElementDefinition subset, ElementDefinition superset, String path, ElementDefinition lDef, ElementDefinition rDef) throws FHIRFormatError {
751    assert(lDef.hasBinding() || rDef.hasBinding());
752    if (!lDef.hasBinding()) {
753      subset.setBinding(rDef.getBinding());
754      // technically, the super set is unbound, but that's not very useful - so we use the provided on as an example
755      superset.setBinding(rDef.getBinding().copy());
756      superset.getBinding().setStrength(BindingStrength.EXAMPLE);
757      return true;
758    }
759    if (!rDef.hasBinding()) {
760      subset.setBinding(lDef.getBinding());
761      superset.setBinding(lDef.getBinding().copy());
762      superset.getBinding().setStrength(BindingStrength.EXAMPLE);
763      return true;
764    }
765    ElementDefinitionBindingComponent left = lDef.getBinding();
766    ElementDefinitionBindingComponent right = rDef.getBinding();
767    if (Base.compareDeep(left, right, false)) {
768      subset.setBinding(left);
769      superset.setBinding(right);      
770    }
771    
772    // if they're both examples/preferred then:
773    // subset: left wins if they're both the same
774    // superset: 
775    if (isPreferredOrExample(left) && isPreferredOrExample(right)) {
776      if (right.getStrength() == BindingStrength.PREFERRED && left.getStrength() == BindingStrength.EXAMPLE && !Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) { 
777        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Example/preferred bindings differ at "+path+" using binding from "+outcome.rightName(), ValidationMessage.IssueSeverity.INFORMATION));
778        status(subset, ProfileUtilities.STATUS_HINT);
779        subset.setBinding(right);
780        superset.setBinding(unionBindings(superset, outcome, path, left, right));
781      } else {
782        if ((right.getStrength() != BindingStrength.EXAMPLE || left.getStrength() != BindingStrength.EXAMPLE) && !Base.compareDeep(left.getValueSet(), right.getValueSet(), false) ) { 
783          outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Example/preferred bindings differ at "+path+" using binding from "+outcome.leftName(), ValidationMessage.IssueSeverity.INFORMATION));
784          status(subset, ProfileUtilities.STATUS_HINT);
785        }
786        subset.setBinding(left);
787        superset.setBinding(unionBindings(superset, outcome, path, left, right));
788      }
789      return true;
790    }
791    // if either of them are extensible/required, then it wins
792    if (isPreferredOrExample(left)) {
793      subset.setBinding(right);
794      superset.setBinding(unionBindings(superset, outcome, path, left, right));
795      return true;
796    }
797    if (isPreferredOrExample(right)) {
798      subset.setBinding(left);
799      superset.setBinding(unionBindings(superset, outcome, path, left, right));
800      return true;
801    }
802    
803    // ok, both are extensible or required.
804    ElementDefinitionBindingComponent subBinding = new ElementDefinitionBindingComponent();
805    subset.setBinding(subBinding);
806    ElementDefinitionBindingComponent superBinding = new ElementDefinitionBindingComponent();
807    superset.setBinding(superBinding);
808    subBinding.setDescription(mergeText(subset, outcome, path, "description", left.getDescription(), right.getDescription()));
809    superBinding.setDescription(mergeText(subset, outcome, null, "description", left.getDescription(), right.getDescription()));
810    if (left.getStrength() == BindingStrength.REQUIRED || right.getStrength() == BindingStrength.REQUIRED)
811      subBinding.setStrength(BindingStrength.REQUIRED);
812    else
813      subBinding.setStrength(BindingStrength.EXTENSIBLE);
814    if (left.getStrength() == BindingStrength.EXTENSIBLE || right.getStrength() == BindingStrength.EXTENSIBLE)
815      superBinding.setStrength(BindingStrength.EXTENSIBLE);
816    else
817      superBinding.setStrength(BindingStrength.REQUIRED);
818    
819    if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false)) {
820      subBinding.setValueSet(left.getValueSet());
821      superBinding.setValueSet(left.getValueSet());
822      return true;
823    } else if (!left.hasValueSet()) {
824      outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "No left Value set at "+path, ValidationMessage.IssueSeverity.ERROR));
825      return true;      
826    } else if (!right.hasValueSet()) {
827      outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "No right Value set at "+path, ValidationMessage.IssueSeverity.ERROR));
828      return true;      
829    } else {
830      // ok, now we compare the value sets. This may be unresolvable. 
831      ValueSet lvs = resolveVS(outcome.left, left.getValueSet());
832      ValueSet rvs = resolveVS(outcome.right, right.getValueSet());
833      if (lvs == null) {
834        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Unable to resolve left value set "+left.getValueSet().toString()+" at "+path, ValidationMessage.IssueSeverity.ERROR));
835        return true;
836      } else if (rvs == null) {
837        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Unable to resolve right value set "+right.getValueSet().toString()+" at "+path, ValidationMessage.IssueSeverity.ERROR));
838        return true;        
839      } else {
840        // first, we'll try to do it by definition
841        ValueSet cvs = intersectByDefinition(lvs, rvs);
842        if(cvs == null) {
843          // if that didn't work, we'll do it by expansion
844          ValueSetExpansionOutcome le;
845          ValueSetExpansionOutcome re;
846          try {
847            le = context.expandVS(lvs, true, false);
848            re = context.expandVS(rvs, true, false);
849            if (!closed(le.getValueset()) || !closed(re.getValueset())) 
850              throw new DefinitionException("unclosed value sets are not handled yet");
851            cvs = intersectByExpansion(path, le.getValueset(), re.getValueset());
852            if (!cvs.getCompose().hasInclude()) {
853              outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "The value sets "+lvs.getUrl()+" and "+rvs.getUrl()+" do not intersect", ValidationMessage.IssueSeverity.ERROR));
854              status(subset, ProfileUtilities.STATUS_ERROR);
855              return false;
856            }
857          } catch (Exception e){
858            outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "Unable to expand or process value sets "+lvs.getUrl()+" and "+rvs.getUrl()+": "+e.getMessage(), ValidationMessage.IssueSeverity.ERROR));
859            status(subset, ProfileUtilities.STATUS_ERROR);
860            return false;          
861          }
862        }
863        subBinding.setValueSet("#"+addValueSet(cvs));
864        superBinding.setValueSet("#"+addValueSet(unite(superset, outcome, path, lvs, rvs)));
865      }
866    }
867    return false;
868  }
869
870  private ElementDefinitionBindingComponent unionBindings(ElementDefinition ed, ProfileComparison outcome, String path, ElementDefinitionBindingComponent left, ElementDefinitionBindingComponent right) throws FHIRFormatError {
871    ElementDefinitionBindingComponent union = new ElementDefinitionBindingComponent();
872    if (left.getStrength().compareTo(right.getStrength()) < 0)
873      union.setStrength(left.getStrength());
874    else
875      union.setStrength(right.getStrength());
876    union.setDescription(mergeText(ed, outcome, path, "binding.description", left.getDescription(), right.getDescription()));
877    if (Base.compareDeep(left.getValueSet(), right.getValueSet(), false))
878      union.setValueSet(left.getValueSet());
879    else {
880      ValueSet lvs = resolveVS(outcome.left, left.getValueSet());
881      ValueSet rvs = resolveVS(outcome.left, right.getValueSet());
882      if (lvs != null && rvs != null)
883        union.setValueSet("#"+addValueSet(unite(ed, outcome, path, lvs, rvs)));
884      else if (lvs != null)
885        union.setValueSet("#"+addValueSet(lvs));
886      else if (rvs != null)
887        union.setValueSet("#"+addValueSet(rvs));
888    }
889    return union;
890  }
891
892  
893  private ValueSet unite(ElementDefinition ed, ProfileComparison outcome, String path, ValueSet lvs, ValueSet rvs) {
894    ValueSet vs = new ValueSet();
895    vs.setName(path);
896    if (lvs.hasCompose()) {
897      for (ConceptSetComponent inc : lvs.getCompose().getInclude()) 
898        vs.getCompose().getInclude().add(inc);
899      if (lvs.getCompose().hasExclude()) {
900        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "The value sets "+lvs.getUrl()+" has exclude statements, and no union involving it can be correctly determined", ValidationMessage.IssueSeverity.ERROR));
901        status(ed, ProfileUtilities.STATUS_ERROR);
902      }
903    }
904    if (rvs.hasCompose()) {
905      for (ConceptSetComponent inc : rvs.getCompose().getInclude())
906        if (!mergeIntoExisting(vs.getCompose().getInclude(), inc))
907          vs.getCompose().getInclude().add(inc);
908      if (rvs.getCompose().hasExclude()) {
909        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "The value sets "+lvs.getUrl()+" has exclude statements, and no union involving it can be correctly determined", ValidationMessage.IssueSeverity.ERROR));
910        status(ed, ProfileUtilities.STATUS_ERROR);
911      }
912    }    
913    return vs;
914  }
915
916  private boolean mergeIntoExisting(List<ConceptSetComponent> include, ConceptSetComponent inc) {
917    for (ConceptSetComponent dst : include) {
918      if (Base.compareDeep(dst,  inc, false))
919        return true; // they're actually the same
920      if (dst.hasSystem() && dst.getSystem().equals(inc.getSystem())) {
921        if (inc.hasFilter() || dst.hasFilter()) {
922          return false; // just add the new one as a a parallel
923        } else if (inc.hasConcept() && dst.hasConcept()) {
924          for (ConceptReferenceComponent cc : inc.getConcept()) {
925            boolean found = false;
926            for (ConceptReferenceComponent dd : dst.getConcept()) {
927              if (dd.getCode().equals(cc.getCode()))
928                found = true;
929              if (found) {
930                if (cc.hasDisplay() && !dd.hasDisplay())
931                  dd.setDisplay(cc.getDisplay());
932                break;
933              }
934            }
935            if (!found)
936              dst.getConcept().add(cc.copy());
937          }
938        } else
939          dst.getConcept().clear(); // one of them includes the entire code system 
940      }
941    }
942    return false;
943  }
944
945  private ValueSet resolveVS(StructureDefinition ctxtLeft, String vsRef) {
946    if (vsRef == null)
947      return null;
948    return context.fetchResource(ValueSet.class, vsRef);
949  }
950
951  private ValueSet intersectByDefinition(ValueSet lvs, ValueSet rvs) {
952    // this is just a stub. The idea is that we try to avoid expanding big open value sets from SCT, RxNorm, LOINC.
953    // there's a bit of long hand logic coming here, but that's ok.
954    return null;
955  }
956
957  private ValueSet intersectByExpansion(String path, ValueSet lvs, ValueSet rvs) {
958    // this is pretty straight forward - we intersect the lists, and build a compose out of the intersection
959    ValueSet vs = new ValueSet();
960    vs.setName(path);
961    vs.setStatus(PublicationStatus.DRAFT);
962    
963    Map<String, ValueSetExpansionContainsComponent> left = new HashMap<String, ValueSetExpansionContainsComponent>();
964    scan(lvs.getExpansion().getContains(), left);
965    Map<String, ValueSetExpansionContainsComponent> right = new HashMap<String, ValueSetExpansionContainsComponent>();
966    scan(rvs.getExpansion().getContains(), right);
967    Map<String, ConceptSetComponent> inc = new HashMap<String, ConceptSetComponent>();
968    
969    for (String s : left.keySet()) {
970      if (right.containsKey(s)) {
971        ValueSetExpansionContainsComponent cc = left.get(s);
972        ConceptSetComponent c = inc.get(cc.getSystem());
973        if (c == null) {
974          c = vs.getCompose().addInclude().setSystem(cc.getSystem());
975          inc.put(cc.getSystem(), c);
976        }
977        c.addConcept().setCode(cc.getCode()).setDisplay(cc.getDisplay());
978      }
979    }
980    return vs;
981  }
982
983  private void scan(List<ValueSetExpansionContainsComponent> list, Map<String, ValueSetExpansionContainsComponent> map) {
984    for (ValueSetExpansionContainsComponent cc : list) {
985      if (cc.hasSystem() && cc.hasCode()) {
986        String s = cc.getSystem()+"::"+cc.getCode();
987        if (!map.containsKey(s))
988          map.put(s,  cc);
989      }
990      if (cc.hasContains())
991        scan(cc.getContains(), map);
992    }
993  }
994
995  private boolean closed(ValueSet vs) {
996    return !ToolingExtensions.findBooleanExtension(vs.getExpansion(), ToolingExtensions.EXT_UNCLOSED);
997  }
998
999  private boolean isPreferredOrExample(ElementDefinitionBindingComponent binding) {
1000    return binding.getStrength() == BindingStrength.EXAMPLE || binding.getStrength() == BindingStrength.PREFERRED;
1001  }
1002
1003  private Collection<? extends TypeRefComponent> intersectTypes(ElementDefinition ed, ProfileComparison outcome, String path, List<TypeRefComponent> left, List<TypeRefComponent> right) throws DefinitionException, IOException, FHIRFormatError {
1004    List<TypeRefComponent> result = new ArrayList<TypeRefComponent>();
1005    for (TypeRefComponent l : left) {
1006      if (l.hasAggregation())
1007        throw new DefinitionException("Aggregation not supported: "+path);
1008      boolean pfound = false;
1009      boolean tfound = false;
1010      TypeRefComponent c = l.copy();
1011      for (TypeRefComponent r : right) {
1012        if (r.hasAggregation())
1013          throw new DefinitionException("Aggregation not supported: "+path);
1014        if (!l.hasProfile() && !r.hasProfile()) {
1015          pfound = true;    
1016        } else if (!r.hasProfile()) {
1017          pfound = true; 
1018        } else if (!l.hasProfile()) {
1019          pfound = true;
1020          c.setProfile(r.getProfile());
1021        } else {
1022          StructureDefinition sdl = resolveProfile(ed, outcome, path, l.getProfile().get(0).getValue(), outcome.leftName());
1023          StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getProfile().get(0).getValue(), outcome.rightName());
1024          if (sdl != null && sdr != null) {
1025            if (sdl == sdr) {
1026              pfound = true;
1027            } else if (derivesFrom(sdl, sdr)) {
1028              pfound = true;
1029            } else if (derivesFrom(sdr, sdl)) {
1030              c.setProfile(r.getProfile());
1031              pfound = true;
1032            } else if (sdl.getType().equals(sdr.getType())) {
1033              ProfileComparison comp = compareProfiles(sdl, sdr);
1034              if (comp.getSubset() != null) {
1035                pfound = true;
1036                c.addProfile("#"+comp.id);
1037              }
1038            }
1039          }
1040        }
1041        if (!l.hasTargetProfile() && !r.hasTargetProfile()) {
1042          tfound = true;    
1043        } else if (!r.hasTargetProfile()) {
1044          tfound = true; 
1045        } else if (!l.hasTargetProfile()) {
1046          tfound = true;
1047          c.setTargetProfile(r.getTargetProfile());
1048        } else {
1049          StructureDefinition sdl = resolveProfile(ed, outcome, path, l.getTargetProfile().get(0).getValue(), outcome.leftName());
1050          StructureDefinition sdr = resolveProfile(ed, outcome, path, r.getTargetProfile().get(0).getValue(), outcome.rightName());
1051          if (sdl != null && sdr != null) {
1052            if (sdl == sdr) {
1053              tfound = true;
1054            } else if (derivesFrom(sdl, sdr)) {
1055              tfound = true;
1056            } else if (derivesFrom(sdr, sdl)) {
1057              c.setTargetProfile(r.getTargetProfile());
1058              tfound = true;
1059            } else if (sdl.getType().equals(sdr.getType())) {
1060              ProfileComparison comp = compareProfiles(sdl, sdr);
1061              if (comp.getSubset() != null) {
1062                tfound = true;
1063                c.addTargetProfile("#"+comp.id);
1064              }
1065            }
1066          }
1067        }
1068      }
1069      if (pfound && tfound)
1070        result.add(c);
1071    }
1072    return result;
1073  }
1074
1075  private StructureDefinition resolveProfile(ElementDefinition ed, ProfileComparison outcome, String path, String url, String name) {
1076    StructureDefinition res = context.fetchResource(StructureDefinition.class, url);
1077    if (res == null) {
1078      outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.INFORMATIONAL, path, "Unable to resolve profile "+url+" in profile "+name, ValidationMessage.IssueSeverity.WARNING));
1079      status(ed, ProfileUtilities.STATUS_HINT);
1080    }
1081    return res;
1082  }
1083
1084  private Collection<? extends TypeRefComponent> unionTypes(String path, List<TypeRefComponent> left, List<TypeRefComponent> right) throws DefinitionException, IOException, FHIRFormatError {
1085    List<TypeRefComponent> result = new ArrayList<TypeRefComponent>();
1086    for (TypeRefComponent l : left) 
1087      checkAddTypeUnion(path, result, l);
1088    for (TypeRefComponent r : right) 
1089      checkAddTypeUnion(path, result, r);
1090    return result;
1091  }    
1092    
1093  private void checkAddTypeUnion(String path, List<TypeRefComponent> results, TypeRefComponent nw) throws DefinitionException, IOException, FHIRFormatError {
1094    boolean pfound = false;
1095    boolean tfound = false;
1096    nw = nw.copy();
1097    if (nw.hasAggregation())
1098      throw new DefinitionException("Aggregation not supported: "+path);
1099    for (TypeRefComponent ex : results) {
1100      if (Utilities.equals(ex.getWorkingCode(), nw.getWorkingCode())) {
1101        if (!ex.hasProfile() && !nw.hasProfile())
1102          pfound = true;
1103        else if (!ex.hasProfile()) {
1104          pfound = true; 
1105        } else if (!nw.hasProfile()) {
1106          pfound = true;
1107          ex.setProfile(null);
1108        } else {
1109          // both have profiles. Is one derived from the other? 
1110          StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getProfile().get(0).getValue());
1111          StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getProfile().get(0).getValue());
1112          if (sdex != null && sdnw != null) {
1113            if (sdex == sdnw) {
1114              pfound = true;
1115            } else if (derivesFrom(sdex, sdnw)) {
1116              ex.setProfile(nw.getProfile());
1117              pfound = true;
1118            } else if (derivesFrom(sdnw, sdex)) {
1119              pfound = true;
1120            } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
1121              ProfileComparison comp = compareProfiles(sdex, sdnw);
1122              if (comp.getSuperset() != null) {
1123                pfound = true;
1124                ex.addProfile("#"+comp.id);
1125              }
1126            }
1127          }
1128        }        
1129        if (!ex.hasTargetProfile() && !nw.hasTargetProfile())
1130          tfound = true;
1131        else if (!ex.hasTargetProfile()) {
1132          tfound = true; 
1133        } else if (!nw.hasTargetProfile()) {
1134          tfound = true;
1135          ex.setTargetProfile(null);
1136        } else {
1137          // both have profiles. Is one derived from the other? 
1138          StructureDefinition sdex = context.fetchResource(StructureDefinition.class, ex.getTargetProfile().get(0).getValue());
1139          StructureDefinition sdnw = context.fetchResource(StructureDefinition.class, nw.getTargetProfile().get(0).getValue());
1140          if (sdex != null && sdnw != null) {
1141            if (sdex == sdnw) {
1142              tfound = true;
1143            } else if (derivesFrom(sdex, sdnw)) {
1144              ex.setTargetProfile(nw.getTargetProfile());
1145              tfound = true;
1146            } else if (derivesFrom(sdnw, sdex)) {
1147              tfound = true;
1148            } else if (sdnw.getSnapshot().getElement().get(0).getPath().equals(sdex.getSnapshot().getElement().get(0).getPath())) {
1149              ProfileComparison comp = compareProfiles(sdex, sdnw);
1150              if (comp.getSuperset() != null) {
1151                tfound = true;
1152                ex.addTargetProfile("#"+comp.id);
1153              }
1154            }
1155          }
1156        }        
1157      }
1158    }
1159    if (!tfound || !pfound)
1160      results.add(nw);      
1161  }
1162
1163  
1164  private boolean derivesFrom(StructureDefinition left, StructureDefinition right) {
1165    // left derives from right if it's base is the same as right
1166    // todo: recursive...
1167    return left.hasBaseDefinition() && left.getBaseDefinition().equals(right.getUrl());
1168  }
1169
1170
1171  private String mergeText(ElementDefinition ed, ProfileComparison outcome, String path, String name, String left, String right) {
1172    if (left == null && right == null)
1173      return null;
1174    if (left == null)
1175      return right;
1176    if (right == null)
1177      return left;
1178    left = stripLinks(left);
1179    right = stripLinks(right);
1180    if (left.equalsIgnoreCase(right))
1181      return left;
1182    if (path != null) {
1183      outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.INFORMATIONAL, path, "Elements differ in definition for "+name+":\r\n  \""+left+"\"\r\n  \""+right+"\"", 
1184          "Elements differ in definition for "+name+":<br/>\""+Utilities.escapeXml(left)+"\"<br/>\""+Utilities.escapeXml(right)+"\"", ValidationMessage.IssueSeverity.INFORMATION));
1185      status(ed, ProfileUtilities.STATUS_HINT);
1186    }
1187    return "left: "+left+"; right: "+right;
1188  }
1189
1190  
1191  private String stripLinks(String s) {
1192    while (s.contains("](")) {
1193      int i = s.indexOf("](");
1194      int j = s.substring(i).indexOf(")");
1195      if (j == -1)
1196        return s;
1197      else
1198        s = s.substring(0, i+1)+s.substring(i+j+1);
1199    }
1200    return s;
1201  }
1202
1203  private List<Coding> mergeCodings(List<Coding> left, List<Coding> right) {
1204    List<Coding> result = new ArrayList<Coding>();
1205    result.addAll(left);
1206    for (Coding c : right) {
1207      boolean found = false;
1208      for (Coding ct : left)
1209        if (Utilities.equals(c.getSystem(), ct.getSystem()) && Utilities.equals(c.getCode(), ct.getCode()))
1210          found = true;
1211      if (!found)
1212        result.add(c);
1213    }
1214    return result;
1215  }
1216
1217  private List<StringType> mergeStrings(List<StringType> left, List<StringType> right) {
1218    List<StringType> result = new ArrayList<StringType>();
1219    result.addAll(left);
1220    for (StringType c : right) {
1221      boolean found = false;
1222      for (StringType ct : left)
1223        if (Utilities.equals(c.getValue(), ct.getValue()))
1224          found = true;
1225      if (!found)
1226        result.add(c);
1227    }
1228    return result;
1229  }
1230
1231  private List<ElementDefinitionMappingComponent> mergeMappings(List<ElementDefinitionMappingComponent> left, List<ElementDefinitionMappingComponent> right) {
1232    List<ElementDefinitionMappingComponent> result = new ArrayList<ElementDefinitionMappingComponent>();
1233    result.addAll(left);
1234    for (ElementDefinitionMappingComponent c : right) {
1235      boolean found = false;
1236      for (ElementDefinitionMappingComponent ct : left)
1237        if (Utilities.equals(c.getIdentity(), ct.getIdentity()) && Utilities.equals(c.getLanguage(), ct.getLanguage()) && Utilities.equals(c.getMap(), ct.getMap()))
1238          found = true;
1239      if (!found)
1240        result.add(c);
1241    }
1242    return result;
1243  }
1244
1245  // we can't really know about constraints. We create warnings, and collate them 
1246  private List<ElementDefinitionConstraintComponent> unionConstraints(ElementDefinition ed, ProfileComparison outcome, String path, List<ElementDefinitionConstraintComponent> left, List<ElementDefinitionConstraintComponent> right) {
1247    List<ElementDefinitionConstraintComponent> result = new ArrayList<ElementDefinitionConstraintComponent>();
1248    for (ElementDefinitionConstraintComponent l : left) {
1249      boolean found = false;
1250      for (ElementDefinitionConstraintComponent r : right)
1251        if (Utilities.equals(r.getId(), l.getId()) || (Utilities.equals(r.getXpath(), l.getXpath()) && r.getSeverity() == l.getSeverity()))
1252          found = true;
1253      if (!found) {
1254        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "StructureDefinition "+outcome.leftName()+" has a constraint that is not found in "+outcome.rightName()+" and it is uncertain whether they are compatible ("+l.getXpath()+")", ValidationMessage.IssueSeverity.INFORMATION));
1255        status(ed, ProfileUtilities.STATUS_WARNING);
1256      }
1257      result.add(l);
1258    }
1259    for (ElementDefinitionConstraintComponent r : right) {
1260      boolean found = false;
1261      for (ElementDefinitionConstraintComponent l : left)
1262        if (Utilities.equals(r.getId(), l.getId()) || (Utilities.equals(r.getXpath(), l.getXpath()) && r.getSeverity() == l.getSeverity()))
1263          found = true;
1264      if (!found) {
1265        outcome.messages.add(new ValidationMessage(Source.ProfileComparer, ValidationMessage.IssueType.STRUCTURE, path, "StructureDefinition "+outcome.rightName()+" has a constraint that is not found in "+outcome.leftName()+" and it is uncertain whether they are compatible ("+r.getXpath()+")", ValidationMessage.IssueSeverity.INFORMATION));
1266        status(ed, ProfileUtilities.STATUS_WARNING);
1267        result.add(r);
1268      }
1269    }
1270    return result;
1271  }
1272
1273
1274  private List<ElementDefinitionConstraintComponent> intersectConstraints(String path, List<ElementDefinitionConstraintComponent> left, List<ElementDefinitionConstraintComponent> right) {
1275  List<ElementDefinitionConstraintComponent> result = new ArrayList<ElementDefinitionConstraintComponent>();
1276  for (ElementDefinitionConstraintComponent l : left) {
1277    boolean found = false;
1278    for (ElementDefinitionConstraintComponent r : right)
1279      if (Utilities.equals(r.getId(), l.getId()) || (Utilities.equals(r.getXpath(), l.getXpath()) && r.getSeverity() == l.getSeverity()))
1280        found = true;
1281    if (found)
1282      result.add(l);
1283  }
1284  return result;
1285}
1286
1287  private String card(DefinitionNavigator defn) {
1288    return Integer.toString(defn.current().getMin())+".."+defn.current().getMax();
1289  }
1290  
1291  private String typeCode(DefinitionNavigator defn) {
1292    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
1293    for (TypeRefComponent t : defn.current().getType())
1294      b.append(t.getWorkingCode()+(t.hasProfile() ? "("+t.getProfile()+")" : "")+(t.hasTargetProfile() ? "("+t.getTargetProfile()+")" : "")); // todo: other properties
1295    return b.toString();
1296  }
1297
1298  private int intersectMin(int left, int right) {
1299    if (left > right)
1300      return left;
1301    else
1302      return right;
1303  }
1304
1305  private int unionMin(int left, int right) {
1306    if (left > right)
1307      return right;
1308    else
1309      return left;
1310  }
1311
1312  private String intersectMax(String left, String right) {
1313    int l = "*".equals(left) ? Integer.MAX_VALUE : Integer.parseInt(left);
1314    int r = "*".equals(right) ? Integer.MAX_VALUE : Integer.parseInt(right);
1315    if (l < r)
1316      return left;
1317    else
1318      return right;
1319  }
1320
1321  private String unionMax(String left, String right) {
1322    int l = "*".equals(left) ? Integer.MAX_VALUE : Integer.parseInt(left);
1323    int r = "*".equals(right) ? Integer.MAX_VALUE : Integer.parseInt(right);
1324    if (l < r)
1325      return right;
1326    else
1327      return left;
1328  }
1329
1330  private IntegerType intersectMaxLength(int left, int right) {
1331    if (left == 0) 
1332      left = Integer.MAX_VALUE;
1333    if (right == 0) 
1334      right = Integer.MAX_VALUE;
1335    if (left < right)
1336      return left == Integer.MAX_VALUE ? null : new IntegerType(left);
1337    else
1338      return right == Integer.MAX_VALUE ? null : new IntegerType(right);
1339  }
1340
1341  private IntegerType unionMaxLength(int left, int right) {
1342    if (left == 0) 
1343      left = Integer.MAX_VALUE;
1344    if (right == 0) 
1345      right = Integer.MAX_VALUE;
1346    if (left < right)
1347      return right == Integer.MAX_VALUE ? null : new IntegerType(right);
1348    else
1349      return left == Integer.MAX_VALUE ? null : new IntegerType(left);
1350  }
1351
1352  
1353  public String addValueSet(ValueSet cvs) {
1354    String id = Integer.toString(valuesets.size()+1);
1355    cvs.setId(id);
1356    valuesets.add(cvs);
1357    return id;
1358  }
1359
1360  
1361  
1362  public String getId() {
1363    return id;
1364  }
1365
1366  public void setId(String id) {
1367    this.id = id;
1368  }
1369
1370  public String getTitle() {
1371    return title;
1372  }
1373
1374  public void setTitle(String title) {
1375    this.title = title;
1376  }
1377
1378  public String getLeftLink() {
1379    return leftLink;
1380  }
1381
1382  public void setLeftLink(String leftLink) {
1383    this.leftLink = leftLink;
1384  }
1385
1386  public String getLeftName() {
1387    return leftName;
1388  }
1389
1390  public void setLeftName(String leftName) {
1391    this.leftName = leftName;
1392  }
1393
1394  public String getRightLink() {
1395    return rightLink;
1396  }
1397
1398  public void setRightLink(String rightLink) {
1399    this.rightLink = rightLink;
1400  }
1401
1402  public String getRightName() {
1403    return rightName;
1404  }
1405
1406  public void setRightName(String rightName) {
1407    this.rightName = rightName;
1408  }
1409
1410  private String genPCLink(String leftName, String leftLink) {
1411    if (leftLink == null)
1412      return leftName;
1413    else
1414      return "<a href=\""+leftLink+"\">"+Utilities.escapeXml(leftName)+"</a>";
1415  }
1416  
1417  private String genValueSets(String base) throws IOException {
1418    StringBuilder b = new StringBuilder();
1419    b.append("<ul>\r\n");
1420    for (ValueSet vs : getValuesets()) {
1421      System.out.println("  .. Value set: "+vs.getName());
1422      b.append("<li>");
1423      b.append(" <td><a href=\""+base+"-"+vs.getId()+".html\">"+Utilities.escapeXml(vs.present())+"</a></td>");
1424      b.append("</li>\r\n");
1425      genValueSetFile(base+"-"+vs.getId()+".html", vs);
1426    }
1427    b.append("</ul>\r\n");
1428    return b.toString();   
1429  }
1430  
1431  private void genValueSetFile(String filename, ValueSet vs) throws IOException {
1432    NarrativeGenerator gen = new NarrativeGenerator("", "http://hl7.org/fhir", context);
1433    gen.setNoSlowLookup(true);
1434    gen.generate(null, vs, false);
1435    String s = new XhtmlComposer(XhtmlComposer.HTML).compose(vs.getText().getDiv());
1436    StringBuilder b = new StringBuilder();
1437    b.append("<html>");
1438    b.append("<head>");
1439    b.append("<title>"+vs.present()+"</title>");
1440    b.append("<link rel=\"stylesheet\" href=\"fhir.css\"/>\r\n");
1441    b.append("</head>");
1442    b.append("<body>");
1443    b.append("<h2>"+vs.present()+"</h2>");
1444    b.append(s);    
1445    b.append("</body>");
1446    b.append("</html>");
1447    TextFile.stringToFile(b.toString(), filename);
1448  }
1449
1450  private String genPCTable() {
1451    StringBuilder b = new StringBuilder();
1452
1453    b.append("<table class=\"grid\">\r\n");
1454    b.append("<tr>");
1455    b.append(" <td><b>Left</b></td>");
1456    b.append(" <td><b>Right</b></td>");
1457    b.append(" <td><b>Comparison</b></td>");
1458    b.append(" <td><b>Error #</b></td>");
1459    b.append(" <td><b>Warning #</b></td>");
1460    b.append(" <td><b>Hint #</b></td>");
1461    b.append("</tr>");
1462
1463    for (ProfileComparison cmp : getComparisons()) {
1464      b.append("<tr>");
1465      b.append(" <td><a href=\""+cmp.getLeft().getUserString("path")+"\">"+Utilities.escapeXml(cmp.getLeft().getName())+"</a></td>");
1466      b.append(" <td><a href=\""+cmp.getRight().getUserString("path")+"\">"+Utilities.escapeXml(cmp.getRight().getName())+"</a></td>");
1467      b.append(" <td><a href=\""+getId()+"."+cmp.getId()+".html\">Click Here</a></td>");
1468      b.append(" <td>"+cmp.getErrorCount()+"</td>");
1469      b.append(" <td>"+cmp.getWarningCount()+"</td>");
1470      b.append(" <td>"+cmp.getHintCount()+"</td>");
1471      b.append("</tr>");
1472    }
1473    b.append("</table>\r\n");
1474
1475    return b.toString();
1476  }
1477
1478
1479  private String genCmpMessages(ProfileComparison cmp) {
1480    StringBuilder b = new StringBuilder();
1481    b.append("<table class=\"grid\">\r\n");
1482    b.append("<tr><td><b>Path</b></td><td><b>Message</b></td></tr>\r\n");
1483    b.append("<tr><td colspan=\"2\" style=\"background: #eeeeee\">Errors Detected</td></tr>\r\n");
1484    boolean found = false;
1485    for (ValidationMessage vm : cmp.getMessages())
1486      if (vm.getLevel() == IssueSeverity.ERROR || vm.getLevel() == IssueSeverity.FATAL) {
1487        found = true;
1488        b.append("<tr><td>"+vm.getLocation()+"</td><td>"+vm.getHtml()+(vm.getLevel() == IssueSeverity.FATAL ? "(<span style=\"color: maroon\">This error terminated the comparison process</span>)" : "")+"</td></tr>\r\n");
1489      }
1490    if (!found)
1491    b.append("<tr><td colspan=\"2\">(None)</td></tr>\r\n");
1492
1493    boolean first = true;
1494    for (ValidationMessage vm : cmp.getMessages())
1495      if (vm.getLevel() == IssueSeverity.WARNING) {
1496        if (first) {
1497          first = false;
1498          b.append("<tr><td colspan=\"2\" style=\"background: #eeeeee\">Warnings about the comparison</td></tr>\r\n");
1499        }
1500        b.append("<tr><td>"+vm.getLocation()+"</td><td>"+vm.getHtml()+"</td></tr>\r\n");
1501      }
1502    first = true;
1503    for (ValidationMessage vm : cmp.getMessages())
1504      if (vm.getLevel() == IssueSeverity.INFORMATION) {
1505        if (first) {
1506          b.append("<tr><td colspan=\"2\" style=\"background: #eeeeee\">Notes about differences (e.g. definitions)</td></tr>\r\n");
1507          first = false;
1508        }
1509        b.append("<tr><td>"+vm.getLocation()+"</td><td>"+vm.getHtml()+"</td></tr>\r\n");
1510      }
1511    b.append("</table>\r\n");
1512    return b.toString();
1513  }
1514
1515  private String genCompModel(StructureDefinition sd, String name, String base, String prefix, String dest) throws FHIRException, IOException {
1516    if (sd == null)
1517      return "<p style=\"color: maroon\">No "+name+" could be generated</p>\r\n";
1518    return new XhtmlComposer(XhtmlComposer.HTML).compose(new ProfileUtilities(context, null, this).generateTable("??", sd, false, dest, false, base, true, prefix, prefix, false, false, null, true));
1519  }
1520
1521
1522  public String generate() throws IOException {
1523    for (ValueSet vs : valuesets) {
1524      vs.setUserData("path", folder+"/"+getId()+"-vs-"+vs.getId()+".html");
1525    }
1526    // ok, all compared; now produce the output
1527    // first page we produce is simply the index
1528    Map<String, String> vars = new HashMap<String, String>();
1529    vars.put("title", getTitle());
1530    vars.put("left", genPCLink(getLeftName(), getLeftLink()));
1531    vars.put("right", genPCLink(getRightName(), getRightLink()));
1532    vars.put("table", genPCTable());
1533    vars.put("valuesets", genValueSets(folder+"/"+getId()+"-vs"));
1534    producePage(summaryTemplate(), Utilities.path(folder, getId()+".html"), vars);
1535    
1536    // then we produce a comparison page for each pair
1537    for (ProfileComparison cmp : getComparisons()) {
1538      vars.clear();
1539      vars.put("title", getTitle());
1540      vars.put("left", genPCLink(getLeftName(), getLeftLink()));
1541      vars.put("right", genPCLink(getRightName(), getRightLink()));
1542      vars.put("messages", genCmpMessages(cmp));
1543      vars.put("subset", genCompModel(cmp.getSubset(), "intersection", getId()+"."+cmp.getId(), "", folder));
1544      vars.put("superset", genCompModel(cmp.getSuperset(), "union", getId()+"."+cmp.getId(), "", folder));
1545      producePage(singleTemplate(), Utilities.path(folder, getId()+"."+cmp.getId()+".html"), vars);
1546    }
1547//      //   and also individual pages for each pair outcome
1548//    // then we produce value set pages for each value set
1549//
1550//    // TODO Auto-generated method stub
1551    return Utilities.path(folder, getId()+".html");
1552  }
1553
1554  
1555  private void producePage(String src, String path, Map<String, String> vars) throws IOException {
1556    while (src.contains("[%"))
1557    {
1558      int i1 = src.indexOf("[%");
1559      int i2 = src.substring(i1).indexOf("%]")+i1;
1560      String s1 = src.substring(0, i1);
1561      String s2 = src.substring(i1 + 2, i2).trim();
1562      String s3 = src.substring(i2+2);
1563      String v = vars.containsKey(s2) ? vars.get(s2) : "???";
1564      src = s1+v+s3;
1565    }
1566    TextFile.stringToFile(src, path);
1567  }
1568
1569  private String summaryTemplate() throws IOException {
1570    return TextFile.fileToString(Utilities.path(folder, "template-comparison-set.html"));
1571  }
1572
1573  private String singleTemplate() throws IOException {
1574    return TextFile.fileToString(Utilities.path(folder, "template-comparison.html"));
1575  }
1576
1577  private String cachedFetch(String id, String source) throws IOException {
1578    String tmpDir = System.getProperty("java.io.tmpdir");
1579    String local = Utilities.path(tmpDir, id);
1580    File f = new File(local);
1581    if (f.exists())
1582      return TextFile.fileToString(f);
1583    URL url = new URL(source);
1584    URLConnection c = url.openConnection();
1585    String result = TextFile.streamToString(c.getInputStream());
1586    TextFile.stringToFile(result, f);
1587    return result;
1588  }
1589
1590  @Override
1591  public boolean isDatatype(String typeSimple) {
1592    throw new Error("Not done yet");
1593  }
1594
1595  @Override
1596  public boolean isResource(String typeSimple) {
1597    throw new Error("Not done yet");
1598  }
1599
1600  @Override
1601  public boolean hasLinkFor(String name) {
1602    StructureDefinition sd = context.fetchTypeDefinition(name);
1603    return sd != null && sd.hasUserData("path");
1604  }
1605
1606  @Override
1607  public String getLinkFor(String corePath, String name) {
1608    StructureDefinition sd = context.fetchTypeDefinition(name);
1609    return sd == null ? null : sd.getUserString("path");
1610  }
1611
1612  @Override
1613  public BindingResolution resolveBinding(StructureDefinition def, ElementDefinitionBindingComponent binding, String path) throws FHIRException {
1614    return resolveBindingInt(def, binding.getValueSet(), binding.getDescription());
1615  }
1616
1617  @Override
1618  public BindingResolution resolveBinding(StructureDefinition def, String url, String path) throws FHIRException {
1619    return resolveBindingInt(def, url, url);
1620  }
1621
1622  public BindingResolution resolveBindingInt(StructureDefinition def, String url, String desc) throws FHIRException {
1623    ValueSet vs = null;
1624    if (url != null && url.startsWith("#")) {
1625      for (ValueSet t : valuesets) {
1626        if (("#"+t.getId()).equals(url)) {
1627          vs = t;
1628          break;
1629        }
1630      }
1631    }
1632    if (url != null && vs == null) 
1633      context.fetchResource(ValueSet.class, url);
1634    BindingResolution br = new BindingResolution();
1635    if (vs != null) {
1636      br.display = vs.present();
1637      br.url = vs.getUserString("path");
1638    } else {
1639      br.display = desc;
1640    }
1641    return br;
1642  }
1643
1644  @Override
1645  public String getLinkForProfile(StructureDefinition profile, String url) {
1646    StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
1647    return sd == null ? null : sd.getUserString("path");
1648  }
1649
1650  @Override
1651  public boolean prependLinks() {
1652    return false;
1653  }
1654
1655  @Override
1656  public String getLinkForUrl(String corePath, String s) {
1657    return null;
1658  }
1659
1660  public int getErrCount() {
1661    int res = 0;
1662    for (ProfileComparison pc : comparisons) {
1663      res = res + pc.getErrorCount();
1664    }
1665    return res;
1666    
1667  }
1668
1669
1670  
1671  
1672}