Search in sources :

Example 1 with XmlSpecGenerator

use of org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator in project kindling by HL7.

the class Publisher method produceProfile.

private void produceProfile(ResourceDefn resource, Profile pack, ConstraintStructure profile, SectionTracker st, String intro, String notes, String prefix, ImplementationGuideDefn ig) throws Exception {
    File tmp = Utilities.createTempFile("tmp", ".tmp");
    String title = profile.getId();
    int level = (ig == null || ig.isCore()) ? 0 : 1;
    // you have to validate a profile, because it has to be merged with it's
    // base resource to fill out all the missing bits
    // validateProfile(profile);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    XmlSpecGenerator gen = new XmlSpecGenerator(bs, title + "-definitions.html", "", page, ig.isCore() ? "" : "../");
    gen.generate(profile.getResource());
    gen.close();
    String xml = new String(bs.toByteArray());
    bs = new ByteArrayOutputStream();
    JsonSpecGenerator genJ = new JsonSpecGenerator(bs, title + "-definitions.html", "", page, ig.isCore() ? "" : "../", page.getVersion().toCode());
    genJ.generate(profile.getResource());
    genJ.close();
    String json = new String(bs.toByteArray());
    XmlParser comp = new XmlParser();
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + prefix + title + ".profile.xml");
    comp.setOutputStyle(OutputStyle.PRETTY).compose(s, profile.getResource());
    s.close();
    Utilities.copyFile(new CSFile(page.getFolders().dstDir + prefix + title + ".profile.xml"), new CSFile(page.getFolders().dstDir + "examples" + File.separator + title + ".profile.xml"));
    JsonParser jcomp = new JsonParser();
    s = new FileOutputStream(page.getFolders().dstDir + prefix + title + ".profile.json");
    jcomp.setOutputStyle(OutputStyle.PRETTY).compose(s, profile.getResource());
    s.close();
    // String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, profile.getResource());
    // TextFile.stringToFile(shex, Utilities.changeFileExt(page.getFolders().dstDir + prefix +title + ".profile.shex", ".shex"));
    // shexToXhtml(prefix +title + ".profile", "ShEx statement for " + prefix +title, shex, "profile-instance:type:" + title, "Type");
    TerminologyNotesGenerator tgen = new TerminologyNotesGenerator(new FileOutputStream(tmp), page);
    tgen.generate(level == 0 ? "" : "../", profile);
    tgen.close();
    String tx = TextFile.fileToString(tmp.getAbsolutePath());
    String src = TextFile.fileToString(page.getFolders().templateDir + "template-profile.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + ".html", level, null);
    else if (ig != null && !ig.isCore()) {
        src = addSectionNumbers(title + ".html", title, src, null, 1, null, ig);
        st = page.getSectionTrackerCache().get(ig.getCode() + "::" + title);
    }
    page.getHTMLChecker().registerFile(prefix + title + ".html", "StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, false);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + ".html");
    new ProfileUtilities(page.getWorkerContext(), page.getValidationErrors(), page).generateSchematrons(new FileOutputStream(page.getFolders().dstDir + prefix + title + ".sch"), profile.getResource());
    if (pack.getExamples().size() > 0) {
        src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-examples.html");
        src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
        page.getHTMLChecker().registerFile(prefix + title + "-examples.html", "Examples for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
        TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-examples.html");
    }
    src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-definitions.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + "-definitions.html", level, null);
    page.getHTMLChecker().registerFile(prefix + title + "-definitions.html", "Definitions for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-definitions.html");
    src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-mappings.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + "-mappings.html", level, null);
    page.getHTMLChecker().registerFile(prefix + title + "-mappings.html", "Mappings for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-mappings.html");
    try {
        processQuestionnaire(resource, profile.getResource(), st, false, prefix, ig);
    } catch (Exception e) {
        e.printStackTrace();
        page.log("Questionnaire Generation Failed: " + e.getMessage(), LogMessageType.Error);
    }
    new ReviewSpreadsheetGenerator().generate(page.getFolders().dstDir + prefix + Utilities.changeFileExt((String) profile.getResource().getUserData("filename"), "-review.xls"), "Health Level Seven International", page.getGenDate(), profile.getResource(), page);
    // xml to xhtml of xml
    // first pass is to strip the xsi: stuff. seems to need double
    // processing in order to delete namespace crap
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xdoc = builder.parse(new CSFileInputStream(page.getFolders().dstDir + prefix + title + ".profile.xml"));
    XmlGenerator xmlgen = new XmlGenerator();
    xmlgen.generate(xdoc.getDocumentElement(), tmp, "http://hl7.org/fhir", xdoc.getDocumentElement().getLocalName());
    // reload it now
    builder = factory.newDocumentBuilder();
    xdoc = builder.parse(new CSFileInputStream(tmp.getAbsolutePath()));
    XhtmlGenerator xhtml = new XhtmlGenerator(new ExampleAdorner(page.getDefinitions(), page.genlevel(level)));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    xhtml.generate(xdoc, b, "StructureDefinition", profile.getTitle(), 0, true, title + ".profile.xml.html");
    String html = TextFile.fileToString(page.getFolders().templateDir + "template-profile-example-xml.html").replace("<%example%>", b.toString());
    html = page.processProfileIncludes(title + ".profile.xml.html", profile.getId(), pack, profile, "", "", "", html, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, hasNarrative(xdoc));
    TextFile.stringToFile(html, page.getFolders().dstDir + prefix + title + ".profile.xml.html");
    page.getHTMLChecker().registerFile(prefix + title + ".profile.xml.html", "StructureDefinition", HTMLLinkChecker.XHTML_TYPE, false);
    String n = prefix + title + ".profile";
    json = resource2Json(profile.getResource());
    json = "<div class=\"example\">\r\n<p>" + Utilities.escapeXml("StructureDefinition for " + profile.getResource().getDescription()) + "</p>\r\n<p><a href=\"" + title + ".profile.json\">Raw JSON</a></p>\r\n<pre class=\"json\">\r\n" + Utilities.escapeXml(json) + "\r\n</pre>\r\n</div>\r\n";
    html = TextFile.fileToString(page.getFolders().templateDir + "template-profile-example-json.html").replace("<%example%>", json);
    html = page.processProfileIncludes(title + ".profile.json.html", profile.getId(), pack, profile, "", "", "", html, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    TextFile.stringToFile(html, page.getFolders().dstDir + prefix + title + ".profile.json.html");
    // page.getEpub().registerFile(n + ".json.html", description, EPubManager.XHTML_TYPE);
    page.getHTMLChecker().registerExternal(n + ".json.html");
    tmp.delete();
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) JsonSpecGenerator(org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XmlGenerator(org.hl7.fhir.utilities.xml.XmlGenerator) TerminologyNotesGenerator(org.hl7.fhir.definitions.generators.specification.TerminologyNotesGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) Document(org.w3c.dom.Document) XhtmlDocument(org.hl7.fhir.utilities.xhtml.XhtmlDocument) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ReviewSpreadsheetGenerator(org.hl7.fhir.definitions.generators.specification.ReviewSpreadsheetGenerator) XhtmlGenerator(org.hl7.fhir.utilities.xml.XhtmlGenerator) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 2 with XmlSpecGenerator

use of org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator in project kindling by HL7.

the class PageProcessor method xmlForDt.

private String xmlForDt(String dt, String pn) throws Exception {
    File tmp = Utilities.createTempFile("tmp", ".tmp");
    XmlSpecGenerator gen = new XmlSpecGenerator(new FileOutputStream(tmp), pn == null ? null : pn.substring(0, pn.indexOf(".")) + "-definitions.html", null, this, "");
    TypeParser tp = new TypeParser(version.toCode());
    TypeRef t = tp.parse(dt, false, null, workerContext, true).get(0);
    ElementDefn e = definitions.getElementDefn(t.getName());
    if (e == null) {
        gen.close();
        throw new Exception("unable to find definition for " + dt);
    } else {
        gen.generate(e, e.getName().equals("Element") || e.getName().equals("BackboneElement"), false);
        gen.close();
    }
    String val = TextFile.fileToString(tmp.getAbsolutePath()) + "\r\n";
    tmp.delete();
    return val;
}
Also used : XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) ITypeParser(org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) TypeRef(org.hl7.fhir.definitions.model.TypeRef) FileOutputStream(java.io.FileOutputStream) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 3 with XmlSpecGenerator

use of org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator in project kindling by HL7.

the class Publisher method produceResource1.

private void produceResource1(ResourceDefn resource, boolean isAbstract) throws Exception {
    String n = resource.getName().toLowerCase();
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    XmlSpecGenerator gen = new XmlSpecGenerator(bs, n + "-definitions.html", null, page, "");
    gen.generate(resource.getRoot(), isAbstract, true);
    gen.close();
    String xml = new String(bs.toByteArray());
    bs = new ByteArrayOutputStream();
    JsonSpecGenerator genJ = new JsonSpecGenerator(bs, n + "-definitions.html", null, page, "", page.getVersion().toCode());
    genJ.generate(resource.getRoot(), true, isAbstract);
    genJ.close();
    String json = new String(bs.toByteArray());
    bs = new ByteArrayOutputStream();
    TurtleSpecGenerator gent = new TurtleSpecGenerator(bs, n + "-definitions.html", null, page, "", page.getVersion().toCode());
    gent.generate(resource.getRoot(), isAbstract);
    gent.close();
    String ttl = new String(bs.toByteArray());
    xmls.put(n, xml);
    jsons.put(n, json);
    ttls.put(n, ttl);
    generateProfile(resource, n, xml, json, ttl, false);
}
Also used : XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) JsonSpecGenerator(org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator) TurtleSpecGenerator(org.hl7.fhir.definitions.generators.specification.TurtleSpecGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with XmlSpecGenerator

use of org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator in project kindling by HL7.

the class Publisher method produceExtensionDefinition.

private void produceExtensionDefinition(StructureDefinition ed) throws FileNotFoundException, Exception {
    if (!ped.contains(ed)) {
        ped.add(ed);
        ImplementationGuideDefn ig = page.getDefinitions().getIgs().get(ed.getUserString(ToolResourceUtilities.NAME_RES_IG));
        String prefix = ig.isCore() ? "" : ig.getCode() + File.separator;
        String filename = ed.getUserString("filename");
        FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + prefix + filename + ".xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, ed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + prefix + filename + ".canonical.xml");
        new XmlParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, ed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + prefix + filename + ".json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, ed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + prefix + filename + ".canonical.json");
        new JsonParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, ed);
        s.close();
        cloneToXhtml(prefix + filename, ed.getName(), false, "summary-instance", ed.getUrl(), null, wg(ed));
        jsonToXhtml(prefix + filename, ed.getName(), resource2Json(ed), "extension", ed.getUrl(), null, wg(ed));
        ttlToXhtml(prefix + filename, ed.getName(), resource2Ttl(ed), "extension", ed.getUrl(), null, wg(ed));
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        XmlSpecGenerator gen = new XmlSpecGenerator(bytes, filename + "-definitions.html", null, /*"http://hl7.org/fhir/"*/
        page, page.genlevel(ig.isCore() ? 0 : 1));
        gen.generateExtension(ed);
        gen.close();
        String xml = bytes.toString();
        bytes = new ByteArrayOutputStream();
        JsonSpecGenerator genj = new JsonSpecGenerator(bytes, filename + "-definitions.html", null, /*"http://hl7.org/fhir/"*/
        page, page.genlevel(ig.isCore() ? 0 : 1), page.getVersion().toCode());
        genj.generateExtension(ed);
        genj.close();
        String json = bytes.toString();
        bytes = new ByteArrayOutputStream();
        TurtleSpecGenerator gent = new TurtleSpecGenerator(bytes, filename + "-definitions.html", null, /*"http://hl7.org/fhir/"*/
        page, page.genlevel(ig.isCore() ? 0 : 1), page.getVersion().toCode());
        gent.generateExtension(ed);
        gent.close();
        String ttl = bytes.toString();
        bytes = new ByteArrayOutputStream();
        TerminologyNotesGenerator tgen = new TerminologyNotesGenerator(bytes, page);
        tgen.generateExtension("", ed);
        tgen.close();
        String tx = bytes.toString();
        String src = TextFile.fileToString(page.getFolders().templateDir + "template-extension-mappings.html");
        src = page.processExtensionIncludes(filename, ed, xml, json, ttl, tx, src, filename + ".html", ig);
        page.getHTMLChecker().registerFile(prefix + filename + "-mappings.html", "Mappings for Extension " + ed.getName(), HTMLLinkChecker.XHTML_TYPE, true);
        TextFile.stringToFile(src, page.getFolders().dstDir + prefix + filename + "-mappings.html");
        src = TextFile.fileToString(page.getFolders().templateDir + "template-extension-definitions.html");
        src = page.processExtensionIncludes(filename, ed, xml, json, ttl, tx, src, filename + ".html", ig);
        page.getHTMLChecker().registerFile(prefix + filename + "-definitions.html", "Definitions for Extension " + ed.getName(), HTMLLinkChecker.XHTML_TYPE, true);
        TextFile.stringToFile(src, page.getFolders().dstDir + prefix + filename + "-definitions.html");
        src = TextFile.fileToString(page.getFolders().templateDir + "template-extension.html");
        src = page.processExtensionIncludes(filename, ed, xml, json, ttl, tx, src, filename + ".html", ig);
        page.getHTMLChecker().registerFile(prefix + filename + ".html", "Extension " + ed.getName(), HTMLLinkChecker.XHTML_TYPE, true);
        TextFile.stringToFile(src, page.getFolders().dstDir + prefix + filename + ".html");
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) JsonSpecGenerator(org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator) TerminologyNotesGenerator(org.hl7.fhir.definitions.generators.specification.TerminologyNotesGenerator) FileOutputStream(java.io.FileOutputStream) TurtleSpecGenerator(org.hl7.fhir.definitions.generators.specification.TurtleSpecGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImplementationGuideDefn(org.hl7.fhir.definitions.model.ImplementationGuideDefn) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Aggregations

XmlSpecGenerator (org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 JsonSpecGenerator (org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 TransformerException (javax.xml.transform.TransformerException)2 TerminologyNotesGenerator (org.hl7.fhir.definitions.generators.specification.TerminologyNotesGenerator)2 TurtleSpecGenerator (org.hl7.fhir.definitions.generators.specification.TurtleSpecGenerator)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 JsonParser (org.hl7.fhir.r5.formats.JsonParser)2 XmlParser (org.hl7.fhir.r5.formats.XmlParser)2 CSFile (org.hl7.fhir.utilities.CSFile)2 IniFile (org.hl7.fhir.utilities.IniFile)2 TextFile (org.hl7.fhir.utilities.TextFile)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1